From 58013042d42dffa83cb268b7fc66a9d701b3e6d9 Mon Sep 17 00:00:00 2001 From: "opencode-agent[bot]" Date: Mon, 8 Jun 2026 11:10:15 +0000 Subject: [PATCH] TryFinallyTest converted to JUnit4, 9 tests pass Co-authored-by: LSantha --- .../test/org/jnode/test/CoreTestSuite.java | 1 + .../test/org/jnode/test/TryFinallyTest.java | 129 +++++++++++++++++- 2 files changed, 123 insertions(+), 7 deletions(-) diff --git a/core/src/test/org/jnode/test/CoreTestSuite.java b/core/src/test/org/jnode/test/CoreTestSuite.java index f56f5066eb..0161b52100 100644 --- a/core/src/test/org/jnode/test/CoreTestSuite.java +++ b/core/src/test/org/jnode/test/CoreTestSuite.java @@ -35,6 +35,7 @@ @SuiteClasses({ NumberUtilsTest.class, VersionTest.class, + TryFinallyTest.class, } ) public class CoreTestSuite { diff --git a/core/src/test/org/jnode/test/TryFinallyTest.java b/core/src/test/org/jnode/test/TryFinallyTest.java index 78866651f7..38f4e2cd6c 100644 --- a/core/src/test/org/jnode/test/TryFinallyTest.java +++ b/core/src/test/org/jnode/test/TryFinallyTest.java @@ -17,23 +17,138 @@ * along with this library; If not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ - + package org.jnode.test; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; -/** - * @author Ewout Prangsma (epr@users.sourceforge.net) - */ public class TryFinallyTest { - public void test() { - int i; + @Test + public void testFinallyOnNormalCompletion() { + int i = 0; + try { + i = 1; + } finally { + i = 5; + } + assertEquals(5, i); + } + + @Test + public void testFinallyOnException() { + int i = 0; + try { + i = 1; + throw new RuntimeException("test"); + } catch (RuntimeException e) { + // expected + } finally { + i = 5; + } + assertEquals(5, i); + } + + @Test + public void testFinallyOnReturnInTry() { + int i = 0; + try { + i = 1; + return; + } finally { + i = 5; + } + } + + @Test + public void testFinallyOnReturnInTryWithValue() { + final int[] result = new int[1]; + int value = runWithReturn(result); + assertEquals(5, result[0]); + } + + private int runWithReturn(int[] result) { + try { + result[0] = 1; + return 1; + } finally { + result[0] = 5; + } + } + + @Test + public void testFinallyOnBreak() { + int i = 0; + for (int j = 0; j < 1; j++) { + try { + i = 1; + break; + } finally { + i = 5; + } + } + assertEquals(5, i); + } + + @Test + public void testFinallyOnContinue() { + int i = 0; + for (int j = 0; j < 1; j++) { + try { + i = 1; + continue; + } finally { + i = 5; + } + } + assertEquals(5, i); + } + + @Test + public void testFinallyWithNestedTryFinally() { + int i = 0; try { - i = 0; + try { + i = 1; + } finally { + i = 2; + } } finally { i = 5; } + assertEquals(5, i); } + @Test(expected = RuntimeException.class) + public void testFinallyWithExceptionInFinally() { + int i = 0; + try { + i = 1; + } finally { + i = 5; + throw new RuntimeException("from finally"); + } + } + @Test + public void testMultipleFinallyBlocks() { + int i = 0; + try { + try { + i = 1; + } finally { + i = 2; + } + try { + i = 3; + } finally { + i = 4; + } + } finally { + i = 5; + } + assertEquals(5, i); + } }