KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > CoverException


1 package org.hanseltest;
2
3 /**
4  * Class used to test coverage of exception handlers.
5  * @author Niklas Mehner
6  */

7 public class CoverException {
8
9     /**
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 JavaDoc 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 JavaDoc e) {
50         int result = 0;
51
52         try {
53              if (e != null) {
54                 throw e;
55              }
56
57              result += 1;
58         } catch (RuntimeException JavaDoc re) {
59             result += 2;
60         } catch (Exception JavaDoc exc) {
61             result += 4;
62         } finally {
63             result += 8;
64         }
65
66         return result;
67     }
68
69 }
70
Popular Tags