1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package ch.elca.el4j.tests.services.exceptionhandler;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.fail;
22
23 import org.junit.Test;
24 import org.springframework.context.ApplicationContext;
25
26 import ch.elca.el4j.core.context.ModuleApplicationContext;
27
28
29
30
31
32
33
34
35
36
37
38 public class SafetyFacadeTest {
39
40
41 private ApplicationContext m_appContext;
42
43
44 private A m_a;
45
46
47 private A m_unsafeA;
48
49
50
51
52 public SafetyFacadeTest() {
53 m_appContext = new ModuleApplicationContext(
54 "scenarios/services/exceptionhandler/safetyFacadeTest.xml", false);
55
56 m_a = (A) m_appContext.getBean("A");
57 m_unsafeA = (A) m_appContext.getBean("unsafeA");
58 }
59
60
61
62
63
64 @Test
65 public void testA() {
66 try {
67 int result = m_a.div(1, 0);
68 assertEquals("null was not transformed to 0.", 0, result);
69 } catch (Throwable t) {
70 fail("Caught exception although it wasn't expected. " + t);
71 }
72 }
73
74
75
76
77 @Test
78 public void testUnsafeA() {
79 try {
80 m_unsafeA.div(1, 0);
81 fail("Didn't catch any exception.");
82 } catch (ArithmeticException ae) {
83
84 } catch (Throwable t) {
85 fail("Caught wrong exception: " + t);
86 }
87 }
88
89
90
91
92 @Test
93 public void testRTException() {
94 try {
95 m_a.throwRTException();
96 } catch (Throwable t) {
97 fail("Caught exception although it wasn't expected. " + t);
98 }
99 }
100
101
102
103
104 @Test
105 public void testForwardInterfaceExceptions() {
106 try {
107 m_a.throwException();
108 } catch (ApplicationException ae) {
109
110 } catch (RuntimeException re) {
111 fail("Expected ApplicationException but caught RunitmeException.");
112 } catch (Throwable t) {
113 fail("Expected RuntimeException but caught another one: " + t);
114 }
115 }
116
117
118
119
120
121
122
123
124
125
126 @Test
127 public void testReconfigurationExceptionHandler() {
128 int result = 0;
129 try {
130 result = m_a.add(4, 5);
131 } catch (Throwable t) {
132 fail("Caught exception although it wasn't expected. " + t);
133 }
134 assertEquals("Wrongly added (first call).", 9, result);
135 assertEquals("Called wrong method (B, first call)",
136 1, B.s_numberOfAddCalls);
137 assertEquals("Called wrong method (C, first call)",
138 1, C.s_numberOfAddCalls);
139
140 B.reset();
141 C.reset();
142
143 result = m_a.add(4, 5);
144 assertEquals("Wrongly added (second call).", 9, result);
145 assertEquals("Called wrong method (B, second call)",
146 0, B.s_numberOfAddCalls);
147 assertEquals("Called wrong method (C, second call)",
148 1, C.s_numberOfAddCalls);
149 }
150
151
152
153
154
155
156
157
158 @Test
159 public void testRoundRobinExceptonHandler() {
160 String foo = "foo";
161 String bar = "bar";
162 String result = null;
163 try {
164 result = m_a.concat(foo, bar);
165 } catch (Throwable t) {
166 fail("Caught exception although it wasn't expected. " + t);
167 }
168 assertEquals("Strings are badly concatenated (first call).",
169 foo.concat(bar), result);
170 assertEquals("Called wrong method (A, first call)",
171 1, AImpl.s_numberOfConcatCalls);
172 assertEquals("Called wrong method (B, first call)",
173 1, B.s_numberOfConcatCalls);
174
175 AImpl.reset();
176 B.reset();
177
178
179 result = m_a.concat(foo, bar);
180 assertEquals("Strings are badly concatenated (second call).",
181 foo.concat(bar), result);
182 assertEquals("Called wrong method (A, second call)",
183 0, AImpl.s_numberOfConcatCalls);
184 assertEquals("Called wrong method (B, second call)",
185 1, B.s_numberOfConcatCalls);
186
187 AImpl.reset(); AImpl.s_concatFails = false;
188 B.reset(); B.s_concatFails = true;
189
190
191 result = m_a.concat(foo, bar);
192 assertEquals("Strings are badly concatenated (third call).",
193 foo.concat(bar), result);
194 assertEquals("Called wrong method (A, third call)",
195 1, AImpl.s_numberOfConcatCalls);
196 assertEquals("Called wrong method (B, third call)",
197 1, B.s_numberOfConcatCalls);
198
199 AImpl.reset(); AImpl.s_concatFails = false;
200 B.reset(); B.s_concatFails = true;
201
202
203 result = m_a.concat(foo, bar);
204 assertEquals("Strings are badly concatenated (fourth call).",
205 foo.concat(bar), result);
206 assertEquals("Called wrong method (A, fourth call)",
207 1, AImpl.s_numberOfConcatCalls);
208 assertEquals("Called wrong method (B, fourth call)",
209 0, B.s_numberOfConcatCalls);
210 }
211
212
213
214
215
216
217
218 @Test
219 public void testRetry() {
220 int result = 0;
221 try {
222 result = m_a.sub(10, 8);
223 } catch (IllegalArgumentException rt) {
224 fail("Caught unexpected IllegalArgumentException.");
225 }
226 assertEquals("Calculated wrong value (first call)", 2, result);
227
228 m_a.setRetries(6);
229 try {
230 m_a.sub(10, 8);
231 fail("Didn't caught the expected IllegalArgumentException"
232 + "(second call).");
233 } catch (IllegalArgumentException rt) {
234
235 } catch (Throwable t) {
236 fail("Caught unexpected exception (second call): " + t);
237 }
238 }
239 }
240
241