Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/test/org/jnode/test/CoreTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
@SuiteClasses({
NumberUtilsTest.class,
VersionTest.class,
TryFinallyTest.class,
VarArgsTest.class,
}
)
Expand Down
129 changes: 122 additions & 7 deletions core/src/test/org/jnode/test/TryFinallyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading