Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M4</version>
<scope>test</scope>
</dependency>

Expand Down
117 changes: 107 additions & 10 deletions src/test/java/option/OptionalExample.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package option;

import org.junit.Test;

import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import static org.junit.Assert.assertEquals;

public class OptionalExample {

Expand All @@ -18,7 +23,8 @@ public void get() {

o1.orElse("t");
o1.orElseGet(() -> "t");
o1.orElseThrow(() -> new UnsupportedOperationException());
assertThrows(UnsupportedOperationException.class,
() -> o1.orElseThrow(UnsupportedOperationException::new));
}

@Test
Expand All @@ -35,17 +41,12 @@ public void ifPresent() {
@Test
public void map() {
final Optional<String> o1 = getOptional();

final Function<String, Integer> getLength = String::length;

final Optional<Integer> expected = o1.map(getLength);

final Optional<Integer> actual;
if (o1.isPresent()) {
actual = Optional.of(getLength.apply(o1.get()));
} else {
actual = Optional.empty();
}
actual = o1.map(getLength::apply);

assertEquals(expected, actual);
}
Expand All @@ -55,4 +56,100 @@ private Optional<String> getOptional() {
? Optional.empty()
: Optional.of("abc");
}

@Test
void filter() {
Optional<String> o1 = Optional.of("abc");
Predicate<String> predicate = s -> s.equals("test");
Optional<String> expected = o1.filter(predicate);
Optional<String> actual = predicate.test(o1.get())? o1 : Optional.empty();

assertEquals(expected, actual);


o1 = Optional.empty();
expected = o1.filter(predicate);
actual = Optional.empty();

assertEquals(expected, actual);
}

@Test
void flatMap() {
Optional<String> o1 = Optional.of("abc");
Function<String, Optional<String>> function = s -> Optional.of("test");
Optional<String> expected = o1.flatMap(function);
Optional<String> actual;
actual = function.apply(o1.get());

assertEquals(expected, actual);

o1 = Optional.empty();
expected = o1.flatMap(function);
actual = Optional.empty();

assertEquals(expected, actual);
}

@Test
void orElse() {
Optional<String> o1 = Optional.of("abc");
String expected = o1.orElse("other");
String actual;
actual = o1.get();

assertEquals(expected, actual);


o1 = Optional.empty();
expected = o1.orElse("other");
actual = "other";

assertEquals(expected, actual);
}

@Test
void orElseThrow() {
final Optional<String> o1 = Optional.of("abc");
Supplier<NullPointerException> supplier = NullPointerException::new;

final Class<NullPointerException> exception = NullPointerException.class;
o1.orElseThrow(supplier);
o1.get();

final Optional<String> o2 = Optional.empty();
assertThrows(exception, () -> o2.orElseThrow(supplier));
}

@Test
void orElseGet() {
Optional<String> o1 = Optional.of("abc");
final Supplier<String> supplier = () -> "test";
String expected = o1.orElseGet(supplier);
String actual = o1.get();

assertEquals(expected,actual);


o1 = Optional.empty();
expected = o1.orElseGet(supplier);
actual = supplier.get();

assertEquals(expected,actual);

}

// TODO: 07.07.17 fill out and test method using map and flatMap
private static <T1, T2, R> Optional<R> zipMap(Optional<T1> o1, Optional<T2> o2, BiFunction<T1, T2, R> f) {
return o1.flatMap(x -> o2.map(y -> f.apply(x,y)));
}

@Test
void testZipMap(){
final BiFunction<String, String, String> mrTractor = String::concat;

assertEquals(zipMap(Optional.of("KuKa"), Optional.of("ReKu"), mrTractor), Optional.of("KuKaReKu"));
assertEquals(zipMap(Optional.of("Aloha, boys"), Optional.empty(), mrTractor), Optional.empty());
assertEquals(zipMap(Optional.empty(), Optional.empty(), mrTractor), Optional.empty());
}
}