1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package junitx.ddtunit;
39
40 import java.lang.reflect.InvocationTargetException;
41
42 import junit.framework.AssertionFailedError;
43 import junit.framework.TestCase;
44 import junitx.ddtunit.data.ExceptionAsserter;
45 import junitx.ddtunit.data.ObjectAsserter;
46 import junitx.ddtunit.data.TestClusterDataSet;
47 import junitx.ddtunit.data.processing.ClusterDataSetTestCreator;
48
49
50
51
52
53
54 public class ExceptionHandlerTest extends TestCase {
55 private ExceptionHandler handler;
56
57 private String testClass;
58
59 private String testMethod;
60
61 private String testId;
62
63 private TestClusterDataSet classDataSet;
64
65 private static final String LF = System.getProperty("line.separator");
66
67
68
69
70
71
72 public ExceptionHandlerTest(String name) {
73 super(name);
74 }
75
76
77
78
79 protected void setUp() throws Exception {
80 testClass = "ExceptionHandlerTest";
81 testMethod = "methodName";
82 testId = "testId";
83 classDataSet = ClusterDataSetTestCreator.getClusterDataSet(testClass,
84 testMethod, testId);
85 handler = new ExceptionHandler(testMethod);
86 }
87
88
89
90
91
92
93 public void testProcessUnknownException() throws Throwable {
94 String message = "Uncaught exception";
95 try {
96 handler.process(testId, new DDTException(message),
97 this.classDataSet.getAssertMap(this.testMethod, this.testId));
98 handler.summarizeProblems(this.classDataSet.size(this.testMethod));
99 fail("Should throw unexpected exception.");
100 } catch (DDTException ex) {
101 assertEquals("Wrong exception message", message, ex.getMessage());
102 }
103 }
104
105
106
107
108
109
110 public void testProcessInvocationTargetExNoAsserts() throws Throwable {
111 String message = "Uncaught exception";
112 try {
113 Exception testEx = new InvocationTargetException(new DDTException(
114 message));
115
116 handler.process(testId, testEx, null);
117 handler.summarizeProblems(this.classDataSet.size(this.testMethod));
118 fail("Should throw unexpected exception.");
119 } catch (DDTException ex) {
120 assertEquals("Wrong exception message", message, ex.getMessage());
121 }
122 }
123
124
125
126
127
128
129 public void testProcessExpectedException() {
130 try {
131 String exceptionId = "exception0";
132 String exceptionMessage = "Expected exception";
133 ExceptionAsserter assertObject = new ExceptionAsserter(exceptionId,
134 "java.lang.Exception", "ISEQUAL");
135
136 assertObject.setValue(new Exception(exceptionMessage));
137 this.classDataSet.getTestDataSet(this.testMethod, testId)
138 .getAssertMap().put(exceptionId, assertObject);
139
140 Exception myEx = new Exception(exceptionMessage);
141 Throwable actualEx = new InvocationTargetException(myEx);
142 assertObject.setActualObject(myEx);
143 handler.process(this.testId, actualEx, this.classDataSet
144 .getAssertMap(this.testMethod, this.testId));
145 handler.summarizeProblems(this.classDataSet.size(this.testMethod));
146 } catch (Throwable e) {
147 fail("Should not throw exception. " + e.getClass().getName());
148 }
149 }
150
151
152
153
154 public void testCheckOnExpectedException() {
155 String exceptionId = "exception0";
156 String exceptionMessage = "Expected exception";
157 String expectedMessage = "There is/are 1 expected exception(s) defined in test 'testId'"
158 + LF
159 + " (last as hint): java.lang.Exception - Expected exception";
160
161 ExceptionAsserter assertObject = new ExceptionAsserter(exceptionId,
162 "java.lang.Exception", ObjectAsserter.ASSERT_ACTION_ISEQUAL);
163
164 assertObject.setValue(new Exception(exceptionMessage));
165 this.classDataSet.getTestDataSet(this.testMethod, testId)
166 .getAssertMap().put(exceptionId, assertObject);
167
168 Exception myEx = new Exception(exceptionMessage);
169 assertObject.setActualObject(myEx);
170 try {
171 handler.checkOnExpectedException(this.testId, this.classDataSet
172 .getAssertMap(this.testMethod, this.testId));
173 fail("Expected AssertionFailedError");
174 } catch (AssertionFailedError ex) {
175 assertEquals("Wrong exception message", expectedMessage, ex
176 .getMessage());
177 }
178 }
179 }