Skip to content

Commit b3fd9b3

Browse files
committed
Add tests for inconsistency around Predicate and its method
This commit adds a test, in PreferJavaUtilPredicateTest, to reveal the inconsistency between the conversion of `com.google.common.base.Predicate` and its `apply` method. The test is similar to `NoGuavaPredicateTest.doNotChangeWhenUsingCollectionsFilter()`, and as a consequence, the method must remain unchanged. Bug: #908
1 parent 74646a2 commit b3fd9b3

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/test/java/org/openrewrite/java/migrate/guava/NoGuavaPredicateTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ void doNotChangeWhenUsingCollectionsFilter() {
183183
import java.util.Collection;
184184
185185
class Test {
186-
Predicate<String> notEmpty = s -> !s.isEmpty();
186+
Predicate<String> notEmpty = new Predicate<String>() {
187+
@Override public boolean apply(String s) {
188+
return !s.isEmpty();
189+
}
190+
};
187191
188192
public Collection<String> filterCollection(Collection<String> input) {
189193
return Collections2.filter(input, notEmpty);

src/test/java/org/openrewrite/java/migrate/guava/PreferJavaUtilPredicateTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.InMemoryExecutionContext;
21+
import org.openrewrite.Issue;
2122
import org.openrewrite.java.JavaParser;
2223
import org.openrewrite.test.RecipeSpec;
2324
import org.openrewrite.test.RewriteTest;
@@ -128,4 +129,32 @@ public static Predicate<String> isHelloPredicate() {
128129
)
129130
);
130131
}
132+
133+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/899")
134+
@Test
135+
void doNotChangeWhenUsingCollectionsFilter() {
136+
// Collections2.filter requires Guava Predicate as last parameter
137+
rewriteRun(
138+
//language=java
139+
java(
140+
"""
141+
import com.google.common.base.Predicate;
142+
import com.google.common.collect.Collections2;
143+
import java.util.Collection;
144+
145+
class Test {
146+
Predicate<String> notEmpty = new Predicate<String>() {
147+
@Override public boolean apply(String s) {
148+
return !s.isEmpty();
149+
}
150+
};
151+
152+
public Collection<String> filterCollection(Collection<String> input) {
153+
return Collections2.filter(input, notEmpty);
154+
}
155+
}
156+
"""
157+
)
158+
);
159+
}
131160
}

0 commit comments

Comments
 (0)