Skip to content

Commit 4ce9005

Browse files
committed
add java 7 predicate example
1 parent 145513d commit 4ce9005

File tree

5 files changed

+77
-42
lines changed

5 files changed

+77
-42
lines changed

gradle/junit.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ allprojects {
22
// read more: https://docs.gradle.org/4.6/release-notes.html#junit-5-support
33

44
dependencies {
5+
testImplementation 'org.assertj:assertj-core:3.10.0'
56
testImplementation "junit:junit:$junit4Version"
67
testImplementation "org.hamcrest:hamcrest-core:$hamcrestVersion"
78
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion"

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@
6060
<version>1.2.3</version>
6161
</dependency>
6262

63+
<!-- assertj -->
64+
<dependency>
65+
<groupId>org.assertj</groupId>
66+
<artifactId>assertj-core</artifactId>
67+
<version>3.10.0</version>
68+
<scope>test</scope>
69+
</dependency>
6370
<!-- junit 4 -->
6471
<dependency>
6572
<groupId>junit</groupId>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
11
package daggerok.java7.predicate;
22

3+
import lombok.RequiredArgsConstructor;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import static java.util.Objects.nonNull;
10+
311
public class Java7Predicate {
12+
13+
public interface MyPredicate {
14+
boolean test(final String value);
15+
}
16+
17+
@RequiredArgsConstructor
18+
public static class Is implements MyPredicate {
19+
final String value;
20+
21+
@Override
22+
public boolean test(String value) {
23+
return value != null && value.contains(this.value);
24+
}
25+
}
26+
27+
public static class IsRed implements MyPredicate {
28+
@Override
29+
public boolean test(final String value) {
30+
return nonNull(value) && value.toLowerCase().contains("red");
31+
}
32+
}
33+
34+
public static void sideEffect(final MyPredicate predicate) {
35+
final List<String> list = Arrays.asList("ololo", "trololo", "red", "blue", "green", "red devil");
36+
for (String i : list) {
37+
if (predicate.test(i)) {
38+
System.out.println("i = " + i);
39+
}
40+
}
41+
}
442
}

src/test/java/daggerok/AppJUnit5Test.java

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package daggerok.java7.predicate;
2+
3+
import daggerok.extensions.CaptureSystemOutput;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.assertj.core.api.Assertions;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static daggerok.java7.predicate.Java7Predicate.sideEffect;
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
@Slf4j
13+
@CaptureSystemOutput
14+
@DisplayName("test predicate API: java 7")
15+
class Java7PredicateTest {
16+
17+
@Test
18+
@DisplayName("test positive")
19+
void testPredicate(final CaptureSystemOutput.OutputCapture outputCapture) {
20+
21+
assertAll(() -> {
22+
23+
sideEffect(new Java7Predicate.IsRed());
24+
Assertions.assertThat(outputCapture.toString())
25+
.contains("i = red", "i = red devil");
26+
27+
sideEffect(new Java7Predicate.Is("green"));
28+
Assertions.assertThat(outputCapture.toString().contains("t = green"));
29+
});
30+
}
31+
}

0 commit comments

Comments
 (0)