1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.perfectjpattern.support.test;
22
23 import junit.framework.*;
24
25
26
27
28
29
30
31
32 public abstract
33 class AbstractTestRuntimeException<E extends RuntimeException>
34 extends TestCase
35 {
36
37
38
39 public
40 AbstractTestRuntimeException(Class<E> aClass)
41 {
42 assert aClass != null : "'aClass' must not be null";
43
44 theClass = aClass;
45 }
46
47
48 public void
49 testException()
50 throws Exception
51 {
52 String myClassName = theClass.getSimpleName();
53
54 String myCause2 = "Database corrupt";
55 Throwable myCause3 = new RuntimeException("Disconnected");
56 String myCause4a = "Illegal Statement";
57 Throwable myCause4b = new RuntimeException(myCause4a);
58
59 RuntimeException myException1 = theClass.newInstance();
60 RuntimeException myException2 = theClass.getDeclaredConstructor(String.
61 class).newInstance(myCause2);
62 RuntimeException myException3 = theClass.getDeclaredConstructor(
63 Throwable.class).newInstance(myCause3);
64 RuntimeException myException4 = theClass.getDeclaredConstructor(
65 String.class, Throwable.class).newInstance(myCause4a, myCause4b);
66
67 try
68 {
69 throw myException1;
70 }
71
72 catch (RuntimeException anException)
73
74 {
75 assertNull(myClassName + " did not work as expected", anException.
76 getMessage());
77 }
78
79 try
80 {
81 throw myException2;
82 }
83
84 catch (RuntimeException anException)
85
86 {
87 assertEquals(myClassName + " did not work as expected", myCause2,
88 anException.getMessage());
89 }
90
91 try
92 {
93 throw myException3;
94 }
95
96 catch (RuntimeException anException)
97
98 {
99 assertEquals(myClassName + " did not work as expected", myCause3,
100 anException.getCause());
101 }
102
103 try
104 {
105 throw myException4;
106 }
107
108 catch (RuntimeException anException)
109
110 {
111 assertEquals(myClassName + " did not work as expected", myCause4b,
112 anException.getCause());
113 }
114 }
115
116
117
118
119 private final Class<E> theClass;
120 }