|
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 |
| package org.objectweb.easybeans.tests.enhancer.interceptors.business; |
|
27 |
| |
|
28 |
| import java.util.Arrays; |
|
29 |
| |
|
30 |
| import org.objectweb.easybeans.tests.enhancer.interceptors.business.bean.StatelessBean; |
|
31 |
| import org.objectweb.easybeans.tests.enhancer.interceptors.business.bean.TestException; |
|
32 |
| import org.objectweb.easybeans.tests.enhancer.interceptors.business.bean.TestException2; |
|
33 |
| import org.testng.annotations.Configuration; |
|
34 |
| import org.testng.annotations.Test; |
|
35 |
| |
|
36 |
| import static org.testng.AssertJUnit.assertTrue; |
|
37 |
| import static org.testng.AssertJUnit.assertEquals; |
|
38 |
| import static org.testng.AssertJUnit.fail; |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| public class BusinessInterceptorsTestCase{ |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| private StatelessBean statelessBean = null; |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| private static boolean enhancingDone = false; |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
36
| @Configuration(beforeTestMethod = true)
|
|
61 |
| protected void setUp() throws Exception { |
|
62 |
36
| if (!enhancingDone) {
|
|
63 |
2
| BusinessInterceptorsClassesEnhancer.enhance();
|
|
64 |
2
| enhancingDone = true;
|
|
65 |
| } |
|
66 |
36
| statelessBean = new StatelessBean();
|
|
67 |
| } |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
2
| @Test
|
|
74 |
| public void testBoolean() { |
|
75 |
2
| assertEquals(true, statelessBean.getBoolean(true));
|
|
76 |
2
| assertEquals(false, statelessBean.getBoolean(false));
|
|
77 |
| |
|
78 |
2
| boolean[] booleans = new boolean[] {false, true, false};
|
|
79 |
2
| assertTrue(Arrays.equals(booleans, statelessBean.getBooleans(booleans)));
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
2
| @Test
|
|
87 |
| public void testByte() { |
|
88 |
2
| byte b = 1;
|
|
89 |
2
| assertEquals(b, statelessBean.getByte(b));
|
|
90 |
| |
|
91 |
2
| byte[] bytes = new byte[] {0, 1};
|
|
92 |
2
| assertTrue(Arrays.equals(bytes, statelessBean.getBytes(bytes)));
|
|
93 |
| } |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
2
| @Test
|
|
100 |
| public void testChar() { |
|
101 |
2
| char c = 'a';
|
|
102 |
2
| assertEquals(c, statelessBean.getChar(c));
|
|
103 |
| |
|
104 |
2
| char[] chars = new char[] {'a', 'b', 'c'};
|
|
105 |
2
| assertTrue(Arrays.equals(chars, statelessBean.getChars(chars)));
|
|
106 |
| } |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
2
| @Test
|
|
114 |
| public void testDouble() { |
|
115 |
2
| double d = 1;
|
|
116 |
2
| assertEquals(Double.valueOf(d), Double.valueOf(statelessBean.getDouble(d)));
|
|
117 |
| |
|
118 |
2
| double[] doubles = new double[] {0, 1};
|
|
119 |
2
| assertEquals(doubles, statelessBean.getDoubles(doubles));
|
|
120 |
| } |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
2
| @Test
|
|
127 |
| public void testFloat() { |
|
128 |
2
| float f = 1;
|
|
129 |
2
| assertEquals(Float.valueOf(f), Float.valueOf(statelessBean.getFloat(f)));
|
|
130 |
| |
|
131 |
2
| float[] floats = new float[] {0, 1};
|
|
132 |
2
| assertEquals(floats, statelessBean.getFloats(floats));
|
|
133 |
| } |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
2
| @Test
|
|
141 |
| public void testInt() { |
|
142 |
2
| assertEquals(1, statelessBean.getInt(1));
|
|
143 |
2
| assertEquals(1, statelessBean.addInt(0, 1));
|
|
144 |
2
| int[] ints = new int[] {0, 1};
|
|
145 |
2
| assertEquals(ints, statelessBean.getInts(ints));
|
|
146 |
| } |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
2
| @Test
|
|
153 |
| public void testLong() { |
|
154 |
2
| long l = 1;
|
|
155 |
2
| assertEquals(Long.valueOf(l), Long.valueOf(statelessBean.getLong(l)));
|
|
156 |
| |
|
157 |
2
| long[] longs = new long[] {0, 1};
|
|
158 |
2
| assertEquals(longs, statelessBean.getLongs(longs));
|
|
159 |
| } |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
2
| @Test
|
|
165 |
| public void testShort() { |
|
166 |
2
| short s = 1;
|
|
167 |
2
| assertEquals(Short.valueOf(s), Short.valueOf(statelessBean.getShort(s)));
|
|
168 |
| |
|
169 |
2
| short[] shorts = new short[] {0, 1};
|
|
170 |
2
| assertEquals(shorts, statelessBean.getShorts(shorts));
|
|
171 |
| } |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
2
| @Test
|
|
177 |
| @SuppressWarnings("boxing") |
|
178 |
| public void testMix() { |
|
179 |
2
| boolean flag = false;
|
|
180 |
2
| byte b = 0;
|
|
181 |
2
| char c = 'c';
|
|
182 |
2
| double d = 1;
|
|
183 |
2
| float f = 1;
|
|
184 |
2
| int i = 1;
|
|
185 |
2
| long l = 2;
|
|
186 |
2
| Object o = new Object();
|
|
187 |
| |
|
188 |
2
| Object[] returnObject = new Object[] {flag, b, c, d, f, i, l, o};
|
|
189 |
2
| assertTrue(Arrays.equals(returnObject, statelessBean.getPrimitive(flag, b, c, d, f, i, l, o)));
|
|
190 |
| } |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
2
| @Test
|
|
197 |
| public void testUserDefinedException() { |
|
198 |
2
| try {
|
|
199 |
2
| statelessBean.someCustomizedExceptions();
|
|
200 |
0
| fail("Should throw an exception");
|
|
201 |
| } catch (TestException test) { |
|
202 |
2
| if (!test.getMessage().equals("someCustomizedExceptions")) {
|
|
203 |
0
| fail("Exception doesn't contain the expected value");
|
|
204 |
| } |
|
205 |
| } catch (Exception e) { |
|
206 |
0
| fail("Not the expected type of exception");
|
|
207 |
| } |
|
208 |
| } |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
2
| @Test
|
|
214 |
| public void testUserDefinedException2() { |
|
215 |
2
| try {
|
|
216 |
2
| statelessBean.someCustomizedExceptions2(1);
|
|
217 |
0
| fail("Should throw an exception");
|
|
218 |
| } catch (TestException test) { |
|
219 |
2
| if (!test.getMessage().equals("someCustomizedExceptions2.TestException")) {
|
|
220 |
0
| fail("Exception doesn't contain the expected value");
|
|
221 |
| } |
|
222 |
| } catch (Exception e) { |
|
223 |
0
| fail("Not the expected type of exception");
|
|
224 |
| } |
|
225 |
| |
|
226 |
2
| try {
|
|
227 |
2
| statelessBean.someCustomizedExceptions2(2);
|
|
228 |
0
| fail("Should throw an exception");
|
|
229 |
| } catch (TestException2 test) { |
|
230 |
2
| if (!test.getMessage().equals("someCustomizedExceptions2.TestException2")) {
|
|
231 |
0
| fail("Exception doesn't contain the expected value");
|
|
232 |
| } |
|
233 |
| } catch (Exception e) { |
|
234 |
0
| fail("Not the expected type of exception");
|
|
235 |
| } |
|
236 |
| } |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
2
| @Test
|
|
242 |
| public void testMultipleException3() { |
|
243 |
2
| int i = 1;
|
|
244 |
| |
|
245 |
2
| try {
|
|
246 |
2
| statelessBean.someCustomizedExceptions3(i++);
|
|
247 |
0
| fail("Should throw an exception");
|
|
248 |
| } catch (TestException test) { |
|
249 |
2
| if (!test.getMessage().equals("someCustomizedExceptions3.TestException")) {
|
|
250 |
0
| fail("Exception doesn't contain the expected value");
|
|
251 |
| } |
|
252 |
| } catch (Exception e) { |
|
253 |
0
| fail("Not the expected type of exception");
|
|
254 |
| } |
|
255 |
| |
|
256 |
2
| try {
|
|
257 |
2
| statelessBean.someCustomizedExceptions3(i++);
|
|
258 |
0
| fail("Should throw an exception");
|
|
259 |
| } catch (TestException2 test) { |
|
260 |
2
| if (!test.getMessage().equals("someCustomizedExceptions3.TestException2")) {
|
|
261 |
0
| fail("Exception doesn't contain the expected value");
|
|
262 |
| } |
|
263 |
| } catch (Exception e) { |
|
264 |
0
| fail("Not the expected type of exception");
|
|
265 |
| } |
|
266 |
| |
|
267 |
2
| try {
|
|
268 |
2
| statelessBean.someCustomizedExceptions3(i++);
|
|
269 |
0
| fail("Should throw an exception");
|
|
270 |
| } catch (Exception e) { |
|
271 |
2
| if (!e.getMessage().equals("someCustomizedExceptions3.Exception")) {
|
|
272 |
0
| fail("Exception doesn't contain the expected value");
|
|
273 |
| } |
|
274 |
| } |
|
275 |
| |
|
276 |
2
| try {
|
|
277 |
2
| statelessBean.someCustomizedExceptions3(i++);
|
|
278 |
0
| fail("Should throw an exception");
|
|
279 |
| } catch (RuntimeException e) { |
|
280 |
2
| if (!e.getMessage().equals("someCustomizedExceptions3.RuntimeException")) {
|
|
281 |
0
| fail("Exception doesn't contain the expected value");
|
|
282 |
| } |
|
283 |
| } catch (Exception e) { |
|
284 |
0
| fail("Not the expected type of exception");
|
|
285 |
| } |
|
286 |
| |
|
287 |
| } |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
2
| @Test
|
|
294 |
| public void testCounter() { |
|
295 |
2
| int count = statelessBean.getCounter();
|
|
296 |
2
| assert count == 0;
|
|
297 |
2
| assertEquals(1, statelessBean.addInt(0, 1));
|
|
298 |
2
| assert statelessBean.getCounter() > 0;
|
|
299 |
| } |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
2
| @Test
|
|
305 |
| public void testOtherClassInterceptorCounter() { |
|
306 |
2
| int count = statelessBean.getOtherInterceptorCounter();
|
|
307 |
2
| assert count == 0;
|
|
308 |
2
| assertEquals(1, statelessBean.addInt(0, 1));
|
|
309 |
2
| assert statelessBean.getOtherInterceptorCounter() > 0;
|
|
310 |
| } |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
2
| @Test
|
|
316 |
| public void testInterceptorThrowException() { |
|
317 |
2
| try {
|
|
318 |
2
| statelessBean.throwExceptionByInterceptor();
|
|
319 |
0
| fail("Should have thrown an exception by the interceptor.");
|
|
320 |
| } catch (RuntimeException e) { |
|
321 |
2
| assertEquals(e.getCause().getMessage(), "Throw an exception on throwExceptionByInterceptor");
|
|
322 |
| } |
|
323 |
| } |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
2
| @Test
|
|
330 |
| public void testValueDoubledByInterceptor() { |
|
331 |
2
| int i = 1;
|
|
332 |
2
| assertEquals(i * 2, statelessBean.valueDoubledByInterceptor(i));
|
|
333 |
| } |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
2
| @Test
|
|
340 |
| public void testSingleMethodIntercepted() { |
|
341 |
2
| int count = statelessBean.getIncrementSingleMethodInterceptedCounter();
|
|
342 |
2
| assertTrue(count == 0);
|
|
343 |
| |
|
344 |
2
| statelessBean.singleMethodIntercepted();
|
|
345 |
2
| assertEquals(1, statelessBean.getIncrementSingleMethodInterceptedCounter());
|
|
346 |
| |
|
347 |
2
| statelessBean.addInt(1, 2);
|
|
348 |
2
| assertEquals(1, statelessBean.getIncrementSingleMethodInterceptedCounter());
|
|
349 |
| } |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
2
| @Test
|
|
355 |
| public void testExcludedInterceptorsMethod() { |
|
356 |
| |
|
357 |
2
| int count = statelessBean.getOtherInterceptorCounter();
|
|
358 |
2
| assert count == 0;
|
|
359 |
| |
|
360 |
| |
|
361 |
2
| statelessBean.excludedInterceptorsMethod();
|
|
362 |
| |
|
363 |
| |
|
364 |
2
| assert statelessBean.getOtherInterceptorCounter() == 0;
|
|
365 |
| } |
|
366 |
| |
|
367 |
| } |