From 6ac3fa22b439a762e145fb86757135c07be19c81 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 3 Jan 2024 10:29:30 +0100 Subject: [PATCH] Decouple TemplateProcessorTest from RefasterTemplateProcessor --- .../RefasterTemplateProcessorTest.java | 10 ++- .../java/template/TemplateProcessorTest.java | 6 +- ...erReuse.java => ParameterReuseRecipe.java} | 24 +++--- .../template/ShouldAddClasspath.java | 78 ------------------- .../template/ShouldAddClasspathRecipes.java | 58 ++++++++++++++ 5 files changed, 77 insertions(+), 99 deletions(-) rename src/test/resources/template/{ParameterReuse.java => ParameterReuseRecipe.java} (56%) delete mode 100644 src/test/resources/template/ShouldAddClasspath.java create mode 100644 src/test/resources/template/ShouldAddClasspathRecipes.java diff --git a/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java b/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java index 1576b7c6..1916d3e2 100644 --- a/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java +++ b/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java @@ -22,7 +22,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.openrewrite.java.template.processor.RefasterTemplateProcessor; -import org.openrewrite.java.template.processor.TemplateProcessor; +import org.openrewrite.java.template.processor.TypeAwareProcessor; import java.io.File; import java.net.URL; @@ -80,10 +80,14 @@ void nestedRecipes(String recipeName) { .hasSourceEquivalentTo(JavaFileObjects.forResource("refaster/" + recipeName + "Recipes.java")); } - static Compilation compile(String resourceName) { + private static Compilation compile(String resourceName) { + return compile(resourceName, new RefasterTemplateProcessor()); + } + + static Compilation compile(String resourceName, TypeAwareProcessor processor) { // As per https://github.com/google/compile-testing/blob/v0.21.0/src/main/java/com/google/testing/compile/package-info.java#L53-L55 return javac() - .withProcessors(new RefasterTemplateProcessor(), new TemplateProcessor()) + .withProcessors(processor) .withClasspath(Arrays.asList( fileForClass(BeforeTemplate.class), fileForClass(AfterTemplate.class), diff --git a/src/test/java/org/openrewrite/java/template/TemplateProcessorTest.java b/src/test/java/org/openrewrite/java/template/TemplateProcessorTest.java index 23c13edf..684e7da8 100644 --- a/src/test/java/org/openrewrite/java/template/TemplateProcessorTest.java +++ b/src/test/java/org/openrewrite/java/template/TemplateProcessorTest.java @@ -20,12 +20,12 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import org.openrewrite.java.template.processor.TemplateProcessor; import static com.google.testing.compile.CompilationSubject.assertThat; import static org.openrewrite.java.template.RefasterTemplateProcessorTest.compile; class TemplateProcessorTest { - @ParameterizedTest @ValueSource(strings = { "Unqualified", @@ -35,7 +35,7 @@ class TemplateProcessorTest { }) void qualification(String qualifier) { // As per https://github.com/google/compile-testing/blob/v0.21.0/src/main/java/com/google/testing/compile/package-info.java#L53-L55 - Compilation compilation = compile("template/ShouldAddClasspath.java"); + Compilation compilation = compile("template/ShouldAddClasspathRecipes.java", new TemplateProcessor()); assertThat(compilation).succeeded(); assertThat(compilation) .generatedSourceFile("foo/ShouldAddClasspathRecipes$" + qualifier + "Recipe$1_before") @@ -47,7 +47,7 @@ void qualification(String qualifier) { @Test void parameterReuse() { - Compilation compilation = compile("template/ParameterReuse.java"); + Compilation compilation = compile("template/ParameterReuseRecipe.java", new TemplateProcessor()); assertThat(compilation).succeeded(); assertThat(compilation) .generatedSourceFile("foo/ParameterReuseRecipe$1_before") diff --git a/src/test/resources/template/ParameterReuse.java b/src/test/resources/template/ParameterReuseRecipe.java similarity index 56% rename from src/test/resources/template/ParameterReuse.java rename to src/test/resources/template/ParameterReuseRecipe.java index ed1058f8..4b9deb59 100644 --- a/src/test/resources/template/ParameterReuse.java +++ b/src/test/resources/template/ParameterReuseRecipe.java @@ -15,20 +15,14 @@ */ package foo; -import com.google.errorprone.refaster.annotation.AfterTemplate; -import com.google.errorprone.refaster.annotation.BeforeTemplate; -import org.openrewrite.java.template.Matches; -import org.openrewrite.java.template.MethodInvocationMatcher; -import org.openrewrite.java.template.NotMatches; +import org.openrewrite.ExecutionContext; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.template.Semantics; -public class ParameterReuse { - @BeforeTemplate - boolean before(String s) { - return s.equals(s); - } - - @AfterTemplate - boolean after() { - return true; - } +public class ParameterReuseRecipe { + JavaIsoVisitor visitor = new JavaIsoVisitor() { + JavaTemplate.Builder before = Semantics.expression(this, "before", (String s) -> s.equals(s)); + JavaTemplate.Builder after = Semantics.expression(this, "after", (String s) -> true); + }; } diff --git a/src/test/resources/template/ShouldAddClasspath.java b/src/test/resources/template/ShouldAddClasspath.java deleted file mode 100644 index bb06e944..00000000 --- a/src/test/resources/template/ShouldAddClasspath.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2023 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package foo; - -import com.google.errorprone.refaster.annotation.AfterTemplate; -import com.google.errorprone.refaster.annotation.BeforeTemplate; -import org.openrewrite.java.template.Primitive; -import org.slf4j.LoggerFactory; - -import java.util.regex.Pattern; - -import static java.util.regex.Pattern.DOTALL; -import static org.slf4j.LoggerFactory.getLogger; - -public class ShouldAddClasspath { - - class Unqualified { - @BeforeTemplate - void before(String message) { - System.out.println(message); - } - - @AfterTemplate - void after(String message) { - getLogger(message); - } - } - - class FullyQualified { - @BeforeTemplate - void before(String message) { - System.out.println(message); - } - - @AfterTemplate - void after(String message) { - org.slf4j.LoggerFactory.getLogger(message); - } - } - - class FullyQualifiedField { - @BeforeTemplate - void before(String message) { - Pattern.compile(message, DOTALL); - } - - @AfterTemplate - void after(String message) { - System.out.println(message); - } - } - - class Primitive { - @BeforeTemplate - void before(@org.openrewrite.java.template.Primitive int i) { - System.out.println(i); - } - - @AfterTemplate - void after(@org.openrewrite.java.template.Primitive int i) { - System.out.print(i); - } - } - -} \ No newline at end of file diff --git a/src/test/resources/template/ShouldAddClasspathRecipes.java b/src/test/resources/template/ShouldAddClasspathRecipes.java new file mode 100644 index 00000000..327f585f --- /dev/null +++ b/src/test/resources/template/ShouldAddClasspathRecipes.java @@ -0,0 +1,58 @@ +/* + * Copyright 2023 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package foo; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import org.openrewrite.ExecutionContext; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.template.Semantics; + +import static java.util.regex.Pattern.DOTALL; +import static org.slf4j.LoggerFactory.getLogger; + +public class ShouldAddClasspathRecipes { + + class UnqualifiedRecipe { + JavaIsoVisitor visitor = new JavaIsoVisitor() { + JavaTemplate.Builder before = Semantics.statement(this, "before", (String message) -> System.out.println(message)); + JavaTemplate.Builder after = Semantics.statement(this, "after", (String message) -> getLogger(message)); + }; + } + + class FullyQualifiedRecipe { + JavaIsoVisitor visitor = new JavaIsoVisitor() { + JavaTemplate.Builder before = Semantics.statement(this, "before", (String message) -> System.out.println(message)); + JavaTemplate.Builder after = Semantics.statement(this, "after", (String message) -> org.slf4j.LoggerFactory.getLogger(message)); + }; + } + + class FullyQualifiedFieldRecipe { + JavaIsoVisitor visitor = new JavaIsoVisitor() { + JavaTemplate.Builder before = Semantics.statement(this, "before", (String message) -> java.util.regex.Pattern.compile(message, DOTALL)); + JavaTemplate.Builder after = Semantics.statement(this, "after", (String message) -> System.out.println(message)); + }; + } + + class PrimitiveRecipe { + JavaIsoVisitor visitor = new JavaIsoVisitor() { + JavaTemplate.Builder before = Semantics.statement(this, "before", (@org.openrewrite.java.template.Primitive Integer i) -> System.out.println(i)); + JavaTemplate.Builder after = Semantics.statement(this, "after", (@org.openrewrite.java.template.Primitive Integer i) -> System.out.print(i)); + }; + } + +}