Skip to content

Commit 2eca150

Browse files
committed
Fix ChangeTestAnnotation not putting {} around some single-line statements in lambda bodies
1 parent d931a18 commit 2eca150

File tree

5 files changed

+106
-20
lines changed

5 files changed

+106
-20
lines changed

src/before/java/org/openrewrite/java/testing/junit5/ExampleJunitTestClass.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.testing.junit5;
1717

1818
import org.junit.AfterClass;
19+
import org.junit.Assert;
1920
import org.junit.BeforeClass;
2021
import org.junit.Rule;
2122
import org.junit.Test;
@@ -50,14 +51,25 @@ public void foo() throws IOException {
5051
throw new RuntimeException(foo);
5152
}
5253

53-
@Test(timeout = 500)
54-
public void bar() { }
54+
@Test(expected = IndexOutOfBoundsException.class)
55+
public void foo2() {
56+
int arr = new int[]{}[0];
57+
}
5558

56-
private File newFolder(File root, String ... folders) throws IOException {
57-
File result = new File(root, String.join("/", folders));
58-
if(!result.mkdirs()) {
59-
throw new IOException("Couldn't create folders " + root);
59+
@Test
60+
public void assertsStuff() {
61+
Assert.assertEquals(1, 1);
62+
Assert.assertArrayEquals(new int[]{}, new int[]{});
63+
Assert.assertNotEquals(1, 2);
64+
Assert.assertFalse(false);
65+
Assert.assertTrue(true);
6066
}
61-
return result;
62-
}
67+
68+
@Test
69+
public void lambdaThrow() {
70+
71+
}
72+
73+
@Test(timeout = 500)
74+
public void bar() { }
6375
}

src/main/java/org/openrewrite/java/testing/junit5/ChangeTestAnnotation.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,21 @@ public J.MethodDecl visitMethod(J.MethodDecl method) {
7575
if(assignParamName.equals("expected")) {
7676
List<Statement> statements = m.getBody().getStatements();
7777

78-
Statement assertBlock = statements.size() == 1 ?
79-
statements.get(0).withPrefix(" ") :
80-
new J.Block<>(
81-
randomId(),
82-
null,
83-
statements,
84-
format(" "),
85-
new J.Block.End(randomId(), format("\n"))
86-
);
78+
// The Java 11 Specification says that lambda bodies can be either a single expression
79+
// or a block. So put a block around anything that isn't exactly one expression.
80+
boolean isSingleExpression = statements.size() == 1 && statements.get(0) instanceof Expression;
81+
Statement assertBlock;
82+
if(isSingleExpression) {
83+
assertBlock = statements.get(0).withPrefix(" ");
84+
} else {
85+
assertBlock = new J.Block<>(
86+
randomId(),
87+
null,
88+
statements,
89+
format(" "),
90+
new J.Block.End(randomId(), format("\n"))
91+
);
92+
}
8793

8894
J.MethodInvocation assertThrows = new J.MethodInvocation(
8995
randomId(),

src/main/resources/META-INF/rewrite/junit5.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ visitors:
4040
type: org.junit.Assume
4141
targetType: org.junit.jupiter.api.Assumptions
4242
---
43+
44+
---
4345
type: specs.openrewrite.org/v1beta/recipe
4446
name: org.openrewrite.java.testing.JUnit5Migration
4547
include:

src/test/kotlin/org/openrewrite/java/testing/junit5/BeforeToAfter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fun main(args: Array<String>) {
3030
.classpath("mockito-all", "junit")
3131
.build()
3232

33-
val visitors = loadVisitorsForTest("org.openrewrite.java.testing.Mockito", "org.openrewrite.java.testing.junit5.migration")
33+
val visitors = loadVisitorsForTest("org.openrewrite.java.testing.JUnit5Migration", "org.openrewrite.java.testing.Mockito1to3Migration")
3434
val sources = parser.parse(listJavaSources(beforeDir), beforeDir)
3535
val changes = Refactor(true)
3636
.visit(visitors)

src/test/kotlin/org/openrewrite/java/testing/junit5/ChangeTestAnnotationTest.kt

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,76 @@ class ChangeTestAnnotationTest: RefactorVisitorTestForParser<J.CompilationUnit>
5151
5252
@Test
5353
public void test() {
54-
assertThrows(IllegalArgumentException.class, () -> throw new IllegalArgumentException("boom"));
54+
assertThrows(IllegalArgumentException.class, () -> {
55+
throw new IllegalArgumentException("boom");
56+
});
5557
}
5658
}
5759
""".trimIndent()
5860
)
5961

62+
@Test
63+
fun assertThrowsSingleStatement() = assertRefactored(
64+
before = """
65+
import org.junit.Test;
66+
67+
public class A {
68+
69+
@Test(expected = IndexOutOfBoundsException.class)
70+
public void test() {
71+
int arr = new int[]{}[0];
72+
}
73+
}
74+
""",
75+
after = """
76+
import org.junit.jupiter.api.Test;
77+
78+
import static org.junit.jupiter.api.Assertions.assertThrows;
79+
80+
public class A {
81+
82+
@Test
83+
public void test() {
84+
assertThrows(IndexOutOfBoundsException.class, () -> {
85+
int arr = new int[]{}[0];
86+
});
87+
}
88+
}
89+
"""
90+
)
91+
92+
@Test
93+
fun assertThrowsMultiLine() = assertRefactored(
94+
before = """
95+
import org.junit.Test;
96+
97+
public class A {
98+
99+
@Test(expected = IllegalArgumentException.class)
100+
public void test() {
101+
String foo = "foo";
102+
throw new IllegalArgumentException("boom");
103+
}
104+
}
105+
""",
106+
after = """
107+
import org.junit.jupiter.api.Test;
108+
109+
import static org.junit.jupiter.api.Assertions.assertThrows;
110+
111+
public class A {
112+
113+
@Test
114+
public void test() {
115+
assertThrows(IllegalArgumentException.class, () -> {
116+
String foo = "foo";
117+
throw new IllegalArgumentException("boom");
118+
});
119+
}
120+
}
121+
"""
122+
)
123+
60124
@Test
61125
fun noTestAnnotationValues() = assertRefactored(
62126
before = """
@@ -127,7 +191,9 @@ class ChangeTestAnnotationTest: RefactorVisitorTestForParser<J.CompilationUnit>
127191
@Test
128192
@Timeout(500)
129193
public void test() {
130-
assertThrows(IllegalArgumentException.class, () -> throw new IllegalArgumentException("boom"));
194+
assertThrows(IllegalArgumentException.class, () -> {
195+
throw new IllegalArgumentException("boom");
196+
});
131197
}
132198
}
133199
"""

0 commit comments

Comments
 (0)