diff --git a/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java b/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java index 147cd9ed7..bd828d288 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatement.java @@ -16,6 +16,7 @@ package org.openrewrite.java.testing.junit5; import lombok.Getter; +import org.jspecify.annotations.Nullable; import org.openrewrite.*; import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.StringUtils; @@ -32,6 +33,8 @@ import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker; import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -113,6 +116,14 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl J.Block body = (J.Block) lambda.getBody(); List lambdaStatements = body.getStatements(); + if (lambdaStatements.isEmpty()) { + return methodStatement; + } + + Predicate mustStayInLambda = mustStayInLambda(arguments.get(0), m); + if (lambdaStatements.subList(0, lambdaStatements.size() - 1).stream().anyMatch(s -> mayThrow(s, mustStayInLambda))) { + return methodStatement; + } // TODO Check to see if last line in lambda does not use a non-final variable @@ -124,7 +135,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl List variableAssignments = new ArrayList<>(); Space prefix = methodStatement.getPrefix().withComments(emptyList()); - final Statement newLambdaStatement = extractExpressionArguments(body, lambdaStatement, variableAssignments, prefix); + final Statement newLambdaStatement = extractExpressionArguments(body, lambdaStatement, mustStayInLambda, variableAssignments, prefix); J.MethodInvocation newAssertThrows = methodInvocation.withArguments( ListUtils.map(arguments, (argIdx, argument) -> { // The second argument is the lambda which is tested. @@ -151,7 +162,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl }))); } - private Statement extractExpressionArguments(J.Block body, Statement lambdaStatement, List precedingVars, Space varPrefix) { + private Statement extractExpressionArguments(J.Block body, Statement lambdaStatement, Predicate mustStayInLambda, List precedingVars, Space varPrefix) { if (lambdaStatement instanceof J.MethodInvocation) { boolean kotlin = getCursor().firstEnclosing(K.CompilationUnit.class) != null; J.MethodInvocation mi = (J.MethodInvocation) lambdaStatement; @@ -170,6 +181,11 @@ private Statement extractExpressionArguments(J.Block body, Statement lambdaState return e; } + String typeName = getTypeName(type); + if ((!kotlin && typeName == null) || mayThrow(e, mustStayInLambda)) { + return e; + } + String variableName = getVariableName(e, generatedVariableSuffixes); // Kotlin infers the type; add `val name = expr` as a statement on the method body, since replacing @@ -186,35 +202,14 @@ private Statement extractExpressionArguments(J.Block body, Statement lambdaState return varDecl.getVariables().get(0).getName().withPrefix(e.getPrefix()).withType(e.getType()); } - Object variableTypeShort = "Object"; - JavaType variableTypeFqn = null; - if (type instanceof JavaType.Primitive) { - variableTypeShort = type.toString(); - variableTypeFqn = type; - } else if (type instanceof JavaType.Parameterized) { - JavaType.Parameterized paramType = (JavaType.Parameterized) type; - // TODO look into possibly employing `TypeUtils.toString()` here, possibly with some changes upstream allowing for non-fully-qualified names - variableTypeShort = buildParameterizedTypeName(paramType); - variableTypeFqn = paramType; - maybeAddImport(paramType.getFullyQualifiedName()); - for (JavaType typeParam : paramType.getTypeParameters()) { - if (typeParam instanceof JavaType.FullyQualified) { - maybeAddImport(((JavaType.FullyQualified) typeParam).getFullyQualifiedName()); - } - } - } else if (type instanceof JavaType.FullyQualified) { - JavaType.FullyQualified aClass = (JavaType.FullyQualified) type; - variableTypeShort = aClass.getClassName(); - variableTypeFqn = aClass; - maybeAddImport(aClass.getFullyQualifiedName()); - } + maybeAddImports(type); Cursor blockCursor = new Cursor(getCursor(), body); Cursor c = new Cursor(blockCursor, lambdaStatement); try { - J.VariableDeclarations varDecl = JavaTemplate.apply("#{} #{} = #{any()};", c, lambdaStatement.getCoordinates().replace(), variableTypeShort, variableName, e); - precedingVars.add(varDecl.withPrefix(varPrefix).withType(variableTypeFqn)); - return varDecl.getVariables().get(0).getName().withPrefix(e.getPrefix()).withType(variableTypeFqn); + J.VariableDeclarations varDecl = JavaTemplate.apply("#{} #{} = #{any()};", c, lambdaStatement.getCoordinates().replace(), typeName, variableName, e); + precedingVars.add(varDecl.withPrefix(varPrefix).withType(type)); + return varDecl.getVariables().get(0).getName().withPrefix(e.getPrefix()).withType(type); } catch (Exception ex) { // Some types (e.g. anonymous or local classes) don't render as a valid variable // declaration; leave the argument inline rather than failing the whole recipe run @@ -225,6 +220,52 @@ private Statement extractExpressionArguments(J.Block body, Statement lambdaState return lambdaStatement; } + // Moving a call out of the lambda makes the test fail when it throws the expected exception, and breaks + // compilation when it declares a checked exception the test method doesn't + private Predicate mustStayInLambda(Expression exceptionClass, J.MethodDeclaration testMethod) { + JavaType.Parameterized classType = TypeUtils.asParameterized(exceptionClass.getType()); + JavaType expected = classType != null && classType.getTypeParameters().size() == 1 ? classType.getTypeParameters().get(0) : null; + boolean kotlin = getCursor().firstEnclosing(K.CompilationUnit.class) != null; + List handled = testMethod.getThrows() == null ? emptyList() : testMethod.getThrows(); + return thrown -> expected == null || + TypeUtils.isAssignableTo(expected, thrown) || + TypeUtils.isAssignableTo(thrown, expected) || + !kotlin && isChecked(thrown) && handled.stream().noneMatch(h -> TypeUtils.isAssignableTo(h.getType(), thrown)); + } + + private boolean isChecked(JavaType exception) { + return !TypeUtils.isAssignableTo("java.lang.RuntimeException", exception) && + !TypeUtils.isAssignableTo("java.lang.Error", exception); + } + + private boolean mayThrow(J tree, Predicate mustStayInLambda) { + return new JavaIsoVisitor() { + @Override + public J preVisit(J t, AtomicBoolean found) { + if (found.get()) { + stopAfterPreVisit(); + } else if (t instanceof J.MethodInvocation || t instanceof J.NewClass) { + JavaType.Method methodType = ((MethodCall) t).getMethodType(); + found.set(methodType == null || methodType.getThrownExceptions().stream().anyMatch(mustStayInLambda)); + } + return t; + } + }.reduce(tree, new AtomicBoolean()).get(); + } + + private void maybeAddImports(JavaType type) { + if (type instanceof JavaType.Array) { + maybeAddImports(((JavaType.Array) type).getElemType()); + } else if (type instanceof JavaType.FullyQualified) { + maybeAddImport((JavaType.FullyQualified) type); + if (type instanceof JavaType.Parameterized) { + for (JavaType typeParam : ((JavaType.Parameterized) type).getTypeParameters()) { + maybeAddImports(typeParam); + } + } + } + } + private String getVariableName(Expression e, Map generatedVariableSuffixes) { String variableName; if (e instanceof J.MethodInvocation) { @@ -257,33 +298,34 @@ private String getTypeBasedVariableName(J.MethodInvocation mi) { return "x"; } - private String buildParameterizedTypeName(JavaType.Parameterized paramType) { - StringBuilder sb = new StringBuilder(paramType.getClassName()); + private @Nullable String buildParameterizedTypeName(JavaType.Parameterized paramType) { List typeParams = paramType.getTypeParameters(); - if (!typeParams.isEmpty()) { - sb.append("<"); - for (int i = 0; i < typeParams.size(); i++) { - if (i > 0) { - sb.append(", "); - } - sb.append(getTypeName(typeParams.get(i))); + if (typeParams.isEmpty()) { + return paramType.getClassName(); + } + StringJoiner joiner = new StringJoiner(", ", paramType.getClassName() + "<", ">"); + for (JavaType typeParam : typeParams) { + String typeParamName = getTypeName(typeParam); + if (typeParamName == null) { + return null; } - sb.append(">"); + joiner.add(typeParamName); } - return sb.toString(); + return joiner.toString(); } - private String getTypeName(JavaType type) { + private @Nullable String getTypeName(JavaType type) { if (type instanceof JavaType.Parameterized) { return buildParameterizedTypeName((JavaType.Parameterized) type); - } else if (type instanceof JavaType.FullyQualified) { + } else if (type instanceof JavaType.FullyQualified && !(type instanceof JavaType.Unknown)) { return ((JavaType.FullyQualified) type).getClassName(); } else if (type instanceof JavaType.Primitive) { return type.toString(); } else if (type instanceof JavaType.Array) { - return getTypeName(((JavaType.Array) type).getElemType()) + "[]"; + String elemTypeName = getTypeName(((JavaType.Array) type).getElemType()); + return elemTypeName == null ? null : elemTypeName + "[]"; } - return "Object"; + return null; } private String ensureUniqueVariableName(String variableName, Map generatedVariableSuffixes) { diff --git a/src/test/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatementTest.java b/src/test/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatementTest.java index 6cd1e5221..72fe410a9 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatementTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AssertThrowsOnLastStatementTest.java @@ -1284,7 +1284,6 @@ void test() throws Exception { ); } - @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/2861") @Test void argumentWithAnonymousClassTypeIsLeftInline() { // An anonymous (or local) class type renders to an invalid variable declaration (e.g. `MyTest.1 x = ...`); @@ -1340,4 +1339,235 @@ void consume(Object o) {} ) ); } + + @Test + void argumentDeclaringExpectedExceptionStaysInline() { + rewriteRun( + //language=java + java( + """ + import java.io.IOException; + import java.io.UnsupportedEncodingException; + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.*; + + class MyTest { + @Test + void test() { + assertThrows(UnsupportedEncodingException.class, () -> { + String probe = "A string"; + assertArrayEquals(probe.getBytes(), decode(probe.getBytes(), "junk").getBytes()); + }); + } + static String decode(byte[] bytes, String encoding) throws IOException { + return new String(bytes, encoding); + } + } + """, + """ + import java.io.IOException; + import java.io.UnsupportedEncodingException; + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.*; + + class MyTest { + @Test + void test() { + String probe = "A string"; + byte[] bytes = probe.getBytes(); + assertThrows(UnsupportedEncodingException.class, () -> + assertArrayEquals(bytes, decode(probe.getBytes(), "junk").getBytes())); + } + static String decode(byte[] bytes, String encoding) throws IOException { + return new String(bytes, encoding); + } + } + """ + ) + ); + } + + @Test + void argumentDeclaringUnhandledCheckedExceptionStaysInline() { + rewriteRun( + //language=java + java( + """ + import java.io.IOException; + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.*; + + class MyTest { + @Test + void test() { + assertThrows(IllegalArgumentException.class, () -> { + testThing(read(), getA()); + }); + } + String read() throws IOException { return ""; } + String getA() { return "A"; } + void testThing(String one, String two) {} + } + """, + """ + import java.io.IOException; + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.*; + + class MyTest { + @Test + void test() { + String a = getA(); + assertThrows(IllegalArgumentException.class, () -> + testThing(read(), a)); + } + String read() throws IOException { return ""; } + String getA() { return "A"; } + void testThing(String one, String two) {} + } + """ + ) + ); + } + + @Test + void argumentDeclaringCheckedExceptionHandledByTestMethodIsExtracted() { + rewriteRun( + //language=java + java( + """ + import java.io.IOException; + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.*; + + class MyTest { + @Test + void test() throws Exception { + assertThrows(IllegalArgumentException.class, () -> { + testThing(read()); + }); + } + String read() throws IOException { return ""; } + void testThing(String one) {} + } + """, + """ + import java.io.IOException; + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.*; + + class MyTest { + @Test + void test() throws Exception { + String read = read(); + assertThrows(IllegalArgumentException.class, () -> + testThing(read)); + } + String read() throws IOException { return ""; } + void testThing(String one) {} + } + """ + ) + ); + } + + @Test + void leadingStatementDeclaringExpectedExceptionIsNotChanged() { + rewriteRun( + //language=java + java( + """ + import java.io.IOException; + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.*; + + class MyTest { + @Test + void test() throws IOException { + assertThrows(IOException.class, () -> { + read(); + close(); + }); + } + void read() throws IOException {} + void close() {} + } + """ + ) + ); + } + + @Test + void emptyLambdaIsNotChanged() { + rewriteRun( + //language=java + java( + """ + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.*; + + class MyTest { + @Test + void test() { + assertThrows(RuntimeException.class, () -> {}); + } + } + """ + ) + ); + } + + @Test + void unrelatedBlockLambdaIsKept() { + rewriteRun( + //language=java + java( + """ + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.*; + + class MyTest { + @Test + void test() { + Runnable runnable = () -> { + foo(); + }; + assertThrows(RuntimeException.class, () -> { + foo(); + runnable.run(); + }); + } + void foo() {} + } + """, + """ + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.*; + + class MyTest { + @Test + void test() { + Runnable runnable = () -> { + foo(); + }; + foo(); + assertThrows(RuntimeException.class, () -> + runnable.run()); + } + void foo() {} + } + """ + ) + ); + } }