diff --git a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestInstanceOfToJUnit5.java b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestInstanceOfToJUnit5.java new file mode 100644 index 000000000..0fbcdb35f --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestInstanceOfToJUnit5.java @@ -0,0 +1,116 @@ +/* + * Copyright 2023 the original author or authors. + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.testing.hamcrest;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Preconditions;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.search.UsesMethod;
+import org.openrewrite.java.tree.Expression;
+import org.openrewrite.java.tree.J;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class HamcrestInstanceOfToJUnit5 extends Recipe {
+ @Override
+ public String getDisplayName() {
+ return "Migrate from Hamcrest `instanceOf` matcher to JUnit 5";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Migrate from Hamcrest `instanceOf` and `isA` matcher to JUnit5 `assertInstanceOf` assertion.";
+ }
+
+ private static final MethodMatcher INSTANCE_OF_MATCHER = new MethodMatcher("org.hamcrest.Matchers instanceOf(..)");
+ private static final MethodMatcher IS_A_MATCHER = new MethodMatcher("org.hamcrest.Matchers isA(..)");
+ private static final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher("org.hamcrest.MatcherAssert assertThat(.., org.hamcrest.Matcher)");
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ TreeVisitor, ExecutionContext> preconditions = Preconditions.and(
+ new UsesMethod<>(ASSERT_THAT_MATCHER),
+ Preconditions.or(
+ new UsesMethod<>(INSTANCE_OF_MATCHER),
+ new UsesMethod<>(IS_A_MATCHER)));
+ return Preconditions.check(preconditions, new JavaIsoVisitor
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.testing.hamcrest;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Preconditions;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.search.UsesMethod;
+import org.openrewrite.java.tree.Expression;
+import org.openrewrite.java.tree.J;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.BiFunction;
+
+public class HamcrestMatcherToJUnit5 extends Recipe {
+
+ private static final MethodMatcher MATCHER_ASSERT_MATCHER = new MethodMatcher("org.hamcrest.MatcherAssert assertThat(.., org.hamcrest.Matcher)");
+
+ @Override
+ public String getDisplayName() {
+ return "Migrate from Hamcrest `Matcher` to JUnit 5";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Migrate from Hamcrest `Matcher` to JUnit 5 assertions.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return Preconditions.check(
+ new UsesMethod<>(MATCHER_ASSERT_MATCHER),
+ new MigrationFromHamcrestVisitor());
+ }
+
+ enum Replacement {
+ EQUALTO("equalTo", "assertEquals", "assertNotEquals", "#{any(java.lang.Object)}, #{any(java.lang.Object)}", "examinedObjThenMatcherArgs"),
+ EMPTYARRAY("emptyArray", "assertEquals", "assertNotEquals", "0, #{anyArray(java.lang.Object)}.length", "examinedObjOnly"),
+ HASENTRY("hasEntry", "assertEquals", "assertNotEquals", "#{any(java.lang.Object)}, #{any(java.util.Map)}.get(#{any(java.lang.Object)})", "matcher1ExaminedObjMatcher0"),
+ HASSIZE("hasSize", "assertEquals", "assertNotEquals", "#{any(java.util.Collection)}.size(), #{any(double)}", "examinedObjThenMatcherArgs"),
+ HASTOSTRING("hasToString", "assertEquals", "assertNotEquals", "#{any(java.lang.Object)}.toString(), #{any(java.lang.String)}", "examinedObjThenMatcherArgs"),
+ CLOSETO("closeTo", "assertTrue", "assertFalse", "Math.abs(#{any(double)} - #{any(double)}) < #{any(double)}", "examinedObjThenMatcherArgs"),
+ CONTAINSSTRING("containsString", "assertTrue", "assertFalse", "#{any(java.lang.String)}.contains(#{any(java.lang.String)}", "examinedObjThenMatcherArgs"),
+ EMPTY("empty", "assertTrue", "assertFalse", "#{any(java.util.Collection)}.isEmpty()", "examinedObjOnly"),
+ ENDSWITH("endsWith", "assertTrue", "assertFalse", "#{any(java.lang.String)}.endsWith(#{any(java.lang.String)})", "examinedObjThenMatcherArgs"),
+ EQUALTOIGNORINGCASE("equalToIgnoringCase", "assertTrue", "assertFalse", "#{any(java.lang.String)}.equalsIgnoreCase(#{any(java.lang.String)})", "examinedObjThenMatcherArgs"),
+ GREATERTHAN("greaterThan", "assertTrue", "assertFalse", "#{any(double)} > #{any(double)}", "examinedObjThenMatcherArgs"),
+ GREATERTHANOREQUALTO("greaterThanOrEqualTo", "assertTrue", "assertFalse", "#{any(double)} >= #{any(double)}", "examinedObjThenMatcherArgs"),
+ HASKEY("hasKey", "assertTrue", "assertFalse", "#{any(java.util.Map)}.containsKey(#{any(java.lang.Object)})", "examinedObjThenMatcherArgs"),
+ HASVALUE("hasValue", "assertTrue", "assertFalse", "#{any(java.util.Map)}.containsValue(#{any(java.lang.Object)})", "examinedObjThenMatcherArgs"),
+ LESSTHAN("lessThan", "assertTrue", "assertFalse", "#{any(double)} < #{any(double)}", "examinedObjThenMatcherArgs"),
+ LESSTHANOREQUALTO("lessThanOrEqualTo", "assertTrue", "assertFalse", "#{any(double)} <= #{any(double)}", "examinedObjThenMatcherArgs"),
+ STARTSWITH("startsWith", "assertTrue", "assertFalse", "#{any(java.lang.String)}.startsWith(#{any(java.lang.String)})", "examinedObjThenMatcherArgs"),
+ TYPECOMPATIBLEWITH("typeCompatibleWith", "assertTrue", "assertFalse", "#{any(java.lang.Class)}.isAssignableFrom(#{any(java.lang.Class)})", "matcherArgsThenExaminedObj"),
+ NOTNULLVALUE("notNullValue", "assertNotNull", "assertNull", "#{any(java.lang.Object)}", "examinedObjOnly"),
+ NULLVALUE("nullValue", "assertNull", "assertNotNull", "#{any(java.lang.Object)}", "examinedObjOnly"),
+ SAMEINSTANCE("sameInstance", "assertSame", "assertNotSame", "#{any(java.lang.Object)}, #{any(java.lang.Object)}", "examinedObjThenMatcherArgs"),
+ THEINSTANCE("theInstance", "assertSame", "assertNotSame", "#{any(java.lang.Object)}, #{any(java.lang.Object)}", "examinedObjThenMatcherArgs"),
+ EMPTYITERABLE("emptyIterable", "assertFalse", "assertTrue", "#{any(java.lang.Iterable)}.iterator().hasNext()", "examinedObjOnly");
+
+ final String hamcrest, junitPositive, junitNegative, template;
+ final String argumentsMethod;
+
+ private static final Map
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.testing.hamcrest;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.tree.J;
+
+import java.security.InvalidParameterException;
+import java.util.Objects;
+
+class RemoveNotMatcherVisitor extends JavaIsoVisitor
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.testing.hamcrest;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class HamcrestInstanceOfToJUnit5Test implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9", "hamcrest-2.2"))
+ .recipe(new HamcrestInstanceOfToJUnit5());
+ }
+
+ @DocumentExample
+ @Test
+ void instanceOf() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+ import java.util.List;
+
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.instanceOf;
+ import static org.hamcrest.Matchers.isA;
+ import static org.hamcrest.Matchers.not;
+
+ class ATest {
+ private static final List
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.testing.hamcrest;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class HamcrestMatcherToJUnit5Test implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9", "hamcrest-2.2"))
+ .recipe(new HamcrestMatcherToJUnit5());
+ }
+
+ @Test
+ void equalToObject() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ class Biscuit {
+ String name;
+ Biscuit(String name) {
+ this.name = name;
+ }
+ }
+ """
+ ),
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.equalTo;
+
+ class BiscuitTest {
+ @Test
+ void testEquals() {
+ Biscuit theBiscuit = new Biscuit("Ginger");
+ Biscuit myBiscuit = new Biscuit("Ginger");
+ assertThat(theBiscuit, equalTo(myBiscuit));
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+
+ import static org.junit.jupiter.api.Assertions.assertEquals;
+
+ class BiscuitTest {
+ @Test
+ void testEquals() {
+ Biscuit theBiscuit = new Biscuit("Ginger");
+ Biscuit myBiscuit = new Biscuit("Ginger");
+ assertEquals(theBiscuit, myBiscuit);
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @DocumentExample
+ @Test
+ void equalToString() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.equalTo;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ String str1 = "Hello world!";
+ String str2 = "Hello world!";
+ assertThat(str1, equalTo(str2));
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+
+ import static org.junit.jupiter.api.Assertions.assertEquals;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ String str1 = "Hello world!";
+ String str2 = "Hello world!";
+ assertEquals(str1, str2);
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void notEqualToString() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.equalTo;
+ import static org.hamcrest.Matchers.not;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ String str1 = "Hello world!";
+ String str2 = "Hello world!";
+ assertThat(str1, not(equalTo(str2)));
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+
+ import static org.junit.jupiter.api.Assertions.assertNotEquals;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ String str1 = "Hello world!";
+ String str2 = "Hello world!";
+ assertNotEquals(str1, str2);
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void greaterThan() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.greaterThan;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ int intt = 7;
+ assertThat(10, greaterThan(intt));
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+
+ import static org.junit.jupiter.api.Assertions.assertTrue;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ int intt = 7;
+ assertTrue(10 > intt);
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void greaterThanOrEqualTo() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+
+ class ATest {
+ @Test
+ void testGreaterThanOrEqualTo() {
+ int intt = 7;
+ assertThat(10, greaterThanOrEqualTo(intt));
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+
+ import static org.junit.jupiter.api.Assertions.assertTrue;
+
+ class ATest {
+ @Test
+ void testGreaterThanOrEqualTo() {
+ int intt = 7;
+ assertTrue(10 >= intt);
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void closeTo() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.closeTo;
+
+ class ATest {
+ @Test
+ void testCloseTo() {
+ double dbl = 179.1;
+ assertThat(dbl, closeTo(178.2, 1.0));
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+
+ import static org.junit.jupiter.api.Assertions.assertTrue;
+
+ class ATest {
+ @Test
+ void testCloseTo() {
+ double dbl = 179.1;
+ assertTrue(Math.abs(dbl - 178.2) < 1.0);
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void collections() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+ import java.util.ArrayList;
+ import java.util.Collection;
+
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.empty;
+ import static org.hamcrest.Matchers.hasSize;
+
+ class ATest {
+ private static final Collection
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.testing.hamcrest;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class MigrateHamcrestToJUnitTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9", "hamcrest-2.2"))
+ .recipeFromResource("/META-INF/rewrite/hamcrest.yml", "org.openrewrite.java.testing.hamcrest.MigrateHamcrestToJUnit5");
+ }
+
+ @DocumentExample
+ @Test
+ void equalToString() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.equalTo;
+ import static org.hamcrest.Matchers.is;
+ import static org.hamcrest.Matchers.not;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ String str1 = "Hello world!";
+ String str2 = "Hello world!";
+ assertThat(str1, is(equalTo(str2)));
+ assertThat(str1, is(not(equalTo(str2 + "!"))));
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.junit.jupiter.api.Assertions.assertEquals;
+ import static org.junit.jupiter.api.Assertions.assertNotEquals;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ String str1 = "Hello world!";
+ String str2 = "Hello world!";
+ assertEquals(str1, str2);
+ assertNotEquals(str1, str2 + "!");
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void assertWithLogicOp() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+
+ import static org.hamcrest.MatcherAssert.assertThat;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ int a = 7;
+ int b = 29;
+ assertThat("Not equal", a == b);
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+
+ import static org.junit.jupiter.api.Assertions.assertTrue;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ int a = 7;
+ int b = 29;
+ assertTrue(a == b, "Not equal");
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void assertWithMethodCall() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+
+ import static org.hamcrest.MatcherAssert.assertThat;
+
+ class ATest {
+ @Test
+ void testContains() {
+ String string = "Hello world";
+ assertThat("Does not contain", string.contains("llo wor"));
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+
+ import static org.junit.jupiter.api.Assertions.assertTrue;
+
+ class ATest {
+ @Test
+ void testContains() {
+ String string = "Hello world";
+ assertTrue(string.contains("llo wor"), "Does not contain");
+ }
+ }
+ """
+ )
+ );
+ }
+}
diff --git a/src/test/java/org/openrewrite/java/testing/hamcrest/RemoveNotMatcherTest.java b/src/test/java/org/openrewrite/java/testing/hamcrest/RemoveNotMatcherTest.java
new file mode 100644
index 000000000..418c81a78
--- /dev/null
+++ b/src/test/java/org/openrewrite/java/testing/hamcrest/RemoveNotMatcherTest.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.testing.hamcrest;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+import static org.openrewrite.test.RewriteTest.toRecipe;
+
+class RemoveNotMatcherTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9", "hamcrest-2.2"))
+ .recipe(toRecipe(RemoveNotMatcherVisitor::new));
+ }
+
+ @DocumentExample
+ void nestedNotMatcher() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.equalTo;
+ import static org.hamcrest.Matchers.not;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ String str1 = "Hello world!";
+ String str2 = "Hello world!";
+ assertThat(str1, not(equalTo(str2)));
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.equalTo;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ String str1 = "Hello world!";
+ String str2 = "Hello world!";
+ assertThat(str1, equalTo(str2));
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void notMatcher() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.not;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ String str1 = "Hello world!";
+ String str2 = "Hello world!";
+ assertThat(str1, not(str2));
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+ import static org.hamcrest.MatcherAssert.assertThat;
+ import static org.hamcrest.Matchers.equalTo;
+
+ class ATest {
+ @Test
+ void testEquals() {
+ String str1 = "Hello world!";
+ String str2 = "Hello world!";
+ assertThat(str1, equalTo(str2));
+ }
+ }
+ """
+ )
+ );
+ }
+
+}