Skip to content

Commit

Permalink
Merge branch 'main' into feature/issue636
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Nov 18, 2024
2 parents 8c41ec2 + 93d76cf commit ce3cc0d
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.junit5;

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.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import java.util.ArrayList;
import java.util.List;

public class UseAssertSame extends Recipe {
@Override
public String getDisplayName() {
return "Use JUnit5's `assertSame` or `assertNotSame` instead of `assertTrue(... == ...)`";
}

@Override
public String getDescription() {
return "Prefers the usage of `assertSame` or `assertNotSame` methods instead of using of vanilla assertTrue " +
"or assertFalse with a boolean comparison.";
}

private static final MethodMatcher ASSERT_TRUE_MATCHER = new MethodMatcher("org.junit.jupiter.api.Assertions assertTrue(..)");
private static final MethodMatcher ASSERT_FALSE_MATCHER = new MethodMatcher("org.junit.jupiter.api.Assertions assertFalse(..)");

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
JavaIsoVisitor<ExecutionContext> visitor = new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocation, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(methodInvocation, ctx);
if (!ASSERT_TRUE_MATCHER.matches(mi) && !ASSERT_FALSE_MATCHER.matches(mi)) {
return mi;
}

Expression firstArgument = mi.getArguments().get(0);
if (!(firstArgument instanceof J.Binary)) {
return mi;
}
J.Binary binary = (J.Binary) firstArgument;
if (binary.getOperator() != J.Binary.Type.Equal && binary.getOperator() != J.Binary.Type.NotEqual) {
return mi;
}
List<Expression> newArguments = new ArrayList<>();
newArguments.add(binary.getLeft());
newArguments.add(binary.getRight());
newArguments.addAll(mi.getArguments().subList(1, mi.getArguments().size()));

String newMethodName = binary.getOperator() == J.Binary.Type.Equal == ASSERT_TRUE_MATCHER.matches(mi) ?
"assertSame" : "assertNotSame";

maybeRemoveImport("org.junit.jupiter.api.Assertions");
maybeAddImport("org.junit.jupiter.api.Assertions", newMethodName);

JavaType.Method newType = ((JavaType.Method) mi.getName().getType()).withName(newMethodName);
return mi.withName(mi.getName().withSimpleName(newMethodName).withType(newType))
.withMethodType(newType)
.withArguments(newArguments);
}
};
return Preconditions.check(
Preconditions.or(
new UsesMethod<>(ASSERT_TRUE_MATCHER),
new UsesMethod<>(ASSERT_FALSE_MATCHER)),
visitor);
}

}
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/rewrite/junit5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ recipeList:
# Workaround for https://github.com/testcontainers/testcontainers-java/issues/970:
- org.openrewrite.maven.RemoveExclusion:
groupId: org.testcontainers
artifactId: testcontainers
artifactId: '*'
exclusionGroupId: junit
exclusionArtifactId: junit
# Similar for https://github.com/openrewrite/rewrite-testing-frameworks/issues/477
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void classReference() {
java(
"""
import org.junit.Test;
public class Sample {
void method() {
Class<Test> c = Test.class;
Expand All @@ -64,7 +64,7 @@ void method() {
""",
"""
import org.junit.jupiter.api.Test;
public class Sample {
void method() {
Class<Test> c = Test.class;
Expand All @@ -85,10 +85,10 @@ void assertThatReceiver() {
"""
import org.junit.Assert;
import org.junit.Test;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.containsInAnyOrder;
public class SampleTest {
@SuppressWarnings("ALL")
@Test
Expand All @@ -100,11 +100,11 @@ public void filterShouldRemoveUnusedConfig() {
""",
"""
import org.junit.jupiter.api.Test;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
public class SampleTest {
@SuppressWarnings("ALL")
@Test
Expand Down Expand Up @@ -178,6 +178,30 @@ void dontExcludeJunit4DependencyfromTestcontainers() {
rewriteRun(pomXml(before, before));
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/429")
void dontExcludeJunit4DependencyfromTestcontainersJupiter() {
//language=xml
String before = """
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.jackson</groupId>
<artifactId>test-plugins</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.18.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
""";
// Output identical, but we want to make sure we don't exclude junit4 from testcontainers
rewriteRun(pomXml(before, before));
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/477")
void dontExcludeJunit4DependencyfromSpringBootTestcontainers() {
Expand Down Expand Up @@ -231,7 +255,7 @@ void assertEqualsWithArrayArgumentToAssertArrayEquals() {
java(
"""
import org.junit.Assert;
class MyTest {
void test() {
Assert.assertEquals(new Object[1], new Object[1]);
Expand All @@ -240,7 +264,7 @@ void test() {
""",
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void test() {
Assertions.assertArrayEquals(new Object[1], new Object[1]);
Expand All @@ -261,16 +285,16 @@ void migrateInheritedTestBeforeAfterAnnotations() {
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class AbstractTest {
@Before
public void before() {
}
@After
public void after() {
}
@Test
public void test() {
}
Expand All @@ -280,16 +304,16 @@ public void test() {
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class AbstractTest {
@BeforeEach
public void before() {
}
@AfterEach
public void after() {
}
@Test
public void test() {
}
Expand All @@ -301,10 +325,10 @@ public void test() {
public class A extends AbstractTest {
public void before() {
}
public void after() {
}
public void test() {
}
}
Expand All @@ -313,16 +337,16 @@ public void test() {
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class A extends AbstractTest {
@BeforeEach
public void before() {
}
@AfterEach
public void after() {
}
@Test
public void test() {
}
Expand Down
Loading

0 comments on commit ce3cc0d

Please sign in to comment.