KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > enhancer > interceptors > business > BusinessInterceptorsTestCase


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: BusinessInterceptorsTestCase.java 911 2006-07-24 14:13:45Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.tests.enhancer.interceptors.business;
27
28 import static org.testng.AssertJUnit.assertEquals;
29 import static org.testng.AssertJUnit.assertTrue;
30 import static org.testng.AssertJUnit.fail;
31
32 import java.util.Arrays JavaDoc;
33
34 import org.objectweb.easybeans.tests.enhancer.interceptors.business.bean.StatelessBean;
35 import org.objectweb.easybeans.tests.enhancer.interceptors.business.bean.TestException;
36 import org.objectweb.easybeans.tests.enhancer.interceptors.business.bean.TestException2;
37 import org.testng.annotations.BeforeMethod;
38 import org.testng.annotations.Test;
39
40 /**
41  * Call bean and see if interceptor has not broken methods calls.
42  * @author Florent Benoit
43  */

44 public class BusinessInterceptorsTestCase{
45
46     /**
47      * Bean tested.
48      */

49     private StatelessBean statelessBean = null;
50
51     /**
52      * Enhancing has been done ?
53      */

54     private static boolean enhancingDone = false;
55
56     /**
57      * Setup for test case.
58      * @throws Exception if super method fails
59      */

60     @BeforeMethod
61     protected void setUp() throws Exception JavaDoc {
62         if (!enhancingDone) {
63             BusinessInterceptorsClassesEnhancer.enhance();
64             enhancingDone = true;
65         }
66         statelessBean = new StatelessBean();
67     }
68
69
70     /**
71      * Tests the boolean.
72      */

73     @Test
74     public void testBoolean() {
75         assertEquals(true, statelessBean.getBoolean(true));
76         assertEquals(false, statelessBean.getBoolean(false));
77
78         boolean[] booleans = new boolean[] {false, true, false};
79         assertTrue(Arrays.equals(booleans, statelessBean.getBooleans(booleans)));
80     }
81
82
83     /**
84      * Tests the bytes.
85      */

86     @Test
87     public void testByte() {
88         byte b = 1;
89         assertEquals(b, statelessBean.getByte(b));
90
91         byte[] bytes = new byte[] {0, 1};
92         assertTrue(Arrays.equals(bytes, statelessBean.getBytes(bytes)));
93     }
94
95
96     /**
97      * Tests the chars.
98      */

99     @Test
100     public void testChar() {
101         char c = 'a';
102         assertEquals(c, statelessBean.getChar(c));
103
104         char[] chars = new char[] {'a', 'b', 'c'};
105         assertTrue(Arrays.equals(chars, statelessBean.getChars(chars)));
106     }
107
108
109
110     /**
111      * Tests the double.
112      */

113     @Test
114     public void testDouble() {
115         double d = 1;
116         assertEquals(Double.valueOf(d), Double.valueOf(statelessBean.getDouble(d)));
117
118         double[] doubles = new double[] {0, 1};
119         assertEquals(doubles, statelessBean.getDoubles(doubles));
120     }
121
122
123     /**
124      * Tests the float.
125      */

126     @Test
127     public void testFloat() {
128         float f = 1;
129         assertEquals(Float.valueOf(f), Float.valueOf(statelessBean.getFloat(f)));
130
131         float[] floats = new float[] {0, 1};
132         assertEquals(floats, statelessBean.getFloats(floats));
133     }
134
135
136
137     /**
138      * Tests the int.
139      */

140     @Test
141     public void testInt() {
142         assertEquals(1, statelessBean.getInt(1));
143         assertEquals(1, statelessBean.addInt(0, 1));
144         int[] ints = new int[] {0, 1};
145         assertEquals(ints, statelessBean.getInts(ints));
146     }
147
148
149     /**
150      * Tests the long.
151      */

152     @Test
153     public void testLong() {
154         long l = 1;
155         assertEquals(Long.valueOf(l), Long.valueOf(statelessBean.getLong(l)));
156
157         long[] longs = new long[] {0, 1};
158         assertEquals(longs, statelessBean.getLongs(longs));
159     }
160
161     /**
162      * Tests the short.
163      */

164     @Test
165     public void testShort() {
166         short s = 1;
167         assertEquals(Short.valueOf(s), Short.valueOf(statelessBean.getShort(s)));
168
169         short[] shorts = new short[] {0, 1};
170         assertEquals(shorts, statelessBean.getShorts(shorts));
171     }
172
173     /**
174      * Mix of primitive / object.
175      */

176     @Test
177     @SuppressWarnings JavaDoc("boxing")
178     public void testMix() {
179         boolean flag = false;
180         byte b = 0;
181         char c = 'c';
182         double d = 1;
183         float f = 1;
184         int i = 1;
185         long l = 2;
186         Object JavaDoc o = new Object JavaDoc();
187
188         Object JavaDoc[] returnObject = new Object JavaDoc[] {flag, b, c, d, f, i, l, o};
189         assertTrue(Arrays.equals(returnObject, statelessBean.getPrimitive(flag, b, c, d, f, i, l, o)));
190     }
191
192
193     /**
194      * Test a user defined exception.
195      */

196     @Test
197     public void testUserDefinedException() {
198          try {
199              statelessBean.someCustomizedExceptions();
200              fail("Should throw an exception");
201          } catch (TestException test) {
202              if (!test.getMessage().equals("someCustomizedExceptions")) {
203                  fail("Exception doesn't contain the expected value");
204              }
205          } catch (Exception JavaDoc e) {
206              fail("Not the expected type of exception");
207          }
208     }
209
210     /**
211      * Test a user defined exception.
212      */

213     @Test
214     public void testUserDefinedException2() {
215          try {
216              statelessBean.someCustomizedExceptions2(1);
217              fail("Should throw an exception");
218          } catch (TestException test) {
219              if (!test.getMessage().equals("someCustomizedExceptions2.TestException")) {
220                  fail("Exception doesn't contain the expected value");
221              }
222          } catch (Exception JavaDoc e) {
223              fail("Not the expected type of exception");
224          }
225
226          try {
227              statelessBean.someCustomizedExceptions2(2);
228              fail("Should throw an exception");
229          } catch (TestException2 test) {
230              if (!test.getMessage().equals("someCustomizedExceptions2.TestException2")) {
231                  fail("Exception doesn't contain the expected value");
232              }
233          } catch (Exception JavaDoc e) {
234              fail("Not the expected type of exception");
235          }
236     }
237
238     /**
239      * Test user defined exceptions and exceptions.
240      */

241     @Test
242     public void testMultipleException3() {
243         int i = 1;
244
245          try {
246              statelessBean.someCustomizedExceptions3(i++);
247              fail("Should throw an exception");
248          } catch (TestException test) {
249              if (!test.getMessage().equals("someCustomizedExceptions3.TestException")) {
250                  fail("Exception doesn't contain the expected value");
251              }
252          } catch (Exception JavaDoc e) {
253              fail("Not the expected type of exception");
254          }
255
256          try {
257              statelessBean.someCustomizedExceptions3(i++);
258              fail("Should throw an exception");
259          } catch (TestException2 test) {
260              if (!test.getMessage().equals("someCustomizedExceptions3.TestException2")) {
261                  fail("Exception doesn't contain the expected value");
262              }
263          } catch (Exception JavaDoc e) {
264              fail("Not the expected type of exception");
265          }
266
267          try {
268              statelessBean.someCustomizedExceptions3(i++);
269              fail("Should throw an exception");
270          } catch (Exception JavaDoc e) {
271              if (!e.getMessage().equals("someCustomizedExceptions3.Exception")) {
272                  fail("Exception doesn't contain the expected value");
273              }
274          }
275
276          try {
277              statelessBean.someCustomizedExceptions3(i++);
278              fail("Should throw an exception");
279          } catch (RuntimeException JavaDoc e) {
280              if (!e.getMessage().equals("someCustomizedExceptions3.RuntimeException")) {
281                  fail("Exception doesn't contain the expected value");
282              }
283          } catch (Exception JavaDoc e) {
284              fail("Not the expected type of exception");
285          }
286
287     }
288
289
290     /**
291      * Test that aroundInvoke in the bean has increased a counter.
292      */

293     @Test
294     public void testCounter() {
295         int count = statelessBean.getCounter();
296         assert count == 0;
297         assertEquals(1, statelessBean.addInt(0, 1));
298         assert statelessBean.getCounter() > 0;
299     }
300
301     /**
302      * Test that interceptor has increased a counter.
303      */

304     @Test
305     public void testOtherClassInterceptorCounter() {
306         int count = statelessBean.getOtherInterceptorCounter();
307         assert count == 0;
308         assertEquals(1, statelessBean.addInt(0, 1));
309         assert statelessBean.getOtherInterceptorCounter() > 0;
310     }
311
312     /**
313      * Test that interceptor throw an exception.
314      */

315     @Test
316     public void testInterceptorThrowException() {
317         try {
318             statelessBean.throwExceptionByInterceptor();
319             fail("Should have thrown an exception by the interceptor.");
320         } catch (RuntimeException JavaDoc e) {
321             assertEquals(e.getCause().getMessage(), "Throw an exception on throwExceptionByInterceptor");
322         }
323     }
324
325
326     /**
327      * Test that value is increased by interceptor.
328      */

329     @Test
330     public void testValueDoubledByInterceptor() {
331         int i = 1;
332         assertEquals(i * 2, statelessBean.valueDoubledByInterceptor(i));
333     }
334
335     /**
336      * Test that the interceptor is only called on a stateless
337      * bean and on singleMethodIntercepted method.
338      */

339     @Test
340     public void testSingleMethodIntercepted() {
341         int count = statelessBean.getIncrementSingleMethodInterceptedCounter();
342         assertTrue(count == 0);
343         // Counter will increment
344
statelessBean.singleMethodIntercepted();
345         assertEquals(1, statelessBean.getIncrementSingleMethodInterceptedCounter());
346         // counter shouldn't increment with another method
347
statelessBean.addInt(1, 2);
348         assertEquals(1, statelessBean.getIncrementSingleMethodInterceptedCounter());
349     }
350
351     /**
352      * Test that no interceptors are called on this method.
353      */

354     @Test
355     public void testExcludedInterceptorsMethod() {
356         // start : counter = 0, interceptor not called
357
int count = statelessBean.getOtherInterceptorCounter();
358         assert count == 0;
359
360         // call the business method on the bean
361
statelessBean.excludedInterceptorsMethod();
362
363         // Check that counter = 0 <-- means that it was excluded
364
assert statelessBean.getOtherInterceptorCounter() == 0;
365     }
366
367 }
368
Popular Tags