From d3a2bac528cb2207480fd7ecfe712f791b791fa3 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 25 May 2022 13:22:56 +0200 Subject: [PATCH 01/41] Move `RefasterCheckTest` to `.refastertemplates` package --- .../tech/picnic/errorprone/bugpatterns/RefasterCheck.java | 7 +++---- .../RefasterCheckTest.java | 3 ++- 2 files changed, 5 insertions(+), 5 deletions(-) rename error-prone-contrib/src/test/java/tech/picnic/errorprone/{bugpatterns => refastertemplates}/RefasterCheckTest.java (98%) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java index 81212cd168..ae5899c1c9 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java @@ -8,7 +8,6 @@ import static java.util.function.Predicate.not; import com.google.auto.service.AutoService; -import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; @@ -67,9 +66,9 @@ public final class RefasterCheck extends BugChecker implements CompilationUnitTr private static final String REFASTER_TEMPLATE_SUFFIX = ".refaster"; private static final String INCLUDED_TEMPLATES_PATTERN_FLAG = "Refaster:NamePattern"; - @VisibleForTesting - static final Supplier> ALL_CODE_TRANSFORMERS = - Suppliers.memoize(RefasterCheck::loadAllCodeTransformers); + /** All code transformers loaded by {@link RefasterCheck}. */ + public static final Supplier> + ALL_CODE_TRANSFORMERS = Suppliers.memoize(RefasterCheck::loadAllCodeTransformers); private final CodeTransformer codeTransformer; diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterCheckTest.java similarity index 98% rename from error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java rename to error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterCheckTest.java index 9f4686e4cc..3bca1edaaf 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterCheckTest.java @@ -1,4 +1,4 @@ -package tech.picnic.errorprone.bugpatterns; +package tech.picnic.errorprone.refastertemplates; import static com.google.common.collect.ImmutableSetMultimap.toImmutableSetMultimap; import static java.util.function.Function.identity; @@ -18,6 +18,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import tech.picnic.errorprone.bugpatterns.RefasterCheck; final class RefasterCheckTest { /** The names of all Refaster template groups defined in this module. */ From 238d91b839c844634d0474339af6f24e7694352f Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 28 May 2022 09:14:13 +0200 Subject: [PATCH 02/41] Suggestions --- .../java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java | 2 +- .../picnic/errorprone/refastertemplates/RefasterCheckTest.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java index ae5899c1c9..1571ccf8b2 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java @@ -66,7 +66,7 @@ public final class RefasterCheck extends BugChecker implements CompilationUnitTr private static final String REFASTER_TEMPLATE_SUFFIX = ".refaster"; private static final String INCLUDED_TEMPLATES_PATTERN_FLAG = "Refaster:NamePattern"; - /** All code transformers loaded by {@link RefasterCheck}. */ + /** All code transformers found on the classpath, loaded lazily. */ public static final Supplier> ALL_CODE_TRANSFORMERS = Suppliers.memoize(RefasterCheck::loadAllCodeTransformers); diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterCheckTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterCheckTest.java index 3bca1edaaf..e35f8294a8 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterCheckTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterCheckTest.java @@ -20,6 +20,9 @@ import org.junit.jupiter.params.provider.MethodSource; import tech.picnic.errorprone.bugpatterns.RefasterCheck; +// XXX: This test class is located in a different package than the associated main class. This is +// necessary for this class to be able to load the Refaster templates using +// `BugCheckerRefactoringTestHelper`. final class RefasterCheckTest { /** The names of all Refaster template groups defined in this module. */ private static final ImmutableSet TEMPLATE_GROUPS = From 31ece7e2ceb7b9af415fa732bfc8aaa5c68d9dff Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 28 May 2022 10:45:10 +0200 Subject: [PATCH 03/41] Alternative suggestion --- .../tech/picnic/errorprone/bugpatterns/RefasterCheck.java | 7 ++++--- .../RefasterCheckTest.java | 6 +----- 2 files changed, 5 insertions(+), 8 deletions(-) rename error-prone-contrib/src/test/java/tech/picnic/errorprone/{refastertemplates => bugpatterns}/RefasterCheckTest.java (95%) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java index 1571ccf8b2..81212cd168 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java @@ -8,6 +8,7 @@ import static java.util.function.Predicate.not; import com.google.auto.service.AutoService; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; @@ -66,9 +67,9 @@ public final class RefasterCheck extends BugChecker implements CompilationUnitTr private static final String REFASTER_TEMPLATE_SUFFIX = ".refaster"; private static final String INCLUDED_TEMPLATES_PATTERN_FLAG = "Refaster:NamePattern"; - /** All code transformers found on the classpath, loaded lazily. */ - public static final Supplier> - ALL_CODE_TRANSFORMERS = Suppliers.memoize(RefasterCheck::loadAllCodeTransformers); + @VisibleForTesting + static final Supplier> ALL_CODE_TRANSFORMERS = + Suppliers.memoize(RefasterCheck::loadAllCodeTransformers); private final CodeTransformer codeTransformer; diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterCheckTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java similarity index 95% rename from error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterCheckTest.java rename to error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java index e35f8294a8..9f4686e4cc 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterCheckTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java @@ -1,4 +1,4 @@ -package tech.picnic.errorprone.refastertemplates; +package tech.picnic.errorprone.bugpatterns; import static com.google.common.collect.ImmutableSetMultimap.toImmutableSetMultimap; import static java.util.function.Function.identity; @@ -18,11 +18,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import tech.picnic.errorprone.bugpatterns.RefasterCheck; -// XXX: This test class is located in a different package than the associated main class. This is -// necessary for this class to be able to load the Refaster templates using -// `BugCheckerRefactoringTestHelper`. final class RefasterCheckTest { /** The names of all Refaster template groups defined in this module. */ private static final ImmutableSet TEMPLATE_GROUPS = From 66ae36b9d5f2f27fab5ac467d83ff065acb71f19 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 10 Jan 2022 08:49:34 +0100 Subject: [PATCH 04/41] Introduce validator for Refaster template collections and tests New method: `RefasterCollectionTestUtil#validateTemplateCollection`. This can verify that all Refaster templates from a given template collection are covered by at least one test and that the match rewrites code in the correct test method. Introduce the following modules: - `refaster-runner` - `refaster-test-support` --- error-prone-contrib/pom.xml | 5 + .../RefasterTemplatesTest.java | 17 ++ .../AssertJBigDecimalTemplatesTestInput.java | 1 + .../AssertJBigDecimalTemplatesTestOutput.java | 1 + .../AssertJBigIntegerTemplatesTestInput.java | 1 + .../AssertJBigIntegerTemplatesTestOutput.java | 1 + .../AssertJBooleanTemplatesTestInput.java | 1 + .../AssertJBooleanTemplatesTestOutput.java | 1 + .../AssertJByteTemplatesTestInput.java | 1 + .../AssertJByteTemplatesTestOutput.java | 1 + ...AssertJCharSequenceTemplatesTestInput.java | 1 + ...ssertJCharSequenceTemplatesTestOutput.java | 1 + .../AssertJDoubleTemplatesTestInput.java | 1 + .../AssertJDoubleTemplatesTestOutput.java | 1 + .../AssertJEnumerableTemplatesTestInput.java | 1 + .../AssertJEnumerableTemplatesTestOutput.java | 1 + .../AssertJFloatTemplatesTestInput.java | 1 + .../AssertJFloatTemplatesTestOutput.java | 1 + .../AssertJIntegerTemplatesTestInput.java | 1 + .../AssertJIntegerTemplatesTestOutput.java | 1 + .../AssertJLongTemplatesTestInput.java | 1 + .../AssertJLongTemplatesTestOutput.java | 1 + .../AssertJNumberTemplatesTestInput.java | 1 + .../AssertJNumberTemplatesTestOutput.java | 1 + .../AssertJObjectTemplatesTestInput.java | 1 + .../AssertJObjectTemplatesTestOutput.java | 1 + .../AssertJOptionalTemplatesTestInput.java | 1 + .../AssertJOptionalTemplatesTestOutput.java | 1 + .../AssertJShortTemplatesTestInput.java | 1 + .../AssertJShortTemplatesTestOutput.java | 1 + .../AssertJStringTemplatesTestInput.java | 1 + .../AssertJStringTemplatesTestOutput.java | 1 + .../AssortedTemplatesTestInput.java | 1 + .../AssortedTemplatesTestOutput.java | 1 + .../BigDecimalTemplatesTestInput.java | 1 + .../BigDecimalTemplatesTestOutput.java | 1 + .../CollectionTemplatesTestInput.java | 1 + .../CollectionTemplatesTestOutput.java | 1 + .../ComparatorTemplatesTestInput.java | 1 + .../ComparatorTemplatesTestOutput.java | 1 + .../DoubleStreamTemplatesTestInput.java | 1 + .../DoubleStreamTemplatesTestOutput.java | 1 + .../EqualityTemplatesTestInput.java | 1 + .../EqualityTemplatesTestOutput.java | 1 + ...mutableListMultimapTemplatesTestInput.java | 1 + ...utableListMultimapTemplatesTestOutput.java | 1 + .../ImmutableListTemplatesTestInput.java | 1 + .../ImmutableListTemplatesTestOutput.java | 1 + .../ImmutableMapTemplatesTestInput.java | 1 + .../ImmutableMapTemplatesTestOutput.java | 1 + .../ImmutableMultisetTemplatesTestInput.java | 1 + .../ImmutableMultisetTemplatesTestOutput.java | 1 + ...mmutableSetMultimapTemplatesTestInput.java | 1 + ...mutableSetMultimapTemplatesTestOutput.java | 1 + .../ImmutableSetTemplatesTestInput.java | 1 + .../ImmutableSetTemplatesTestOutput.java | 1 + .../ImmutableSortedMapTemplatesTestInput.java | 1 + ...ImmutableSortedMapTemplatesTestOutput.java | 1 + ...tableSortedMultisetTemplatesTestInput.java | 1 + ...ableSortedMultisetTemplatesTestOutput.java | 1 + .../ImmutableSortedSetTemplatesTestInput.java | 1 + ...ImmutableSortedSetTemplatesTestOutput.java | 1 + .../IntStreamTemplatesTestInput.java | 1 + .../IntStreamTemplatesTestOutput.java | 1 + .../JUnitTemplatesTestInput.java | 1 + .../JUnitTemplatesTestOutput.java | 1 + .../LongStreamTemplatesTestInput.java | 1 + .../LongStreamTemplatesTestOutput.java | 1 + .../MapEntryTemplatesTestInput.java | 1 + .../MapEntryTemplatesTestOutput.java | 1 + .../MockitoTemplatesTestInput.java | 1 + .../MockitoTemplatesTestOutput.java | 1 + .../MultimapTemplatesTestInput.java | 1 + .../MultimapTemplatesTestOutput.java | 1 + .../NullTemplatesTestInput.java | 1 + .../NullTemplatesTestOutput.java | 1 + .../OptionalTemplatesTestInput.java | 1 + .../OptionalTemplatesTestOutput.java | 1 + .../PrimitiveTemplatesTestInput.java | 1 + .../PrimitiveTemplatesTestOutput.java | 1 + .../ReactorTemplatesTestInput.java | 1 + .../ReactorTemplatesTestOutput.java | 1 + .../RxJava2AdapterTemplatesTestInput.java | 1 + .../RxJava2AdapterTemplatesTestOutput.java | 1 + .../StreamTemplatesTestInput.java | 1 + .../StreamTemplatesTestOutput.java | 1 + .../StringTemplatesTestInput.java | 1 + .../StringTemplatesTestOutput.java | 1 + .../TestNGToAssertJTemplatesTestInput.java | 1 + .../TestNGToAssertJTemplatesTestOutput.java | 1 + .../TimeTemplatesTestInput.java | 1 + .../TimeTemplatesTestOutput.java | 1 + .../WebClientTemplatesTestInput.java | 4 +- .../WebClientTemplatesTestOutput.java | 4 +- pom.xml | 7 + refaster-runner/pom.xml | 53 +++++ .../refaster/runner/CodeTransformers.java | 76 ++++++ .../refaster/runner}/RefasterCheck.java | 72 +----- .../refaster/runner/package-info.java | 4 + refaster-test-support/pom.xml | 117 ++++++++++ .../test/RefasterCollectionTestUtil.java | 219 ++++++++++++++++++ .../test}/RefasterTemplateTestCase.java | 9 +- .../refaster/test/package-info.java | 4 + .../test/MatchInWrongMethodTemplates.java | 20 ++ .../test/MethodNameWithNumberTemplates.java | 20 ++ .../MissingTestAndWrongTestTemplates.java | 27 +++ .../test/RefasterCollectionTestUtilTest.java | 18 ++ .../test/TemplateWithoutTestTemplates.java | 34 +++ .../MatchInWrongMethodTemplatesTestInput.java | 9 + ...MatchInWrongMethodTemplatesTestOutput.java | 14 ++ ...ethodNameWithNumberTemplatesTestInput.java | 21 ++ ...thodNameWithNumberTemplatesTestOutput.java | 21 ++ ...ingTestAndWrongTestTemplatesTestInput.java | 9 + ...ngTestAndWrongTestTemplatesTestOutput.java | 17 ++ ...TemplateWithoutTestTemplatesTestInput.java | 7 + ...emplateWithoutTestTemplatesTestOutput.java | 11 + 116 files changed, 838 insertions(+), 71 deletions(-) create mode 100644 error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java create mode 100644 refaster-runner/pom.xml create mode 100644 refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java rename {error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns => refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner}/RefasterCheck.java (72%) create mode 100644 refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java create mode 100644 refaster-test-support/pom.xml create mode 100644 refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java rename {error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates => refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test}/RefasterTemplateTestCase.java (82%) create mode 100644 refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java create mode 100644 refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplates.java create mode 100644 refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java create mode 100644 refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplates.java create mode 100644 refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java create mode 100644 refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplates.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index 4fd14a1f28..944acb8855 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -51,6 +51,11 @@ ${project.groupId} refaster-support + + ${project.groupId} + refaster-test-support + test + com.fasterxml.jackson.core jackson-annotations diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java new file mode 100644 index 0000000000..49648f639c --- /dev/null +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java @@ -0,0 +1,17 @@ +package tech.picnic.errorprone.refastertemplates; + +import org.junit.jupiter.api.Test; +import tech.picnic.errorprone.refaster.test.RefasterCollectionTestUtil; + +final class RefasterTemplatesTest { + + @Test + void webClientTemplates() { + RefasterCollectionTestUtil.validateTemplateCollection(WebClientTemplates.class); + } + + @Test + void reactorTemplates() { + RefasterCollectionTestUtil.validateTemplateCollection(ReactorTemplates.class); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplatesTestInput.java index 370a6cf13b..c8ecc2ecb8 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplatesTestInput.java @@ -7,6 +7,7 @@ import com.google.common.collect.ImmutableSet; import java.math.BigDecimal; import org.assertj.core.api.AbstractBigDecimalAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJBigDecimalTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplatesTestOutput.java index c3cb6e3f25..9807aa5488 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplatesTestOutput.java @@ -7,6 +7,7 @@ import com.google.common.collect.ImmutableSet; import java.math.BigDecimal; import org.assertj.core.api.AbstractBigDecimalAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJBigDecimalTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigIntegerTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigIntegerTemplatesTestInput.java index 7aab3da716..3bbbcf0380 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigIntegerTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigIntegerTemplatesTestInput.java @@ -7,6 +7,7 @@ import com.google.common.collect.ImmutableSet; import java.math.BigInteger; import org.assertj.core.api.AbstractBigIntegerAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJBigIntegerTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigIntegerTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigIntegerTemplatesTestOutput.java index 9106ecdcb2..9674b649dd 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigIntegerTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBigIntegerTemplatesTestOutput.java @@ -7,6 +7,7 @@ import com.google.common.collect.ImmutableSet; import java.math.BigInteger; import org.assertj.core.api.AbstractBigIntegerAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJBigIntegerTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBooleanTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBooleanTemplatesTestInput.java index 1193dc6f0d..9181a58247 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBooleanTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBooleanTemplatesTestInput.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractBooleanAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJBooleanTemplatesTest implements RefasterTemplateTestCase { AbstractBooleanAssert testAbstractBooleanAssertIsEqualTo() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBooleanTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBooleanTemplatesTestOutput.java index 110a421c28..da458b13b9 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBooleanTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJBooleanTemplatesTestOutput.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractBooleanAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJBooleanTemplatesTest implements RefasterTemplateTestCase { AbstractBooleanAssert testAbstractBooleanAssertIsEqualTo() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJByteTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJByteTemplatesTestInput.java index 5c0e7a7834..f9a7b2607e 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJByteTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJByteTemplatesTestInput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractByteAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJByteTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJByteTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJByteTemplatesTestOutput.java index 6bf3250142..ea274127a4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJByteTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJByteTemplatesTestOutput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractByteAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJByteTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJCharSequenceTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJCharSequenceTemplatesTestInput.java index 5a71e7a6a8..3cad90b1a0 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJCharSequenceTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJCharSequenceTemplatesTestInput.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJCharSequenceTemplatesTest implements RefasterTemplateTestCase { void testAssertThatCharSequenceIsEmpty() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJCharSequenceTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJCharSequenceTemplatesTestOutput.java index 6395d7258a..9d0fc29bb3 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJCharSequenceTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJCharSequenceTemplatesTestOutput.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJCharSequenceTemplatesTest implements RefasterTemplateTestCase { void testAssertThatCharSequenceIsEmpty() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJDoubleTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJDoubleTemplatesTestInput.java index 95fb107dbf..04eb0c7206 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJDoubleTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJDoubleTemplatesTestInput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractDoubleAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJDoubleTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJDoubleTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJDoubleTemplatesTestOutput.java index d657c3dde8..12ec9c93be 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJDoubleTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJDoubleTemplatesTestOutput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractDoubleAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJDoubleTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestInput.java index 693fd1c4af..7374a993eb 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestInput.java @@ -5,6 +5,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import org.assertj.core.api.EnumerableAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJEnumableTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestOutput.java index 5b7e9013e4..598a38168f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJEnumerableTemplatesTestOutput.java @@ -5,6 +5,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import org.assertj.core.api.EnumerableAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJEnumableTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJFloatTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJFloatTemplatesTestInput.java index 88ced7b924..54b4fac623 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJFloatTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJFloatTemplatesTestInput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractFloatAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJFloatTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJFloatTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJFloatTemplatesTestOutput.java index 01dc692db7..40b9626372 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJFloatTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJFloatTemplatesTestOutput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractFloatAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJFloatTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJIntegerTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJIntegerTemplatesTestInput.java index 320496d205..6b4b7a38c3 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJIntegerTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJIntegerTemplatesTestInput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractIntegerAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJIntegerTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJIntegerTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJIntegerTemplatesTestOutput.java index da912f04e3..87613912ff 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJIntegerTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJIntegerTemplatesTestOutput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractIntegerAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJIntegerTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJLongTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJLongTemplatesTestInput.java index cee37b2227..b55a52774f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJLongTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJLongTemplatesTestInput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractLongAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJLongTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJLongTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJLongTemplatesTestOutput.java index 5a54440edf..1c7a851983 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJLongTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJLongTemplatesTestOutput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractLongAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJLongTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplatesTestInput.java index 22193c9dcd..d92b1cdf14 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplatesTestInput.java @@ -6,6 +6,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import org.assertj.core.api.NumberAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase { ImmutableSet> testNumberAssertIsPositive() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplatesTestOutput.java index a01f1b490f..72b9f27ce4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplatesTestOutput.java @@ -6,6 +6,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import org.assertj.core.api.NumberAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase { ImmutableSet> testNumberAssertIsPositive() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJObjectTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJObjectTemplatesTestInput.java index 521143a501..391c6b1228 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJObjectTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJObjectTemplatesTestInput.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import org.assertj.core.api.AbstractAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJObjectTemplatesTest implements RefasterTemplateTestCase { AbstractAssert testAssertThatIsInstanceOf() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJObjectTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJObjectTemplatesTestOutput.java index 35a273a7d0..3e68603b4a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJObjectTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJObjectTemplatesTestOutput.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import org.assertj.core.api.AbstractAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJObjectTemplatesTest implements RefasterTemplateTestCase { AbstractAssert testAssertThatIsInstanceOf() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJOptionalTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJOptionalTemplatesTestInput.java index 86f91856b5..ca6be8ec0b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJOptionalTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJOptionalTemplatesTestInput.java @@ -6,6 +6,7 @@ import java.util.Optional; import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.OptionalAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJOptionalTemplatesTest implements RefasterTemplateTestCase { AbstractAssert testAssertThatOptional() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJOptionalTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJOptionalTemplatesTestOutput.java index 29bb84d576..df5f706c31 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJOptionalTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJOptionalTemplatesTestOutput.java @@ -6,6 +6,7 @@ import java.util.Optional; import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.OptionalAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJOptionalTemplatesTest implements RefasterTemplateTestCase { AbstractAssert testAssertThatOptional() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJShortTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJShortTemplatesTestInput.java index c79f5d2f8f..e4fc739373 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJShortTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJShortTemplatesTestInput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractShortAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJShortTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJShortTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJShortTemplatesTestOutput.java index d051c08034..58beb71e24 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJShortTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJShortTemplatesTestOutput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractShortAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJShortTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJStringTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJStringTemplatesTestInput.java index 4023eb1f60..e699740a83 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJStringTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJStringTemplatesTestInput.java @@ -4,6 +4,7 @@ import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.AbstractStringAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJStringTemplatesTest implements RefasterTemplateTestCase { void testAbstractStringAssertStringIsEmpty() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJStringTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJStringTemplatesTestOutput.java index 759b00f0cb..1e048f94a2 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJStringTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJStringTemplatesTestOutput.java @@ -4,6 +4,7 @@ import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.AbstractStringAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJStringTemplatesTest implements RefasterTemplateTestCase { void testAbstractStringAssertStringIsEmpty() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssortedTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssortedTemplatesTestInput.java index efa49a9753..d4023e4546 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssortedTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssortedTemplatesTestInput.java @@ -17,6 +17,7 @@ import java.util.HashSet; import java.util.Map; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssortedTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssortedTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssortedTemplatesTestOutput.java index ea92986131..10809a7134 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssortedTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssortedTemplatesTestOutput.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssortedTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/BigDecimalTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/BigDecimalTemplatesTestInput.java index 8ccae257f3..878b1bac28 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/BigDecimalTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/BigDecimalTemplatesTestInput.java @@ -2,6 +2,7 @@ import com.google.common.collect.ImmutableSet; import java.math.BigDecimal; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class BigDecimalTemplatesTest implements RefasterTemplateTestCase { ImmutableSet testBigDecimalZero() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/BigDecimalTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/BigDecimalTemplatesTestOutput.java index e92630e76a..1a379ce9f7 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/BigDecimalTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/BigDecimalTemplatesTestOutput.java @@ -2,6 +2,7 @@ import com.google.common.collect.ImmutableSet; import java.math.BigDecimal; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class BigDecimalTemplatesTest implements RefasterTemplateTestCase { ImmutableSet testBigDecimalZero() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/CollectionTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/CollectionTemplatesTestInput.java index ac952b70d0..a5316647c5 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/CollectionTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/CollectionTemplatesTestInput.java @@ -11,6 +11,7 @@ import java.util.Optional; import java.util.TreeSet; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class CollectionTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/CollectionTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/CollectionTemplatesTestOutput.java index 51807141d4..592621b77b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/CollectionTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/CollectionTemplatesTestOutput.java @@ -11,6 +11,7 @@ import java.util.Optional; import java.util.TreeSet; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class CollectionTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ComparatorTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ComparatorTemplatesTestInput.java index 406b1da7b3..39c10f8add 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ComparatorTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ComparatorTemplatesTestInput.java @@ -8,6 +8,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ComparatorTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ComparatorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ComparatorTemplatesTestOutput.java index 77da76b222..366da20139 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ComparatorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ComparatorTemplatesTestOutput.java @@ -10,6 +10,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ComparatorTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/DoubleStreamTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/DoubleStreamTemplatesTestInput.java index fe20205972..37bd64f8f7 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/DoubleStreamTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/DoubleStreamTemplatesTestInput.java @@ -6,6 +6,7 @@ import java.util.function.DoublePredicate; import java.util.stream.DoubleStream; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class DoubleStreamTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/DoubleStreamTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/DoubleStreamTemplatesTestOutput.java index 75b0e8993f..de39e49000 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/DoubleStreamTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/DoubleStreamTemplatesTestOutput.java @@ -6,6 +6,7 @@ import java.util.function.DoublePredicate; import java.util.stream.DoubleStream; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class DoubleStreamTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/EqualityTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/EqualityTemplatesTestInput.java index f331f88586..f8299fc8ad 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/EqualityTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/EqualityTemplatesTestInput.java @@ -5,6 +5,7 @@ import java.math.RoundingMode; import java.util.Objects; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class EqualityTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/EqualityTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/EqualityTemplatesTestOutput.java index c5ac531191..f142f465f1 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/EqualityTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/EqualityTemplatesTestOutput.java @@ -5,6 +5,7 @@ import java.math.RoundingMode; import java.util.Objects; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class EqualityTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListMultimapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListMultimapTemplatesTestInput.java index be01cdaf0e..f840b67d17 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListMultimapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListMultimapTemplatesTestInput.java @@ -16,6 +16,7 @@ import com.google.common.collect.TreeMultimap; import java.util.Map; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableListMultimapTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListMultimapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListMultimapTemplatesTestOutput.java index cdbb663ea8..7f7e7888b0 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListMultimapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListMultimapTemplatesTestOutput.java @@ -16,6 +16,7 @@ import com.google.common.collect.TreeMultimap; import java.util.Map; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableListMultimapTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListTemplatesTestInput.java index 1a93261fd8..0afd20b0e1 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListTemplatesTestInput.java @@ -11,6 +11,7 @@ import java.util.Comparator; import java.util.List; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableListTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListTemplatesTestOutput.java index 1ba66ba42e..915ab1e1df 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableListTemplatesTestOutput.java @@ -12,6 +12,7 @@ import java.util.Comparator; import java.util.List; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableListTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplatesTestInput.java index 5995d955d7..b2667b9f58 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplatesTestInput.java @@ -12,6 +12,7 @@ import java.util.Collections; import java.util.Map; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableMapTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplatesTestOutput.java index 36d7b5bdae..77153c7466 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplatesTestOutput.java @@ -12,6 +12,7 @@ import java.util.Collections; import java.util.Map; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableMapTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMultisetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMultisetTemplatesTestInput.java index ef50b18791..92f4ed84f1 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMultisetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMultisetTemplatesTestInput.java @@ -8,6 +8,7 @@ import com.google.common.collect.Streams; import java.util.Arrays; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableMultisetTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMultisetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMultisetTemplatesTestOutput.java index 6004eafaf4..84f5122d84 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMultisetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableMultisetTemplatesTestOutput.java @@ -8,6 +8,7 @@ import com.google.common.collect.Streams; import java.util.Arrays; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableMultisetTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetMultimapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetMultimapTemplatesTestInput.java index 3d3ac31850..2f2cd08f0c 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetMultimapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetMultimapTemplatesTestInput.java @@ -13,6 +13,7 @@ import com.google.common.collect.TreeMultimap; import java.util.Map; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableSetMultimapTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetMultimapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetMultimapTemplatesTestOutput.java index 647d9b96b3..f887977a8b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetMultimapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetMultimapTemplatesTestOutput.java @@ -13,6 +13,7 @@ import com.google.common.collect.TreeMultimap; import java.util.Map; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableSetMultimapTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplatesTestInput.java index 29891c942b..d2b38ad2ac 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplatesTestInput.java @@ -10,6 +10,7 @@ import java.util.Collections; import java.util.Set; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableSetTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplatesTestOutput.java index 3943f5908f..e6ad2e44ad 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplatesTestOutput.java @@ -10,6 +10,7 @@ import java.util.Collections; import java.util.Set; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableSetTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMapTemplatesTestInput.java index 1443707ece..a2df4ae2b0 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMapTemplatesTestInput.java @@ -10,6 +10,7 @@ import java.util.Comparator; import java.util.Map; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableSortedMapTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMapTemplatesTestOutput.java index 98c212f89f..7f394fa1f1 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMapTemplatesTestOutput.java @@ -10,6 +10,7 @@ import java.util.Comparator; import java.util.Map; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableSortedMapTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMultisetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMultisetTemplatesTestInput.java index f42fd53211..085fb1e8c6 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMultisetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMultisetTemplatesTestInput.java @@ -11,6 +11,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableSortedMultisetTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMultisetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMultisetTemplatesTestOutput.java index 818a933154..843c2922e6 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMultisetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedMultisetTemplatesTestOutput.java @@ -11,6 +11,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableSortedMultisetTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedSetTemplatesTestInput.java index 02181575c0..4a90a87457 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedSetTemplatesTestInput.java @@ -10,6 +10,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableSortedSetTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedSetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedSetTemplatesTestOutput.java index 2d696c95fe..b944a2ef31 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedSetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ImmutableSortedSetTemplatesTestOutput.java @@ -10,6 +10,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ImmutableSortedSetTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/IntStreamTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/IntStreamTemplatesTestInput.java index fe7d93763d..03cf380352 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/IntStreamTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/IntStreamTemplatesTestInput.java @@ -6,6 +6,7 @@ import java.util.function.IntPredicate; import java.util.stream.IntStream; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class IntStreamTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/IntStreamTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/IntStreamTemplatesTestOutput.java index 30260b53d6..c40930f27a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/IntStreamTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/IntStreamTemplatesTestOutput.java @@ -6,6 +6,7 @@ import java.util.function.IntPredicate; import java.util.stream.IntStream; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class IntStreamTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/JUnitTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/JUnitTemplatesTestInput.java index 88d26b05f4..77c83e8916 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/JUnitTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/JUnitTemplatesTestInput.java @@ -2,6 +2,7 @@ import com.google.common.collect.ImmutableSet; import org.junit.jupiter.params.provider.Arguments; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class JUnitTemplatesTest implements RefasterTemplateTestCase { ImmutableSet testArgumentsEnumeration() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/JUnitTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/JUnitTemplatesTestOutput.java index 6e8e77582c..13151144b2 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/JUnitTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/JUnitTemplatesTestOutput.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableSet; import org.junit.jupiter.params.provider.Arguments; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class JUnitTemplatesTest implements RefasterTemplateTestCase { ImmutableSet testArgumentsEnumeration() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/LongStreamTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/LongStreamTemplatesTestInput.java index fd284c65fe..51a32a1d3a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/LongStreamTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/LongStreamTemplatesTestInput.java @@ -6,6 +6,7 @@ import java.util.function.LongPredicate; import java.util.stream.LongStream; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class LongStreamTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/LongStreamTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/LongStreamTemplatesTestOutput.java index 6b1b54229a..c87219b7fd 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/LongStreamTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/LongStreamTemplatesTestOutput.java @@ -6,6 +6,7 @@ import java.util.function.LongPredicate; import java.util.stream.LongStream; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class LongStreamTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MapEntryTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MapEntryTemplatesTestInput.java index c9203ab5ee..e829d26833 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MapEntryTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MapEntryTemplatesTestInput.java @@ -5,6 +5,7 @@ import java.util.AbstractMap; import java.util.Comparator; import java.util.Map; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class MapEntryTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MapEntryTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MapEntryTemplatesTestOutput.java index bf1327aeb7..056bb169ee 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MapEntryTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MapEntryTemplatesTestOutput.java @@ -8,6 +8,7 @@ import java.util.AbstractMap; import java.util.Comparator; import java.util.Map; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class MapEntryTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MockitoTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MockitoTemplatesTestInput.java index 676a15f5c1..f48eec9c48 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MockitoTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MockitoTemplatesTestInput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import org.mockito.verification.VerificationMode; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class MockitoTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MockitoTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MockitoTemplatesTestOutput.java index 258fab726a..367ffba744 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MockitoTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MockitoTemplatesTestOutput.java @@ -7,6 +7,7 @@ import com.google.common.collect.ImmutableSet; import org.mockito.verification.VerificationMode; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class MockitoTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MultimapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MultimapTemplatesTestInput.java index 5528056bbd..3348249ba7 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MultimapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MultimapTemplatesTestInput.java @@ -6,6 +6,7 @@ import com.google.common.collect.Multimaps; import java.util.Collection; import java.util.Set; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class MultimapTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MultimapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MultimapTemplatesTestOutput.java index 5c027237fb..0aeab3fc01 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MultimapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/MultimapTemplatesTestOutput.java @@ -6,6 +6,7 @@ import com.google.common.collect.Multimaps; import java.util.Collection; import java.util.Set; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class MultimapTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/NullTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/NullTemplatesTestInput.java index 7b28ad4ae0..ac02a7b084 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/NullTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/NullTemplatesTestInput.java @@ -3,6 +3,7 @@ import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableSet; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class NullTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/NullTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/NullTemplatesTestOutput.java index cda3ab9634..617aa31dbc 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/NullTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/NullTemplatesTestOutput.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSet; import java.util.Objects; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class NullTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/OptionalTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/OptionalTemplatesTestInput.java index a954478eb0..d32ba426d2 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/OptionalTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/OptionalTemplatesTestInput.java @@ -5,6 +5,7 @@ import java.util.Optional; import java.util.function.Function; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class OptionalTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/OptionalTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/OptionalTemplatesTestOutput.java index 100d8aca28..d7e4f8573f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/OptionalTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/OptionalTemplatesTestOutput.java @@ -7,6 +7,7 @@ import java.util.Optional; import java.util.function.Function; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class OptionalTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/PrimitiveTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/PrimitiveTemplatesTestInput.java index 1580b6063a..d41931b501 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/PrimitiveTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/PrimitiveTemplatesTestInput.java @@ -2,6 +2,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.primitives.Ints; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class PrimitiveTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/PrimitiveTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/PrimitiveTemplatesTestOutput.java index 91caf7c011..ba2647ad83 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/PrimitiveTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/PrimitiveTemplatesTestOutput.java @@ -2,6 +2,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.primitives.Ints; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class PrimitiveTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java index ca0bd1d486..0fc64f7670 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java @@ -11,6 +11,7 @@ import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import reactor.test.publisher.PublisherProbe; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ReactorTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java index 90f73ae0ba..18fd8b92b9 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java @@ -12,6 +12,7 @@ import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import reactor.test.publisher.PublisherProbe; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class ReactorTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/RxJava2AdapterTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/RxJava2AdapterTemplatesTestInput.java index 7169ebfb98..42fc8b73c6 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/RxJava2AdapterTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/RxJava2AdapterTemplatesTestInput.java @@ -12,6 +12,7 @@ import reactor.adapter.rxjava.RxJava2Adapter; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class RxJava2AdapterTemplatesTest implements RefasterTemplateTestCase { ImmutableSet> testCompletableToMono() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/RxJava2AdapterTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/RxJava2AdapterTemplatesTestOutput.java index 4a523d60e6..6ad2980890 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/RxJava2AdapterTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/RxJava2AdapterTemplatesTestOutput.java @@ -12,6 +12,7 @@ import reactor.adapter.rxjava.RxJava2Adapter; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class RxJava2AdapterTemplatesTest implements RefasterTemplateTestCase { ImmutableSet> testCompletableToMono() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StreamTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StreamTemplatesTestInput.java index 2384fa9409..f59538c6b0 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StreamTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StreamTemplatesTestInput.java @@ -11,6 +11,7 @@ import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class StreamTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StreamTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StreamTemplatesTestOutput.java index 071f789045..c41746138d 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StreamTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StreamTemplatesTestOutput.java @@ -13,6 +13,7 @@ import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class StreamTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StringTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StringTemplatesTestInput.java index 3d2b50ec31..bbf600371a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StringTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StringTemplatesTestInput.java @@ -12,6 +12,7 @@ import java.util.Arrays; import java.util.Optional; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class StringTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StringTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StringTemplatesTestOutput.java index 4f9067c88c..b37280462f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StringTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/StringTemplatesTestOutput.java @@ -13,6 +13,7 @@ import java.util.Arrays; import java.util.Optional; import java.util.stream.Stream; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class StringTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TestNGToAssertJTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TestNGToAssertJTemplatesTestInput.java index 67bc31e03c..34d0a9fd48 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TestNGToAssertJTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TestNGToAssertJTemplatesTestInput.java @@ -17,6 +17,7 @@ import com.google.common.collect.Iterators; import java.util.ArrayList; import java.util.Collections; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class TestNGToAssertJTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TestNGToAssertJTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TestNGToAssertJTemplatesTestOutput.java index 8e10a5ef06..496620306b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TestNGToAssertJTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TestNGToAssertJTemplatesTestOutput.java @@ -22,6 +22,7 @@ import com.google.common.collect.Iterators; import java.util.ArrayList; import java.util.Collections; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class TestNGToAssertJTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TimeTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TimeTemplatesTestInput.java index f212564c56..09060a69b1 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TimeTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TimeTemplatesTestInput.java @@ -13,6 +13,7 @@ import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class TimeTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TimeTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TimeTemplatesTestOutput.java index b4215e599b..215c93fe8c 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TimeTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/TimeTemplatesTestOutput.java @@ -13,6 +13,7 @@ import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class TimeTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestInput.java index fd33f65edb..1832be992c 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestInput.java @@ -11,6 +11,7 @@ import com.google.common.collect.ImmutableSet; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.web.reactive.function.client.WebClient; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class WebClientTemplatesTest implements RefasterTemplateTestCase { @Override @@ -18,8 +19,9 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(fromValue(""), GET, HEAD, OPTIONS, PATCH, POST, PUT); } - ImmutableSet testBodyValue() { + ImmutableSet testBodyVallue() { return ImmutableSet.of( + WebClient.create().post().body(fromValue("bar")), WebClient.create().post().body(fromValue("bar")), WebTestClient.bindToServer().build().post().body(fromValue("bar"))); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestOutput.java index 4f3685963d..de1380a7aa 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestOutput.java @@ -11,6 +11,7 @@ import com.google.common.collect.ImmutableSet; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.web.reactive.function.client.WebClient; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class WebClientTemplatesTest implements RefasterTemplateTestCase { @Override @@ -18,8 +19,9 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(fromValue(""), GET, HEAD, OPTIONS, PATCH, POST, PUT); } - ImmutableSet testBodyValue() { + ImmutableSet testBodyVallue() { return ImmutableSet.of( + WebClient.create().post().bodyValue("bar"), WebClient.create().post().bodyValue("bar"), WebTestClient.bindToServer().build().post().bodyValue("bar")); } diff --git a/pom.xml b/pom.xml index e5423c0ee9..968d75a624 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,9 @@ error-prone-contrib refaster-compiler + refaster-runner refaster-support + refaster-test-support @@ -193,6 +195,11 @@ refaster-support ${project.version} + + ${project.groupId} + refaster-test-support + ${project.version} + com.fasterxml.jackson jackson-bom diff --git a/refaster-runner/pom.xml b/refaster-runner/pom.xml new file mode 100644 index 0000000000..a273ca9938 --- /dev/null +++ b/refaster-runner/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + + tech.picnic.error-prone-support + error-prone-support + 0.0.1-SNAPSHOT + + + refaster-runner + + Picnic :: Error Prone Support :: Refaster Runner + Apply Refaster templates found on the classpath. + + + + ${groupId.error-prone} + error_prone_annotation + provided + + + ${groupId.error-prone} + error_prone_annotations + provided + + + ${groupId.error-prone} + error_prone_check_api + provided + + + com.google.auto.service + auto-service-annotations + provided + + + com.google.code.findbugs + jsr305 + provided + + + com.google.errorprone + javac + provided + + + com.google.guava + guava + provided + + + diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java new file mode 100644 index 0000000000..5f97ef47e6 --- /dev/null +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java @@ -0,0 +1,76 @@ +package tech.picnic.errorprone.refaster.runner; + +import com.google.common.collect.ImmutableListMultimap; +import com.google.common.collect.ImmutableSet; +import com.google.common.reflect.ClassPath; +import com.google.common.reflect.ClassPath.ResourceInfo; +import com.google.errorprone.CodeTransformer; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.UncheckedIOException; +import java.util.NoSuchElementException; +import java.util.Optional; + +/** Scans the classpath for `.refaster` files and loads them as {@link CodeTransformer}s. */ +public final class CodeTransformers { + private static final String REFASTER_TEMPLATE_SUFFIX = ".refaster"; + + private CodeTransformers() {} + + /** + * Use the classpath to find Refaster templates and return a map of the names with {@link + * CodeTransformer}s. + * + * @return A mapping from Refaster template names to {@link CodeTransformer}s. + */ + public static ImmutableListMultimap loadAllCodeTransformers() { + ImmutableListMultimap.Builder transformers = + ImmutableListMultimap.builder(); + + for (ResourceInfo resource : getClassPathResources()) { + getRefasterTemplateName(resource) + .ifPresent( + templateName -> + loadCodeTransformer(resource) + .ifPresent(transformer -> transformers.put(templateName, transformer))); + } + + return transformers.build(); + } + + private static ImmutableSet getClassPathResources() { + try { + return ClassPath.from(ClassLoader.getSystemClassLoader()).getResources(); + } catch (IOException e) { + throw new UncheckedIOException("Failed to scan classpath for resources", e); + } + } + + private static Optional getRefasterTemplateName(ResourceInfo resource) { + String resourceName = resource.getResourceName(); + if (!resourceName.endsWith(REFASTER_TEMPLATE_SUFFIX)) { + return Optional.empty(); + } + + int lastPathSeparator = resourceName.lastIndexOf('/'); + int beginIndex = lastPathSeparator < 0 ? 0 : lastPathSeparator + 1; + int endIndex = resourceName.length() - REFASTER_TEMPLATE_SUFFIX.length(); + return Optional.of(resourceName.substring(beginIndex, endIndex)); + } + + private static Optional loadCodeTransformer(ResourceInfo resource) { + try (InputStream in = resource.url().openStream(); + ObjectInputStream ois = new ObjectInputStream(in)) { + @SuppressWarnings("BanSerializableRead" /* Part of the Refaster API. */) + CodeTransformer codeTransformer = (CodeTransformer) ois.readObject(); + return Optional.of(codeTransformer); + } catch (NoSuchElementException e) { + /* For some reason we can't load the resource. Skip it. */ + // XXX: Should we log this? + return Optional.empty(); + } catch (IOException | ClassNotFoundException e) { + throw new IllegalStateException("Can't load `CodeTransformer` from " + resource, e); + } + } +} diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/RefasterCheck.java similarity index 72% rename from error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java rename to refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/RefasterCheck.java index 81212cd168..6375d60385 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RefasterCheck.java +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/RefasterCheck.java @@ -1,4 +1,4 @@ -package tech.picnic.errorprone.bugpatterns; +package tech.picnic.errorprone.refaster.runner; import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.ImmutableRangeSet.toImmutableRangeSet; @@ -8,24 +8,21 @@ import static java.util.function.Predicate.not; import com.google.auto.service.AutoService; -import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableRangeSet; -import com.google.common.collect.ImmutableSet; import com.google.common.collect.Range; import com.google.common.collect.RangeSet; import com.google.common.collect.TreeRangeSet; -import com.google.common.reflect.ClassPath; -import com.google.common.reflect.ClassPath.ResourceInfo; import com.google.errorprone.BugPattern; import com.google.errorprone.CodeTransformer; import com.google.errorprone.CompositeCodeTransformer; import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.SubContext; import com.google.errorprone.VisitorState; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.bugpatterns.BugChecker; import com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher; import com.google.errorprone.fixes.Replacement; @@ -33,16 +30,10 @@ import com.sun.source.tree.CompilationUnitTree; import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; -import java.io.UncheckedIOException; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Optional; import java.util.function.Supplier; import java.util.regex.Pattern; import java.util.stream.Stream; @@ -63,13 +54,13 @@ severity = SUGGESTION, tags = SIMPLIFICATION) public final class RefasterCheck extends BugChecker implements CompilationUnitTreeMatcher { + /** Flag to pass a pattern that restricts which Refaster templates are loaded. */ + public static final String INCLUDED_TEMPLATES_PATTERN_FLAG = "Refaster:NamePattern"; + private static final long serialVersionUID = 1L; - private static final String REFASTER_TEMPLATE_SUFFIX = ".refaster"; - private static final String INCLUDED_TEMPLATES_PATTERN_FLAG = "Refaster:NamePattern"; - @VisibleForTesting static final Supplier> ALL_CODE_TRANSFORMERS = - Suppliers.memoize(RefasterCheck::loadAllCodeTransformers); + Suppliers.memoize(CodeTransformers::loadAllCodeTransformers); private final CodeTransformer codeTransformer; @@ -88,6 +79,7 @@ public RefasterCheck(ErrorProneFlags flags) { } @Override + @CanIgnoreReturnValue public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { /* First, collect all matches. */ List matches = new ArrayList<>(); @@ -178,54 +170,4 @@ private static ImmutableList filterCodeTransformers( .map(Map.Entry::getValue) .collect(toImmutableList()); } - - private static ImmutableListMultimap loadAllCodeTransformers() { - ImmutableListMultimap.Builder transformers = - ImmutableListMultimap.builder(); - - for (ResourceInfo resource : getClassPathResources()) { - getRefasterTemplateName(resource) - .ifPresent( - templateName -> - loadCodeTransformer(resource) - .ifPresent(transformer -> transformers.put(templateName, transformer))); - } - - return transformers.build(); - } - - private static ImmutableSet getClassPathResources() { - try { - return ClassPath.from(RefasterCheck.class.getClassLoader()).getResources(); - } catch (IOException e) { - throw new UncheckedIOException("Failed to scan classpath for resources", e); - } - } - - private static Optional getRefasterTemplateName(ResourceInfo resource) { - String resourceName = resource.getResourceName(); - if (!resourceName.endsWith(REFASTER_TEMPLATE_SUFFIX)) { - return Optional.empty(); - } - - int lastPathSeparator = resourceName.lastIndexOf('/'); - int beginIndex = lastPathSeparator < 0 ? 0 : lastPathSeparator + 1; - int endIndex = resourceName.length() - REFASTER_TEMPLATE_SUFFIX.length(); - return Optional.of(resourceName.substring(beginIndex, endIndex)); - } - - private static Optional loadCodeTransformer(ResourceInfo resource) { - try (InputStream in = resource.url().openStream(); - ObjectInputStream ois = new ObjectInputStream(in)) { - @SuppressWarnings("BanSerializableRead" /* Part of the Refaster API. */) - CodeTransformer codeTransformer = (CodeTransformer) ois.readObject(); - return Optional.of(codeTransformer); - } catch (NoSuchElementException e) { - /* For some reason we can't load the resource. Skip it. */ - // XXX: Should we log this? - return Optional.empty(); - } catch (IOException | ClassNotFoundException e) { - throw new IllegalStateException("Can't load `CodeTransformer` from " + resource, e); - } - } } diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java new file mode 100644 index 0000000000..830d98f634 --- /dev/null +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java @@ -0,0 +1,4 @@ +/** Classes used to find Refaster templates on the classpath and to apply them. */ +@com.google.errorprone.annotations.CheckReturnValue +@javax.annotation.ParametersAreNonnullByDefault +package tech.picnic.errorprone.refaster.runner; diff --git a/refaster-test-support/pom.xml b/refaster-test-support/pom.xml new file mode 100644 index 0000000000..f77088e609 --- /dev/null +++ b/refaster-test-support/pom.xml @@ -0,0 +1,117 @@ + + + 4.0.0 + + + tech.picnic.error-prone-support + error-prone-support + 0.0.1-SNAPSHOT + + + refaster-test-support + + Picnic :: Error Prone Support :: Refaster Test Support + Utilities to support with the validation of Refaster templates tests. + + + + ${groupId.error-prone} + error_prone_annotation + provided + + + ${groupId.error-prone} + error_prone_annotations + provided + + + ${groupId.error-prone} + error_prone_check_api + provided + + + ${groupId.error-prone} + error_prone_test_helpers + + + ${project.groupId} + refaster-compiler + + provided + + + ${project.groupId} + refaster-runner + ${project.version} + + + com.google.auto.service + auto-service-annotations + provided + + + com.google.code.findbugs + jsr305 + provided + + + com.google.errorprone + javac + provided + + + com.google.guava + guava + provided + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.jupiter + junit-jupiter-params + test + + + + + + + + com.coveo + fmt-maven-plugin + + + ${basedir}/src/test/resources + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + ${project.groupId} + refaster-compiler + ${project.version} + + + + -Xplugin:RefasterRuleCompiler + + + + + + + diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java new file mode 100644 index 0000000000..2f456dc9a4 --- /dev/null +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java @@ -0,0 +1,219 @@ +package tech.picnic.errorprone.refaster.test; + +import static com.google.common.collect.ImmutableSet.toImmutableSet; +import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH; +import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; +import static java.util.stream.Collectors.joining; +import static tech.picnic.errorprone.refaster.runner.RefasterCheck.INCLUDED_TEMPLATES_PATTERN_FLAG; + +import com.google.common.base.Suppliers; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableListMultimap; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableRangeMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Range; +import com.google.common.collect.Sets; +import com.google.errorprone.BugCheckerRefactoringTestHelper; +import com.google.errorprone.BugPattern; +import com.google.errorprone.CodeTransformer; +import com.google.errorprone.ErrorProneFlags; +import com.google.errorprone.FileObjects; +import com.google.errorprone.SubContext; +import com.google.errorprone.VisitorState; +import com.google.errorprone.bugpatterns.BugChecker; +import com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher; +import com.google.errorprone.fixes.Replacement; +import com.google.errorprone.fixes.SuggestedFix; +import com.google.errorprone.matchers.Description; +import com.google.errorprone.util.ASTHelpers; +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.MethodTree; +import com.sun.source.util.TreeScanner; +import com.sun.tools.javac.tree.EndPosTable; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.function.Supplier; +import java.util.stream.Collector; +import javax.tools.JavaFileObject; +import tech.picnic.errorprone.refaster.runner.CodeTransformers; +import tech.picnic.errorprone.refaster.runner.RefasterCheck; + +/** Utility to test Refaster templates and validate the tests for template collections. */ +public final class RefasterCollectionTestUtil { + static final Supplier> ALL_CODE_TRANSFORMERS = + Suppliers.memoize(CodeTransformers::loadAllCodeTransformers); + + private RefasterCollectionTestUtil() {} + + /** + * Verifies that all Refaster templates from a collection are covered by at least one test and + * that the match rewrites code in the correct test method. + * + *

Note that this doesn't guarantee full coverage: this test does not ascertain that all {@link + * com.google.errorprone.refaster.Refaster#anyOf} branches are tested. Idem for {@link + * com.google.errorprone.refaster.annotation.BeforeTemplate} methods in case there are multiple. + * + * @param clazz The Refaster template collection under test. + */ + public static void validateTemplateCollection(Class clazz) { + String className = clazz.getSimpleName(); + JavaFileObject input = FileObjects.forResource(clazz, className + "TestInput.java"); + JavaFileObject output = FileObjects.forResource(clazz, className + "TestOutput.java"); + String inputFileContent; + String outputFileContent; + try { + inputFileContent = input.getCharContent(false).toString(); + outputFileContent = output.getCharContent(false).toString(); + } catch (IOException e) { + throw new IllegalStateException( + "Can't retrieve content from input or output file for class " + clazz.getName(), e); + } + BugCheckerRefactoringTestHelper.newInstance( + RefasterTestBugChecker.class, RefasterCollectionTestUtil.class) + .setArgs(ImmutableList.of("-XepOpt:RefasterTestChecker:TemplateCollection=" + className)) + .addInputLines(clazz.getName() + "TestInput.java", inputFileContent) + .addOutputLines(clazz.getName() + "TestOutput.java", outputFileContent) + .doTest(TEXT_MATCH); + } + + /** + * A {@link BugChecker} that tests the Refaster templates of a given template collection and + * validates its tests. + */ + @BugPattern( + name = "RefasterTestBugChecker", + summary = "Validate a Refaster template collection and its tests", + severity = ERROR) + public static final class RefasterTestBugChecker extends BugChecker + implements CompilationUnitTreeMatcher { + private static final long serialVersionUID = 1L; + private static final Collector LIST_COLLECTOR = + joining("\n- ", "\n- ", "\n"); + + private final ImmutableSet templateNamesFromClassPath; + private final RefasterCheck delegate; + + /** + * Instantiates a customized {@link RefasterTestBugChecker}. + * + * @param flags Any provided command line flags. + */ + public RefasterTestBugChecker(ErrorProneFlags flags) { + String templateCollection = flags.get("RefasterTestChecker:TemplateCollection").orElseThrow(); + delegate = + new RefasterCheck( + ErrorProneFlags.fromMap( + ImmutableMap.of(INCLUDED_TEMPLATES_PATTERN_FLAG, templateCollection + ".*"))); + templateNamesFromClassPath = + ALL_CODE_TRANSFORMERS.get().keySet().stream() + .filter(k -> k.contains(templateCollection)) + .map(k -> k.replace(templateCollection + "$", "")) + .sorted() + .collect(toImmutableSet()); + } + + @Override + public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { + List matches = new ArrayList<>(); + delegate.matchCompilationUnit( + tree, + VisitorState.createForCustomFindingCollection(new SubContext(state.context), matches::add) + .withPath(state.getPath())); + + ImmutableRangeMap matchesRangeMap = + buildRangeMapForMatches(matches, ((JCCompilationUnit) tree).endPositions); + + ImmutableSet templatesWithoutMatch = getTemplateNamesWithoutMatch(matches); + if (!templatesWithoutMatch.isEmpty()) { + appendCommentToCompilationUnit( + String.format( + "Did not encounter test in %s for the following template(s)", + ASTHelpers.getFileName(tree)), + templatesWithoutMatch.stream().collect(LIST_COLLECTOR), + state); + } + + ValidateMatchesInMethodsScanner scanner = + new ValidateMatchesInMethodsScanner(matchesRangeMap); + scanner.scan(tree.getTypeDecls(), state); + + matches.forEach(state::reportMatch); + return Description.NO_MATCH; + } + + private ImmutableSet getTemplateNamesWithoutMatch(List matches) { + ImmutableSet templateNamesOfMatches = + matches.stream() + .map(description -> description.checkName) + .map(RefasterTestBugChecker::getNameFromFQCN) + .collect(toImmutableSet()); + + return Sets.difference(templateNamesFromClassPath, templateNamesOfMatches).immutableCopy(); + } + + private void appendCommentToCompilationUnit(String message, String list, VisitorState state) { + String comment = String.format("\n/* %s:%s*/", message, list); + CompilationUnitTree compilationUnit = state.getPath().getCompilationUnit(); + state.reportMatch( + describeMatch(compilationUnit, SuggestedFix.postfixWith(compilationUnit, comment))); + } + + private static ImmutableRangeMap buildRangeMapForMatches( + List matches, EndPosTable endPositions) { + ImmutableRangeMap.Builder rangeMap = ImmutableRangeMap.builder(); + + for (Description description : matches) { + Set replacements = + Iterables.getOnlyElement(description.fixes).getReplacements(endPositions); + Replacement replacement = Iterables.getOnlyElement(replacements); + + rangeMap.put(replacement.range(), getNameFromFQCN(description.checkName)); + } + return rangeMap.build(); + } + + private static String getNameFromFQCN(String fqcn) { + return fqcn.substring(fqcn.lastIndexOf('.') + 1); + } + + private class ValidateMatchesInMethodsScanner extends TreeScanner { + private final ImmutableRangeMap matchesRangeMap; + + ValidateMatchesInMethodsScanner(ImmutableRangeMap matchesRangeMap) { + this.matchesRangeMap = matchesRangeMap; + } + + @Override + public Void visitMethod(MethodTree tree, VisitorState state) { + if (ASTHelpers.isGeneratedConstructor(tree)) { + return super.visitMethod(tree, state); + } + + String methodName = tree.getName().toString().replace("test", ""); + int startPosition = ASTHelpers.getStartPosition(tree); + int endPosition = state.getEndPosition(tree); + + ImmutableRangeMap matchesInCurrentMethod = + matchesRangeMap.subRangeMap(Range.open(startPosition, endPosition)); + boolean correctTemplatesMatchedInMethod = + matchesInCurrentMethod.asMapOfRanges().values().stream().allMatch(methodName::equals); + if (!correctTemplatesMatchedInMethod) { + appendCommentToCompilationUnit( + String.format( + "The following matches occurred in method `%s` (position: [%s,%s])", + tree.getName(), startPosition, endPosition), + matchesRangeMap.asMapOfRanges().entrySet().stream() + .map(e -> "Template `" + e.getValue() + "` matched on position: " + e.getKey()) + .collect(LIST_COLLECTOR), + state); + } + return super.visitMethod(tree, state); + } + } + } +} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplateTestCase.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateTestCase.java similarity index 82% rename from error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplateTestCase.java rename to refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateTestCase.java index 42bf96d472..2ecf0b91d6 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplateTestCase.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateTestCase.java @@ -1,20 +1,21 @@ -package tech.picnic.errorprone.refastertemplates; +package tech.picnic.errorprone.refaster.test; import com.google.common.collect.ImmutableSet; /** * Interface implemented by classes that exercise Refaster templates. These classes come in {@code * *Input.java} and {@code *Output.java} pairs, demonstrating the expected result of applying the - * associated Refaster templates. These classes are resources in the {@code - * tech.picnic.errorprone.bugpatterns} package. + * associated Refaster templates. These classes are resources. */ -interface RefasterTemplateTestCase { +public interface RefasterTemplateTestCase { /** * In some test classes there are types and statically imported methods that are fully replaced by * the Refaster templates under test. In those cases Refaster does not remove the associated * imports, while Google Java Formatter does. Subclasses can extend this method to enumerate such * types and statically imported methods, such that any imports present in the input file are also * present in the output file. + * + * @return The classes and methods needed to get a properly formatted file. */ default ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(); diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java new file mode 100644 index 0000000000..f20681e8cc --- /dev/null +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java @@ -0,0 +1,4 @@ +/** Utilities that support testing Refaster templates and validate the tests. */ +@com.google.errorprone.annotations.CheckReturnValue +@javax.annotation.ParametersAreNonnullByDefault +package tech.picnic.errorprone.refaster.test; diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplates.java new file mode 100644 index 0000000000..029610802e --- /dev/null +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplates.java @@ -0,0 +1,20 @@ +package tech.picnic.errorprone.refaster.test; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; + +final class MatchInWrongMethodTemplates { + private MatchInWrongMethodTemplates() {} + + static final class StringIsEmpty { + @BeforeTemplate + boolean before(String string) { + return string.equals(""); + } + + @AfterTemplate + boolean after(String string) { + return string.isEmpty(); + } + } +} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java new file mode 100644 index 0000000000..10bc17f38d --- /dev/null +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java @@ -0,0 +1,20 @@ +package tech.picnic.errorprone.refaster.test; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; + +final class MethodNameWithNumberTemplates { + private MethodNameWithNumberTemplates() {} + + static final class StringIsEmpty2 { + @BeforeTemplate + boolean before(String string) { + return string.equals(""); + } + + @AfterTemplate + boolean after(String string) { + return string.isEmpty(); + } + } +} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplates.java new file mode 100644 index 0000000000..df90c09440 --- /dev/null +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplates.java @@ -0,0 +1,27 @@ +package tech.picnic.errorprone.refaster.test; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; + +final class MissingTestAndWrongTestTemplates { + private MissingTestAndWrongTestTemplates() {} + + static final class StringIsEmpty { + @BeforeTemplate + boolean before(String string) { + return string.equals(""); + } + + @AfterTemplate + boolean after(String string) { + return string.isEmpty(); + } + } + + static final class TemplateWithoutTest { + @BeforeTemplate + boolean before(String string) { + return string.equals("foo"); + } + } +} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java new file mode 100644 index 0000000000..60311588a1 --- /dev/null +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java @@ -0,0 +1,18 @@ +package tech.picnic.errorprone.refaster.test; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +final class RefasterCollectionTestUtilTest { + @ParameterizedTest + @ValueSource( + classes = { + MatchInWrongMethodTemplates.class, + MethodNameWithNumberTemplates.class, + MissingTestAndWrongTestTemplates.class, + TemplateWithoutTestTemplates.class + }) + void verifyRefasterTemplateCollections(Class clazz) { + RefasterCollectionTestUtil.validateTemplateCollection(clazz); + } +} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplates.java new file mode 100644 index 0000000000..6b9fff7297 --- /dev/null +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplates.java @@ -0,0 +1,34 @@ +package tech.picnic.errorprone.refaster.test; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; + +final class TemplateWithoutTestTemplates { + private TemplateWithoutTestTemplates() {} + + static final class StringIsEmpty { + @BeforeTemplate + boolean before(String string) { + return string.equals(""); + } + + @AfterTemplate + boolean after(String string) { + return string.isEmpty(); + } + } + + static final class TemplateWithoutTest { + @BeforeTemplate + boolean before(String string) { + return string.equals("foo"); + } + } + + static final class AnotherTemplateWithoutTest { + @BeforeTemplate + boolean before(String string) { + return string.equals("bar"); + } + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java new file mode 100644 index 0000000000..302b004450 --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java @@ -0,0 +1,9 @@ +package tech.picnic.errorprone.refaster.test; + +final class MatchInWrongMethodTemplatesTest implements RefasterTemplateTestCase { + boolean testWrongName() { + "foo".equals(""); + "bar".equals(""); + return "baz".equals(""); + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java new file mode 100644 index 0000000000..26e2081ab1 --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java @@ -0,0 +1,14 @@ +package tech.picnic.errorprone.refaster.test; + +final class MatchInWrongMethodTemplatesTest implements RefasterTemplateTestCase { + boolean testWrongName() { + "foo".isEmpty(); + "bar".isEmpty(); + return "baz".isEmpty(); + } +} +/* The following matches occurred in method `testWrongName` (position: [131,233]): +- Template `StringIsEmpty` matched on position: [161..177) +- Template `StringIsEmpty` matched on position: [183..199) +- Template `StringIsEmpty` matched on position: [212..228) +*/ diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java new file mode 100644 index 0000000000..7458817a8d --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java @@ -0,0 +1,21 @@ +package tech.picnic.errorprone.refaster.test; + +import com.google.common.collect.ImmutableSet; +import java.util.Collections; + +final class MethodNameWithNumberTemplatesTest implements RefasterTemplateTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(Collections.class); + } + + boolean testStringIsEmpty2() { + return "foo".equals(""); + } + + private void foo() {} + + public String bar() { + return ""; + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java new file mode 100644 index 0000000000..180c81051a --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java @@ -0,0 +1,21 @@ +package tech.picnic.errorprone.refaster.test; + +import com.google.common.collect.ImmutableSet; +import java.util.Collections; + +final class MethodNameWithNumberTemplatesTest implements RefasterTemplateTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(Collections.class); + } + + boolean testStringIsEmpty2() { + return "foo".isEmpty(); + } + + private void foo() {} + + public String bar() { + return ""; + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java new file mode 100644 index 0000000000..c099c33959 --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java @@ -0,0 +1,9 @@ +package tech.picnic.errorprone.refaster.test; + +final class MissingTestAndWrongTestTemplatesTest implements RefasterTemplateTestCase { + boolean testWrongName() { + "foo".equals(""); + "bar".equals(""); + return "baz".equals(""); + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java new file mode 100644 index 0000000000..62a5671637 --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java @@ -0,0 +1,17 @@ +package tech.picnic.errorprone.refaster.test; + +final class MissingTestAndWrongTestTemplatesTest implements RefasterTemplateTestCase { + boolean testWrongName() { + "foo".isEmpty(); + "bar".isEmpty(); + return "baz".isEmpty(); + } +} +/* Did not encounter test in /tech.picnic.errorprone.refaster.test.MissingTestAndWrongTestTemplatesTestInput.java for the following template(s): +- TemplateWithoutTest +*/ +/* The following matches occurred in method `testWrongName` (position: [136,238]): +- Template `StringIsEmpty` matched on position: [166..182) +- Template `StringIsEmpty` matched on position: [188..204) +- Template `StringIsEmpty` matched on position: [217..233) +*/ diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java new file mode 100644 index 0000000000..bb93115baa --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java @@ -0,0 +1,7 @@ +package tech.picnic.errorprone.refaster.test; + +final class TemplateWithoutTestTemplatesTest implements RefasterTemplateTestCase { + boolean testStringIsEmpty() { + return "foo".equals(""); + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java new file mode 100644 index 0000000000..8b5b700615 --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java @@ -0,0 +1,11 @@ +package tech.picnic.errorprone.refaster.test; + +final class TemplateWithoutTestTemplatesTest implements RefasterTemplateTestCase { + boolean testStringIsEmpty() { + return "foo".isEmpty(); + } +} +/* Did not encounter test in /tech.picnic.errorprone.refaster.test.TemplateWithoutTestTemplatesTestInput.java for the following template(s): +- AnotherTemplateWithoutTest +- TemplateWithoutTest +*/ From c8967109e32314d2d3267e4e975164aae4a45f27 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 9 Mar 2022 11:06:29 +0100 Subject: [PATCH 05/41] Remove validation in `error-prone-contrib` --- .../RefasterTemplatesTest.java | 17 ----------------- .../WebClientTemplatesTestInput.java | 3 +-- .../WebClientTemplatesTestOutput.java | 3 +-- 3 files changed, 2 insertions(+), 21 deletions(-) delete mode 100644 error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java deleted file mode 100644 index 49648f639c..0000000000 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package tech.picnic.errorprone.refastertemplates; - -import org.junit.jupiter.api.Test; -import tech.picnic.errorprone.refaster.test.RefasterCollectionTestUtil; - -final class RefasterTemplatesTest { - - @Test - void webClientTemplates() { - RefasterCollectionTestUtil.validateTemplateCollection(WebClientTemplates.class); - } - - @Test - void reactorTemplates() { - RefasterCollectionTestUtil.validateTemplateCollection(ReactorTemplates.class); - } -} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestInput.java index 1832be992c..323899bb31 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestInput.java @@ -19,9 +19,8 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(fromValue(""), GET, HEAD, OPTIONS, PATCH, POST, PUT); } - ImmutableSet testBodyVallue() { + ImmutableSet testBodyValue() { return ImmutableSet.of( - WebClient.create().post().body(fromValue("bar")), WebClient.create().post().body(fromValue("bar")), WebTestClient.bindToServer().build().post().body(fromValue("bar"))); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestOutput.java index de1380a7aa..9e0d57831d 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/WebClientTemplatesTestOutput.java @@ -19,9 +19,8 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(fromValue(""), GET, HEAD, OPTIONS, PATCH, POST, PUT); } - ImmutableSet testBodyVallue() { + ImmutableSet testBodyValue() { return ImmutableSet.of( - WebClient.create().post().bodyValue("bar"), WebClient.create().post().bodyValue("bar"), WebTestClient.bindToServer().build().post().bodyValue("bar")); } From 73a6b47039d57f7f45a85046aab1e23988edfc31 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 21 Mar 2022 16:53:09 +0100 Subject: [PATCH 06/41] Apply suggestions --- pom.xml | 5 +++++ refaster-test-support/pom.xml | 1 - .../test/RefasterCollectionTestUtil.java | 20 +++++++++++++------ .../refaster/test/package-info.java | 2 +- .../test/MatchInWrongMethodTemplates.java | 3 +++ .../test/MethodNameWithNumberTemplates.java | 1 + .../MissingTestAndWrongTestTemplates.java | 4 ++++ .../test/RefasterCollectionTestUtilTest.java | 1 + .../test/TemplateWithoutTestTemplates.java | 1 + .../MatchInWrongMethodTemplatesTestInput.java | 1 + ...MatchInWrongMethodTemplatesTestOutput.java | 9 +++++---- ...ethodNameWithNumberTemplatesTestInput.java | 1 + ...thodNameWithNumberTemplatesTestOutput.java | 1 + ...ingTestAndWrongTestTemplatesTestInput.java | 1 + ...ngTestAndWrongTestTemplatesTestOutput.java | 11 +++++----- ...TemplateWithoutTestTemplatesTestInput.java | 1 + ...emplateWithoutTestTemplatesTestOutput.java | 3 ++- 17 files changed, 48 insertions(+), 18 deletions(-) diff --git a/pom.xml b/pom.xml index 968d75a624..50ce50213c 100644 --- a/pom.xml +++ b/pom.xml @@ -190,6 +190,11 @@ refaster-compiler ${project.version} + + ${project.groupId} + refaster-runner + ${project.version} + ${project.groupId} refaster-support diff --git a/refaster-test-support/pom.xml b/refaster-test-support/pom.xml index f77088e609..6d56d037c0 100644 --- a/refaster-test-support/pom.xml +++ b/refaster-test-support/pom.xml @@ -44,7 +44,6 @@ ${project.groupId} refaster-runner - ${project.version} com.google.auto.service diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java index 2f456dc9a4..77be97eebf 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java @@ -29,6 +29,7 @@ import com.google.errorprone.matchers.Description; import com.google.errorprone.util.ASTHelpers; import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.LineMap; import com.sun.source.tree.MethodTree; import com.sun.source.util.TreeScanner; import com.sun.tools.javac.tree.EndPosTable; @@ -125,15 +126,16 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s VisitorState.createForCustomFindingCollection(new SubContext(state.context), matches::add) .withPath(state.getPath())); + JCCompilationUnit compilationUnit = (JCCompilationUnit) tree; ImmutableRangeMap matchesRangeMap = - buildRangeMapForMatches(matches, ((JCCompilationUnit) tree).endPositions); + buildRangeMapForMatches(matches, compilationUnit.endPositions); ImmutableSet templatesWithoutMatch = getTemplateNamesWithoutMatch(matches); if (!templatesWithoutMatch.isEmpty()) { appendCommentToCompilationUnit( String.format( - "Did not encounter test in %s for the following template(s)", - ASTHelpers.getFileName(tree)), + "Did not encounter a test in `%s` for the following template(s)", + getNameFromFQCN(compilationUnit.sourcefile.getName().replace(".java", ""))), templatesWithoutMatch.stream().collect(LIST_COLLECTOR), state); } @@ -197,6 +199,7 @@ public Void visitMethod(MethodTree tree, VisitorState state) { String methodName = tree.getName().toString().replace("test", ""); int startPosition = ASTHelpers.getStartPosition(tree); int endPosition = state.getEndPosition(tree); + LineMap lineMap = state.getPath().getCompilationUnit().getLineMap(); ImmutableRangeMap matchesInCurrentMethod = matchesRangeMap.subRangeMap(Range.open(startPosition, endPosition)); @@ -205,10 +208,15 @@ public Void visitMethod(MethodTree tree, VisitorState state) { if (!correctTemplatesMatchedInMethod) { appendCommentToCompilationUnit( String.format( - "The following matches occurred in method `%s` (position: [%s,%s])", - tree.getName(), startPosition, endPosition), + "The following matches unexpectedly occurred in method `%s`", tree.getName()), matchesRangeMap.asMapOfRanges().entrySet().stream() - .map(e -> "Template `" + e.getValue() + "` matched on position: " + e.getKey()) + .map( + e -> + String.format( + "Template `%s` matches on line %s, while it should match in a method named `test%s`.", + e.getValue(), + lineMap.getLineNumber(e.getKey().lowerEndpoint()), + e.getValue())) .collect(LIST_COLLECTOR), state); } diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java index f20681e8cc..87bf718fe4 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java @@ -1,4 +1,4 @@ -/** Utilities that support testing Refaster templates and validate the tests. */ +/** Utilities that support testing Refaster templates and allows for validating these tests. */ @com.google.errorprone.annotations.CheckReturnValue @javax.annotation.ParametersAreNonnullByDefault package tech.picnic.errorprone.refaster.test; diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplates.java index 029610802e..3e202f17dd 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplates.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplates.java @@ -3,6 +3,9 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; +/** + * Refaster template collection to validate reporting of a match occurring in an unexpected place. + */ final class MatchInWrongMethodTemplates { private MatchInWrongMethodTemplates() {} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java index 10bc17f38d..6795d2254e 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java @@ -3,6 +3,7 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; +/** Refaster template with a number as suffix to validate that it is reported correctly. */ final class MethodNameWithNumberTemplates { private MethodNameWithNumberTemplates() {} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplates.java index df90c09440..089b3bf989 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplates.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplates.java @@ -3,6 +3,10 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; +/** + * Refaster template collection to validate that a missing test and misplaced tests are both + * reported. + */ final class MissingTestAndWrongTestTemplates { private MissingTestAndWrongTestTemplates() {} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java index 60311588a1..fd8b7773a6 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java @@ -3,6 +3,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +/** Validate error reporting of the Refaster template collections. */ final class RefasterCollectionTestUtilTest { @ParameterizedTest @ValueSource( diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplates.java index 6b9fff7297..35e404c4c7 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplates.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplates.java @@ -3,6 +3,7 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; +/** Refaster template collection to validate the reporting of missing test methods. */ final class TemplateWithoutTestTemplates { private TemplateWithoutTestTemplates() {} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java index 302b004450..92c95596ba 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refaster.test; +/** Code to test the Refaster templates from `MatchInWrongMethodTemplates`. */ final class MatchInWrongMethodTemplatesTest implements RefasterTemplateTestCase { boolean testWrongName() { "foo".equals(""); diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java index 26e2081ab1..14f84ad943 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refaster.test; +/** Code to test the Refaster templates from `MatchInWrongMethodTemplates`. */ final class MatchInWrongMethodTemplatesTest implements RefasterTemplateTestCase { boolean testWrongName() { "foo".isEmpty(); @@ -7,8 +8,8 @@ boolean testWrongName() { return "baz".isEmpty(); } } -/* The following matches occurred in method `testWrongName` (position: [131,233]): -- Template `StringIsEmpty` matched on position: [161..177) -- Template `StringIsEmpty` matched on position: [183..199) -- Template `StringIsEmpty` matched on position: [212..228) +/* The following matches unexpectedly occurred in method `testWrongName`: +- Template `StringIsEmpty` matches on line 6, while it should match in a method named `testStringIsEmpty`. +- Template `StringIsEmpty` matches on line 7, while it should match in a method named `testStringIsEmpty`. +- Template `StringIsEmpty` matches on line 8, while it should match in a method named `testStringIsEmpty`. */ diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java index 7458817a8d..7657740c27 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java @@ -3,6 +3,7 @@ import com.google.common.collect.ImmutableSet; import java.util.Collections; +/** Code to test the Refaster templates from `MethodNameWithNumberTemplates`. */ final class MethodNameWithNumberTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java index 180c81051a..ce103339c4 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java @@ -3,6 +3,7 @@ import com.google.common.collect.ImmutableSet; import java.util.Collections; +/** Code to test the Refaster templates from `MethodNameWithNumberTemplates`. */ final class MethodNameWithNumberTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java index c099c33959..52656a223b 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refaster.test; +/** Code to test the Refaster templates from `MissingTestAndWrongTestTemplates`. */ final class MissingTestAndWrongTestTemplatesTest implements RefasterTemplateTestCase { boolean testWrongName() { "foo".equals(""); diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java index 62a5671637..4af5cd857e 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refaster.test; +/** Code to test the Refaster templates from `MissingTestAndWrongTestTemplates`. */ final class MissingTestAndWrongTestTemplatesTest implements RefasterTemplateTestCase { boolean testWrongName() { "foo".isEmpty(); @@ -7,11 +8,11 @@ boolean testWrongName() { return "baz".isEmpty(); } } -/* Did not encounter test in /tech.picnic.errorprone.refaster.test.MissingTestAndWrongTestTemplatesTestInput.java for the following template(s): +/* Did not encounter a test in `MissingTestAndWrongTestTemplatesTestInput` for the following template(s): - TemplateWithoutTest */ -/* The following matches occurred in method `testWrongName` (position: [136,238]): -- Template `StringIsEmpty` matched on position: [166..182) -- Template `StringIsEmpty` matched on position: [188..204) -- Template `StringIsEmpty` matched on position: [217..233) +/* The following matches unexpectedly occurred in method `testWrongName`: +- Template `StringIsEmpty` matches on line 6, while it should match in a method named `testStringIsEmpty`. +- Template `StringIsEmpty` matches on line 7, while it should match in a method named `testStringIsEmpty`. +- Template `StringIsEmpty` matches on line 8, while it should match in a method named `testStringIsEmpty`. */ diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java index bb93115baa..89563a85c3 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refaster.test; +/** Code to test the Refaster templates from `TemplateWithoutTestTemplates`. */ final class TemplateWithoutTestTemplatesTest implements RefasterTemplateTestCase { boolean testStringIsEmpty() { return "foo".equals(""); diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java index 8b5b700615..2a39707b48 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java @@ -1,11 +1,12 @@ package tech.picnic.errorprone.refaster.test; +/** Code to test the Refaster templates from `TemplateWithoutTestTemplates`. */ final class TemplateWithoutTestTemplatesTest implements RefasterTemplateTestCase { boolean testStringIsEmpty() { return "foo".isEmpty(); } } -/* Did not encounter test in /tech.picnic.errorprone.refaster.test.TemplateWithoutTestTemplatesTestInput.java for the following template(s): +/* Did not encounter a test in `TemplateWithoutTestTemplatesTestInput` for the following template(s): - AnotherTemplateWithoutTest - TemplateWithoutTest */ From 42d05d3922712042faa86f953383603eb7723a94 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 28 Mar 2022 15:05:52 +0200 Subject: [PATCH 07/41] Minor improvements and move class --- .../test/RefasterCollectionTestUtil.java | 209 ++---------------- .../refaster/test/RefasterValidateTests.java | 182 +++++++++++++++ 2 files changed, 197 insertions(+), 194 deletions(-) create mode 100644 refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java index 77be97eebf..0a0ec42988 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java @@ -1,54 +1,15 @@ package tech.picnic.errorprone.refaster.test; -import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH; -import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; -import static java.util.stream.Collectors.joining; -import static tech.picnic.errorprone.refaster.runner.RefasterCheck.INCLUDED_TEMPLATES_PATTERN_FLAG; -import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableListMultimap; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableRangeMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; -import com.google.common.collect.Range; -import com.google.common.collect.Sets; import com.google.errorprone.BugCheckerRefactoringTestHelper; -import com.google.errorprone.BugPattern; -import com.google.errorprone.CodeTransformer; -import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.FileObjects; -import com.google.errorprone.SubContext; -import com.google.errorprone.VisitorState; -import com.google.errorprone.bugpatterns.BugChecker; -import com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher; -import com.google.errorprone.fixes.Replacement; -import com.google.errorprone.fixes.SuggestedFix; -import com.google.errorprone.matchers.Description; -import com.google.errorprone.util.ASTHelpers; -import com.sun.source.tree.CompilationUnitTree; -import com.sun.source.tree.LineMap; -import com.sun.source.tree.MethodTree; -import com.sun.source.util.TreeScanner; -import com.sun.tools.javac.tree.EndPosTable; -import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.function.Supplier; -import java.util.stream.Collector; import javax.tools.JavaFileObject; -import tech.picnic.errorprone.refaster.runner.CodeTransformers; -import tech.picnic.errorprone.refaster.runner.RefasterCheck; /** Utility to test Refaster templates and validate the tests for template collections. */ public final class RefasterCollectionTestUtil { - static final Supplier> ALL_CODE_TRANSFORMERS = - Suppliers.memoize(CodeTransformers::loadAllCodeTransformers); - private RefasterCollectionTestUtil() {} /** @@ -63,165 +24,25 @@ private RefasterCollectionTestUtil() {} */ public static void validateTemplateCollection(Class clazz) { String className = clazz.getSimpleName(); - JavaFileObject input = FileObjects.forResource(clazz, className + "TestInput.java"); - JavaFileObject output = FileObjects.forResource(clazz, className + "TestOutput.java"); - String inputFileContent; - String outputFileContent; - try { - inputFileContent = input.getCharContent(false).toString(); - outputFileContent = output.getCharContent(false).toString(); - } catch (IOException e) { - throw new IllegalStateException( - "Can't retrieve content from input or output file for class " + clazz.getName(), e); - } + BugCheckerRefactoringTestHelper.newInstance( - RefasterTestBugChecker.class, RefasterCollectionTestUtil.class) - .setArgs(ImmutableList.of("-XepOpt:RefasterTestChecker:TemplateCollection=" + className)) - .addInputLines(clazz.getName() + "TestInput.java", inputFileContent) - .addOutputLines(clazz.getName() + "TestOutput.java", outputFileContent) + RefasterValidateTests.class, RefasterCollectionTestUtil.class) + .setArgs(ImmutableList.of("-XepOpt:RefasterValidateTests:TemplateCollection=" + className)) + .addInputLines( + clazz.getName() + "TestInput.java", + getContentOfResourceFile(clazz, className + "TestInput.java")) + .addOutputLines( + clazz.getName() + "TestOutput.java", + getContentOfResourceFile(clazz, className + "TestOutput.java")) .doTest(TEXT_MATCH); } - /** - * A {@link BugChecker} that tests the Refaster templates of a given template collection and - * validates its tests. - */ - @BugPattern( - name = "RefasterTestBugChecker", - summary = "Validate a Refaster template collection and its tests", - severity = ERROR) - public static final class RefasterTestBugChecker extends BugChecker - implements CompilationUnitTreeMatcher { - private static final long serialVersionUID = 1L; - private static final Collector LIST_COLLECTOR = - joining("\n- ", "\n- ", "\n"); - - private final ImmutableSet templateNamesFromClassPath; - private final RefasterCheck delegate; - - /** - * Instantiates a customized {@link RefasterTestBugChecker}. - * - * @param flags Any provided command line flags. - */ - public RefasterTestBugChecker(ErrorProneFlags flags) { - String templateCollection = flags.get("RefasterTestChecker:TemplateCollection").orElseThrow(); - delegate = - new RefasterCheck( - ErrorProneFlags.fromMap( - ImmutableMap.of(INCLUDED_TEMPLATES_PATTERN_FLAG, templateCollection + ".*"))); - templateNamesFromClassPath = - ALL_CODE_TRANSFORMERS.get().keySet().stream() - .filter(k -> k.contains(templateCollection)) - .map(k -> k.replace(templateCollection + "$", "")) - .sorted() - .collect(toImmutableSet()); - } - - @Override - public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { - List matches = new ArrayList<>(); - delegate.matchCompilationUnit( - tree, - VisitorState.createForCustomFindingCollection(new SubContext(state.context), matches::add) - .withPath(state.getPath())); - - JCCompilationUnit compilationUnit = (JCCompilationUnit) tree; - ImmutableRangeMap matchesRangeMap = - buildRangeMapForMatches(matches, compilationUnit.endPositions); - - ImmutableSet templatesWithoutMatch = getTemplateNamesWithoutMatch(matches); - if (!templatesWithoutMatch.isEmpty()) { - appendCommentToCompilationUnit( - String.format( - "Did not encounter a test in `%s` for the following template(s)", - getNameFromFQCN(compilationUnit.sourcefile.getName().replace(".java", ""))), - templatesWithoutMatch.stream().collect(LIST_COLLECTOR), - state); - } - - ValidateMatchesInMethodsScanner scanner = - new ValidateMatchesInMethodsScanner(matchesRangeMap); - scanner.scan(tree.getTypeDecls(), state); - - matches.forEach(state::reportMatch); - return Description.NO_MATCH; - } - - private ImmutableSet getTemplateNamesWithoutMatch(List matches) { - ImmutableSet templateNamesOfMatches = - matches.stream() - .map(description -> description.checkName) - .map(RefasterTestBugChecker::getNameFromFQCN) - .collect(toImmutableSet()); - - return Sets.difference(templateNamesFromClassPath, templateNamesOfMatches).immutableCopy(); - } - - private void appendCommentToCompilationUnit(String message, String list, VisitorState state) { - String comment = String.format("\n/* %s:%s*/", message, list); - CompilationUnitTree compilationUnit = state.getPath().getCompilationUnit(); - state.reportMatch( - describeMatch(compilationUnit, SuggestedFix.postfixWith(compilationUnit, comment))); - } - - private static ImmutableRangeMap buildRangeMapForMatches( - List matches, EndPosTable endPositions) { - ImmutableRangeMap.Builder rangeMap = ImmutableRangeMap.builder(); - - for (Description description : matches) { - Set replacements = - Iterables.getOnlyElement(description.fixes).getReplacements(endPositions); - Replacement replacement = Iterables.getOnlyElement(replacements); - - rangeMap.put(replacement.range(), getNameFromFQCN(description.checkName)); - } - return rangeMap.build(); - } - - private static String getNameFromFQCN(String fqcn) { - return fqcn.substring(fqcn.lastIndexOf('.') + 1); - } - - private class ValidateMatchesInMethodsScanner extends TreeScanner { - private final ImmutableRangeMap matchesRangeMap; - - ValidateMatchesInMethodsScanner(ImmutableRangeMap matchesRangeMap) { - this.matchesRangeMap = matchesRangeMap; - } - - @Override - public Void visitMethod(MethodTree tree, VisitorState state) { - if (ASTHelpers.isGeneratedConstructor(tree)) { - return super.visitMethod(tree, state); - } - - String methodName = tree.getName().toString().replace("test", ""); - int startPosition = ASTHelpers.getStartPosition(tree); - int endPosition = state.getEndPosition(tree); - LineMap lineMap = state.getPath().getCompilationUnit().getLineMap(); - - ImmutableRangeMap matchesInCurrentMethod = - matchesRangeMap.subRangeMap(Range.open(startPosition, endPosition)); - boolean correctTemplatesMatchedInMethod = - matchesInCurrentMethod.asMapOfRanges().values().stream().allMatch(methodName::equals); - if (!correctTemplatesMatchedInMethod) { - appendCommentToCompilationUnit( - String.format( - "The following matches unexpectedly occurred in method `%s`", tree.getName()), - matchesRangeMap.asMapOfRanges().entrySet().stream() - .map( - e -> - String.format( - "Template `%s` matches on line %s, while it should match in a method named `test%s`.", - e.getValue(), - lineMap.getLineNumber(e.getKey().lowerEndpoint()), - e.getValue())) - .collect(LIST_COLLECTOR), - state); - } - return super.visitMethod(tree, state); - } + private static String getContentOfResourceFile(Class clazz, String resourceName) { + JavaFileObject object = FileObjects.forResource(clazz, resourceName); + try { + return object.getCharContent(false).toString(); + } catch (IOException e) { + throw new IllegalStateException("Can't retrieve content for file " + resourceName, e); } } } diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java new file mode 100644 index 0000000000..9804779ba9 --- /dev/null +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java @@ -0,0 +1,182 @@ +package tech.picnic.errorprone.refaster.test; + +import static com.google.common.collect.ImmutableSet.toImmutableSet; +import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; +import static java.util.stream.Collectors.joining; +import static tech.picnic.errorprone.refaster.runner.RefasterCheck.INCLUDED_TEMPLATES_PATTERN_FLAG; + +import com.google.common.base.Suppliers; +import com.google.common.collect.ImmutableListMultimap; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableRangeMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Range; +import com.google.common.collect.Sets; +import com.google.errorprone.BugPattern; +import com.google.errorprone.CodeTransformer; +import com.google.errorprone.ErrorProneFlags; +import com.google.errorprone.SubContext; +import com.google.errorprone.VisitorState; +import com.google.errorprone.bugpatterns.BugChecker; +import com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher; +import com.google.errorprone.fixes.Replacement; +import com.google.errorprone.fixes.SuggestedFix; +import com.google.errorprone.matchers.Description; +import com.google.errorprone.util.ASTHelpers; +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.LineMap; +import com.sun.source.tree.MethodTree; +import com.sun.source.util.TreeScanner; +import com.sun.tools.javac.tree.EndPosTable; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.function.Supplier; +import java.util.stream.Collector; +import tech.picnic.errorprone.refaster.runner.CodeTransformers; +import tech.picnic.errorprone.refaster.runner.RefasterCheck; + +/** + * A {@link BugChecker} that tests the Refaster templates of a given template collection and + * validates its tests. + */ +@BugPattern( + name = "RefasterValidateTests", + summary = "Validate a Refaster template collection and its tests", + severity = ERROR) +public final class RefasterValidateTests extends BugChecker implements CompilationUnitTreeMatcher { + private static final long serialVersionUID = 1L; + private static final Supplier> + ALL_CODE_TRANSFORMERS = Suppliers.memoize(CodeTransformers::loadAllCodeTransformers); + private static final Collector LIST_COLLECTOR = + joining("\n- ", "\n- ", "\n"); + + private final ImmutableSet templateNamesFromClassPath; + private final RefasterCheck delegate; + + /** + * Instantiates a customized {@link RefasterValidateTests}. + * + * @param flags Any provided command line flags. + */ + public RefasterValidateTests(ErrorProneFlags flags) { + String templateCollection = flags.get("RefasterValidateTests:TemplateCollection").orElseThrow(); + delegate = + new RefasterCheck( + ErrorProneFlags.fromMap( + ImmutableMap.of(INCLUDED_TEMPLATES_PATTERN_FLAG, templateCollection + ".*"))); + templateNamesFromClassPath = + ALL_CODE_TRANSFORMERS.get().keySet().stream() + .filter(k -> k.contains(templateCollection)) + .map(k -> k.replace(templateCollection + "$", "")) + .sorted() + .collect(toImmutableSet()); + } + + @Override + public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { + List matches = new ArrayList<>(); + delegate.matchCompilationUnit( + tree, + VisitorState.createForCustomFindingCollection(new SubContext(state.context), matches::add) + .withPath(state.getPath())); + + JCCompilationUnit compilationUnit = (JCCompilationUnit) tree; + ImmutableRangeMap matchesRangeMap = + buildRangeMapForMatches(matches, compilationUnit.endPositions); + + ImmutableSet templatesWithoutMatch = getTemplateNamesWithoutMatch(matches); + if (!templatesWithoutMatch.isEmpty()) { + appendCommentToCompilationUnit( + String.format( + "Did not encounter a test in `%s` for the following template(s)", + getNameFromFQCN(compilationUnit.sourcefile.getName().replace(".java", ""))), + templatesWithoutMatch.stream().collect(LIST_COLLECTOR), + state); + } + + ValidateMatchesInMethodsScanner scanner = new ValidateMatchesInMethodsScanner(matchesRangeMap); + scanner.scan(tree.getTypeDecls(), state); + + matches.forEach(state::reportMatch); + return Description.NO_MATCH; + } + + private ImmutableSet getTemplateNamesWithoutMatch(List matches) { + ImmutableSet templateNamesOfMatches = + matches.stream() + .map(description -> description.checkName) + .map(RefasterValidateTests::getNameFromFQCN) + .collect(toImmutableSet()); + + return Sets.difference(templateNamesFromClassPath, templateNamesOfMatches).immutableCopy(); + } + + private void appendCommentToCompilationUnit(String message, String list, VisitorState state) { + String comment = String.format("\n/* %s:%s*/", message, list); + CompilationUnitTree compilationUnit = state.getPath().getCompilationUnit(); + state.reportMatch( + describeMatch(compilationUnit, SuggestedFix.postfixWith(compilationUnit, comment))); + } + + private static ImmutableRangeMap buildRangeMapForMatches( + List matches, EndPosTable endPositions) { + ImmutableRangeMap.Builder rangeMap = ImmutableRangeMap.builder(); + + for (Description description : matches) { + Set replacements = + Iterables.getOnlyElement(description.fixes).getReplacements(endPositions); + Replacement replacement = Iterables.getOnlyElement(replacements); + + rangeMap.put(replacement.range(), getNameFromFQCN(description.checkName)); + } + return rangeMap.build(); + } + + private static String getNameFromFQCN(String fqcn) { + return fqcn.substring(fqcn.lastIndexOf('.') + 1); + } + + private class ValidateMatchesInMethodsScanner extends TreeScanner { + private final ImmutableRangeMap matchesRangeMap; + + ValidateMatchesInMethodsScanner(ImmutableRangeMap matchesRangeMap) { + this.matchesRangeMap = matchesRangeMap; + } + + @Override + public Void visitMethod(MethodTree tree, VisitorState state) { + if (ASTHelpers.isGeneratedConstructor(tree)) { + return super.visitMethod(tree, state); + } + + String methodName = tree.getName().toString().replace("test", ""); + int startPosition = ASTHelpers.getStartPosition(tree); + int endPosition = state.getEndPosition(tree); + LineMap lineMap = state.getPath().getCompilationUnit().getLineMap(); + + ImmutableRangeMap matchesInCurrentMethod = + matchesRangeMap.subRangeMap(Range.open(startPosition, endPosition)); + boolean correctTemplatesMatchedInMethod = + matchesInCurrentMethod.asMapOfRanges().values().stream().allMatch(methodName::equals); + if (!correctTemplatesMatchedInMethod) { + appendCommentToCompilationUnit( + String.format( + "The following matches unexpectedly occurred in method `%s`", tree.getName()), + matchesRangeMap.asMapOfRanges().entrySet().stream() + .map( + e -> + String.format( + "Template `%s` matches on line %s, while it should match in a method named `test%s`.", + e.getValue(), + lineMap.getLineNumber(e.getKey().lowerEndpoint()), + e.getValue())) + .collect(LIST_COLLECTOR), + state); + } + return super.visitMethod(tree, state); + } + } +} From 384de95a76c484279c0035413d133df7ed41cc1a Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 28 Mar 2022 15:24:07 +0200 Subject: [PATCH 08/41] Apply suggestions --- .../refaster/test/RefasterValidateTests.java | 47 ++++++++++--------- .../test/PartialTestMatchTemplates.java | 32 +++++++++++++ .../test/RefasterCollectionTestUtilTest.java | 1 + ...MatchInWrongMethodTemplatesTestOutput.java | 11 +++-- ...ngTestAndWrongTestTemplatesTestOutput.java | 18 +++---- .../PartialTestMatchTemplatesTestInput.java | 9 ++++ .../PartialTestMatchTemplatesTestOutput.java | 13 +++++ ...emplateWithoutTestTemplatesTestOutput.java | 9 ++-- 8 files changed, 101 insertions(+), 39 deletions(-) create mode 100644 refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestInput.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestOutput.java diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java index 9804779ba9..d679a21f93 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java @@ -6,6 +6,7 @@ import static tech.picnic.errorprone.refaster.runner.RefasterCheck.INCLUDED_TEMPLATES_PATTERN_FLAG; import com.google.common.base.Suppliers; +import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableRangeMap; @@ -27,6 +28,7 @@ import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.LineMap; import com.sun.source.tree.MethodTree; +import com.sun.source.tree.Tree; import com.sun.source.util.TreeScanner; import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; @@ -34,7 +36,7 @@ import java.util.List; import java.util.Set; import java.util.function.Supplier; -import java.util.stream.Collector; +import java.util.stream.Stream; import tech.picnic.errorprone.refaster.runner.CodeTransformers; import tech.picnic.errorprone.refaster.runner.RefasterCheck; @@ -50,8 +52,6 @@ public final class RefasterValidateTests extends BugChecker implements Compilati private static final long serialVersionUID = 1L; private static final Supplier> ALL_CODE_TRANSFORMERS = Suppliers.memoize(CodeTransformers::loadAllCodeTransformers); - private static final Collector LIST_COLLECTOR = - joining("\n- ", "\n- ", "\n"); private final ImmutableSet templateNamesFromClassPath; private final RefasterCheck delegate; @@ -87,13 +87,15 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s ImmutableRangeMap matchesRangeMap = buildRangeMapForMatches(matches, compilationUnit.endPositions); - ImmutableSet templatesWithoutMatch = getTemplateNamesWithoutMatch(matches); + ImmutableSet templatesWithoutMatch = + getTemplateNamesWithoutMatch(matchesRangeMap.asMapOfRanges().values()); if (!templatesWithoutMatch.isEmpty()) { - appendCommentToCompilationUnit( + addCommentToTreeInOutputFile( + tree, String.format( "Did not encounter a test in `%s` for the following template(s)", getNameFromFQCN(compilationUnit.sourcefile.getName().replace(".java", ""))), - templatesWithoutMatch.stream().collect(LIST_COLLECTOR), + templatesWithoutMatch.stream(), state); } @@ -104,21 +106,21 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s return Description.NO_MATCH; } - private ImmutableSet getTemplateNamesWithoutMatch(List matches) { - ImmutableSet templateNamesOfMatches = - matches.stream() - .map(description -> description.checkName) - .map(RefasterValidateTests::getNameFromFQCN) - .collect(toImmutableSet()); - - return Sets.difference(templateNamesFromClassPath, templateNamesOfMatches).immutableCopy(); + private ImmutableSet getTemplateNamesWithoutMatch( + ImmutableCollection templateNamesOfMatches) { + return Sets.difference(templateNamesFromClassPath, ImmutableSet.copyOf(templateNamesOfMatches)) + .immutableCopy(); } - private void appendCommentToCompilationUnit(String message, String list, VisitorState state) { - String comment = String.format("\n/* %s:%s*/", message, list); - CompilationUnitTree compilationUnit = state.getPath().getCompilationUnit(); - state.reportMatch( - describeMatch(compilationUnit, SuggestedFix.postfixWith(compilationUnit, comment))); + private void addCommentToTreeInOutputFile( + Tree tree, String message, Stream violations, VisitorState state) { + String listOfViolations = violations.collect(joining("\n* - ", "\n* - ", "\n")); + String comment = String.format("/*\n* %s:%s*/\n", message, listOfViolations); + SuggestedFix fixWithComment = + tree instanceof MethodTree + ? SuggestedFix.prefixWith(tree, comment) + : SuggestedFix.postfixWith(tree, "\n" + comment); + state.reportMatch(describeMatch(tree, fixWithComment)); } private static ImmutableRangeMap buildRangeMapForMatches( @@ -162,18 +164,19 @@ public Void visitMethod(MethodTree tree, VisitorState state) { boolean correctTemplatesMatchedInMethod = matchesInCurrentMethod.asMapOfRanges().values().stream().allMatch(methodName::equals); if (!correctTemplatesMatchedInMethod) { - appendCommentToCompilationUnit( + addCommentToTreeInOutputFile( + tree, String.format( "The following matches unexpectedly occurred in method `%s`", tree.getName()), matchesRangeMap.asMapOfRanges().entrySet().stream() + .filter(e -> !e.getValue().equals(methodName)) .map( e -> String.format( "Template `%s` matches on line %s, while it should match in a method named `test%s`.", e.getValue(), lineMap.getLineNumber(e.getKey().lowerEndpoint()), - e.getValue())) - .collect(LIST_COLLECTOR), + e.getValue())), state); } return super.visitMethod(tree, state); diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java new file mode 100644 index 0000000000..1db6ec3579 --- /dev/null +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java @@ -0,0 +1,32 @@ +package tech.picnic.errorprone.refaster.test; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; + +final class PartialTestMatchTemplates { + private PartialTestMatchTemplates() {} + + static final class StringEquals { + @BeforeTemplate + boolean before(String string) { + return string.length() == 0; + } + + @AfterTemplate + boolean after(String string) { + return string.equals(""); + } + } + + static final class StringIsEmpty { + @BeforeTemplate + boolean before(String string) { + return string.equals(""); + } + + @AfterTemplate + boolean after(String string) { + return string.isEmpty(); + } + } +} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java index fd8b7773a6..c49c7c4e05 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java @@ -11,6 +11,7 @@ final class RefasterCollectionTestUtilTest { MatchInWrongMethodTemplates.class, MethodNameWithNumberTemplates.class, MissingTestAndWrongTestTemplates.class, + PartialTestMatchTemplates.class, TemplateWithoutTestTemplates.class }) void verifyRefasterTemplateCollections(Class clazz) { diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java index 14f84ad943..7f693ab7e9 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java @@ -2,14 +2,15 @@ /** Code to test the Refaster templates from `MatchInWrongMethodTemplates`. */ final class MatchInWrongMethodTemplatesTest implements RefasterTemplateTestCase { + /* + * The following matches unexpectedly occurred in method `testWrongName`: + * - Template `StringIsEmpty` matches on line 6, while it should match in a method named `testStringIsEmpty`. + * - Template `StringIsEmpty` matches on line 7, while it should match in a method named `testStringIsEmpty`. + * - Template `StringIsEmpty` matches on line 8, while it should match in a method named `testStringIsEmpty`. + */ boolean testWrongName() { "foo".isEmpty(); "bar".isEmpty(); return "baz".isEmpty(); } } -/* The following matches unexpectedly occurred in method `testWrongName`: -- Template `StringIsEmpty` matches on line 6, while it should match in a method named `testStringIsEmpty`. -- Template `StringIsEmpty` matches on line 7, while it should match in a method named `testStringIsEmpty`. -- Template `StringIsEmpty` matches on line 8, while it should match in a method named `testStringIsEmpty`. -*/ diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java index 4af5cd857e..e203475245 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java @@ -2,17 +2,19 @@ /** Code to test the Refaster templates from `MissingTestAndWrongTestTemplates`. */ final class MissingTestAndWrongTestTemplatesTest implements RefasterTemplateTestCase { + /* + * The following matches unexpectedly occurred in method `testWrongName`: + * - Template `StringIsEmpty` matches on line 6, while it should match in a method named `testStringIsEmpty`. + * - Template `StringIsEmpty` matches on line 7, while it should match in a method named `testStringIsEmpty`. + * - Template `StringIsEmpty` matches on line 8, while it should match in a method named `testStringIsEmpty`. + */ boolean testWrongName() { "foo".isEmpty(); "bar".isEmpty(); return "baz".isEmpty(); } } -/* Did not encounter a test in `MissingTestAndWrongTestTemplatesTestInput` for the following template(s): -- TemplateWithoutTest -*/ -/* The following matches unexpectedly occurred in method `testWrongName`: -- Template `StringIsEmpty` matches on line 6, while it should match in a method named `testStringIsEmpty`. -- Template `StringIsEmpty` matches on line 7, while it should match in a method named `testStringIsEmpty`. -- Template `StringIsEmpty` matches on line 8, while it should match in a method named `testStringIsEmpty`. -*/ +/* + * Did not encounter a test in `MissingTestAndWrongTestTemplatesTestInput` for the following template(s): + * - TemplateWithoutTest + */ diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestInput.java new file mode 100644 index 0000000000..696065afb6 --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestInput.java @@ -0,0 +1,9 @@ +package tech.picnic.errorprone.refaster.test; + +/** Code to test the Refaster templates from {@link PartialTestMatchTemplates}. */ +final class PartialTestMatchTemplatesTest implements RefasterTemplateTestCase { + boolean testStringIsEmpty() { + boolean b = "foo".length() == 0; + return "bar".equals(""); + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestOutput.java new file mode 100644 index 0000000000..7171cf8942 --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestOutput.java @@ -0,0 +1,13 @@ +package tech.picnic.errorprone.refaster.test; + +/** Code to test the Refaster templates from {@link PartialTestMatchTemplates}. */ +final class PartialTestMatchTemplatesTest implements RefasterTemplateTestCase { + /* + * The following matches unexpectedly occurred in method `testStringIsEmpty`: + * - Template `StringEquals` matches on line 6, while it should match in a method named `testStringEquals`. + */ + boolean testStringIsEmpty() { + boolean b = "foo".equals(""); + return "bar".isEmpty(); + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java index 2a39707b48..92a8486e46 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java @@ -6,7 +6,8 @@ boolean testStringIsEmpty() { return "foo".isEmpty(); } } -/* Did not encounter a test in `TemplateWithoutTestTemplatesTestInput` for the following template(s): -- AnotherTemplateWithoutTest -- TemplateWithoutTest -*/ +/* + * Did not encounter a test in `TemplateWithoutTestTemplatesTestInput` for the following template(s): + * - AnotherTemplateWithoutTest + * - TemplateWithoutTest + */ From be1967f1a4726f8eaaf9d30f10d40f6b8782658f Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Thu, 31 Mar 2022 20:56:41 +0200 Subject: [PATCH 09/41] Add explanation in `RefasterCollectionTestUtilTest.java` --- .../refaster/test/RefasterCollectionTestUtilTest.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java index c49c7c4e05..b12e97034c 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java @@ -3,7 +3,15 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -/** Validate error reporting of the Refaster template collections. */ +/** + * Validate error reporting of the Refaster template collections. + * + *

The goal of the Refaster template collections under test is to verify the reporting of {@link + * RefasterCollectionTestUtil} using the associated `TestInput` and `TestOutput` files. Normally, + * {@link RefasterCollectionTestUtil} will report error messages directly in the console where the + * results of the tests are shown. However, to verify that these messages are correct, the + * `*TestOutput` files in this package contain error reporting that is normally not written. + */ final class RefasterCollectionTestUtilTest { @ParameterizedTest @ValueSource( From 0e48bdf3a6f6a5791657972df2a244bd70b10bfa Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Fri, 1 Apr 2022 16:55:36 +0200 Subject: [PATCH 10/41] First version of README.md --- refaster-test-support/README.md | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 refaster-test-support/README.md diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md new file mode 100644 index 0000000000..8f6a0e4546 --- /dev/null +++ b/refaster-test-support/README.md @@ -0,0 +1,58 @@ +# Refaster test support + +This package provides utilities to validate Refaster template collections. + +## What does this module do? + +These utilities allow for validating the rewrites performed by Refaster templates, or absence thereof. Each collection +of Refaster templates is applied to an input file and the result of that should match the provided output file. + +This extension ensures that each Refaster template has a test and has a match at the expected place. If a test is +missing, an error is reported in the test's console. This way, the developer has some guidance on how to write tests and +assurance that every template is tested. + +## How to test a collection of Refaster templates? + +In summary, to test Refaster templates using the `RefasterCollectionTestUtil`, one should create an input and output +file. The Refaster templates from the collection are applied on the input file and should exactly match the content of +the provided output file. + +To test Refaster templates, one can create a (parameterized) test for every class containing the Refaster templates to +invoke the `RefasterCollectionTestUtil`. A class that contains one or more Refaster templates is called a Refaster +template collection. This test utility applies the Refaster templates in the collection to a provided input file, and +expects the result to exactly match the contents of a provided output file. + +To adopt this setup, the following requirements have to be met: + +- Create a class with a (parameterized) test method that calls + the `RefasterCollectionTestUtil#validateTemplateCollection` and passes the collection(s) to validate. The Refaster + template collection must match the naming convention `Templates.java`. +- An input file matching the naming convention `TemplatesTestInput.java` is added for every + template collection. +- An output file matching the naming convention `TemplatesTestOutput.java` + file is added for every template collection. +- For each Refaster template in the collection, the input and output file contain a method. The name of the method is + equal to the name of the Refaster template prefixed with `test` (e.g. `test`). +- The method contains at least one expression that matches the `@BeforeTemplate`. As a result, the output file contains + the same method with an updated expression, matching the content of the `@AfterTemplate`. + +As a result from these tests, unexpected output will be shown in the console. + +An example of a folder structure for such a setup: + +``` +main/java/ + tech.picnic.errorprone.refastertemplates + └── ExampleTemplates.java -- Contains multiple Refaster templates + └── Example1Template + └── Example2Template + +test/ + java/ + └── tech.picnic.errorprone.refastertemplates + └── RefasterCollectionTest.java -- Here the test invokes `RefasterCollectionTestUtil#validateTemplateCollection`. + resources/ + └── tech.picnic.errorprone.refastertemplates + └── ExampleTemplatesTestInput.java -- Contains a class named `ExampleTemplatesTest` + └── ExampleTemplatesTestOutput.java -- Contains a class named `ExampleTemplatesTest` +``` \ No newline at end of file From 3aee01620040c23c5fa5feeefe48c9f4ed1b9c05 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 4 Apr 2022 17:24:20 +0200 Subject: [PATCH 11/41] README suggestions --- refaster-test-support/README.md | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index 8f6a0e4546..329d086f38 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -26,19 +26,20 @@ To adopt this setup, the following requirements have to be met: - Create a class with a (parameterized) test method that calls the `RefasterCollectionTestUtil#validateTemplateCollection` and passes the collection(s) to validate. The Refaster - template collection must match the naming convention `Templates.java`. -- An input file matching the naming convention `TemplatesTestInput.java` is added for every + template collection must match the naming convention `Templates.java`. +- An input file matching the naming convention `TemplatesTestInput.java` is added for every template collection. -- An output file matching the naming convention `TemplatesTestOutput.java` +- An output file matching the naming convention `TemplatesTestOutput.java` file is added for every template collection. -- For each Refaster template in the collection, the input and output file contain a method. The name of the method is - equal to the name of the Refaster template prefixed with `test` (e.g. `test`). -- The method contains at least one expression that matches the `@BeforeTemplate`. As a result, the output file contains - the same method with an updated expression, matching the content of the `@AfterTemplate`. +- For each Refaster template in the collection, the input and output file must contain a method. The name of the method + is equal to the name of the Refaster template prefixed with `test` (e.g. `test`). +- The method contains at least one expression that matches the `@BeforeTemplate` one specific Refaster template. As a + result, the output file contains the same method with an updated expression, matching the content of + the `@AfterTemplate`. Additionally, incorrect matches of _other_ Refaster templates in the method are flagged. As a result from these tests, unexpected output will be shown in the console. -An example of a folder structure for such a setup: +An example of a folder structure for such a setup is as follows: ``` main/java/ @@ -53,6 +54,8 @@ test/ └── RefasterCollectionTest.java -- Here the test invokes `RefasterCollectionTestUtil#validateTemplateCollection`. resources/ └── tech.picnic.errorprone.refastertemplates - └── ExampleTemplatesTestInput.java -- Contains a class named `ExampleTemplatesTest` - └── ExampleTemplatesTestOutput.java -- Contains a class named `ExampleTemplatesTest` + └── ExampleTemplatesTestInput.java -- Contains a class named `ExampleTemplatesTest` and + two methods named `testExample1Template` and `testExample2Template`. + └── ExampleTemplatesTestOutput.java -- Contains a class named `ExampleTemplatesTest` and + two methods named `testExample1Template` and `testExample2Template`. ``` \ No newline at end of file From 5b781cfbc3f273efcfc2bfe095d2cf48d00ac8a7 Mon Sep 17 00:00:00 2001 From: Pieter Dirk Soels Date: Mon, 4 Apr 2022 18:08:22 +0200 Subject: [PATCH 12/41] Minor README tweaks --- refaster-test-support/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index 329d086f38..1ac884f411 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -33,8 +33,8 @@ To adopt this setup, the following requirements have to be met: file is added for every template collection. - For each Refaster template in the collection, the input and output file must contain a method. The name of the method is equal to the name of the Refaster template prefixed with `test` (e.g. `test`). -- The method contains at least one expression that matches the `@BeforeTemplate` one specific Refaster template. As a - result, the output file contains the same method with an updated expression, matching the content of +- The method contains at least one expression that matches the `@BeforeTemplate` of one specific Refaster template. As + a result, the output file contains the same method with an updated expression, matching the content of the `@AfterTemplate`. Additionally, incorrect matches of _other_ Refaster templates in the method are flagged. As a result from these tests, unexpected output will be shown in the console. @@ -44,7 +44,7 @@ An example of a folder structure for such a setup is as follows: ``` main/java/ tech.picnic.errorprone.refastertemplates - └── ExampleTemplates.java -- Contains multiple Refaster templates + └── ExampleTemplates.java -- Contains multiple Refaster templates. └── Example1Template └── Example2Template @@ -58,4 +58,4 @@ test/ two methods named `testExample1Template` and `testExample2Template`. └── ExampleTemplatesTestOutput.java -- Contains a class named `ExampleTemplatesTest` and two methods named `testExample1Template` and `testExample2Template`. -``` \ No newline at end of file +``` From 6ebcda1bf97358e1e734b20b69a7e5e79e1909bf Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Sat, 21 May 2022 13:25:58 +0200 Subject: [PATCH 13/41] Global fmt configuration and wrap lines in README --- error-prone-contrib/pom.xml | 9 ---- pom.xml | 3 ++ refaster-runner/pom.xml | 2 +- refaster-test-support/README.md | 79 ++++++++++++++++++++------------- refaster-test-support/pom.xml | 9 ---- 5 files changed, 53 insertions(+), 49 deletions(-) diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index 944acb8855..4664aa878d 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -210,15 +210,6 @@ - - com.spotify.fmt - fmt-maven-plugin - - - ${basedir}/src/test/resources - - - org.apache.maven.plugins maven-compiler-plugin diff --git a/pom.xml b/pom.xml index 50ce50213c..987eecd951 100644 --- a/pom.xml +++ b/pom.xml @@ -449,6 +449,9 @@ fmt-maven-plugin 2.19 + + ${basedir}/src/test/resources + never false diff --git a/refaster-runner/pom.xml b/refaster-runner/pom.xml index a273ca9938..b66abe2da1 100644 --- a/refaster-runner/pom.xml +++ b/refaster-runner/pom.xml @@ -11,7 +11,7 @@ refaster-runner Picnic :: Error Prone Support :: Refaster Runner - Apply Refaster templates found on the classpath. + Applies Refaster templates found on the classpath. diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index 1ac884f411..ca05112966 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -4,58 +4,77 @@ This package provides utilities to validate Refaster template collections. ## What does this module do? -These utilities allow for validating the rewrites performed by Refaster templates, or absence thereof. Each collection -of Refaster templates is applied to an input file and the result of that should match the provided output file. +These utilities allow for validating the rewrites performed by Refaster +templates, or absence thereof. Each collection of Refaster templates is applied +to an input file and the result of that should match the provided output file. -This extension ensures that each Refaster template has a test and has a match at the expected place. If a test is -missing, an error is reported in the test's console. This way, the developer has some guidance on how to write tests and +This extension ensures that each Refaster template has a test and has a match at +the expected place. If a test is missing, an error is reported in the test's +console. This way, the developer has some guidance on how to write tests and assurance that every template is tested. ## How to test a collection of Refaster templates? -In summary, to test Refaster templates using the `RefasterCollectionTestUtil`, one should create an input and output -file. The Refaster templates from the collection are applied on the input file and should exactly match the content of +In summary, to test Refaster templates using the `RefasterCollectionTestUtil`, +one should create an input and output file. The Refaster templates from the +collection are applied on the input file and should exactly match the content of the provided output file. -To test Refaster templates, one can create a (parameterized) test for every class containing the Refaster templates to -invoke the `RefasterCollectionTestUtil`. A class that contains one or more Refaster templates is called a Refaster -template collection. This test utility applies the Refaster templates in the collection to a provided input file, and -expects the result to exactly match the contents of a provided output file. +To test Refaster templates, one can create a (parameterized) test for every +class containing the Refaster templates to invoke the +`RefasterCollectionTestUtil`. A class that contains one or more Refaster +templates is called a Refaster template collection. This test utility applies +the Refaster templates in the collection to a provided input file, and expects +the result to exactly match the contents of a provided output file. To adopt this setup, the following requirements have to be met: - Create a class with a (parameterized) test method that calls - the `RefasterCollectionTestUtil#validateTemplateCollection` and passes the collection(s) to validate. The Refaster - template collection must match the naming convention `Templates.java`. -- An input file matching the naming convention `TemplatesTestInput.java` is added for every + the `RefasterCollectionTestUtil#validateTemplateCollection` and passes the + collection(s) to validate. The Refaster template collection must match the + naming convention `Templates.java`. +- An input file matching the naming convention + `TemplatesTestInput.java` is added for every template + collection. +- An output file matching the naming convention + `TemplatesTestOutput.java` file is added for every template collection. -- An output file matching the naming convention `TemplatesTestOutput.java` - file is added for every template collection. -- For each Refaster template in the collection, the input and output file must contain a method. The name of the method - is equal to the name of the Refaster template prefixed with `test` (e.g. `test`). -- The method contains at least one expression that matches the `@BeforeTemplate` of one specific Refaster template. As - a result, the output file contains the same method with an updated expression, matching the content of - the `@AfterTemplate`. Additionally, incorrect matches of _other_ Refaster templates in the method are flagged. +- For each Refaster template in the collection, the input and output file must + contain a method. The name of the method is equal to the name of the Refaster + template prefixed with `test` (e.g. `test`). +- The method contains at least one expression that matches the `@BeforeTemplate` + of one specific Refaster template. As a result, the output file contains the + same method with an updated expression, matching the content of the + `@AfterTemplate`. Additionally, incorrect matches of _other_ Refaster + templates in the method are flagged. As a result from these tests, unexpected output will be shown in the console. An example of a folder structure for such a setup is as follows: ``` -main/java/ - tech.picnic.errorprone.refastertemplates - └── ExampleTemplates.java -- Contains multiple Refaster templates. - └── Example1Template - └── Example2Template +main/ + java/ + tech.picnic.errorprone.refastertemplates + └── ExampleTemplates.java -- Contains multiple Refaster templates. + └── Example1Template + └── Example2Template test/ java/ └── tech.picnic.errorprone.refastertemplates - └── RefasterCollectionTest.java -- Here the test invokes `RefasterCollectionTestUtil#validateTemplateCollection`. + └── RefasterCollectionTest.java + -- Here the test invokes + -- `RefasterCollectionTestUtil#validateTemplateCollection`. resources/ └── tech.picnic.errorprone.refastertemplates - └── ExampleTemplatesTestInput.java -- Contains a class named `ExampleTemplatesTest` and - two methods named `testExample1Template` and `testExample2Template`. - └── ExampleTemplatesTestOutput.java -- Contains a class named `ExampleTemplatesTest` and - two methods named `testExample1Template` and `testExample2Template`. + └── ExampleTemplatesTestInput.java + -- Contains a class named `ExampleTemplatesTest` and + -- two methods named `testExample1Template` and + -- `testExample2Template`. + └── ExampleTemplatesTestOutput.java + -- Contains a class named `ExampleTemplatesTest` and + -- two methods named `testExample1Template` and + -- `testExample2Template`. ``` + diff --git a/refaster-test-support/pom.xml b/refaster-test-support/pom.xml index 6d56d037c0..12f60bbc3f 100644 --- a/refaster-test-support/pom.xml +++ b/refaster-test-support/pom.xml @@ -85,15 +85,6 @@ - - com.coveo - fmt-maven-plugin - - - ${basedir}/src/test/resources - - - org.apache.maven.plugins maven-compiler-plugin From 79b9e31b72a2b8ad093d5625f800533a7771e691 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 22 May 2022 17:19:30 +0200 Subject: [PATCH 14/41] Suggestions --- error-prone-contrib/pom.xml | 5 ----- pom.xml | 5 ----- refaster-runner/pom.xml | 2 +- refaster-test-support/pom.xml | 10 ---------- 4 files changed, 1 insertion(+), 21 deletions(-) diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index 4664aa878d..66eba535b5 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -51,11 +51,6 @@ ${project.groupId} refaster-support - - ${project.groupId} - refaster-test-support - test - com.fasterxml.jackson.core jackson-annotations diff --git a/pom.xml b/pom.xml index 987eecd951..33c519efe9 100644 --- a/pom.xml +++ b/pom.xml @@ -200,11 +200,6 @@ refaster-support ${project.version} - - ${project.groupId} - refaster-test-support - ${project.version} - com.fasterxml.jackson jackson-bom diff --git a/refaster-runner/pom.xml b/refaster-runner/pom.xml index b66abe2da1..9d319d635e 100644 --- a/refaster-runner/pom.xml +++ b/refaster-runner/pom.xml @@ -11,7 +11,7 @@ refaster-runner Picnic :: Error Prone Support :: Refaster Runner - Applies Refaster templates found on the classpath. + Exposes Refaster templates found on the classpath through a regular Error Prone check. diff --git a/refaster-test-support/pom.xml b/refaster-test-support/pom.xml index 12f60bbc3f..5ae525a2b7 100644 --- a/refaster-test-support/pom.xml +++ b/refaster-test-support/pom.xml @@ -45,11 +45,6 @@ ${project.groupId} refaster-runner - - com.google.auto.service - auto-service-annotations - provided - com.google.code.findbugs jsr305 @@ -65,11 +60,6 @@ guava provided - - org.junit.jupiter - junit-jupiter-api - test - org.junit.jupiter junit-jupiter-engine From 73381fdd6ec386d516aafe7f38a6d80aca571ab3 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 23 May 2022 15:06:35 +0200 Subject: [PATCH 15/41] Improve the `RefasterValidateTests` BugPattern --- .../AssertJMapTemplatesTestInput.java | 1 + .../AssertJMapTemplatesTestOutput.java | 1 + .../refaster/test/RefasterValidateTests.java | 10 +++++----- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJMapTemplatesTestInput.java index 020b18a476..84d9fb02d3 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJMapTemplatesTestInput.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableMap; import org.assertj.core.api.AbstractMapAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJMapTemplatesTest implements RefasterTemplateTestCase { AbstractMapAssert diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJMapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJMapTemplatesTestOutput.java index ff2a771151..65d1a3b4cb 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJMapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJMapTemplatesTestOutput.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableMap; import org.assertj.core.api.AbstractMapAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJMapTemplatesTest implements RefasterTemplateTestCase { AbstractMapAssert diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java index d679a21f93..07d0ef5f17 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java @@ -1,7 +1,8 @@ package tech.picnic.errorprone.refaster.test; -import static com.google.common.collect.ImmutableSet.toImmutableSet; +import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet; import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; +import static java.util.Comparator.naturalOrder; import static java.util.stream.Collectors.joining; import static tech.picnic.errorprone.refaster.runner.RefasterCheck.INCLUDED_TEMPLATES_PATTERN_FLAG; @@ -69,10 +70,9 @@ public RefasterValidateTests(ErrorProneFlags flags) { ImmutableMap.of(INCLUDED_TEMPLATES_PATTERN_FLAG, templateCollection + ".*"))); templateNamesFromClassPath = ALL_CODE_TRANSFORMERS.get().keySet().stream() - .filter(k -> k.contains(templateCollection)) + .filter(k -> k.startsWith(templateCollection)) .map(k -> k.replace(templateCollection + "$", "")) - .sorted() - .collect(toImmutableSet()); + .collect(toImmutableSortedSet(naturalOrder())); } @Override @@ -168,7 +168,7 @@ public Void visitMethod(MethodTree tree, VisitorState state) { tree, String.format( "The following matches unexpectedly occurred in method `%s`", tree.getName()), - matchesRangeMap.asMapOfRanges().entrySet().stream() + matchesInCurrentMethod.asMapOfRanges().entrySet().stream() .filter(e -> !e.getValue().equals(methodName)) .map( e -> From f9584ebcf394539e35d276e1fc458cd82fa1588a Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 29 May 2022 15:44:33 +0200 Subject: [PATCH 16/41] Suggestions --- .../refaster/runner/CodeTransformers.java | 21 +- .../refaster/runner/RefasterCheck.java | 8 +- refaster-test-support/README.md | 38 +-- refaster-test-support/pom.xml | 2 +- .../test/RefasterCollectionTestUtil.java | 41 ++- .../test/RefasterTemplateTestCase.java | 6 +- .../refaster/test/RefasterValidateTests.java | 244 ++++++++++++------ .../refaster/test/package-info.java | 2 +- .../test/MethodNameWithNumberTemplates.java | 8 +- ...MatchInWrongMethodTemplatesTestOutput.java | 2 +- ...thodNameWithNumberTemplatesTestOutput.java | 2 + ...ngTestAndWrongTestTemplatesTestOutput.java | 4 +- .../PartialTestMatchTemplatesTestOutput.java | 2 +- ...emplateWithoutTestTemplatesTestOutput.java | 2 +- 14 files changed, 238 insertions(+), 144 deletions(-) diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java index 5f97ef47e6..466b12b98f 100644 --- a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refaster.runner; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.reflect.ClassPath; @@ -11,18 +12,32 @@ import java.io.UncheckedIOException; import java.util.NoSuchElementException; import java.util.Optional; +import java.util.function.Supplier; /** Scans the classpath for `.refaster` files and loads them as {@link CodeTransformer}s. */ public final class CodeTransformers { private static final String REFASTER_TEMPLATE_SUFFIX = ".refaster"; + private static final Supplier> + ALL_CODE_TRANSFORMERS = Suppliers.memoize(CodeTransformers::loadAllCodeTransformers); private CodeTransformers() {} /** - * Use the classpath to find Refaster templates and return a map of the names with {@link - * CodeTransformer}s. + * Returns all Refaster {@link CodeTransformer}s found on the classpath. * - * @return A mapping from Refaster template names to {@link CodeTransformer}s. + *

This method returns a cached view; all invocations exception the first are very cheap. + * + * @return A mapping from Refaster template names to associated {@link CodeTransformer}s. + */ + public static ImmutableListMultimap getAllCodeTransformers() { + return ALL_CODE_TRANSFORMERS.get(); + } + + /** + * Scans the classpath to for compiled Refaster templates and returns the associated deserialized + * {@link CodeTransformer}s, indexed by their name. + * + * @return A mapping from Refaster template names to associated {@link CodeTransformer}s. */ public static ImmutableListMultimap loadAllCodeTransformers() { ImmutableListMultimap.Builder transformers = diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/RefasterCheck.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/RefasterCheck.java index 6375d60385..aba639aa40 100644 --- a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/RefasterCheck.java +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/RefasterCheck.java @@ -8,7 +8,6 @@ import static java.util.function.Predicate.not; import com.google.auto.service.AutoService; -import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableListMultimap; @@ -34,7 +33,6 @@ import java.util.Comparator; import java.util.List; import java.util.Map; -import java.util.function.Supplier; import java.util.regex.Pattern; import java.util.stream.Stream; @@ -59,9 +57,6 @@ public final class RefasterCheck extends BugChecker implements CompilationUnitTr private static final long serialVersionUID = 1L; - static final Supplier> ALL_CODE_TRANSFORMERS = - Suppliers.memoize(CodeTransformers::loadAllCodeTransformers); - private final CodeTransformer codeTransformer; /** Instantiates the default {@link RefasterCheck}. */ @@ -153,7 +148,8 @@ private static Stream getReplacements( } private static CodeTransformer createCompositeCodeTransformer(ErrorProneFlags flags) { - ImmutableListMultimap allTransformers = ALL_CODE_TRANSFORMERS.get(); + ImmutableListMultimap allTransformers = + CodeTransformers.getAllCodeTransformers(); return CompositeCodeTransformer.compose( flags .get(INCLUDED_TEMPLATES_PATTERN_FLAG) diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index ca05112966..6ae3bee466 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -1,24 +1,28 @@ # Refaster test support -This package provides utilities to validate Refaster template collections. +This module provides utilities to validate Refaster template collections. ## What does this module do? -These utilities allow for validating the rewrites performed by Refaster -templates, or absence thereof. Each collection of Refaster templates is applied -to an input file and the result of that should match the provided output file. +These utilities allow validating the rewrites (or their absence) performed by +Refaster templates. Each collection of Refaster templates defined in the same +top-level class is applied to an input file, and the resulting rewrites should +match the associated output file. -This extension ensures that each Refaster template has a test and has a match at -the expected place. If a test is missing, an error is reported in the test's -console. This way, the developer has some guidance on how to write tests and -assurance that every template is tested. +This extension ensures that each Refaster template is tested, making sure that +it matches and transforms code as intended. If a Refaster template is not +covered by a test, if it influences unrelated test code code, or if the +associated test doesn't follow certain established standards, then this +irregularity will be reported, and the associated template collection test will +fail. This way developers receive guidance on how to write Refaster template +tests and assurance that every template is properly tested. ## How to test a collection of Refaster templates? In summary, to test Refaster templates using the `RefasterCollectionTestUtil`, one should create an input and output file. The Refaster templates from the -collection are applied on the input file and should exactly match the content of -the provided output file. +collection are applied on the input file and should exactly match the content +of the provided output file. To test Refaster templates, one can create a (parameterized) test for every class containing the Refaster templates to invoke the @@ -29,8 +33,8 @@ the result to exactly match the contents of a provided output file. To adopt this setup, the following requirements have to be met: -- Create a class with a (parameterized) test method that calls - the `RefasterCollectionTestUtil#validateTemplateCollection` and passes the +- Create a class with a (parameterized) test method that calls the + `RefasterCollectionTestUtil#validateTemplateCollection` and passes the collection(s) to validate. The Refaster template collection must match the naming convention `Templates.java`. - An input file matching the naming convention @@ -42,11 +46,11 @@ To adopt this setup, the following requirements have to be met: - For each Refaster template in the collection, the input and output file must contain a method. The name of the method is equal to the name of the Refaster template prefixed with `test` (e.g. `test`). -- The method contains at least one expression that matches the `@BeforeTemplate` - of one specific Refaster template. As a result, the output file contains the - same method with an updated expression, matching the content of the - `@AfterTemplate`. Additionally, incorrect matches of _other_ Refaster - templates in the method are flagged. +- The method contains at least one expression that matches the + `@BeforeTemplate` of one specific Refaster template. As a result, the output + file contains the same method with an updated expression, matching the + content of the `@AfterTemplate`. Additionally, incorrect matches of _other_ + Refaster templates in the method are flagged. As a result from these tests, unexpected output will be shown in the console. diff --git a/refaster-test-support/pom.xml b/refaster-test-support/pom.xml index 5ae525a2b7..a40765e51d 100644 --- a/refaster-test-support/pom.xml +++ b/refaster-test-support/pom.xml @@ -11,7 +11,7 @@ refaster-test-support Picnic :: Error Prone Support :: Refaster Test Support - Utilities to support with the validation of Refaster templates tests. + Opinionated utilities for the testing of Refaster templates. diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java index 0a0ec42988..1ec70ca1aa 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java @@ -4,45 +4,34 @@ import com.google.common.collect.ImmutableList; import com.google.errorprone.BugCheckerRefactoringTestHelper; -import com.google.errorprone.FileObjects; -import java.io.IOException; -import javax.tools.JavaFileObject; -/** Utility to test Refaster templates and validate the tests for template collections. */ +/** + * Utility to test Refaster templates and validate that the tests use meet all requirements for + * template collection tests. + */ +// XXX: Consider dropping this class in favour of a static +// `RefasterTemplateCollectionValidator#validate` method. public final class RefasterCollectionTestUtil { private RefasterCollectionTestUtil() {} /** - * Verifies that all Refaster templates from a collection are covered by at least one test and - * that the match rewrites code in the correct test method. + * Verifies that all Refaster templates in the given collection class are covered by precisely one + * test method, defined explicitly for the purpose of exercising that template. * - *

Note that this doesn't guarantee full coverage: this test does not ascertain that all {@link - * com.google.errorprone.refaster.Refaster#anyOf} branches are tested. Idem for {@link - * com.google.errorprone.refaster.annotation.BeforeTemplate} methods in case there are multiple. + *

Note that a passing test does not guarantee full coverage: this test does not ascertain that + * all {@link com.google.errorprone.refaster.Refaster#anyOf} branches are tested. Likewise for + * {@link com.google.errorprone.refaster.annotation.BeforeTemplate} methods in case there are + * multiple. * * @param clazz The Refaster template collection under test. */ public static void validateTemplateCollection(Class clazz) { String className = clazz.getSimpleName(); - BugCheckerRefactoringTestHelper.newInstance( - RefasterValidateTests.class, RefasterCollectionTestUtil.class) + BugCheckerRefactoringTestHelper.newInstance(RefasterValidateTests.class, clazz) .setArgs(ImmutableList.of("-XepOpt:RefasterValidateTests:TemplateCollection=" + className)) - .addInputLines( - clazz.getName() + "TestInput.java", - getContentOfResourceFile(clazz, className + "TestInput.java")) - .addOutputLines( - clazz.getName() + "TestOutput.java", - getContentOfResourceFile(clazz, className + "TestOutput.java")) + .addInput(className + "TestInput.java") + .addOutput(className + "TestOutput.java") .doTest(TEXT_MATCH); } - - private static String getContentOfResourceFile(Class clazz, String resourceName) { - JavaFileObject object = FileObjects.forResource(clazz, resourceName); - try { - return object.getCharContent(false).toString(); - } catch (IOException e) { - throw new IllegalStateException("Can't retrieve content for file " + resourceName, e); - } - } } diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateTestCase.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateTestCase.java index 2ecf0b91d6..de60400f4b 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateTestCase.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateTestCase.java @@ -5,7 +5,7 @@ /** * Interface implemented by classes that exercise Refaster templates. These classes come in {@code * *Input.java} and {@code *Output.java} pairs, demonstrating the expected result of applying the - * associated Refaster templates. These classes are resources. + * associated Refaster templates. These classes are resources on the test classpath. */ public interface RefasterTemplateTestCase { /** @@ -15,7 +15,9 @@ public interface RefasterTemplateTestCase { * types and statically imported methods, such that any imports present in the input file are also * present in the output file. * - * @return The classes and methods needed to get a properly formatted file. + * @return Any values that are the result of expressions defined to ensure that all {@code + * *Input.java} import statements are also present in the associated {@code *Output.java} + * file. */ default ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(); diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java index 07d0ef5f17..5b6819c73d 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java @@ -1,13 +1,13 @@ package tech.picnic.errorprone.refaster.test; +import static com.google.common.base.Preconditions.checkState; +import static com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap; +import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet; import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; import static java.util.Comparator.naturalOrder; -import static java.util.stream.Collectors.joining; import static tech.picnic.errorprone.refaster.runner.RefasterCheck.INCLUDED_TEMPLATES_PATTERN_FLAG; -import com.google.common.base.Suppliers; -import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableRangeMap; @@ -16,7 +16,6 @@ import com.google.common.collect.Range; import com.google.common.collect.Sets; import com.google.errorprone.BugPattern; -import com.google.errorprone.CodeTransformer; import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.SubContext; import com.google.errorprone.VisitorState; @@ -33,46 +32,69 @@ import com.sun.source.util.TreeScanner; import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; +import com.sun.tools.javac.util.Position; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Optional; import java.util.Set; -import java.util.function.Supplier; -import java.util.stream.Stream; +import java.util.regex.Pattern; import tech.picnic.errorprone.refaster.runner.CodeTransformers; import tech.picnic.errorprone.refaster.runner.RefasterCheck; /** - * A {@link BugChecker} that tests the Refaster templates of a given template collection and - * validates its tests. + * A {@link BugChecker} that applies a Refaster template collection by delegating to {@link + * RefasterCheck} and subsequently validates that each template modifies exactly one distinct + * method, as indicate by each method's name. */ +// XXX: Rename to `RefasterTemplateCollectionValidator`. Be sure to update all references to +// `RefasterValidateTests`. @BugPattern( name = "RefasterValidateTests", - summary = "Validate a Refaster template collection and its tests", + summary = "Exercises a Refaster template collection", severity = ERROR) public final class RefasterValidateTests extends BugChecker implements CompilationUnitTreeMatcher { private static final long serialVersionUID = 1L; - private static final Supplier> - ALL_CODE_TRANSFORMERS = Suppliers.memoize(CodeTransformers::loadAllCodeTransformers); + private static final String TEMPLATE_COLLECTION_FLAG = "RefasterValidateTests:TemplateCollection"; + private static final String TEST_METHOD_NAME_PREFIX = "test"; - private final ImmutableSet templateNamesFromClassPath; + private final ImmutableSet templatesUnderTest; private final RefasterCheck delegate; /** - * Instantiates a customized {@link RefasterValidateTests}. + * Instantiates a {@link RefasterValidateTests} instance. * * @param flags Any provided command line flags. */ public RefasterValidateTests(ErrorProneFlags flags) { - String templateCollection = flags.get("RefasterValidateTests:TemplateCollection").orElseThrow(); - delegate = - new RefasterCheck( - ErrorProneFlags.fromMap( - ImmutableMap.of(INCLUDED_TEMPLATES_PATTERN_FLAG, templateCollection + ".*"))); - templateNamesFromClassPath = - ALL_CODE_TRANSFORMERS.get().keySet().stream() - .filter(k -> k.startsWith(templateCollection)) - .map(k -> k.replace(templateCollection + "$", "")) - .collect(toImmutableSortedSet(naturalOrder())); + String templateCollectionUnderTest = getTemplateCollectionUnderTest(flags); + delegate = createRefasterCheck(templateCollectionUnderTest); + templatesUnderTest = getTemplatesUnderTest(templateCollectionUnderTest); + } + + private static String getTemplateCollectionUnderTest(ErrorProneFlags flags) { + return flags + .get(TEMPLATE_COLLECTION_FLAG) + .orElseThrow( + () -> + new IllegalStateException( + String.format( + "Error Prone flag `%s` must be specified", TEMPLATE_COLLECTION_FLAG))); + } + + private static RefasterCheck createRefasterCheck(String templateCollectionUnderTest) { + return new RefasterCheck( + ErrorProneFlags.fromMap( + ImmutableMap.of( + INCLUDED_TEMPLATES_PATTERN_FLAG, + Pattern.quote(templateCollectionUnderTest) + ".*"))); + } + + private static ImmutableSet getTemplatesUnderTest(String templateCollectionUnderTest) { + return CodeTransformers.getAllCodeTransformers().keySet().stream() + .filter(k -> k.startsWith(templateCollectionUnderTest)) + .map(k -> k.replace(templateCollectionUnderTest + '$', "")) + .collect(toImmutableSortedSet(naturalOrder())); } @Override @@ -83,39 +105,65 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s VisitorState.createForCustomFindingCollection(new SubContext(state.context), matches::add) .withPath(state.getPath())); - JCCompilationUnit compilationUnit = (JCCompilationUnit) tree; - ImmutableRangeMap matchesRangeMap = - buildRangeMapForMatches(matches, compilationUnit.endPositions); + ImmutableRangeMap indexedMatches = + indexTemplateMatches(matches, ((JCCompilationUnit) tree).endPositions); + + matches.forEach(state::reportMatch); + reportMissingMatches(tree, state, indexedMatches); + reportUnexpectedMatches(tree, state, indexedMatches); + return Description.NO_MATCH; + } + + private static ImmutableRangeMap indexTemplateMatches( + List matches, EndPosTable endPositions) { + ImmutableRangeMap.Builder templateMatches = ImmutableRangeMap.builder(); + + for (Description description : matches) { + Set replacements = + Iterables.getOnlyElement(description.fixes).getReplacements(endPositions); + for (Replacement replacement : replacements) { + templateMatches.put( + replacement.range(), getSubstringAfterLastChar('.', description.checkName)); + } + } + + return templateMatches.build(); + } + + private void reportMissingMatches( + CompilationUnitTree tree, + VisitorState state, + ImmutableRangeMap indexedMatches) { ImmutableSet templatesWithoutMatch = - getTemplateNamesWithoutMatch(matchesRangeMap.asMapOfRanges().values()); + Sets.difference( + templatesUnderTest, ImmutableSet.copyOf(indexedMatches.asMapOfRanges().values())) + .immutableCopy(); if (!templatesWithoutMatch.isEmpty()) { - addCommentToTreeInOutputFile( + String sourceFile = ((JCCompilationUnit) tree).sourcefile.getName(); + reportViolations( tree, String.format( "Did not encounter a test in `%s` for the following template(s)", - getNameFromFQCN(compilationUnit.sourcefile.getName().replace(".java", ""))), - templatesWithoutMatch.stream(), + getSubstringAfterLastChar('/', sourceFile)), + templatesWithoutMatch, state); } - - ValidateMatchesInMethodsScanner scanner = new ValidateMatchesInMethodsScanner(matchesRangeMap); - scanner.scan(tree.getTypeDecls(), state); - - matches.forEach(state::reportMatch); - return Description.NO_MATCH; } - private ImmutableSet getTemplateNamesWithoutMatch( - ImmutableCollection templateNamesOfMatches) { - return Sets.difference(templateNamesFromClassPath, ImmutableSet.copyOf(templateNamesOfMatches)) - .immutableCopy(); + private void reportUnexpectedMatches( + CompilationUnitTree tree, + VisitorState state, + ImmutableRangeMap indexedMatches) { + UnexpectedMatchReporter unexpectedMatchReporter = new UnexpectedMatchReporter(indexedMatches); + unexpectedMatchReporter.scan(tree.getTypeDecls(), state); } - private void addCommentToTreeInOutputFile( - Tree tree, String message, Stream violations, VisitorState state) { - String listOfViolations = violations.collect(joining("\n* - ", "\n* - ", "\n")); - String comment = String.format("/*\n* %s:%s*/\n", message, listOfViolations); + private void reportViolations( + Tree tree, String message, ImmutableSet violations, VisitorState state) { + String violationEnumeration = String.join("\n* - ", violations); + String comment = + String.format("/*\n* ERROR: %s:\n* - %s\n*/\n", message, violationEnumeration); SuggestedFix fixWithComment = tree instanceof MethodTree ? SuggestedFix.prefixWith(tree, comment) @@ -123,63 +171,95 @@ private void addCommentToTreeInOutputFile( state.reportMatch(describeMatch(tree, fixWithComment)); } - private static ImmutableRangeMap buildRangeMapForMatches( - List matches, EndPosTable endPositions) { - ImmutableRangeMap.Builder rangeMap = ImmutableRangeMap.builder(); - - for (Description description : matches) { - Set replacements = - Iterables.getOnlyElement(description.fixes).getReplacements(endPositions); - Replacement replacement = Iterables.getOnlyElement(replacements); - - rangeMap.put(replacement.range(), getNameFromFQCN(description.checkName)); - } - return rangeMap.build(); + private static String getSubstringAfterLastChar(char delimiter, String value) { + int index = value.lastIndexOf(delimiter); + checkState(index >= 0, "String '%s' does not contain character '%s'", value, delimiter); + return value.substring(index + 1); } - private static String getNameFromFQCN(String fqcn) { - return fqcn.substring(fqcn.lastIndexOf('.') + 1); - } + private class UnexpectedMatchReporter extends TreeScanner { + private final ImmutableRangeMap indexedMatches; - private class ValidateMatchesInMethodsScanner extends TreeScanner { - private final ImmutableRangeMap matchesRangeMap; - - ValidateMatchesInMethodsScanner(ImmutableRangeMap matchesRangeMap) { - this.matchesRangeMap = matchesRangeMap; + UnexpectedMatchReporter(ImmutableRangeMap indexedMatches) { + this.indexedMatches = indexedMatches; } @Override public Void visitMethod(MethodTree tree, VisitorState state) { - if (ASTHelpers.isGeneratedConstructor(tree)) { - return super.visitMethod(tree, state); + if (!ASTHelpers.isGeneratedConstructor(tree)) { + getTemplateUnderTest(tree, state) + .ifPresent( + templateUnderTest -> reportUnexpectedMatches(tree, state, templateUnderTest)); } - String methodName = tree.getName().toString().replace("test", ""); - int startPosition = ASTHelpers.getStartPosition(tree); - int endPosition = state.getEndPosition(tree); - LineMap lineMap = state.getPath().getCompilationUnit().getLineMap(); + return super.visitMethod(tree, state); + } + + private void reportUnexpectedMatches( + MethodTree tree, VisitorState state, String templateUnderTest) { + // XXX: Validate that `getMatchesInTree(tree, state)` returns a non-empty result (strictly + // speaking one of the values should match `templateUnderTest`, but we can skip that check). - ImmutableRangeMap matchesInCurrentMethod = - matchesRangeMap.subRangeMap(Range.open(startPosition, endPosition)); - boolean correctTemplatesMatchedInMethod = - matchesInCurrentMethod.asMapOfRanges().values().stream().allMatch(methodName::equals); - if (!correctTemplatesMatchedInMethod) { - addCommentToTreeInOutputFile( + ImmutableListMultimap unexpectedMatchesByLineNumber = + getUnexpectedMatchesByLineNumber(getMatchesInTree(tree, state), templateUnderTest, state); + + if (!unexpectedMatchesByLineNumber.isEmpty()) { + reportViolations( tree, String.format( "The following matches unexpectedly occurred in method `%s`", tree.getName()), - matchesInCurrentMethod.asMapOfRanges().entrySet().stream() - .filter(e -> !e.getValue().equals(methodName)) + unexpectedMatchesByLineNumber.entries().stream() .map( e -> String.format( "Template `%s` matches on line %s, while it should match in a method named `test%s`.", - e.getValue(), - lineMap.getLineNumber(e.getKey().lowerEndpoint()), - e.getValue())), + e.getValue(), e.getKey(), e.getValue())) + .collect(toImmutableSet()), state); } - return super.visitMethod(tree, state); + } + + private Optional getTemplateUnderTest(MethodTree tree, VisitorState state) { + String methodName = tree.getName().toString(); + if (methodName.startsWith(TEST_METHOD_NAME_PREFIX)) { + return Optional.of(methodName.substring(TEST_METHOD_NAME_PREFIX.length())); + } + + /* + * Unless this method is `RefasterTemplateTestCase#elidedTypesAndStaticImports`, it's + * misnamed. + */ + if (!"elidedTypesAndStaticImports".equals(methodName)) { + state.reportMatch( + describeMatch( + tree, + SuggestedFix.prefixWith( + tree, "/* ERROR: Method names should start with `test`. */\n"))); + } + + return Optional.empty(); + } + + private ImmutableRangeMap getMatchesInTree( + MethodTree tree, VisitorState state) { + int startPosition = ASTHelpers.getStartPosition(tree); + int endPosition = state.getEndPosition(tree); + + checkState( + startPosition != Position.NOPOS && endPosition != Position.NOPOS, + "Cannot determine location of method in source code"); + + return indexedMatches.subRangeMap(Range.open(startPosition, endPosition)); + } + + private ImmutableListMultimap getUnexpectedMatchesByLineNumber( + ImmutableRangeMap matches, String templateUnderTest, VisitorState state) { + LineMap lineMap = state.getPath().getCompilationUnit().getLineMap(); + return matches.asMapOfRanges().entrySet().stream() + .filter(e -> !e.getValue().equals(templateUnderTest)) + .collect( + toImmutableListMultimap( + e -> lineMap.getLineNumber(e.getKey().lowerEndpoint()), Map.Entry::getValue)); } } } diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java index 87bf718fe4..0c61555b5c 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/package-info.java @@ -1,4 +1,4 @@ -/** Utilities that support testing Refaster templates and allows for validating these tests. */ +/** Opinionated utilities for the testing of Refaster templates. */ @com.google.errorprone.annotations.CheckReturnValue @javax.annotation.ParametersAreNonnullByDefault package tech.picnic.errorprone.refaster.test; diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java index 6795d2254e..75bf37b248 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java @@ -3,7 +3,13 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; -/** Refaster template with a number as suffix to validate that it is reported correctly. */ +/** + * Refaster template collection containing a template with a numeric name suffix, used to validate + * that it is reported correctly. + */ +// XXX: Rename and generalize to test more happy flow cases? +// XXX: If so, also test block templates with placeholders and templates that introduce static +// imports. final class MethodNameWithNumberTemplates { private MethodNameWithNumberTemplates() {} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java index 7f693ab7e9..4bcc31adc8 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java @@ -3,7 +3,7 @@ /** Code to test the Refaster templates from `MatchInWrongMethodTemplates`. */ final class MatchInWrongMethodTemplatesTest implements RefasterTemplateTestCase { /* - * The following matches unexpectedly occurred in method `testWrongName`: + * ERROR: The following matches unexpectedly occurred in method `testWrongName`: * - Template `StringIsEmpty` matches on line 6, while it should match in a method named `testStringIsEmpty`. * - Template `StringIsEmpty` matches on line 7, while it should match in a method named `testStringIsEmpty`. * - Template `StringIsEmpty` matches on line 8, while it should match in a method named `testStringIsEmpty`. diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java index ce103339c4..c9dc3ac542 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java @@ -14,8 +14,10 @@ boolean testStringIsEmpty2() { return "foo".isEmpty(); } + /* ERROR: Method names should start with `test`. */ private void foo() {} + /* ERROR: Method names should start with `test`. */ public String bar() { return ""; } diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java index e203475245..7f861d12d9 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java @@ -3,7 +3,7 @@ /** Code to test the Refaster templates from `MissingTestAndWrongTestTemplates`. */ final class MissingTestAndWrongTestTemplatesTest implements RefasterTemplateTestCase { /* - * The following matches unexpectedly occurred in method `testWrongName`: + * ERROR: The following matches unexpectedly occurred in method `testWrongName`: * - Template `StringIsEmpty` matches on line 6, while it should match in a method named `testStringIsEmpty`. * - Template `StringIsEmpty` matches on line 7, while it should match in a method named `testStringIsEmpty`. * - Template `StringIsEmpty` matches on line 8, while it should match in a method named `testStringIsEmpty`. @@ -15,6 +15,6 @@ boolean testWrongName() { } } /* - * Did not encounter a test in `MissingTestAndWrongTestTemplatesTestInput` for the following template(s): + * ERROR: Did not encounter a test in `MissingTestAndWrongTestTemplatesTestInput.java` for the following template(s): * - TemplateWithoutTest */ diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestOutput.java index 7171cf8942..afe9f4740f 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestOutput.java @@ -3,7 +3,7 @@ /** Code to test the Refaster templates from {@link PartialTestMatchTemplates}. */ final class PartialTestMatchTemplatesTest implements RefasterTemplateTestCase { /* - * The following matches unexpectedly occurred in method `testStringIsEmpty`: + * ERROR: The following matches unexpectedly occurred in method `testStringIsEmpty`: * - Template `StringEquals` matches on line 6, while it should match in a method named `testStringEquals`. */ boolean testStringIsEmpty() { diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java index 92a8486e46..62cdc772f6 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java @@ -7,7 +7,7 @@ boolean testStringIsEmpty() { } } /* - * Did not encounter a test in `TemplateWithoutTestTemplatesTestInput` for the following template(s): + * ERROR: Did not encounter a test in `TemplateWithoutTestTemplatesTestInput.java` for the following template(s): * - AnotherTemplateWithoutTest * - TemplateWithoutTest */ From 499584004cd3ef0dcdfc060b1ce1cf3700318dd7 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 31 May 2022 11:32:04 +0200 Subject: [PATCH 17/41] Apply feedback --- .../refaster/runner/CodeTransformers.java | 4 ++-- .../test/RefasterCollectionTestUtil.java | 4 ++-- .../refaster/test/RefasterValidateTests.java | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java index 466b12b98f..61c2a7a6d6 100644 --- a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java @@ -25,7 +25,7 @@ private CodeTransformers() {} /** * Returns all Refaster {@link CodeTransformer}s found on the classpath. * - *

This method returns a cached view; all invocations exception the first are very cheap. + *

This method returns a cached view; all invocations except the first are very cheap. * * @return A mapping from Refaster template names to associated {@link CodeTransformer}s. */ @@ -34,7 +34,7 @@ public static ImmutableListMultimap getAllCodeTransform } /** - * Scans the classpath to for compiled Refaster templates and returns the associated deserialized + * Scans the classpath for compiled Refaster templates and returns the associated deserialized * {@link CodeTransformer}s, indexed by their name. * * @return A mapping from Refaster template names to associated {@link CodeTransformer}s. diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java index 1ec70ca1aa..50be40a3d1 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java @@ -6,8 +6,8 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper; /** - * Utility to test Refaster templates and validate that the tests use meet all requirements for - * template collection tests. + * Utility to test Refaster templates and validate that the tests meet all requirements for template + * collection tests. */ // XXX: Consider dropping this class in favour of a static // `RefasterTemplateCollectionValidator#validate` method. diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java index 5b6819c73d..c98ec045d9 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java @@ -109,8 +109,8 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s indexTemplateMatches(matches, ((JCCompilationUnit) tree).endPositions); matches.forEach(state::reportMatch); - reportMissingMatches(tree, state, indexedMatches); - reportUnexpectedMatches(tree, state, indexedMatches); + reportMissingMatches(tree, indexedMatches, state); + reportUnexpectedMatches(tree, indexedMatches, state); return Description.NO_MATCH; } @@ -133,8 +133,8 @@ private static ImmutableRangeMap indexTemplateMatches( private void reportMissingMatches( CompilationUnitTree tree, - VisitorState state, - ImmutableRangeMap indexedMatches) { + ImmutableRangeMap indexedMatches, + VisitorState state) { ImmutableSet templatesWithoutMatch = Sets.difference( templatesUnderTest, ImmutableSet.copyOf(indexedMatches.asMapOfRanges().values())) @@ -153,8 +153,8 @@ private void reportMissingMatches( private void reportUnexpectedMatches( CompilationUnitTree tree, - VisitorState state, - ImmutableRangeMap indexedMatches) { + ImmutableRangeMap indexedMatches, + VisitorState state) { UnexpectedMatchReporter unexpectedMatchReporter = new UnexpectedMatchReporter(indexedMatches); unexpectedMatchReporter.scan(tree.getTypeDecls(), state); } @@ -189,14 +189,14 @@ public Void visitMethod(MethodTree tree, VisitorState state) { if (!ASTHelpers.isGeneratedConstructor(tree)) { getTemplateUnderTest(tree, state) .ifPresent( - templateUnderTest -> reportUnexpectedMatches(tree, state, templateUnderTest)); + templateUnderTest -> reportUnexpectedMatches(tree, templateUnderTest, state)); } return super.visitMethod(tree, state); } private void reportUnexpectedMatches( - MethodTree tree, VisitorState state, String templateUnderTest) { + MethodTree tree, String templateUnderTest, VisitorState state) { // XXX: Validate that `getMatchesInTree(tree, state)` returns a non-empty result (strictly // speaking one of the values should match `templateUnderTest`, but we can skip that check). @@ -249,7 +249,7 @@ private ImmutableRangeMap getMatchesInTree( startPosition != Position.NOPOS && endPosition != Position.NOPOS, "Cannot determine location of method in source code"); - return indexedMatches.subRangeMap(Range.open(startPosition, endPosition)); + return indexedMatches.subRangeMap(Range.closedOpen(startPosition, endPosition)); } private ImmutableListMultimap getUnexpectedMatchesByLineNumber( From 22644e96d2014e52a06a74d5ccfb7f9d4167f60a Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 31 May 2022 13:00:12 +0200 Subject: [PATCH 18/41] `s/RefasterValidateTests/RefasterTemplateCollectionValidator` --- refaster-test-support/README.md | 24 +++++------ .../test/RefasterCollectionTestUtil.java | 37 ----------------- ... RefasterTemplateCollectionValidator.java} | 40 +++++++++++++++---- ...asterTemplateCollectionValidatorTest.java} | 13 +++--- 4 files changed, 51 insertions(+), 63 deletions(-) delete mode 100644 refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java rename refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/{RefasterValidateTests.java => RefasterTemplateCollectionValidator.java} (86%) rename refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/{RefasterCollectionTestUtilTest.java => RefasterTemplateCollectionValidatorTest.java} (56%) diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index 6ae3bee466..95d8c7ba00 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -19,24 +19,24 @@ tests and assurance that every template is properly tested. ## How to test a collection of Refaster templates? -In summary, to test Refaster templates using the `RefasterCollectionTestUtil`, -one should create an input and output file. The Refaster templates from the -collection are applied on the input file and should exactly match the content -of the provided output file. +In summary, to test Refaster templates using the +`RefasterTemplateCollectionValidator`, one should create an input and output +file. The Refaster templates from the collection are applied on the input file +and should exactly match the content of the provided output file. To test Refaster templates, one can create a (parameterized) test for every class containing the Refaster templates to invoke the -`RefasterCollectionTestUtil`. A class that contains one or more Refaster -templates is called a Refaster template collection. This test utility applies -the Refaster templates in the collection to a provided input file, and expects -the result to exactly match the contents of a provided output file. +`RefasterTemplateCollectionValidator`. A class that contains one or more +Refaster templates is called a Refaster template collection. This test utility +applies the Refaster templates in the collection to a provided input file, and +expects the result to exactly match the contents of a provided output file. To adopt this setup, the following requirements have to be met: - Create a class with a (parameterized) test method that calls the - `RefasterCollectionTestUtil#validateTemplateCollection` and passes the - collection(s) to validate. The Refaster template collection must match the - naming convention `Templates.java`. + `RefasterTemplateCollectionValidator#validate` and passes the collection(s) + to validate. The Refaster template collection must match the naming + convention `Templates.java`. - An input file matching the naming convention `TemplatesTestInput.java` is added for every template collection. @@ -69,7 +69,7 @@ test/ └── tech.picnic.errorprone.refastertemplates └── RefasterCollectionTest.java -- Here the test invokes - -- `RefasterCollectionTestUtil#validateTemplateCollection`. + -- `RefasterTemplateCollectionValidator#validate`. resources/ └── tech.picnic.errorprone.refastertemplates └── ExampleTemplatesTestInput.java diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java deleted file mode 100644 index 50be40a3d1..0000000000 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtil.java +++ /dev/null @@ -1,37 +0,0 @@ -package tech.picnic.errorprone.refaster.test; - -import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH; - -import com.google.common.collect.ImmutableList; -import com.google.errorprone.BugCheckerRefactoringTestHelper; - -/** - * Utility to test Refaster templates and validate that the tests meet all requirements for template - * collection tests. - */ -// XXX: Consider dropping this class in favour of a static -// `RefasterTemplateCollectionValidator#validate` method. -public final class RefasterCollectionTestUtil { - private RefasterCollectionTestUtil() {} - - /** - * Verifies that all Refaster templates in the given collection class are covered by precisely one - * test method, defined explicitly for the purpose of exercising that template. - * - *

Note that a passing test does not guarantee full coverage: this test does not ascertain that - * all {@link com.google.errorprone.refaster.Refaster#anyOf} branches are tested. Likewise for - * {@link com.google.errorprone.refaster.annotation.BeforeTemplate} methods in case there are - * multiple. - * - * @param clazz The Refaster template collection under test. - */ - public static void validateTemplateCollection(Class clazz) { - String className = clazz.getSimpleName(); - - BugCheckerRefactoringTestHelper.newInstance(RefasterValidateTests.class, clazz) - .setArgs(ImmutableList.of("-XepOpt:RefasterValidateTests:TemplateCollection=" + className)) - .addInput(className + "TestInput.java") - .addOutput(className + "TestOutput.java") - .doTest(TEXT_MATCH); - } -} diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java similarity index 86% rename from refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java rename to refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java index c98ec045d9..4a1153f4d0 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterValidateTests.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java @@ -4,10 +4,12 @@ import static com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap; import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet; +import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH; import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; import static java.util.Comparator.naturalOrder; import static tech.picnic.errorprone.refaster.runner.RefasterCheck.INCLUDED_TEMPLATES_PATTERN_FLAG; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableRangeMap; @@ -15,6 +17,7 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Range; import com.google.common.collect.Sets; +import com.google.errorprone.BugCheckerRefactoringTestHelper; import com.google.errorprone.BugPattern; import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.SubContext; @@ -45,28 +48,28 @@ /** * A {@link BugChecker} that applies a Refaster template collection by delegating to {@link * RefasterCheck} and subsequently validates that each template modifies exactly one distinct - * method, as indicate by each method's name. + * method, as indicated by each method's name. */ -// XXX: Rename to `RefasterTemplateCollectionValidator`. Be sure to update all references to -// `RefasterValidateTests`. @BugPattern( - name = "RefasterValidateTests", + name = "RefasterTemplateCollectionValidator", summary = "Exercises a Refaster template collection", severity = ERROR) -public final class RefasterValidateTests extends BugChecker implements CompilationUnitTreeMatcher { +public final class RefasterTemplateCollectionValidator extends BugChecker + implements CompilationUnitTreeMatcher { private static final long serialVersionUID = 1L; - private static final String TEMPLATE_COLLECTION_FLAG = "RefasterValidateTests:TemplateCollection"; + private static final String TEMPLATE_COLLECTION_FLAG = + "RefasterTemplateCollectionValidator:TemplateCollection"; private static final String TEST_METHOD_NAME_PREFIX = "test"; private final ImmutableSet templatesUnderTest; private final RefasterCheck delegate; /** - * Instantiates a {@link RefasterValidateTests} instance. + * Instantiates a {@link RefasterTemplateCollectionValidator} instance. * * @param flags Any provided command line flags. */ - public RefasterValidateTests(ErrorProneFlags flags) { + public RefasterTemplateCollectionValidator(ErrorProneFlags flags) { String templateCollectionUnderTest = getTemplateCollectionUnderTest(flags); delegate = createRefasterCheck(templateCollectionUnderTest); templatesUnderTest = getTemplatesUnderTest(templateCollectionUnderTest); @@ -97,6 +100,27 @@ private static ImmutableSet getTemplatesUnderTest(String templateCollect .collect(toImmutableSortedSet(naturalOrder())); } + /** + * Verifies that all Refaster templates in the given collection class are covered by precisely one + * test method, defined explicitly for the purpose of exercising that template. + * + *

Note that a passing test does not guarantee full coverage: this test does not ascertain that + * all {@link com.google.errorprone.refaster.Refaster#anyOf} branches are tested. Likewise for + * {@link com.google.errorprone.refaster.annotation.BeforeTemplate} methods in case there are + * multiple. + * + * @param clazz The Refaster template collection under test. + */ + public static void validate(Class clazz) { + String className = clazz.getSimpleName(); + + BugCheckerRefactoringTestHelper.newInstance(RefasterTemplateCollectionValidator.class, clazz) + .setArgs(ImmutableList.of("-XepOpt:" + TEMPLATE_COLLECTION_FLAG + "=" + className)) + .addInput(className + "TestInput.java") + .addOutput(className + "TestOutput.java") + .doTest(TEXT_MATCH); + } + @Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { List matches = new ArrayList<>(); diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java similarity index 56% rename from refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java rename to refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java index b12e97034c..3e256ff7f0 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterCollectionTestUtilTest.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java @@ -7,12 +7,13 @@ * Validate error reporting of the Refaster template collections. * *

The goal of the Refaster template collections under test is to verify the reporting of {@link - * RefasterCollectionTestUtil} using the associated `TestInput` and `TestOutput` files. Normally, - * {@link RefasterCollectionTestUtil} will report error messages directly in the console where the - * results of the tests are shown. However, to verify that these messages are correct, the - * `*TestOutput` files in this package contain error reporting that is normally not written. + * RefasterTemplateCollectionValidator} using the associated `TestInput` and `TestOutput` files. + * Normally, {@link RefasterTemplateCollectionValidator} will report error messages directly in the + * console where the results of the tests are shown. However, to verify that these messages are + * correct, the `*TestOutput` files in this package contain error reporting that is normally not + * written. */ -final class RefasterCollectionTestUtilTest { +final class RefasterTemplateCollectionValidatorTest { @ParameterizedTest @ValueSource( classes = { @@ -23,6 +24,6 @@ final class RefasterCollectionTestUtilTest { TemplateWithoutTestTemplates.class }) void verifyRefasterTemplateCollections(Class clazz) { - RefasterCollectionTestUtil.validateTemplateCollection(clazz); + RefasterTemplateCollectionValidator.validate(clazz); } } From 2795daf1e56708652e6d97a1dd1d1455e5e62f4b Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 31 May 2022 14:54:36 +0200 Subject: [PATCH 19/41] Add more happy flow examples --- .../refaster/test/DummyTemplates.java | 65 +++++++++++++++++++ .../test/MethodNameWithNumberTemplates.java | 27 -------- ...fasterTemplateCollectionValidatorTest.java | 2 +- .../test/DummyTemplatesTestInput.java | 38 +++++++++++ .../test/DummyTemplatesTestOutput.java | 41 ++++++++++++ ...ethodNameWithNumberTemplatesTestInput.java | 22 ------- ...thodNameWithNumberTemplatesTestOutput.java | 24 ------- 7 files changed, 145 insertions(+), 74 deletions(-) create mode 100644 refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/DummyTemplates.java delete mode 100644 refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestInput.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestOutput.java delete mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java delete mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/DummyTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/DummyTemplates.java new file mode 100644 index 0000000000..a74e3a34cf --- /dev/null +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/DummyTemplates.java @@ -0,0 +1,65 @@ +package tech.picnic.errorprone.refaster.test; + +import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; + +import com.google.common.base.Strings; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.Placeholder; +import com.google.errorprone.refaster.annotation.UseImportPolicy; +import java.util.Set; +import javax.annotation.Nullable; + +/** + * Refaster template collection containing arbitrary examples to validate that violations are + * reported correctly. + */ +// XXX: Rename class and update Javadoc. +final class DummyTemplates { + private DummyTemplates() {} + + static final class StringIsEmpty2 { + @BeforeTemplate + boolean before(String string) { + return string.equals(""); + } + + @AfterTemplate + boolean after(String string) { + return string.isEmpty(); + } + } + + static final class StaticImportStringLength { + @BeforeTemplate + boolean before(@Nullable String string) { + return string == null || string.isEmpty(); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + boolean after(String string) { + return Strings.isNullOrEmpty(string); + } + } + + abstract class SetAddElement { + @Placeholder + abstract void doAfterAdd(E element); + + @BeforeTemplate + void before(Set set, E elem) { + if (!set.contains(elem)) { + set.add(elem); + doAfterAdd(elem); + } + } + + @AfterTemplate + void after(Set set, E elem) { + if (set.add(elem)) { + doAfterAdd(elem); + } + } + } +} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java deleted file mode 100644 index 75bf37b248..0000000000 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplates.java +++ /dev/null @@ -1,27 +0,0 @@ -package tech.picnic.errorprone.refaster.test; - -import com.google.errorprone.refaster.annotation.AfterTemplate; -import com.google.errorprone.refaster.annotation.BeforeTemplate; - -/** - * Refaster template collection containing a template with a numeric name suffix, used to validate - * that it is reported correctly. - */ -// XXX: Rename and generalize to test more happy flow cases? -// XXX: If so, also test block templates with placeholders and templates that introduce static -// imports. -final class MethodNameWithNumberTemplates { - private MethodNameWithNumberTemplates() {} - - static final class StringIsEmpty2 { - @BeforeTemplate - boolean before(String string) { - return string.equals(""); - } - - @AfterTemplate - boolean after(String string) { - return string.isEmpty(); - } - } -} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java index 3e256ff7f0..340284ead2 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java @@ -17,8 +17,8 @@ final class RefasterTemplateCollectionValidatorTest { @ParameterizedTest @ValueSource( classes = { + DummyTemplates.class, MatchInWrongMethodTemplates.class, - MethodNameWithNumberTemplates.class, MissingTestAndWrongTestTemplates.class, PartialTestMatchTemplates.class, TemplateWithoutTestTemplates.class diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestInput.java new file mode 100644 index 0000000000..45e710c73d --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestInput.java @@ -0,0 +1,38 @@ +package tech.picnic.errorprone.refaster.test; + +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableSet; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +/** Code to test the Refaster templates from `DummyTemplates`. */ +final class DummyTemplatesTest implements RefasterTemplateTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(Collections.class, Strings.class); + } + + boolean testStringIsEmpty2() { + return "foo".equals(""); + } + + boolean testStaticImportStringLength() { + return "foo" == null || "foo".isEmpty(); + } + + void testSetAddElement() { + Set set = new HashSet<>(); + Integer element = 1; + if (!set.contains(element)) { + set.add(element); + System.out.print("added the following element to set: " + element); + } + } + + private void foo() {} + + public String bar() { + return ""; + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestOutput.java new file mode 100644 index 0000000000..b2b37d0b31 --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestOutput.java @@ -0,0 +1,41 @@ +package tech.picnic.errorprone.refaster.test; + +import static com.google.common.base.Strings.isNullOrEmpty; + +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableSet; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +/** Code to test the Refaster templates from `DummyTemplates`. */ +final class DummyTemplatesTest implements RefasterTemplateTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(Collections.class, Strings.class); + } + + boolean testStringIsEmpty2() { + return "foo".isEmpty(); + } + + boolean testStaticImportStringLength() { + return isNullOrEmpty("foo"); + } + + void testSetAddElement() { + Set set = new HashSet<>(); + Integer element = 1; + if (set.add(element)) { + System.out.print("added the following element to set: " + element); + } + } + + /* ERROR: Method names should start with `test`. */ + private void foo() {} + + /* ERROR: Method names should start with `test`. */ + public String bar() { + return ""; + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java deleted file mode 100644 index 7657740c27..0000000000 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestInput.java +++ /dev/null @@ -1,22 +0,0 @@ -package tech.picnic.errorprone.refaster.test; - -import com.google.common.collect.ImmutableSet; -import java.util.Collections; - -/** Code to test the Refaster templates from `MethodNameWithNumberTemplates`. */ -final class MethodNameWithNumberTemplatesTest implements RefasterTemplateTestCase { - @Override - public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Collections.class); - } - - boolean testStringIsEmpty2() { - return "foo".equals(""); - } - - private void foo() {} - - public String bar() { - return ""; - } -} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java deleted file mode 100644 index c9dc3ac542..0000000000 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodNameWithNumberTemplatesTestOutput.java +++ /dev/null @@ -1,24 +0,0 @@ -package tech.picnic.errorprone.refaster.test; - -import com.google.common.collect.ImmutableSet; -import java.util.Collections; - -/** Code to test the Refaster templates from `MethodNameWithNumberTemplates`. */ -final class MethodNameWithNumberTemplatesTest implements RefasterTemplateTestCase { - @Override - public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Collections.class); - } - - boolean testStringIsEmpty2() { - return "foo".isEmpty(); - } - - /* ERROR: Method names should start with `test`. */ - private void foo() {} - - /* ERROR: Method names should start with `test`. */ - public String bar() { - return ""; - } -} From b417c100fef93c79f8921761f3c7f1e1f97a3872 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 31 May 2022 15:56:55 +0200 Subject: [PATCH 20/41] Post-rebase fix --- .../bugpatterns/RefasterCheckTest.java | 191 ------------------ 1 file changed, 191 deletions(-) delete mode 100644 error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java deleted file mode 100644 index 9f4686e4cc..0000000000 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java +++ /dev/null @@ -1,191 +0,0 @@ -package tech.picnic.errorprone.bugpatterns; - -import static com.google.common.collect.ImmutableSetMultimap.toImmutableSetMultimap; -import static java.util.function.Function.identity; -import static java.util.function.Predicate.not; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; -import static org.junit.jupiter.params.provider.Arguments.arguments; - -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.ImmutableSetMultimap; -import com.google.errorprone.BugCheckerRefactoringTestHelper; -import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode; -import java.util.regex.Pattern; -import java.util.stream.Stream; -import org.junit.ComparisonFailure; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -final class RefasterCheckTest { - /** The names of all Refaster template groups defined in this module. */ - private static final ImmutableSet TEMPLATE_GROUPS = - ImmutableSet.of( - "AssertJ", - "AssertJBigDecimal", - "AssertJBigInteger", - "AssertJBoolean", - "AssertJByte", - "AssertJCharSequence", - "AssertJDouble", - "AssertJEnumerable", - "AssertJFloat", - "AssertJInteger", - "AssertJLong", - "AssertJNumber", - "AssertJMap", - "AssertJObject", - "AssertJOptional", - "AssertJShort", - "AssertJString", - "AssertJThrowingCallable", - "Assorted", - "BigDecimal", - "Collection", - "Comparator", - "DoubleStream", - "Equality", - "ImmutableList", - "ImmutableListMultimap", - "ImmutableMap", - "ImmutableMultiset", - "ImmutableSet", - "ImmutableSetMultimap", - "ImmutableSortedMap", - "ImmutableSortedMultiset", - "ImmutableSortedSet", - "IntStream", - "JUnit", - "LongStream", - "MapEntry", - "Mockito", - "Multimap", - "Null", - "Optional", - "Primitive", - "Reactor", - "RxJava2Adapter", - "Stream", - "String", - "TestNGToAssertJ", - "Time", - "WebClient"); - /** - * Matches the parts of the fully-qualified name of a template class that should be removed in - * order to produce the associated {@link #TEMPLATE_GROUPS template group name}. - */ - private static final Pattern TEMPLATE_FQCN_TRIM_FOR_GROUP_NAME = - Pattern.compile(".*\\.|Templates\\$.*"); - /** - * A mapping from template group names to associated template names. - * - *

In effect, the values correspond to nested classes that represent individual Refaster - * templates, while the keys correspond to the associated top-level "aggregator" classes. - */ - private static final ImmutableSetMultimap TEMPLATES_BY_GROUP = - indexTemplateNamesByGroup(RefasterCheck.ALL_CODE_TRANSFORMERS.get().keySet()); - - /** Returns every known template group name as a parameterized test argument. */ - @SuppressWarnings("UnusedMethod" /* Used as a `@MethodSource`. */) - private static Stream templateGroupsUnderTest() { - // XXX: Drop the filter once we have added tests for AssertJ! - return TEMPLATES_BY_GROUP.keySet().stream().filter(not("AssertJ"::equals)).map(Arguments::of); - } - - /** - * Returns every known (template group name, template name) pair as a parameterized test argument. - */ - @SuppressWarnings("UnusedMethod" /* Used as a `@MethodSource`. */) - private static Stream templatesUnderTest() { - // XXX: Drop the filter once we have added tests for AssertJ! - return TEMPLATES_BY_GROUP.entries().stream() - .filter(e -> !"AssertJ".equals(e.getKey())) - .map(e -> arguments(e.getKey(), e.getValue())); - } - - /** - * Verifies that {@link RefasterCheck#loadAllCodeTransformers} finds at least one code transformer - * for all of the {@link #TEMPLATE_GROUPS}. - * - *

This test is just as much about ensuring that {@link #TEMPLATE_GROUPS} is exhaustive, so - * that in turn {@link #replacement}'s coverage is exhaustive. - */ - @Test - void loadAllCodeTransformers() { - assertThat(TEMPLATES_BY_GROUP.keySet()).hasSameElementsAs(TEMPLATE_GROUPS); - } - - /** - * Verifies for each of the {@link #TEMPLATE_GROUPS} that the associated code transformers have - * the desired effect. - */ - @MethodSource("templateGroupsUnderTest") - @ParameterizedTest - void replacement(String group) { - verifyRefactoring(group, namePattern(group)); - } - - /** - * Verifies that all loaded Refaster templates are covered by at least one test. - * - *

Note that this doesn't guarantee full coverage: this test cannot ascertain that all {@link - * com.google.errorprone.refaster.Refaster#anyOf} branches are tested. Idem for {@link - * com.google.errorprone.refaster.annotation.BeforeTemplate} methods in case there are multiple . - */ - @MethodSource("templatesUnderTest") - @ParameterizedTest - void coverage(String group, String template) { - assertThatCode(() -> verifyRefactoring(group, namePattern(group, template))) - .withFailMessage( - "Template %s does not affect the tests for group %s; is it tested?", template, group) - .isInstanceOf(ComparisonFailure.class); - } - - private static ImmutableSetMultimap indexTemplateNamesByGroup( - ImmutableSet templateNames) { - return templateNames.stream() - .collect( - toImmutableSetMultimap( - n -> TEMPLATE_FQCN_TRIM_FOR_GROUP_NAME.matcher(n).replaceAll(""), identity())); - } - - private static String namePattern(String groupName, String excludedTemplate) { - return "(?!" + Pattern.quote(excludedTemplate) + ')' + namePattern(groupName); - } - - private static String namePattern(String groupName) { - return Pattern.compile(Pattern.quote(groupName)) + "Templates.*"; - } - - private void verifyRefactoring(String groupName, String templateNamePattern) { - createRestrictedRefactoringTestHelper(templateNamePattern) - .addInput(groupName + "TemplatesTestInput.java") - .addOutput(groupName + "TemplatesTestOutput.java") - .doTest(TestMode.TEXT_MATCH); - } - - private BugCheckerRefactoringTestHelper createRestrictedRefactoringTestHelper( - String namePattern) { - return BugCheckerRefactoringTestHelper.newInstance( - RefasterCheck.class, getRefasterTemplateCollectionClass()) - .setArgs("-XepOpt:Refaster:NamePattern=" + namePattern); - } - - /** - * Returns an arbitrary Refaster template collection class inside the {@code - * tech.picnic.errorprone.refastertemplates} package, enabling {@link - * BugCheckerRefactoringTestHelper} to load test resources from said package. - */ - private Class getRefasterTemplateCollectionClass() { - try { - return Class.forName( - "tech.picnic.errorprone.refastertemplates.AssortedTemplates", - /* initialize= */ false, - getClass().getClassLoader()); - } catch (ClassNotFoundException e) { - throw new IllegalStateException("Failed to load Refaster template collection class", e); - } - } -} From 7098ea37b0fec9dd1dce2c108a10403d69b8db80 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 1 Jun 2022 13:58:38 +0200 Subject: [PATCH 21/41] Add Refaster tests for `error-prone-contrib` and introduce `CodeTransfomersTest` --- error-prone-contrib/pom.xml | 11 +++ .../RefasterTemplatesTest.java | 76 +++++++++++++++++++ ...rtJThrowingCallableTemplatesTestInput.java | 1 + ...tJThrowingCallableTemplatesTestOutput.java | 1 + pom.xml | 5 ++ refaster-runner/pom.xml | 51 +++++++++++++ .../refaster/runner/CodeTransformers.java | 2 +- .../refaster/runner/CodeTransformersTest.java | 20 +++++ .../refaster/runner/FooTemplates.java | 22 ++++++ refaster-test-support/pom.xml | 1 + 10 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java create mode 100644 refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/CodeTransformersTest.java create mode 100644 refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/FooTemplates.java diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index 66eba535b5..3143134f2d 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -47,9 +47,20 @@ `annotationProcessorPaths` configuration below. --> provided + + ${project.groupId} + refaster-runner + provided + ${project.groupId} refaster-support + provided + + + ${project.groupId} + refaster-test-support + test com.fasterxml.jackson.core diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java new file mode 100644 index 0000000000..619a4388fb --- /dev/null +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java @@ -0,0 +1,76 @@ +package tech.picnic.errorprone.refastertemplates; + +import com.google.common.collect.ImmutableSet; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import tech.picnic.errorprone.refaster.test.RefasterTemplateCollectionValidator; + +final class RefasterTemplatesTest { + /** The names of all Refaster template groups defined in this module. */ + private static final ImmutableSet> TEMPLATE_COLLECTIONS = + ImmutableSet.of( + AssertJTemplates.class, + AssertJBigDecimalTemplates.class, + AssertJBigIntegerTemplates.class, + AssertJBooleanTemplates.class, + AssertJByteTemplates.class, + AssertJCharSequenceTemplates.class, + AssertJDoubleTemplates.class, + AssertJEnumerableTemplates.class, + AssertJFloatTemplates.class, + AssertJIntegerTemplates.class, + AssertJLongTemplates.class, + AssertJNumberTemplates.class, + AssertJMapTemplates.class, + AssertJObjectTemplates.class, + AssertJOptionalTemplates.class, + AssertJShortTemplates.class, + AssertJStringTemplates.class, + AssertJThrowingCallableTemplates.class, + AssortedTemplates.class, + BigDecimalTemplates.class, + CollectionTemplates.class, + ComparatorTemplates.class, + DoubleStreamTemplates.class, + EqualityTemplates.class, + ImmutableListTemplates.class, + ImmutableListMultimapTemplates.class, + ImmutableMapTemplates.class, + ImmutableMultisetTemplates.class, + ImmutableSetTemplates.class, + ImmutableSetMultimapTemplates.class, + ImmutableSortedMapTemplates.class, + ImmutableSortedMultisetTemplates.class, + ImmutableSortedSetTemplates.class, + IntStreamTemplates.class, + JUnitTemplates.class, + LongStreamTemplates.class, + MapEntryTemplates.class, + MockitoTemplates.class, + MultimapTemplates.class, + NullTemplates.class, + OptionalTemplates.class, + PrimitiveTemplates.class, + ReactorTemplates.class, + RxJava2AdapterTemplates.class, + StreamTemplates.class, + StringTemplates.class, + TestNGToAssertJTemplates.class, + TimeTemplates.class, + WebClientTemplates.class); + + private static Stream templateCollectionsTestCases() { + // XXX: Drop the filter once we have added tests for AssertJ! + return TEMPLATE_COLLECTIONS.stream() + .filter(c -> c != AssertJTemplates.class) + .map(Arguments::arguments); + } + + @ParameterizedTest + @MethodSource("templateCollectionsTestCases") + void validateTemplateCollectionTests(Class clazz) { + RefasterTemplateCollectionValidator.validate(clazz); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJThrowingCallableTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJThrowingCallableTemplatesTestInput.java index 0847ef2393..e3edffd797 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJThrowingCallableTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJThrowingCallableTemplatesTestInput.java @@ -10,6 +10,7 @@ import com.google.common.collect.ImmutableSet; import org.assertj.core.api.AbstractObjectAssert; import org.assertj.core.api.AbstractThrowableAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJThrowingCallableTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJThrowingCallableTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJThrowingCallableTemplatesTestOutput.java index 04ad44edad..38ba2a2ec4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJThrowingCallableTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/AssertJThrowingCallableTemplatesTestOutput.java @@ -11,6 +11,7 @@ import java.io.IOException; import org.assertj.core.api.AbstractObjectAssert; import org.assertj.core.api.AbstractThrowableAssert; +import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; final class AssertJThrowingCallableTemplatesTest implements RefasterTemplateTestCase { @Override diff --git a/pom.xml b/pom.xml index 33c519efe9..987eecd951 100644 --- a/pom.xml +++ b/pom.xml @@ -200,6 +200,11 @@ refaster-support ${project.version} + + ${project.groupId} + refaster-test-support + ${project.version} + com.fasterxml.jackson jackson-bom diff --git a/refaster-runner/pom.xml b/refaster-runner/pom.xml index 9d319d635e..8d0b7cde97 100644 --- a/refaster-runner/pom.xml +++ b/refaster-runner/pom.xml @@ -29,6 +29,19 @@ error_prone_check_api provided + + ${groupId.error-prone} + error_prone_core + test + + + ${project.groupId} + refaster-compiler + + provided + com.google.auto.service auto-service-annotations @@ -49,5 +62,43 @@ guava provided + + org.assertj + assertj-core + provided + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + ${project.groupId} + refaster-compiler + ${project.version} + + + + -Xplugin:RefasterRuleCompiler + + + + + + diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java index 61c2a7a6d6..c99e7e1f03 100644 --- a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java @@ -39,7 +39,7 @@ public static ImmutableListMultimap getAllCodeTransform * * @return A mapping from Refaster template names to associated {@link CodeTransformer}s. */ - public static ImmutableListMultimap loadAllCodeTransformers() { + private static ImmutableListMultimap loadAllCodeTransformers() { ImmutableListMultimap.Builder transformers = ImmutableListMultimap.builder(); diff --git a/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/CodeTransformersTest.java b/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/CodeTransformersTest.java new file mode 100644 index 0000000000..382f0519c1 --- /dev/null +++ b/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/CodeTransformersTest.java @@ -0,0 +1,20 @@ +package tech.picnic.errorprone.refaster.runner; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +final class CodeTransformersTest { + /** + * Verifies that {@link CodeTransformers#getAllCodeTransformers()} finds at least one code + * transformer on the classpath. + * + *

It is expected that the template collection from this package, {@link FooTemplates}, is one + * of these transformers. + */ + @Test + void loadAllCodeTransformers() { + assertThat(CodeTransformers.getAllCodeTransformers().keySet()) + .contains("FooTemplates$SimpleTemplate"); + } +} diff --git a/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/FooTemplates.java b/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/FooTemplates.java new file mode 100644 index 0000000000..30a9aa4054 --- /dev/null +++ b/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/FooTemplates.java @@ -0,0 +1,22 @@ +package tech.picnic.errorprone.refaster.runner; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; + +/** An example template collection used to test {@link CodeTransformers}. */ +final class FooTemplates { + private FooTemplates() {} + + /** Simple template for testing purposes. */ + static final class SimpleTemplate { + @BeforeTemplate + boolean before(String string) { + return string.length() == 1; + } + + @AfterTemplate + boolean after(String string) { + return string.length() > 0; + } + } +} diff --git a/refaster-test-support/pom.xml b/refaster-test-support/pom.xml index a40765e51d..bff6e8ede2 100644 --- a/refaster-test-support/pom.xml +++ b/refaster-test-support/pom.xml @@ -44,6 +44,7 @@ ${project.groupId} refaster-runner + provided com.google.code.findbugs From 299c6e9836136e23345137fc3a4e98ebb060c1f7 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 1 Jun 2022 16:50:04 +0200 Subject: [PATCH 22/41] Apply feedback --- .../RefasterTemplatesTest.java | 10 +++++--- refaster-test-support/pom.xml | 1 - .../test/MethodWithoutPrefixTemplates.java | 25 +++++++++++++++++++ ...fasterTemplateCollectionValidatorTest.java | 5 ++-- ...ummyTemplates.java => ValidTemplates.java} | 11 ++++---- .../MatchInWrongMethodTemplatesTestInput.java | 2 +- ...MatchInWrongMethodTemplatesTestOutput.java | 2 +- ...MethodWithoutPrefixTemplatesTestInput.java | 14 +++++++++++ ...ethodWithoutPrefixTemplatesTestOutput.java | 16 ++++++++++++ ...ingTestAndWrongTestTemplatesTestInput.java | 2 +- ...ngTestAndWrongTestTemplatesTestOutput.java | 2 +- ...TemplateWithoutTestTemplatesTestInput.java | 2 +- ...emplateWithoutTestTemplatesTestOutput.java | 2 +- ...nput.java => ValidTemplatesTestInput.java} | 12 +++------ ...put.java => ValidTemplatesTestOutput.java} | 14 +++-------- 15 files changed, 81 insertions(+), 39 deletions(-) create mode 100644 refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplates.java rename refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/{DummyTemplates.java => ValidTemplates.java} (84%) create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestInput.java create mode 100644 refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestOutput.java rename refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/{DummyTemplatesTestInput.java => ValidTemplatesTestInput.java} (76%) rename refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/{DummyTemplatesTestOutput.java => ValidTemplatesTestOutput.java} (68%) diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java index 619a4388fb..ab1516d1bc 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java @@ -61,16 +61,18 @@ final class RefasterTemplatesTest { TimeTemplates.class, WebClientTemplates.class); - private static Stream templateCollectionsTestCases() { + // XXX: Create a JUnit extension to automatically discover the template collections in a given + // context to make sure the list is exhaustive. + private static Stream validateTemplateCollectionsTestCases() { // XXX: Drop the filter once we have added tests for AssertJ! return TEMPLATE_COLLECTIONS.stream() - .filter(c -> c != AssertJTemplates.class) + .filter(tc -> tc != AssertJTemplates.class) .map(Arguments::arguments); } @ParameterizedTest - @MethodSource("templateCollectionsTestCases") - void validateTemplateCollectionTests(Class clazz) { + @MethodSource("validateTemplateCollectionsTestCases") + void validateTemplateCollections(Class clazz) { RefasterTemplateCollectionValidator.validate(clazz); } } diff --git a/refaster-test-support/pom.xml b/refaster-test-support/pom.xml index bff6e8ede2..a40765e51d 100644 --- a/refaster-test-support/pom.xml +++ b/refaster-test-support/pom.xml @@ -44,7 +44,6 @@ ${project.groupId} refaster-runner - provided com.google.code.findbugs diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplates.java new file mode 100644 index 0000000000..4d939d460c --- /dev/null +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplates.java @@ -0,0 +1,25 @@ +package tech.picnic.errorprone.refaster.test; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; + +/** + * Refaster template collection to validate that in the tests all methods have the prefix `test`. + * + *

The only exception to this is {@link RefasterTemplateTestCase#elidedTypesAndStaticImports()}. + */ +final class MethodWithoutPrefixTemplates { + private MethodWithoutPrefixTemplates() {} + + static final class StringIsEmpty { + @BeforeTemplate + boolean before(String string) { + return string.equals(""); + } + + @AfterTemplate + boolean after(String string) { + return string.isEmpty(); + } + } +} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java index 340284ead2..02680c5a7a 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java @@ -17,11 +17,12 @@ final class RefasterTemplateCollectionValidatorTest { @ParameterizedTest @ValueSource( classes = { - DummyTemplates.class, MatchInWrongMethodTemplates.class, + MethodWithoutPrefixTemplates.class, MissingTestAndWrongTestTemplates.class, PartialTestMatchTemplates.class, - TemplateWithoutTestTemplates.class + TemplateWithoutTestTemplates.class, + ValidTemplates.class }) void verifyRefasterTemplateCollections(Class clazz) { RefasterTemplateCollectionValidator.validate(clazz); diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/DummyTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/ValidTemplates.java similarity index 84% rename from refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/DummyTemplates.java rename to refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/ValidTemplates.java index a74e3a34cf..d38498f528 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/DummyTemplates.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/ValidTemplates.java @@ -11,12 +11,11 @@ import javax.annotation.Nullable; /** - * Refaster template collection containing arbitrary examples to validate that violations are - * reported correctly. + * Refaster template collection with arbitrary valid examples to validate that having no violations + * works as expected. */ -// XXX: Rename class and update Javadoc. -final class DummyTemplates { - private DummyTemplates() {} +final class ValidTemplates { + private ValidTemplates() {} static final class StringIsEmpty2 { @BeforeTemplate @@ -43,7 +42,7 @@ boolean after(String string) { } } - abstract class SetAddElement { + abstract static class BlockTemplateSetAddElement { @Placeholder abstract void doAfterAdd(E element); diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java index 92c95596ba..522d1ef20f 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestInput.java @@ -1,6 +1,6 @@ package tech.picnic.errorprone.refaster.test; -/** Code to test the Refaster templates from `MatchInWrongMethodTemplates`. */ +/** Code to test the Refaster templates from {@link MatchInWrongMethodTemplates}. */ final class MatchInWrongMethodTemplatesTest implements RefasterTemplateTestCase { boolean testWrongName() { "foo".equals(""); diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java index 4bcc31adc8..3e1fc4b7b3 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MatchInWrongMethodTemplatesTestOutput.java @@ -1,6 +1,6 @@ package tech.picnic.errorprone.refaster.test; -/** Code to test the Refaster templates from `MatchInWrongMethodTemplates`. */ +/** Code to test the Refaster templates from {@link MatchInWrongMethodTemplates}. */ final class MatchInWrongMethodTemplatesTest implements RefasterTemplateTestCase { /* * ERROR: The following matches unexpectedly occurred in method `testWrongName`: diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestInput.java new file mode 100644 index 0000000000..49aae06782 --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestInput.java @@ -0,0 +1,14 @@ +package tech.picnic.errorprone.refaster.test; + +/** Code to test the Refaster templates from {@link MethodWithoutPrefixTemplates}. */ +final class MethodWithoutPrefixTemplatesTest implements RefasterTemplateTestCase { + boolean testStringIsEmpty() { + return "foo".equals(""); + } + + private void foo() {} + + public String bar() { + return ""; + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestOutput.java new file mode 100644 index 0000000000..6272a362a2 --- /dev/null +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestOutput.java @@ -0,0 +1,16 @@ +package tech.picnic.errorprone.refaster.test; + +/** Code to test the Refaster templates from {@link MethodWithoutPrefixTemplates}. */ +final class MethodWithoutPrefixTemplatesTest implements RefasterTemplateTestCase { + boolean testStringIsEmpty() { + return "foo".isEmpty(); + } + + /* ERROR: Method names should start with `test`. */ + private void foo() {} + + /* ERROR: Method names should start with `test`. */ + public String bar() { + return ""; + } +} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java index 52656a223b..7c2c949f53 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestInput.java @@ -1,6 +1,6 @@ package tech.picnic.errorprone.refaster.test; -/** Code to test the Refaster templates from `MissingTestAndWrongTestTemplates`. */ +/** Code to test the Refaster templates from {@link MissingTestAndWrongTestTemplates}. */ final class MissingTestAndWrongTestTemplatesTest implements RefasterTemplateTestCase { boolean testWrongName() { "foo".equals(""); diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java index 7f861d12d9..f46a0f65d0 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MissingTestAndWrongTestTemplatesTestOutput.java @@ -1,6 +1,6 @@ package tech.picnic.errorprone.refaster.test; -/** Code to test the Refaster templates from `MissingTestAndWrongTestTemplates`. */ +/** Code to test the Refaster templates from {@link MissingTestAndWrongTestTemplates}. */ final class MissingTestAndWrongTestTemplatesTest implements RefasterTemplateTestCase { /* * ERROR: The following matches unexpectedly occurred in method `testWrongName`: diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java index 89563a85c3..a8c0ab762e 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestInput.java @@ -1,6 +1,6 @@ package tech.picnic.errorprone.refaster.test; -/** Code to test the Refaster templates from `TemplateWithoutTestTemplates`. */ +/** Code to test the Refaster templates from {@link TemplateWithoutTestTemplates}. */ final class TemplateWithoutTestTemplatesTest implements RefasterTemplateTestCase { boolean testStringIsEmpty() { return "foo".equals(""); diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java index 62cdc772f6..31c38abd12 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/TemplateWithoutTestTemplatesTestOutput.java @@ -1,6 +1,6 @@ package tech.picnic.errorprone.refaster.test; -/** Code to test the Refaster templates from `TemplateWithoutTestTemplates`. */ +/** Code to test the Refaster templates from {@link TemplateWithoutTestTemplates}. */ final class TemplateWithoutTestTemplatesTest implements RefasterTemplateTestCase { boolean testStringIsEmpty() { return "foo".isEmpty(); diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestInput.java similarity index 76% rename from refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestInput.java rename to refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestInput.java index 45e710c73d..a594bca087 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestInput.java @@ -6,8 +6,8 @@ import java.util.HashSet; import java.util.Set; -/** Code to test the Refaster templates from `DummyTemplates`. */ -final class DummyTemplatesTest implements RefasterTemplateTestCase { +/** Code to test the Refaster templates from {@link ValidTemplates}. */ +final class ValidTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(Collections.class, Strings.class); @@ -21,7 +21,7 @@ boolean testStaticImportStringLength() { return "foo" == null || "foo".isEmpty(); } - void testSetAddElement() { + void testBlockTemplateSetAddElement() { Set set = new HashSet<>(); Integer element = 1; if (!set.contains(element)) { @@ -29,10 +29,4 @@ void testSetAddElement() { System.out.print("added the following element to set: " + element); } } - - private void foo() {} - - public String bar() { - return ""; - } } diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestOutput.java similarity index 68% rename from refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestOutput.java rename to refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestOutput.java index b2b37d0b31..4f01c2f4ca 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/DummyTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestOutput.java @@ -8,8 +8,8 @@ import java.util.HashSet; import java.util.Set; -/** Code to test the Refaster templates from `DummyTemplates`. */ -final class DummyTemplatesTest implements RefasterTemplateTestCase { +/** Code to test the Refaster templates from {@link ValidTemplates}. */ +final class ValidTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(Collections.class, Strings.class); @@ -23,19 +23,11 @@ boolean testStaticImportStringLength() { return isNullOrEmpty("foo"); } - void testSetAddElement() { + void testBlockTemplateSetAddElement() { Set set = new HashSet<>(); Integer element = 1; if (set.add(element)) { System.out.print("added the following element to set: " + element); } } - - /* ERROR: Method names should start with `test`. */ - private void foo() {} - - /* ERROR: Method names should start with `test`. */ - public String bar() { - return ""; - } } From 8d564de43c89a85cdd9a3a9b80d95f936e8f34aa Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Wed, 1 Jun 2022 20:43:59 +0200 Subject: [PATCH 23/41] Suggestions --- error-prone-contrib/pom.xml | 13 ------------- refaster-runner/pom.xml | 5 ----- refaster-test-support/pom.xml | 16 +++++++++------- .../RefasterTemplateCollectionValidator.java | 4 ++-- 4 files changed, 11 insertions(+), 27 deletions(-) diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index 3143134f2d..311dd58319 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -39,19 +39,6 @@ error_prone_test_helpers test - - ${project.groupId} - refaster-compiler - - provided - - - ${project.groupId} - refaster-runner - provided - ${project.groupId} refaster-support diff --git a/refaster-runner/pom.xml b/refaster-runner/pom.xml index 8d0b7cde97..ff594bc506 100644 --- a/refaster-runner/pom.xml +++ b/refaster-runner/pom.xml @@ -29,11 +29,6 @@ error_prone_check_api provided - - ${groupId.error-prone} - error_prone_core - test - ${project.groupId} refaster-compiler diff --git a/refaster-test-support/pom.xml b/refaster-test-support/pom.xml index a40765e51d..863a9e80ff 100644 --- a/refaster-test-support/pom.xml +++ b/refaster-test-support/pom.xml @@ -31,20 +31,22 @@ ${groupId.error-prone} - error_prone_test_helpers + error_prone_core + test - ${project.groupId} - refaster-compiler - - provided + ${groupId.error-prone} + error_prone_test_helpers ${project.groupId} refaster-runner + + com.google.auto.service + auto-service-annotations + provided + com.google.code.findbugs jsr305 diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java index 4a1153f4d0..a36b09c09b 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java @@ -105,8 +105,8 @@ private static ImmutableSet getTemplatesUnderTest(String templateCollect * test method, defined explicitly for the purpose of exercising that template. * *

Note that a passing test does not guarantee full coverage: this test does not ascertain that - * all {@link com.google.errorprone.refaster.Refaster#anyOf} branches are tested. Likewise for - * {@link com.google.errorprone.refaster.annotation.BeforeTemplate} methods in case there are + * all {@code com.google.errorprone.refaster.Refaster#anyOf} branches are tested. Likewise for + * {@code com.google.errorprone.refaster.annotation.BeforeTemplate} methods in case there are * multiple. * * @param clazz The Refaster template collection under test. From a714ddc60cc4d224becec4540caab1f1503d3ac9 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Thu, 2 Jun 2022 10:59:11 +0200 Subject: [PATCH 24/41] As suggested --- .../errorprone/refaster/test/RefasterTemplateTestCase.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateTestCase.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateTestCase.java index de60400f4b..216791486e 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateTestCase.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateTestCase.java @@ -16,8 +16,8 @@ public interface RefasterTemplateTestCase { * present in the output file. * * @return Any values that are the result of expressions defined to ensure that all {@code - * *Input.java} import statements are also present in the associated {@code *Output.java} - * file. + * *Input.java} import statements are also required to be present in the associated {@code + * *Output.java} file. */ default ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(); From 35d9f7d84402c43cf441c397f960156d08a63ed5 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Fri, 3 Jun 2022 17:12:47 +0200 Subject: [PATCH 25/41] Improvements to make downstream users work --- refaster-test-support/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/refaster-test-support/pom.xml b/refaster-test-support/pom.xml index 863a9e80ff..918888093b 100644 --- a/refaster-test-support/pom.xml +++ b/refaster-test-support/pom.xml @@ -37,6 +37,7 @@ ${groupId.error-prone} error_prone_test_helpers + provided ${project.groupId} From d672444bce9ae01b531f28ffb11904011aa78bb5 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 4 Jun 2022 16:33:51 +0200 Subject: [PATCH 26/41] Suggestion --- refaster-test-support/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/refaster-test-support/pom.xml b/refaster-test-support/pom.xml index 918888093b..863a9e80ff 100644 --- a/refaster-test-support/pom.xml +++ b/refaster-test-support/pom.xml @@ -37,7 +37,6 @@ ${groupId.error-prone} error_prone_test_helpers - provided ${project.groupId} From c3f178c866d534ff0885d56c9da82356fb09febf Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 4 Jun 2022 19:18:13 +0200 Subject: [PATCH 27/41] Rename test methods, more honest dependency scoping --- .../refastertemplates/RefasterTemplatesTest.java | 6 +++--- refaster-runner/pom.xml | 2 +- refaster-test-support/README.md | 9 ++++----- refaster-test-support/pom.xml | 3 --- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java index ab1516d1bc..53828589cb 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java @@ -63,7 +63,7 @@ final class RefasterTemplatesTest { // XXX: Create a JUnit extension to automatically discover the template collections in a given // context to make sure the list is exhaustive. - private static Stream validateTemplateCollectionsTestCases() { + private static Stream validateTemplateCollectionTestCases() { // XXX: Drop the filter once we have added tests for AssertJ! return TEMPLATE_COLLECTIONS.stream() .filter(tc -> tc != AssertJTemplates.class) @@ -71,8 +71,8 @@ private static Stream validateTemplateCollectionsTestCases() { } @ParameterizedTest - @MethodSource("validateTemplateCollectionsTestCases") - void validateTemplateCollections(Class clazz) { + @MethodSource("validateTemplateCollectionTestCases") + void validateTemplateCollection(Class clazz) { RefasterTemplateCollectionValidator.validate(clazz); } } diff --git a/refaster-runner/pom.xml b/refaster-runner/pom.xml index ff594bc506..1fd6e0fdd8 100644 --- a/refaster-runner/pom.xml +++ b/refaster-runner/pom.xml @@ -60,7 +60,7 @@ org.assertj assertj-core - provided + test org.junit.jupiter diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index 95d8c7ba00..4c4073b754 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -21,12 +21,12 @@ tests and assurance that every template is properly tested. In summary, to test Refaster templates using the `RefasterTemplateCollectionValidator`, one should create an input and output -file. The Refaster templates from the collection are applied on the input file -and should exactly match the content of the provided output file. +file. The Refaster templates from the collection are applied to the input file +and should exactly match the contents of the provided output file. To test Refaster templates, one can create a (parameterized) test for every class containing the Refaster templates to invoke the -`RefasterTemplateCollectionValidator`. A class that contains one or more +`RefasterTemplateCollectionValidator`. A class that contains one or more Refaster templates is called a Refaster template collection. This test utility applies the Refaster templates in the collection to a provided input file, and expects the result to exactly match the contents of a provided output file. @@ -35,7 +35,7 @@ To adopt this setup, the following requirements have to be met: - Create a class with a (parameterized) test method that calls the `RefasterTemplateCollectionValidator#validate` and passes the collection(s) - to validate. The Refaster template collection must match the naming + to validate. The Refaster template collection must match the naming convention `Templates.java`. - An input file matching the naming convention `TemplatesTestInput.java` is added for every template @@ -81,4 +81,3 @@ test/ -- two methods named `testExample1Template` and -- `testExample2Template`. ``` - diff --git a/refaster-test-support/pom.xml b/refaster-test-support/pom.xml index 863a9e80ff..b97f3204c7 100644 --- a/refaster-test-support/pom.xml +++ b/refaster-test-support/pom.xml @@ -17,7 +17,6 @@ ${groupId.error-prone} error_prone_annotation - provided ${groupId.error-prone} @@ -27,7 +26,6 @@ ${groupId.error-prone} error_prone_check_api - provided ${groupId.error-prone} @@ -60,7 +58,6 @@ com.google.guava guava - provided org.junit.jupiter From 47c862c8bab7b7090b353a7ea0252ce1a40ebcd1 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Mon, 6 Jun 2022 12:20:49 +0200 Subject: [PATCH 28/41] Make sure `RefasterCheck` actually runs during the build --- pom.xml | 5 +++++ .../picnic/errorprone/refaster/runner/CodeTransformers.java | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 987eecd951..0442e3e50d 100644 --- a/pom.xml +++ b/pom.xml @@ -1355,6 +1355,11 @@ error-prone-contrib ${project.version} + + ${project.groupId} + refaster-runner + ${project.version} + diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java index c99e7e1f03..b9fda3d150 100644 --- a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java @@ -56,7 +56,7 @@ private static ImmutableListMultimap loadAllCodeTransfo private static ImmutableSet getClassPathResources() { try { - return ClassPath.from(ClassLoader.getSystemClassLoader()).getResources(); + return ClassPath.from(CodeTransformers.class.getClassLoader()).getResources(); } catch (IOException e) { throw new UncheckedIOException("Failed to scan classpath for resources", e); } From b1038815d95c5c4e41b45ad50adf9554e9a5f8ba Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Mon, 6 Jun 2022 16:37:59 +0200 Subject: [PATCH 29/41] Handle deserialization failure --- .../picnic/errorprone/refaster/runner/CodeTransformers.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java index b9fda3d150..020b5b5e0b 100644 --- a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java @@ -84,6 +84,10 @@ private static Optional loadCodeTransformer(ResourceInfo resour /* For some reason we can't load the resource. Skip it. */ // XXX: Should we log this? return Optional.empty(); + } catch (ClassCastException e) { + /* This resource does not appear to be compatible with the current classpath. */ + // XXX: Should we log this? + return Optional.empty(); } catch (IOException | ClassNotFoundException e) { throw new IllegalStateException("Can't load `CodeTransformer` from " + resource, e); } From f36df73915d43fd00bb7bac124336bf81d22da2f Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Mon, 6 Jun 2022 16:57:07 +0200 Subject: [PATCH 30/41] Get the build to pass --- .../picnic/errorprone/refaster/runner/RefasterCheck.java | 2 +- .../picnic/errorprone/refaster/runner/FooTemplates.java | 4 ++-- .../refaster/test/PartialTestMatchTemplates.java | 2 +- .../picnic/errorprone/refaster/test/ValidTemplates.java | 5 +++-- .../refaster/test/PartialTestMatchTemplatesTestInput.java | 2 +- .../errorprone/refaster/test/ValidTemplatesTestInput.java | 8 ++++---- .../refaster/test/ValidTemplatesTestOutput.java | 4 ++-- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/RefasterCheck.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/RefasterCheck.java index aba639aa40..af6db21fb5 100644 --- a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/RefasterCheck.java +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/RefasterCheck.java @@ -73,8 +73,8 @@ public RefasterCheck(ErrorProneFlags flags) { codeTransformer = createCompositeCodeTransformer(flags); } - @Override @CanIgnoreReturnValue + @Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { /* First, collect all matches. */ List matches = new ArrayList<>(); diff --git a/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/FooTemplates.java b/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/FooTemplates.java index 30a9aa4054..0625275d9f 100644 --- a/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/FooTemplates.java +++ b/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/FooTemplates.java @@ -11,12 +11,12 @@ private FooTemplates() {} static final class SimpleTemplate { @BeforeTemplate boolean before(String string) { - return string.length() == 1; + return string.toCharArray().length == 0; } @AfterTemplate boolean after(String string) { - return string.length() > 0; + return string.isEmpty(); } } } diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java index 1db6ec3579..83d1126ac9 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java @@ -9,7 +9,7 @@ private PartialTestMatchTemplates() {} static final class StringEquals { @BeforeTemplate boolean before(String string) { - return string.length() == 0; + return string.toCharArray().length == 0; } @AfterTemplate diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/ValidTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/ValidTemplates.java index d38498f528..a02ca68dc3 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/ValidTemplates.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/ValidTemplates.java @@ -7,6 +7,7 @@ import com.google.errorprone.refaster.annotation.BeforeTemplate; import com.google.errorprone.refaster.annotation.Placeholder; import com.google.errorprone.refaster.annotation.UseImportPolicy; +import java.util.Objects; import java.util.Set; import javax.annotation.Nullable; @@ -20,7 +21,7 @@ private ValidTemplates() {} static final class StringIsEmpty2 { @BeforeTemplate boolean before(String string) { - return string.equals(""); + return string.toCharArray().length == 0; } @AfterTemplate @@ -32,7 +33,7 @@ boolean after(String string) { static final class StaticImportStringLength { @BeforeTemplate boolean before(@Nullable String string) { - return string == null || string.isEmpty(); + return Objects.isNull(string) || string.isEmpty(); } @AfterTemplate diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestInput.java index 696065afb6..4816d35ef0 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplatesTestInput.java @@ -3,7 +3,7 @@ /** Code to test the Refaster templates from {@link PartialTestMatchTemplates}. */ final class PartialTestMatchTemplatesTest implements RefasterTemplateTestCase { boolean testStringIsEmpty() { - boolean b = "foo".length() == 0; + boolean b = "foo".toCharArray().length == 0; return "bar".equals(""); } } diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestInput.java index a594bca087..29de6609c2 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestInput.java @@ -2,23 +2,23 @@ import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; -import java.util.Collections; import java.util.HashSet; +import java.util.Objects; import java.util.Set; /** Code to test the Refaster templates from {@link ValidTemplates}. */ final class ValidTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Collections.class, Strings.class); + return ImmutableSet.of(Objects.class, Strings.class); } boolean testStringIsEmpty2() { - return "foo".equals(""); + return "foo".toCharArray().length == 0; } boolean testStaticImportStringLength() { - return "foo" == null || "foo".isEmpty(); + return Objects.isNull("foo") || "foo".isEmpty(); } void testBlockTemplateSetAddElement() { diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestOutput.java index 4f01c2f4ca..dce5fa55ad 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/ValidTemplatesTestOutput.java @@ -4,15 +4,15 @@ import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; -import java.util.Collections; import java.util.HashSet; +import java.util.Objects; import java.util.Set; /** Code to test the Refaster templates from {@link ValidTemplates}. */ final class ValidTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Collections.class, Strings.class); + return ImmutableSet.of(Objects.class, Strings.class); } boolean testStringIsEmpty2() { From 47acea70889f091d647bf04d96a4110a597b1134 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Mon, 1 Aug 2022 21:52:07 +0200 Subject: [PATCH 31/41] Post-rebase fixes --- .../refastertemplates/RefasterTemplatesTest.java | 2 +- .../test/RefasterTemplateCollectionValidator.java | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java index 53828589cb..71ae71346f 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java @@ -70,8 +70,8 @@ private static Stream validateTemplateCollectionTestCases() { .map(Arguments::arguments); } - @ParameterizedTest @MethodSource("validateTemplateCollectionTestCases") + @ParameterizedTest void validateTemplateCollection(Class clazz) { RefasterTemplateCollectionValidator.validate(clazz); } diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java index a36b09c09b..4e6c6cc9d4 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java @@ -14,6 +14,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableRangeMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.Iterables; import com.google.common.collect.Range; import com.google.common.collect.Sets; @@ -42,6 +43,7 @@ import java.util.Optional; import java.util.Set; import java.util.regex.Pattern; +import javax.annotation.Nullable; import tech.picnic.errorprone.refaster.runner.CodeTransformers; import tech.picnic.errorprone.refaster.runner.RefasterCheck; @@ -61,7 +63,7 @@ public final class RefasterTemplateCollectionValidator extends BugChecker "RefasterTemplateCollectionValidator:TemplateCollection"; private static final String TEST_METHOD_NAME_PREFIX = "test"; - private final ImmutableSet templatesUnderTest; + private final ImmutableSortedSet templatesUnderTest; private final RefasterCheck delegate; /** @@ -93,7 +95,8 @@ private static RefasterCheck createRefasterCheck(String templateCollectionUnderT Pattern.quote(templateCollectionUnderTest) + ".*"))); } - private static ImmutableSet getTemplatesUnderTest(String templateCollectionUnderTest) { + private static ImmutableSortedSet getTemplatesUnderTest( + String templateCollectionUnderTest) { return CodeTransformers.getAllCodeTransformers().keySet().stream() .filter(k -> k.startsWith(templateCollectionUnderTest)) .map(k -> k.replace(templateCollectionUnderTest + '$', "")) @@ -208,6 +211,7 @@ private class UnexpectedMatchReporter extends TreeScanner { this.indexedMatches = indexedMatches; } + @Nullable @Override public Void visitMethod(MethodTree tree, VisitorState state) { if (!ASTHelpers.isGeneratedConstructor(tree)) { From a8bc585144bb74ee2b5d3f29a1af950aa5b68e4a Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Mon, 1 Aug 2022 22:17:39 +0200 Subject: [PATCH 32/41] Suggestions --- .../RefasterTemplatesTest.java | 7 ++- .../refaster/runner/CodeTransformers.java | 7 ++- .../refaster/runner/package-info.java | 2 +- .../refaster/runner/CodeTransformersTest.java | 11 ++-- refaster-test-support/README.md | 61 +++++++++---------- .../RefasterTemplateCollectionValidator.java | 16 ++--- .../test/PartialTestMatchTemplates.java | 4 ++ ...fasterTemplateCollectionValidatorTest.java | 14 ++--- .../refaster/test/ValidTemplates.java | 5 +- ...MethodWithoutPrefixTemplatesTestInput.java | 7 +++ ...ethodWithoutPrefixTemplatesTestOutput.java | 7 +++ 11 files changed, 79 insertions(+), 62 deletions(-) diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java index 71ae71346f..c577729ccf 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java @@ -1,5 +1,7 @@ package tech.picnic.errorprone.refastertemplates; +import static java.util.function.Predicate.not; + import com.google.common.collect.ImmutableSet; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; @@ -64,9 +66,10 @@ final class RefasterTemplatesTest { // XXX: Create a JUnit extension to automatically discover the template collections in a given // context to make sure the list is exhaustive. private static Stream validateTemplateCollectionTestCases() { - // XXX: Drop the filter once we have added tests for AssertJ! + // XXX: Drop the filter once we have added tests for AssertJ! We can then also replace this + // method with `@ValueSource(classes = {...})`. return TEMPLATE_COLLECTIONS.stream() - .filter(tc -> tc != AssertJTemplates.class) + .filter(not(AssertJTemplates.class::equals)) .map(Arguments::arguments); } diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java index 020b5b5e0b..f3bde9fd0c 100644 --- a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/CodeTransformers.java @@ -14,7 +14,10 @@ import java.util.Optional; import java.util.function.Supplier; -/** Scans the classpath for `.refaster` files and loads them as {@link CodeTransformer}s. */ +/** + * Scans the classpath for {@value #REFASTER_TEMPLATE_SUFFIX} files and loads them as {@link + * CodeTransformer}s. + */ public final class CodeTransformers { private static final String REFASTER_TEMPLATE_SUFFIX = ".refaster"; private static final Supplier> @@ -88,7 +91,7 @@ private static Optional loadCodeTransformer(ResourceInfo resour /* This resource does not appear to be compatible with the current classpath. */ // XXX: Should we log this? return Optional.empty(); - } catch (IOException | ClassNotFoundException e) { + } catch (ClassNotFoundException | IOException e) { throw new IllegalStateException("Can't load `CodeTransformer` from " + resource, e); } } diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java index 830d98f634..ab1893a045 100644 --- a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java @@ -1,4 +1,4 @@ -/** Classes used to find Refaster templates on the classpath and to apply them. */ +/** Classes used to locate and apply compiled Refaster templates on. */ @com.google.errorprone.annotations.CheckReturnValue @javax.annotation.ParametersAreNonnullByDefault package tech.picnic.errorprone.refaster.runner; diff --git a/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/CodeTransformersTest.java b/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/CodeTransformersTest.java index 382f0519c1..b4a2732102 100644 --- a/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/CodeTransformersTest.java +++ b/refaster-runner/src/test/java/tech/picnic/errorprone/refaster/runner/CodeTransformersTest.java @@ -6,15 +6,12 @@ final class CodeTransformersTest { /** - * Verifies that {@link CodeTransformers#getAllCodeTransformers()} finds at least one code - * transformer on the classpath. - * - *

It is expected that the template collection from this package, {@link FooTemplates}, is one - * of these transformers. + * Verifies that {@link CodeTransformers#getAllCodeTransformers()} finds the code transformer + * compiled from {@link FooTemplates} on the classpath. */ @Test - void loadAllCodeTransformers() { + void getAllCodeTransformers() { assertThat(CodeTransformers.getAllCodeTransformers().keySet()) - .contains("FooTemplates$SimpleTemplate"); + .containsExactly("FooTemplates$SimpleTemplate"); } } diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index 4c4073b754..cd70763c1e 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -5,7 +5,7 @@ This module provides utilities to validate Refaster template collections. ## What does this module do? These utilities allow validating the rewrites (or their absence) performed by -Refaster templates. Each collection of Refaster templates defined in the same +Refaster templates. Each collection of Refaster templates defined in a single top-level class is applied to an input file, and the resulting rewrites should match the associated output file. @@ -17,12 +17,12 @@ irregularity will be reported, and the associated template collection test will fail. This way developers receive guidance on how to write Refaster template tests and assurance that every template is properly tested. -## How to test a collection of Refaster templates? +## How to test a collection of Refaster templates -In summary, to test Refaster templates using the -`RefasterTemplateCollectionValidator`, one should create an input and output -file. The Refaster templates from the collection are applied to the input file -and should exactly match the contents of the provided output file. +In a nutshell, to test a Refaster template collection class using +`RefasterTemplateCollectionValidator`, one should create suitably named input +and output files. The collection's Refaster templates are applied to the input +file and must exactly match the contents of the provided output file. To test Refaster templates, one can create a (parameterized) test for every class containing the Refaster templates to invoke the @@ -54,30 +54,29 @@ To adopt this setup, the following requirements have to be met: As a result from these tests, unexpected output will be shown in the console. -An example of a folder structure for such a setup is as follows: - +An example directory structure for such a setup is as follows: ``` -main/ - java/ - tech.picnic.errorprone.refastertemplates - └── ExampleTemplates.java -- Contains multiple Refaster templates. - └── Example1Template - └── Example2Template - -test/ - java/ - └── tech.picnic.errorprone.refastertemplates - └── RefasterCollectionTest.java - -- Here the test invokes - -- `RefasterTemplateCollectionValidator#validate`. - resources/ - └── tech.picnic.errorprone.refastertemplates - └── ExampleTemplatesTestInput.java - -- Contains a class named `ExampleTemplatesTest` and - -- two methods named `testExample1Template` and - -- `testExample2Template`. - └── ExampleTemplatesTestOutput.java - -- Contains a class named `ExampleTemplatesTest` and - -- two methods named `testExample1Template` and - -- `testExample2Template`. +src/ + main/ + java/ + tech.picnic.errorprone.refastertemplates + └── ExampleTemplates.java -- Contains multiple Refaster templates. + └── Example1Template + └── Example2Template + test/ + java/ + tech.picnic.errorprone.refastertemplates + └── RefasterCollectionTest.java + -- This test class invokes + -- `RefasterTemplateCollectionValidator#validate`. + resources/ + tech.picnic.errorprone.refastertemplates + └── ExampleTemplatesTestInput.java + -- Contains a class named `ExampleTemplatesTest` and + -- two methods named `testExample1Template` and + -- `testExample2Template`. + └── ExampleTemplatesTestOutput.java + -- Contains a class named `ExampleTemplatesTest` and + -- two methods named `testExample1Template` and + -- `testExample2Template`. ``` diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java index 4e6c6cc9d4..8be1f29c24 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java @@ -48,9 +48,9 @@ import tech.picnic.errorprone.refaster.runner.RefasterCheck; /** - * A {@link BugChecker} that applies a Refaster template collection by delegating to {@link - * RefasterCheck} and subsequently validates that each template modifies exactly one distinct - * method, as indicated by each method's name. + * A {@link BugChecker} that applies a Refaster template collection to an associated test input file + * by delegating to {@link RefasterCheck}, and subsequently validates that each template modifies + * exactly one distinct method, as indicated by each method's name. */ @BugPattern( name = "RefasterTemplateCollectionValidator", @@ -118,7 +118,7 @@ public static void validate(Class clazz) { String className = clazz.getSimpleName(); BugCheckerRefactoringTestHelper.newInstance(RefasterTemplateCollectionValidator.class, clazz) - .setArgs(ImmutableList.of("-XepOpt:" + TEMPLATE_COLLECTION_FLAG + "=" + className)) + .setArgs(ImmutableList.of("-XepOpt:" + TEMPLATE_COLLECTION_FLAG + '=' + className)) .addInput(className + "TestInput.java") .addOutput(className + "TestOutput.java") .doTest(TEXT_MATCH); @@ -151,7 +151,7 @@ private static ImmutableRangeMap indexTemplateMatches( Iterables.getOnlyElement(description.fixes).getReplacements(endPositions); for (Replacement replacement : replacements) { templateMatches.put( - replacement.range(), getSubstringAfterLastChar('.', description.checkName)); + replacement.range(), getSubstringAfterFinalDelimiter('.', description.checkName)); } } @@ -172,7 +172,7 @@ private void reportMissingMatches( tree, String.format( "Did not encounter a test in `%s` for the following template(s)", - getSubstringAfterLastChar('/', sourceFile)), + getSubstringAfterFinalDelimiter('/', sourceFile)), templatesWithoutMatch, state); } @@ -194,11 +194,11 @@ private void reportViolations( SuggestedFix fixWithComment = tree instanceof MethodTree ? SuggestedFix.prefixWith(tree, comment) - : SuggestedFix.postfixWith(tree, "\n" + comment); + : SuggestedFix.postfixWith(tree, '\n' + comment); state.reportMatch(describeMatch(tree, fixWithComment)); } - private static String getSubstringAfterLastChar(char delimiter, String value) { + private static String getSubstringAfterFinalDelimiter(char delimiter, String value) { int index = value.lastIndexOf(delimiter); checkState(index >= 0, "String '%s' does not contain character '%s'", value, delimiter); return value.substring(index + 1); diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java index 83d1126ac9..c6af0fb126 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/PartialTestMatchTemplates.java @@ -3,6 +3,10 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; +/** + * Refaster template collection to validate that matches for one template in a test method meant to + * cover another template are reported. + */ final class PartialTestMatchTemplates { private PartialTestMatchTemplates() {} diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java index 02680c5a7a..eefa5185eb 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java @@ -4,14 +4,14 @@ import org.junit.jupiter.params.provider.ValueSource; /** - * Validate error reporting of the Refaster template collections. + * Validates {@link RefasterTemplateCollectionValidator} error reporting. * - *

The goal of the Refaster template collections under test is to verify the reporting of {@link - * RefasterTemplateCollectionValidator} using the associated `TestInput` and `TestOutput` files. - * Normally, {@link RefasterTemplateCollectionValidator} will report error messages directly in the - * console where the results of the tests are shown. However, to verify that these messages are - * correct, the `*TestOutput` files in this package contain error reporting that is normally not - * written. + *

The goal of the Refaster template collections under test is to verify the reporting of + * violations by {@link RefasterTemplateCollectionValidator} using the associated {@code + * TestInput.java} and {@code TestOutput.java} files. Normally, {@link + * RefasterTemplateCollectionValidator} will raise error messages to be rendered in the console or + * IDE. However, to verify that these error messages are as intended, the {@code *TestOutput.java} + * files in this package contain error reporting that is normally not present. */ final class RefasterTemplateCollectionValidatorTest { @ParameterizedTest diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/ValidTemplates.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/ValidTemplates.java index a02ca68dc3..3e479f111d 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/ValidTemplates.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/ValidTemplates.java @@ -11,10 +11,7 @@ import java.util.Set; import javax.annotation.Nullable; -/** - * Refaster template collection with arbitrary valid examples to validate that having no violations - * works as expected. - */ +/** Refaster template collection to validate that having no violations works as expected. */ final class ValidTemplates { private ValidTemplates() {} diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestInput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestInput.java index 49aae06782..c22dbb6ae3 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestInput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestInput.java @@ -1,7 +1,14 @@ package tech.picnic.errorprone.refaster.test; +import com.google.common.collect.ImmutableSet; + /** Code to test the Refaster templates from {@link MethodWithoutPrefixTemplates}. */ final class MethodWithoutPrefixTemplatesTest implements RefasterTemplateTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(); + } + boolean testStringIsEmpty() { return "foo".equals(""); } diff --git a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestOutput.java b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestOutput.java index 6272a362a2..a771efa8b1 100644 --- a/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestOutput.java +++ b/refaster-test-support/src/test/resources/tech/picnic/errorprone/refaster/test/MethodWithoutPrefixTemplatesTestOutput.java @@ -1,7 +1,14 @@ package tech.picnic.errorprone.refaster.test; +import com.google.common.collect.ImmutableSet; + /** Code to test the Refaster templates from {@link MethodWithoutPrefixTemplates}. */ final class MethodWithoutPrefixTemplatesTest implements RefasterTemplateTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(); + } + boolean testStringIsEmpty() { return "foo".isEmpty(); } From 53ffe2bbcd82234b2e31f3cba91d12ae730e5bbc Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Thu, 4 Aug 2022 14:11:43 +0200 Subject: [PATCH 33/41] Apply feedback and update `README.md` --- .../RefasterTemplatesTest.java | 4 +- refaster-test-support/README.md | 40 +++++++++---------- ...r.java => RefasterTemplateCollection.java} | 12 +++--- ...va => RefasterTemplateCollectionTest.java} | 16 ++++---- 4 files changed, 36 insertions(+), 36 deletions(-) rename refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/{RefasterTemplateCollectionValidator.java => RefasterTemplateCollection.java} (97%) rename refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/{RefasterTemplateCollectionValidatorTest.java => RefasterTemplateCollectionTest.java} (50%) diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java index c577729ccf..7b78e49475 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refastertemplates/RefasterTemplatesTest.java @@ -7,7 +7,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import tech.picnic.errorprone.refaster.test.RefasterTemplateCollectionValidator; +import tech.picnic.errorprone.refaster.test.RefasterTemplateCollection; final class RefasterTemplatesTest { /** The names of all Refaster template groups defined in this module. */ @@ -76,6 +76,6 @@ private static Stream validateTemplateCollectionTestCases() { @MethodSource("validateTemplateCollectionTestCases") @ParameterizedTest void validateTemplateCollection(Class clazz) { - RefasterTemplateCollectionValidator.validate(clazz); + RefasterTemplateCollection.validate(clazz); } } diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index cd70763c1e..9719463e72 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -11,39 +11,39 @@ match the associated output file. This extension ensures that each Refaster template is tested, making sure that it matches and transforms code as intended. If a Refaster template is not -covered by a test, if it influences unrelated test code code, or if the -associated test doesn't follow certain established standards, then this -irregularity will be reported, and the associated template collection test will -fail. This way developers receive guidance on how to write Refaster template -tests and assurance that every template is properly tested. +covered by a test, if it influences unrelated test code, or if the associated +test doesn't follow certain established standards, then this irregularity will +be reported, and the associated template collection test will fail. This way +developers receive guidance on how to write Refaster template tests and +assurance that every template is properly tested. ## How to test a collection of Refaster templates -In a nutshell, to test a Refaster template collection class using -`RefasterTemplateCollectionValidator`, one should create suitably named input -and output files. The collection's Refaster templates are applied to the input -file and must exactly match the contents of the provided output file. +A class that contains one or more Refaster templates is called a Refaster +template collection. In a nutshell, to test a Refaster template collection +class using `RefasterTemplateCollection`, one should create suitably named +input and output files. The collection's Refaster templates are applied to the +input file and must exactly match the contents of the provided output file. To test Refaster templates, one can create a (parameterized) test for every -class containing the Refaster templates to invoke the -`RefasterTemplateCollectionValidator`. A class that contains one or more -Refaster templates is called a Refaster template collection. This test utility -applies the Refaster templates in the collection to a provided input file, and -expects the result to exactly match the contents of a provided output file. +class that contains Refaster templates and invoke +`RefasterTemplateCollection#validate`. This test utility applies the Refaster +templates in the collection to a provided input file, and expects the result +to exactly match the contents of a provided output file. To adopt this setup, the following requirements have to be met: -- Create a class with a (parameterized) test method that calls the - `RefasterTemplateCollectionValidator#validate` and passes the collection(s) - to validate. The Refaster template collection must match the naming - convention `Templates.java`. +- Create a class with a (parameterized) test method that invokes + `RefasterTemplateCollection#validate` and passes the collection(s) to + validate. The Refaster template collection must match the naming convention + `Templates.java`. - An input file matching the naming convention `TemplatesTestInput.java` is added for every template collection. - An output file matching the naming convention `TemplatesTestOutput.java` file is added for every template collection. -- For each Refaster template in the collection, the input and output file must +- For every Refaster template in the collection, the input and output file must contain a method. The name of the method is equal to the name of the Refaster template prefixed with `test` (e.g. `test`). - The method contains at least one expression that matches the @@ -68,7 +68,7 @@ src/ tech.picnic.errorprone.refastertemplates └── RefasterCollectionTest.java -- This test class invokes - -- `RefasterTemplateCollectionValidator#validate`. + -- `RefasterTemplateCollection#validate`. resources/ tech.picnic.errorprone.refastertemplates └── ExampleTemplatesTestInput.java diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java similarity index 97% rename from refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java rename to refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java index 8be1f29c24..1ffe29dc9a 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidator.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java @@ -53,25 +53,25 @@ * exactly one distinct method, as indicated by each method's name. */ @BugPattern( - name = "RefasterTemplateCollectionValidator", + name = "RefasterTemplateCollection", summary = "Exercises a Refaster template collection", severity = ERROR) -public final class RefasterTemplateCollectionValidator extends BugChecker +public final class RefasterTemplateCollection extends BugChecker implements CompilationUnitTreeMatcher { private static final long serialVersionUID = 1L; private static final String TEMPLATE_COLLECTION_FLAG = - "RefasterTemplateCollectionValidator:TemplateCollection"; + "RefasterTemplateCollection:TemplateCollection"; private static final String TEST_METHOD_NAME_PREFIX = "test"; private final ImmutableSortedSet templatesUnderTest; private final RefasterCheck delegate; /** - * Instantiates a {@link RefasterTemplateCollectionValidator} instance. + * Instantiates a {@link RefasterTemplateCollection} instance. * * @param flags Any provided command line flags. */ - public RefasterTemplateCollectionValidator(ErrorProneFlags flags) { + public RefasterTemplateCollection(ErrorProneFlags flags) { String templateCollectionUnderTest = getTemplateCollectionUnderTest(flags); delegate = createRefasterCheck(templateCollectionUnderTest); templatesUnderTest = getTemplatesUnderTest(templateCollectionUnderTest); @@ -117,7 +117,7 @@ private static ImmutableSortedSet getTemplatesUnderTest( public static void validate(Class clazz) { String className = clazz.getSimpleName(); - BugCheckerRefactoringTestHelper.newInstance(RefasterTemplateCollectionValidator.class, clazz) + BugCheckerRefactoringTestHelper.newInstance(RefasterTemplateCollection.class, clazz) .setArgs(ImmutableList.of("-XepOpt:" + TEMPLATE_COLLECTION_FLAG + '=' + className)) .addInput(className + "TestInput.java") .addOutput(className + "TestOutput.java") diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionTest.java similarity index 50% rename from refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java rename to refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionTest.java index eefa5185eb..5a2194909e 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionValidatorTest.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionTest.java @@ -4,16 +4,16 @@ import org.junit.jupiter.params.provider.ValueSource; /** - * Validates {@link RefasterTemplateCollectionValidator} error reporting. + * Validates {@link RefasterTemplateCollection} error reporting. * *

The goal of the Refaster template collections under test is to verify the reporting of - * violations by {@link RefasterTemplateCollectionValidator} using the associated {@code - * TestInput.java} and {@code TestOutput.java} files. Normally, {@link - * RefasterTemplateCollectionValidator} will raise error messages to be rendered in the console or - * IDE. However, to verify that these error messages are as intended, the {@code *TestOutput.java} - * files in this package contain error reporting that is normally not present. + * violations by {@link RefasterTemplateCollection} using the associated {@code TestInput.java} and + * {@code TestOutput.java} files. Normally, {@link RefasterTemplateCollection} will raise error + * messages to be rendered in the console or IDE. However, to verify that these error messages are + * as intended, the {@code *TestOutput.java} files in this package contain error reporting that is + * normally not present. */ -final class RefasterTemplateCollectionValidatorTest { +final class RefasterTemplateCollectionTest { @ParameterizedTest @ValueSource( classes = { @@ -25,6 +25,6 @@ final class RefasterTemplateCollectionValidatorTest { ValidTemplates.class }) void verifyRefasterTemplateCollections(Class clazz) { - RefasterTemplateCollectionValidator.validate(clazz); + RefasterTemplateCollection.validate(clazz); } } From b0a8658d028a10511d55f449f1666f41b7c02534 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Thu, 4 Aug 2022 18:27:36 +0200 Subject: [PATCH 34/41] Javadoc tweak --- .../refaster/test/RefasterTemplateCollectionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionTest.java b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionTest.java index 5a2194909e..360a17902c 100644 --- a/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionTest.java +++ b/refaster-test-support/src/test/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollectionTest.java @@ -10,8 +10,8 @@ * violations by {@link RefasterTemplateCollection} using the associated {@code TestInput.java} and * {@code TestOutput.java} files. Normally, {@link RefasterTemplateCollection} will raise error * messages to be rendered in the console or IDE. However, to verify that these error messages are - * as intended, the {@code *TestOutput.java} files in this package contain error reporting that is - * normally not present. + * emitted as intended, the {@code *TestOutput.java} files in this package contain error reporting + * that is normally not present. */ final class RefasterTemplateCollectionTest { @ParameterizedTest From 93785fbfd17f21df7f0fa11cd760d04044c8bcdc Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 7 Aug 2022 18:56:49 +0200 Subject: [PATCH 35/41] Semi-related fix --- .../errorprone/refastertemplates/OptionalTemplates.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/OptionalTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/OptionalTemplates.java index b33ddf3b16..f84351a1e8 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/OptionalTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/OptionalTemplates.java @@ -113,7 +113,7 @@ Optional after(Iterator it) { /** Prefer {@link Optional#filter(Predicate)} over usage of the ternary operator. */ // XXX: This rule may introduce a compilation error: the `test` expression may reference a // non-effectively final variable, which is not allowed in the replacement lambda expression. - // Maybe our RefasterCheck should test `compilesWithFix`? + // Maybe our `Refaster` checker should test `compilesWithFix`? abstract static class TernaryOperatorOptionalPositiveFiltering { @Placeholder abstract boolean test(T value); @@ -133,7 +133,7 @@ Optional after(T input) { /** Prefer {@link Optional#filter(Predicate)} over usage of the ternary operator. */ // XXX: This rule may introduce a compilation error: the `test` expression may reference a // non-effectively final variable, which is not allowed in the replacement lambda expression. - // Maybe our RefasterCheck should test `compilesWithFix`? + // Maybe our `Refaster` checker should test `compilesWithFix`? abstract static class TernaryOperatorOptionalNegativeFiltering { @Placeholder abstract boolean test(T value); From b8e3fd8df5e960e25784d3723ce0ef571cfe808b Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Tue, 9 Aug 2022 08:41:19 +0200 Subject: [PATCH 36/41] Suggestions (partial README review) --- refaster-test-support/README.md | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index 9719463e72..0b999796b6 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -1,6 +1,8 @@ # Refaster test support -This module provides utilities to validate Refaster template collections. +This module provides utilities to validate _Refaster template collections_. A +template collection is a set of Refaster templates represented as static nested +classes, all located in a shared top-level class. ## What does this module do? @@ -9,27 +11,27 @@ Refaster templates. Each collection of Refaster templates defined in a single top-level class is applied to an input file, and the resulting rewrites should match the associated output file. -This extension ensures that each Refaster template is tested, making sure that -it matches and transforms code as intended. If a Refaster template is not -covered by a test, if it influences unrelated test code, or if the associated -test doesn't follow certain established standards, then this irregularity will -be reported, and the associated template collection test will fail. This way -developers receive guidance on how to write Refaster template tests and -assurance that every template is properly tested. +The validation performed by this module ensures that each Refaster template is +tested, making sure that it matches and transforms code as intended. If a +Refaster template is not covered by a test, if it influences unrelated test +code, or if the associated test doesn't follow certain established standards, +then this irregularity will be reported, and the associated template collection +test will fail. This way developers receive guidance on how to write Refaster +template tests and assurance that every template is properly tested. ## How to test a collection of Refaster templates -A class that contains one or more Refaster templates is called a Refaster -template collection. In a nutshell, to test a Refaster template collection -class using `RefasterTemplateCollection`, one should create suitably named -input and output files. The collection's Refaster templates are applied to the -input file and must exactly match the contents of the provided output file. +In a nutshell, to test a Refaster template collection class using the +`RefasterTemplateCollection` class, one should create suitably named input and +output files. The collection's Refaster templates are applied to the input +file, and the generated patches must exactly produce the contents of the +associated output file. To test Refaster templates, one can create a (parameterized) test for every class that contains Refaster templates and invoke `RefasterTemplateCollection#validate`. This test utility applies the Refaster -templates in the collection to a provided input file, and expects the result -to exactly match the contents of a provided output file. +templates in the collection to a provided input file, and expects the result to +exactly match the contents of a provided output file. To adopt this setup, the following requirements have to be met: From dca5a6bea4e8532e8a11abbfa51a74770a456ee4 Mon Sep 17 00:00:00 2001 From: Pieter Dirk Soels Date: Tue, 9 Aug 2022 15:06:45 +0200 Subject: [PATCH 37/41] Apply subset of README.md suggestions --- refaster-test-support/README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index 0b999796b6..cb0774723e 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -16,7 +16,7 @@ tested, making sure that it matches and transforms code as intended. If a Refaster template is not covered by a test, if it influences unrelated test code, or if the associated test doesn't follow certain established standards, then this irregularity will be reported, and the associated template collection -test will fail. This way developers receive guidance on how to write Refaster +test will fail. This way, developers receive guidance on how to write Refaster template tests and assurance that every template is properly tested. ## How to test a collection of Refaster templates @@ -33,12 +33,12 @@ class that contains Refaster templates and invoke templates in the collection to a provided input file, and expects the result to exactly match the contents of a provided output file. -To adopt this setup, the following requirements have to be met: +To adopt this setup, the following requirements must be met: -- Create a class with a (parameterized) test method that invokes - `RefasterTemplateCollection#validate` and passes the collection(s) to - validate. The Refaster template collection must match the naming convention - `Templates.java`. +- A class with a (parameterized) test method that invokes + `RefasterTemplateCollection#validate` is created which provides the + collection(s) to validate. The Refaster template collection must match the + naming convention `Templates.java`. - An input file matching the naming convention `TemplatesTestInput.java` is added for every template collection. @@ -47,10 +47,10 @@ To adopt this setup, the following requirements have to be met: template collection. - For every Refaster template in the collection, the input and output file must contain a method. The name of the method is equal to the name of the Refaster - template prefixed with `test` (e.g. `test`). + template prefixed with `test` (e.g. `test`). - The method contains at least one expression that matches the - `@BeforeTemplate` of one specific Refaster template. As a result, the output - file contains the same method with an updated expression, matching the + `@BeforeTemplate` of the corresponding Refaster template. As a result, the + output file contains the same method with an updated expression, matching the content of the `@AfterTemplate`. Additionally, incorrect matches of _other_ Refaster templates in the method are flagged. From f60da11f74f31104c3cdc21ddd5f2140cfefc10d Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 9 Aug 2022 17:39:10 +0200 Subject: [PATCH 38/41] PSM-1299 Partially apply feedback from Pieter --- refaster-test-support/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index cb0774723e..d9c5d30ccb 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -6,10 +6,10 @@ classes, all located in a shared top-level class. ## What does this module do? -These utilities allow validating the rewrites (or their absence) performed by -Refaster templates. Each collection of Refaster templates defined in a single -top-level class is applied to an input file, and the resulting rewrites should -match the associated output file. +These utilities allow for validating the rewrites (or absence thereof) as +performed by Refaster templates. Each collection of Refaster templates defined +in a single top-level class is applied to an input file, and the resulting +rewrites should match the associated output file. The validation performed by this module ensures that each Refaster template is tested, making sure that it matches and transforms code as intended. If a @@ -47,7 +47,7 @@ To adopt this setup, the following requirements must be met: template collection. - For every Refaster template in the collection, the input and output file must contain a method. The name of the method is equal to the name of the Refaster - template prefixed with `test` (e.g. `test`). + template prefixed with `test` (e.g. `test`). - The method contains at least one expression that matches the `@BeforeTemplate` of the corresponding Refaster template. As a result, the output file contains the same method with an updated expression, matching the From 4bcd8fb33ba83fadaaacedb7bc38afe6d0f5a650 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Tue, 9 Aug 2022 19:16:37 +0200 Subject: [PATCH 39/41] Pieter's suggestions --- .../tech/picnic/errorprone/refaster/runner/package-info.java | 2 +- .../errorprone/refaster/test/RefasterTemplateCollection.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java index ab1893a045..dc08a789c6 100644 --- a/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java +++ b/refaster-runner/src/main/java/tech/picnic/errorprone/refaster/runner/package-info.java @@ -1,4 +1,4 @@ -/** Classes used to locate and apply compiled Refaster templates on. */ +/** Exposes Refaster templates found on the classpath through a regular Error Prone check. */ @com.google.errorprone.annotations.CheckReturnValue @javax.annotation.ParametersAreNonnullByDefault package tech.picnic.errorprone.refaster.runner; diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java index 4b86b18548..9972be464e 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java @@ -51,6 +51,11 @@ * A {@link BugChecker} that applies a Refaster template collection to an associated test input file * by delegating to the {@link Refaster} checker, and subsequently validates that each template * modifies exactly one distinct method, as indicated by each method's name. + * + *

The test input and output files must be classpath resources located in the same package as the + * template collection class. Their names are derived from the template collection class by + * suffixing {@code TestInput.java} and {@code TestOutput.java}, respectively. Each test method's + * name must be derived from the template that modifies said method by prefixing {@code test}. */ @BugPattern(summary = "Exercises a Refaster template collection", severity = ERROR) public final class RefasterTemplateCollection extends BugChecker From b514ad9787c5c8cf9c346a87e9e394b32f3fb2be Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Wed, 10 Aug 2022 09:26:26 +0200 Subject: [PATCH 40/41] Suggestions --- refaster-test-support/README.md | 40 +++++++++---------- .../test/RefasterTemplateCollection.java | 3 ++ 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index d9c5d30ccb..d80cf99e7c 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -6,7 +6,7 @@ classes, all located in a shared top-level class. ## What does this module do? -These utilities allow for validating the rewrites (or absence thereof) as +These utilities allow for validating the rewrites (or absence of rewrites) performed by Refaster templates. Each collection of Refaster templates defined in a single top-level class is applied to an input file, and the resulting rewrites should match the associated output file. @@ -21,7 +21,7 @@ template tests and assurance that every template is properly tested. ## How to test a collection of Refaster templates -In a nutshell, to test a Refaster template collection class using the +In a nutshell, to test a Refaster template collection using the `RefasterTemplateCollection` class, one should create suitably named input and output files. The collection's Refaster templates are applied to the input file, and the generated patches must exactly produce the contents of the @@ -35,26 +35,24 @@ exactly match the contents of a provided output file. To adopt this setup, the following requirements must be met: -- A class with a (parameterized) test method that invokes - `RefasterTemplateCollection#validate` is created which provides the - collection(s) to validate. The Refaster template collection must match the - naming convention `Templates.java`. -- An input file matching the naming convention - `TemplatesTestInput.java` is added for every template - collection. -- An output file matching the naming convention - `TemplatesTestOutput.java` file is added for every - template collection. -- For every Refaster template in the collection, the input and output file must - contain a method. The name of the method is equal to the name of the Refaster - template prefixed with `test` (e.g. `test`). -- The method contains at least one expression that matches the +- Each Refaster template collection must match the naming convention + `Templates.java`. +- There is a test class with a (parameterized) test method that invokes + `RefasterTemplateCollection#validate` on the template collection(s) to be + validated. +- For every template collection there is an input file matching the naming + convention `TemplatesTestInput.java`. +- For every template collection there is an output file matching the naming + convention `TemplatesTestOutput.java`. +- For every Refaster template in a collection, the associated input and output + files must contain a method that validates the template's behavior. The name + of this method must be derived from the name of the Refaster template it aims + to validate, prefixed with `test` (i.e. `test`). +- Each such method contains at least one expression that matches the `@BeforeTemplate` of the corresponding Refaster template. As a result, the - output file contains the same method with an updated expression, matching the - content of the `@AfterTemplate`. Additionally, incorrect matches of _other_ - Refaster templates in the method are flagged. - -As a result from these tests, unexpected output will be shown in the console. + output file must contain the same method with an updated expression, in + accordance with the associated `@AfterTemplate`. +- Such methods must not match any _other_ Refaster templates. An example directory structure for such a setup is as follows: ``` diff --git a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java index 9972be464e..63c4b9e104 100644 --- a/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java +++ b/refaster-test-support/src/main/java/tech/picnic/errorprone/refaster/test/RefasterTemplateCollection.java @@ -57,6 +57,9 @@ * suffixing {@code TestInput.java} and {@code TestOutput.java}, respectively. Each test method's * name must be derived from the template that modifies said method by prefixing {@code test}. */ +// XXX: This check currently only validates that one `Refaster.anyOf` branch in one +// `@BeforeTemplate` method is covered by a test. Review how we can make sure that _all_ +// `@BeforeTemplate` methods and `Refaster.anyOf` branches are covered. @BugPattern(summary = "Exercises a Refaster template collection", severity = ERROR) public final class RefasterTemplateCollection extends BugChecker implements CompilationUnitTreeMatcher { From 9b7beb38b2438b88dd07cbbf6848a9398a723b54 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Wed, 10 Aug 2022 10:24:17 +0200 Subject: [PATCH 41/41] Apply Pieter's suggestion --- refaster-test-support/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/refaster-test-support/README.md b/refaster-test-support/README.md index d80cf99e7c..7c74158215 100644 --- a/refaster-test-support/README.md +++ b/refaster-test-support/README.md @@ -23,9 +23,9 @@ template tests and assurance that every template is properly tested. In a nutshell, to test a Refaster template collection using the `RefasterTemplateCollection` class, one should create suitably named input and -output files. The collection's Refaster templates are applied to the input -file, and the generated patches must exactly produce the contents of the -associated output file. +output source code files. The collection's Refaster templates are applied to +the input file, and the generated patches must exactly produce the contents of +the associated output file. To test Refaster templates, one can create a (parameterized) test for every class that contains Refaster templates and invoke