Skip to content

Commit

Permalink
Update applicability tests to use Applicability#and along with the …
Browse files Browse the repository at this point in the history
…`Assertions#version(SourceSpecs, int)` for setting the JavaVersion marker source specs.
  • Loading branch information
pway99 committed Sep 2, 2022
1 parent f9674f6 commit ccf76c6
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package org.openrewrite.java.migrate.guava;

import org.openrewrite.Applicability;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
Expand Down Expand Up @@ -57,15 +57,9 @@ public Duration getEstimatedEffortPerOccurrence() {
}

@Override
protected JavaVisitor<ExecutionContext> getSingleSourceApplicableTest() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) {
doAfterVisit(new UsesJavaVersion<>(9));
doAfterVisit(new UsesType<>("com.google.common.collect.ImmutableList"));
return cu;
}
};
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return Applicability.and(new UsesJavaVersion<>(9),
new UsesType<>("com.google.common.collect.ImmutableList"));
}

// Code is shared between `NoGuavaImmutableMapOf`, `NoGuavaImmutableListOf`, and `NoGuavaImmutableSetOf`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package org.openrewrite.java.migrate.guava;

import org.openrewrite.Applicability;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
Expand Down Expand Up @@ -57,15 +57,9 @@ public Duration getEstimatedEffortPerOccurrence() {
}

@Override
protected JavaVisitor<ExecutionContext> getSingleSourceApplicableTest() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) {
doAfterVisit(new UsesJavaVersion<>(9));
doAfterVisit(new UsesType<>("com.google.common.collect.ImmutableMap"));
return cu;
}
};
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return Applicability.and(new UsesJavaVersion<>(9),
new UsesType<>("com.google.common.collect.ImmutableMap"));
}

// Code is shared between `NoGuavaImmutableMapOf`, `NoGuavaImmutableListOf`, and `NoGuavaImmutableSetOf`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package org.openrewrite.java.migrate.guava;

import org.openrewrite.Applicability;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
Expand Down Expand Up @@ -60,15 +60,9 @@ public Duration getEstimatedEffortPerOccurrence() {
}

@Override
protected JavaVisitor<ExecutionContext> getSingleSourceApplicableTest() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) {
doAfterVisit(new UsesJavaVersion<>(9));
doAfterVisit(new UsesType<>("com.google.common.collect.ImmutableSet"));
return cu;
}
};
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return Applicability.and(new UsesJavaVersion<>(9),
new UsesType<>("com.google.common.collect.ImmutableSet"));
}

// Code is shared between `NoGuavaImmutableMapOf`, `NoGuavaImmutableListOf`, and `NoGuavaImmutableSetOf`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package org.openrewrite.java.migrate.util;

import org.openrewrite.Applicability;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
Expand Down Expand Up @@ -46,15 +47,9 @@ public String getDescription() {
}

@Override
protected JavaVisitor<ExecutionContext> getSingleSourceApplicableTest() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) {
doAfterVisit(new UsesJavaVersion<>(9));
doAfterVisit(new UsesMethod<>(SINGLETON_LIST));
return cu;
}
};
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return Applicability.and(new UsesJavaVersion<>(9),
new UsesMethod<>(SINGLETON_LIST));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package org.openrewrite.java.migrate.util;

import org.openrewrite.Applicability;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
Expand Down Expand Up @@ -49,15 +50,9 @@ public String getDescription() {
}

@Override
protected JavaVisitor<ExecutionContext> getSingleSourceApplicableTest() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) {
doAfterVisit(new UsesJavaVersion<>(9));
doAfterVisit(new UsesMethod<>(SINGLETON_MAP));
return cu;
}
};
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return Applicability.and(new UsesJavaVersion<>(9),
new UsesMethod<>(SINGLETON_MAP));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package org.openrewrite.java.migrate.util;

import org.openrewrite.Applicability;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
Expand Down Expand Up @@ -46,15 +47,9 @@ public String getDescription() {
}

@Override
protected JavaVisitor<ExecutionContext> getSingleSourceApplicableTest() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) {
doAfterVisit(new UsesJavaVersion<>(9));
doAfterVisit(new UsesMethod<>(SINGLETON_SET));
return cu;
}
};
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return Applicability.and(new UsesJavaVersion<>(9),
new UsesMethod<>(SINGLETON_SET));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package org.openrewrite.java.migrate.util;

import org.openrewrite.Applicability;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
Expand Down Expand Up @@ -50,15 +51,9 @@ public String getDescription() {
}

@Override
protected JavaVisitor<ExecutionContext> getSingleSourceApplicableTest() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) {
doAfterVisit(new UsesJavaVersion<>(9));
doAfterVisit(new UsesMethod<>(UNMODIFIABLE_LIST));
return cu;
}
};
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return Applicability.and(new UsesJavaVersion<>(9),
new UsesMethod<>(UNMODIFIABLE_LIST));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package org.openrewrite.java.migrate.util;

import org.openrewrite.Applicability;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
Expand Down Expand Up @@ -50,15 +51,9 @@ public String getDescription() {
}

@Override
protected JavaVisitor<ExecutionContext> getSingleSourceApplicableTest() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) {
doAfterVisit(new UsesJavaVersion<>(9));
doAfterVisit(new UsesMethod<>(UNMODIFIABLE_SET));
return cu;
}
};
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return Applicability.and(new UsesJavaVersion<>(9),
new UsesMethod<>(UNMODIFIABLE_SET));
}

@Override
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/org/openrewrite/java/migrate/util/UseEnumSetOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@
*/
package org.openrewrite.java.migrate.util;

import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.*;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesJavaVersion;
import org.openrewrite.java.search.UsesMethod;
Expand Down Expand Up @@ -54,15 +50,9 @@ public Duration getEstimatedEffortPerOccurrence() {
}

@Override
protected JavaVisitor<ExecutionContext> getSingleSourceApplicableTest() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) {
doAfterVisit(new UsesJavaVersion<>(9));
doAfterVisit(new UsesMethod<>(SET_OF));
return cu;
}
};
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return Applicability.and(new UsesJavaVersion<>(9),
new UsesMethod<>(SET_OF));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,56 @@ package org.openrewrite.java.migrate.util

import org.junit.jupiter.api.Test
import org.openrewrite.Issue
import org.openrewrite.Recipe
import org.openrewrite.java.JavaRecipeTest
import org.openrewrite.java.Assertions.java
import org.openrewrite.java.Assertions.version
import org.openrewrite.test.RecipeSpec
import org.openrewrite.test.RewriteTest

class MigrateCollectionsSingletonListTest : JavaRecipeTest {
override val recipe: Recipe
get() = MigrateCollectionsSingletonList()
class MigrateCollectionsSingletonListTest : RewriteTest {
override fun defaults(spec: RecipeSpec) {
spec.recipe(MigrateCollectionsSingletonList())
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/72")
@Test
fun singletonList() = assertChanged(
before = """
fun singletonList() = rewriteRun(
version(
java("""
import java.util.*;
class Test {
List<String> list = Collections.singletonList("ABC");
}
""",
after = """
"""
import java.util.List;
class Test {
List<String> list = List.of("ABC");
}
"""
"""), 9)
)

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/72")
@Test
fun singletonListCustomType() = assertChanged(
before = """
fun singletonListCustomType() = rewriteRun(
version(
java("""
import java.util.*;
import java.time.LocalDate;
class Test {
List<LocalDate> list = Collections.singletonList(LocalDate.now());
}
""",
after = """
"""
import java.util.List;
import java.time.LocalDate;
class Test {
List<LocalDate> list = List.of(LocalDate.now());
}
"""
"""), 9)
)

}
Loading

0 comments on commit ccf76c6

Please sign in to comment.