diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/AssertEqualsBooleanToAssertBoolean.java b/src/main/java/org/openrewrite/java/testing/cleanup/AssertEqualsBooleanToAssertBoolean.java new file mode 100644 index 000000000..0627f145c --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/cleanup/AssertEqualsBooleanToAssertBoolean.java @@ -0,0 +1,109 @@ +/* + * Copyright 2022 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.cleanup;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.JavaVisitor;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JavaType;
+import org.openrewrite.java.tree.Statement;
+
+public class AssertEqualsBooleanToAssertBoolean extends Recipe {
+ private static final MethodMatcher ASSERT_EQUALS = new MethodMatcher(
+ "org.junit.jupiter.api.Assertions assertEquals(..)");
+
+ @Override
+ public String getDisplayName() {
+ return "Replace JUnit `assertEquals(false,
+ * 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.cleanup;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.JavaVisitor;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JavaType;
+import org.openrewrite.java.tree.Statement;
+
+public class AssertNotEqualsBooleanToAssertBoolean extends Recipe {
+ private static final MethodMatcher ASSERT_NOT_EQUALS = new MethodMatcher(
+ "org.junit.jupiter.api.Assertions assertNotEquals(..)");
+
+ @Override
+ public String getDisplayName() {
+ return "Replace JUnit `assertNotEquals(false,
+ * 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.cleanup;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.Issue;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class AssertEqualsBooleanToAssertBooleanTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9"))
+ .recipe(new AssertEqualsBooleanToAssertBoolean());
+ }
+
+ @SuppressWarnings({"ConstantConditions"})
+ @Test
+ void assertEqualsFalseToToAssertFalse() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import static org.junit.jupiter.api.Assertions.assertEquals;
+
+ public class Test {
+ void test() {
+ String a = "a";
+ String c = "c";
+ assertEquals(false, a.equals(c));
+ assertEquals(false, a.equals(c), "message");
+ }
+ }
+ """,
+ """
+ import static org.junit.jupiter.api.Assertions.assertFalse;
+
+ public class Test {
+ void test() {
+ String a = "a";
+ String c = "c";
+ assertFalse(a.equals(c));
+ assertFalse(a.equals(c), "message");
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/206")
+ @SuppressWarnings({"ConstantConditions", "SimplifiableAssertion"})
+ @Test
+ void preserveStyleOfStaticImportOrNot() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Assertions;
+
+ public class Test {
+ void test() {
+ String a = "a";
+ String c = "c";
+ Assertions.assertEquals(false, a.equals(c), "message");
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Assertions;
+
+ public class Test {
+ void test() {
+ String a = "a";
+ String c = "c";
+ Assertions.assertFalse(a.equals(c), "message");
+ }
+ }
+ """
+ )
+ );
+ }
+}
diff --git a/src/test/java/org/openrewrite/java/testing/cleanup/AssertNotEqualsBooleanToAssertBooleanTest.java b/src/test/java/org/openrewrite/java/testing/cleanup/AssertNotEqualsBooleanToAssertBooleanTest.java
new file mode 100644
index 000000000..b9dde6291
--- /dev/null
+++ b/src/test/java/org/openrewrite/java/testing/cleanup/AssertNotEqualsBooleanToAssertBooleanTest.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2021 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.cleanup;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.Issue;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class AssertNotEqualsBooleanToAssertBooleanTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9"))
+ .recipe(new AssertNotEqualsBooleanToAssertBoolean());
+ }
+
+ @SuppressWarnings({"ConstantConditions"})
+ @Test
+ void assertEqualsFalseToToAssertFalse() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import static org.junit.jupiter.api.Assertions.assertNotEquals;
+
+ public class Test {
+ void test() {
+ String a = "a";
+ String c = "c";
+ assertNotEquals(false, a.equals(c));
+ assertNotEquals(false, a.equals(c), "message");
+ }
+ }
+ """,
+ """
+ import static org.junit.jupiter.api.Assertions.assertTrue;
+
+ public class Test {
+ void test() {
+ String a = "a";
+ String c = "c";
+ assertTrue(a.equals(c));
+ assertTrue(a.equals(c), "message");
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/206")
+ @SuppressWarnings({"ConstantConditions", "SimplifiableAssertion"})
+ @Test
+ void preserveStyleOfStaticImportOrNot() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Assertions;
+
+ public class Test {
+ void test() {
+ String a = "a";
+ String c = "c";
+ Assertions.assertNotEquals(false, a.equals(c), "message");
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Assertions;
+
+ public class Test {
+ void test() {
+ String a = "a";
+ String c = "c";
+ Assertions.assertTrue(a.equals(c), "message");
+ }
+ }
+ """
+ )
+ );
+ }
+}