Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.openrewrite.marker.Markup;
import org.openrewrite.staticanalysis.LambdaBlockToExpression;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -156,18 +155,32 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
maybeAddImport("org.junit.jupiter.api.Assertions", "assertThrows", false);
}
}
if (cta.timeout != null) {
m = JavaTemplate.builder("@Timeout(value = #{any(long)}, unit = TimeUnit.MILLISECONDS)")
if (cta.timeout != null &&
!J.Literal.isLiteralValue(cta.timeout, 0L) &&
!J.Literal.isLiteralValue(cta.timeout, 0)) {
m = JavaTemplate.builder("org.junit.jupiter.api.function.Executable o = () -> #{};")
.javaParser(javaParser)
.imports("org.junit.jupiter.api.Timeout", "java.util.concurrent.TimeUnit")
.build()
.apply(
updateCursor(m),
m.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)),
cta.timeout
m.getCoordinates().replaceBody(),
m.getBody()
);
maybeAddImport("org.junit.jupiter.api.Timeout");
maybeAddImport("java.util.concurrent.TimeUnit");

assert m.getBody() != null;
J.Lambda lambda = (J.Lambda) ((J.VariableDeclarations) m.getBody().getStatements().get(0))
.getVariables().get(0).getInitializer();

assert lambda != null;

m = JavaTemplate.builder("assertTimeoutPreemptively(Duration.ofMillis(#{any(long)}), #{any(org.junit.jupiter.api.function.Executable)});")
.javaParser(javaParser)
.staticImports("org.junit.jupiter.api.Assertions.assertTimeoutPreemptively")
.imports("java.time.Duration")
.build()
.apply(updateCursor(m), m.getCoordinates().replaceBody(), cta.timeout, lambda);
maybeAddImport("org.junit.jupiter.api.Assertions", "assertTimeoutPreemptively", false);
maybeAddImport("java.time.Duration");
}
maybeAddImport("org.junit.jupiter.api.Test");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ public void test2() {
}

@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/450")
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/1073")
@Test
void annotationWithTimeout() {
//language=java
Expand All @@ -352,15 +353,89 @@ public void test() {
""",
"""
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import java.util.concurrent.TimeUnit;
import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;

public class MyTest {

@Test
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
public void test() {
assertTimeoutPreemptively(Duration.ofMillis(500), () -> {
});
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/1073")
@Test
void timeoutZeroIsNoOpInJUnit4SoAnnotationOnlyMigrated() {
//language=java
rewriteRun(
java(
"""
import org.junit.Test;

public class MyTest {

@Test(timeout = 0)
public void test() {
String foo = "foo";
}
}
""",
"""
import org.junit.jupiter.api.Test;

public class MyTest {

@Test
public void test() {
String foo = "foo";
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/1073")
@Test
void annotationWithTimeoutAndStatements() {
//language=java
rewriteRun(
java(
"""
import org.junit.Test;

public class MyTest {

@Test(timeout = 500)
public void test() {
String foo = "foo";
System.out.println(foo);
}
}
""",
"""
import org.junit.jupiter.api.Test;

import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;

public class MyTest {

@Test
public void test() {
assertTimeoutPreemptively(Duration.ofMillis(500), () -> {
String foo = "foo";
System.out.println(foo);
});
}
}
"""
Expand Down Expand Up @@ -415,6 +490,7 @@ public void test() {
);
}

@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/1073")
@Test
void annotationWithTimeoutAndException() {
//language=java
Expand All @@ -433,19 +509,20 @@ public void test() {
""",
"""
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import java.util.concurrent.TimeUnit;
import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;

public class MyTest {

@Test
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
public void test() {
assertThrows(IllegalArgumentException.class, () -> {
throw new IllegalArgumentException("boom");
assertTimeoutPreemptively(Duration.ofMillis(500), () -> {
assertThrows(IllegalArgumentException.class, () -> {
throw new IllegalArgumentException("boom");
});
});
}
}
Expand Down Expand Up @@ -496,7 +573,6 @@ public void foo() {
);
}


@Test
void migrateDotClass() {
//language=java
Expand Down Expand Up @@ -550,8 +626,8 @@ public void test() {

@Test
void fullyQualified() {
//language=java
rewriteRun(
//language=java
java(
"""
public class MyTest {
Expand All @@ -575,8 +651,8 @@ public void feature1() {

@Test
void mixedFullyQualifiedAndNot() {
//language=java
rewriteRun(
//language=java
java(
"""
import org.junit.Test;
Expand Down