Skip to content

Commit

Permalink
RemoveTryCatchFailBlocks should retain cases with finally
Browse files Browse the repository at this point in the history
Fixes #489
  • Loading branch information
timtebeek committed Mar 2, 2024
1 parent 0d8d129 commit fe49228
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static class RemoveTryCatchBlocksFromUnitsTestsVisitor extends JavaVisit
public J visitTry(J.Try jtry, ExecutionContext ctx) {
J.Try try_ = (J.Try) super.visitTry(jtry, ctx);
// only one catch block, such that we know it's safe to apply this recipe, and doesn't have resources
if (try_.getResources() != null || try_.getCatches().size() != 1) {
if (try_.getResources() != null || try_.getCatches().size() != 1 || try_.getFinally() != null) {
return try_;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -575,4 +576,31 @@ public void testMethod() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/489")
void doesNotRunonTryFinally() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class MyTest {
@Test
public void testMethod() {
try {
int divide = 50 / 0;
} catch (ArithmeticException e) {
Assertions.fail(e.getMessage());
} finally {
System.out.println("Some resource clean up should not be lost");
}
}
}
"""
)
);
}
}

0 comments on commit fe49228

Please sign in to comment.