1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // AbstractTestRuntimeException.java Copyright (c) 2009 Giovanni Azua Garcia
5 // bravegag@hotmail.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 3
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, see <http://www.gnu.org/licenses/>.
19 //
20 //----------------------------------------------------------------------
21 package org.perfectjpattern.support.test;
22
23 import junit.framework.*;
24
25 /**
26 * Abstract base reusable {@link TestCase} for testing user-defined subtypes of
27 * {@link RuntimeException}
28 *
29 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
30 * @version $Revision: 1.0 $Date: Feb 12, 2009 10:06:38 AM $
31 */
32 public abstract
33 class AbstractTestRuntimeException<E extends RuntimeException>
34 extends TestCase
35 {
36 //------------------------------------------------------------------------
37 // public
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 // CHECKSTYLE:OFF
72 catch (RuntimeException anException)
73 // CHECKSTYLE:ON
74 {
75 assertNull(myClassName + " did not work as expected", anException.
76 getMessage());
77 }
78
79 try
80 {
81 throw myException2;
82 }
83 // CHECKSTYLE:OFF
84 catch (RuntimeException anException)
85 // CHECKSTYLE:ON
86 {
87 assertEquals(myClassName + " did not work as expected", myCause2,
88 anException.getMessage());
89 }
90
91 try
92 {
93 throw myException3;
94 }
95 // CHECKSTYLE:OFF
96 catch (RuntimeException anException)
97 // CHECKSTYLE:ON
98 {
99 assertEquals(myClassName + " did not work as expected", myCause3,
100 anException.getCause());
101 }
102
103 try
104 {
105 throw myException4;
106 }
107 // CHECKSTYLE:OFF
108 catch (RuntimeException anException)
109 // CHECKSTYLE:ON
110 {
111 assertEquals(myClassName + " did not work as expected", myCause4b,
112 anException.getCause());
113 }
114 }
115
116 //------------------------------------------------------------------------
117 // members
118 //------------------------------------------------------------------------
119 private final Class<E> theClass;
120 }