Skip to content

Commit

Permalink
Avoid duplicate before/after annotations in JUnit 3 to 5 (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Aug 7, 2024
1 parent ad847c6 commit 519ebcb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
public class MigrateJUnitTestCase extends Recipe {

private static final AnnotationMatcher JUNIT_TEST_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.Test");
private static final AnnotationMatcher JUNIT_AFTER_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.*After*");
private static final AnnotationMatcher JUNIT_BEFORE_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.*Before*");

private static boolean isSupertypeTestCase(@Nullable JavaType.FullyQualified fullyQualified) {
if (fullyQualified == null || fullyQualified.getSupertype() == null || "java.lang.Object".equals(fullyQualified.getFullyQualifiedName())) {
Expand Down Expand Up @@ -123,9 +125,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
updateCursor(md);
if (md.getSimpleName().startsWith("test") && md.getLeadingAnnotations().stream().noneMatch(JUNIT_TEST_ANNOTATION_MATCHER::matches)) {
md = updateMethodDeclarationAnnotationAndModifier(md, "@Test", "org.junit.jupiter.api.Test", ctx);
} else if ("setUp".equals(md.getSimpleName())) {
} else if ("setUp".equals(md.getSimpleName()) && md.getLeadingAnnotations().stream().noneMatch(JUNIT_BEFORE_ANNOTATION_MATCHER::matches)) {
md = updateMethodDeclarationAnnotationAndModifier(md, "@BeforeEach", "org.junit.jupiter.api.BeforeEach", ctx);
} else if ("tearDown".equals(md.getSimpleName())) {
} else if ("tearDown".equals(md.getSimpleName()) && md.getLeadingAnnotations().stream().noneMatch(JUNIT_AFTER_ANNOTATION_MATCHER::matches)) {
md = updateMethodDeclarationAnnotationAndModifier(md, "@AfterEach", "org.junit.jupiter.api.AfterEach", ctx);
}
return md;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ void convertTestCase() {
java(
"""
import junit.framework.TestCase;
public class MathTest extends TestCase {
protected long value1;
protected long value2;
@Override
protected void setUp() {
super.setUp();
value1 = 2;
value2 = 3;
}
public void testAdd() {
setName("primitive test");
long result = value1 + value2;
assertEquals(5, result);
fail("some Failure message");
}
@Override
protected void tearDown() {
super.tearDown();
Expand All @@ -73,27 +73,27 @@ protected void tearDown() {
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class MathTest {
protected long value1;
protected long value2;
@BeforeEach
public void setUp() {
value1 = 2;
value2 = 3;
}
@Test
public void testAdd() {
//setName("primitive test");
long result = value1 + value2;
assertEquals(5, result);
fail("some Failure message");
}
@AfterEach
public void tearDown() {
value1 = 0;
Expand All @@ -116,7 +116,7 @@ void convertExtendedTestCase() {
public abstract class CTest extends TestCase {
@Override
public void setUp() {}
@Override
public void tearDown() {}
}
Expand All @@ -125,11 +125,11 @@ public void tearDown() {}
package com.abc;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
public abstract class CTest {
@BeforeEach
public void setUp() {}
@AfterEach
public void tearDown() {}
}
Expand All @@ -143,18 +143,18 @@ public void tearDown() {}
public class MathTest extends CTest {
protected long value1;
protected long value2;
@Override
protected void setUp() {
value1 = 2;
value2 = 3;
}
public void testAdd() {
long result = value1 + value2;
assertEquals(5, result);
}
@Override
protected void tearDown() {
value1 = 0;
Expand All @@ -168,25 +168,25 @@ protected void tearDown() {
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MathTest extends CTest {
protected long value1;
protected long value2;
@BeforeEach
public void setUp() {
value1 = 2;
value2 = 3;
}
@Test
public void testAdd() {
long result = value1 + value2;
assertEquals(5, result);
}
@AfterEach
public void tearDown() {
value1 = 0;
Expand All @@ -205,9 +205,9 @@ void notTestCaseHasTestCaseAssertion() {
java(
"""
import org.junit.Test;
import static junit.framework.TestCase.assertTrue;
class AaTest {
@Test
public void someTest() {
Expand All @@ -220,9 +220,9 @@ private boolean isSameStuff(String stuff) {
""",
"""
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
class AaTest {
@Test
public void someTest() {
Expand All @@ -244,9 +244,9 @@ void notTestCaseHasAssertAssertion() {
java(
"""
import org.junit.Test;
import static junit.framework.Assert.assertTrue;
class AaTest {
@Test
public void someTest() {
Expand All @@ -259,9 +259,9 @@ private boolean isSameStuff(String stuff) {
""",
"""
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
class AaTest {
@Test
public void someTest() {
Expand Down Expand Up @@ -293,4 +293,48 @@ public void testSomeNumberStuff() {
)
);
}

@Test
void avoidDuplicateAnnotations(){
rewriteRun(
spec -> spec.recipes(
new MigrateJUnitTestCase(),
new UpdateBeforeAfterAnnotations()
),
//language=java
java(
"""
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
public class MathTest extends TestCase {
@Before
public void setUp() {
}
@After
public void tearDown() {
}
}
""",
"""
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
public class MathTest {
@BeforeEach
public void setUp() {
}
@AfterEach
public void tearDown() {
}
}
"""
)
);
}
}

0 comments on commit 519ebcb

Please sign in to comment.