01: package org.hanseltest;
02:
03: /**
04: * Class used to test coverage of exception handlers.
05: * @author Niklas Mehner
06: */
07: public class CoverException {
08:
09: /**
10: * Contains simple exception handler.
11: * @param e some int.
12: * @return 5 / e, or -1 if (e = 0).
13: */
14: public int coverHandler(int e) {
15: try {
16: return 5 / e;
17: } catch (ArithmeticException ae) {
18: return -1;
19: }
20: }
21:
22: /**
23: * Test coverage of finally.
24: * @param e some int.
25: * @param cover Wether the try..finally block is executed.
26: * @return if cover 5 / e, or -1 if (e = 0), 0 otherwise.
27: */
28: public int coverFinally(int e, boolean cover) {
29: int result = 0;
30:
31: if (cover) {
32: try {
33: result = 5 / e;
34: } finally {
35: result += 1;
36: }
37: }
38:
39: return result;
40: }
41:
42: /**
43: * Test a more complex try..catch..finally block.
44: * @param e some Exception.
45: * @return 9 if e == null,
46: * 4 if e != null,
47: * 2 if e instanceof RuntimeException.
48: */
49: public int coverComplex(Exception e) {
50: int result = 0;
51:
52: try {
53: if (e != null) {
54: throw e;
55: }
56:
57: result += 1;
58: } catch (RuntimeException re) {
59: result += 2;
60: } catch (Exception exc) {
61: result += 4;
62: } finally {
63: result += 8;
64: }
65:
66: return result;
67: }
68:
69: }
|