diff --git a/core/src/test/org/jnode/test/CoreTestSuite.java b/core/src/test/org/jnode/test/CoreTestSuite.java index 2ca0e710d..cada15e21 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, VarArgsTest.class, } ) diff --git a/core/src/test/org/jnode/test/TryFinallyTest.java b/core/src/test/org/jnode/test/TryFinallyTest.java index 78866651f..38f4e2cd6 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); + } }