KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > enhancer > interceptors > lifecycle > LifeCycleInterceptorsTestCase


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: LifeCycleInterceptorsTestCase.java 911 2006-07-24 14:13:45Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.tests.enhancer.interceptors.lifecycle;
27
28 import static org.testng.AssertJUnit.assertEquals;
29 import static org.testng.AssertJUnit.assertFalse;
30 import static org.testng.AssertJUnit.assertTrue;
31 import static org.testng.AssertJUnit.fail;
32
33 import org.objectweb.easybeans.api.bean.lifecycle.EasyBeansSFSBLifeCycle;
34 import org.objectweb.easybeans.api.bean.lifecycle.EasyBeansSLSBLifeCycle;
35 import org.objectweb.easybeans.tests.enhancer.interceptors.lifecycle.bean.StatefulBean;
36 import org.objectweb.easybeans.tests.enhancer.interceptors.lifecycle.bean.StatelessBean;
37 import org.objectweb.easybeans.tests.enhancer.interceptors.lifecycle.bean.StatelessBean2;
38 import org.objectweb.easybeans.tests.enhancer.interceptors.lifecycle.bean.StatelessBean3;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
41
42 /**
43  * Call bean and see if callbacks are working.
44  * @author Florent Benoit
45  */

46 public class LifeCycleInterceptorsTestCase{
47
48     /**
49      * Bean (stateless) tested.
50      */

51     private StatelessBean statelessBean = null;
52
53     /**
54      * Bean (stateless2) tested.
55      */

56     private StatelessBean2 statelessBean2 = null;
57
58     /**
59      * Bean (stateless3) tested.
60      */

61     private StatelessBean3 statelessBean3 = null;
62
63     /**
64      * Bean (stateful) tested.
65      */

66     private StatefulBean statefulBean = null;
67
68     /**
69      * Enhancing has been done ?
70      */

71     private static boolean enhancingDone = false;
72
73     /**
74      * Setup for test case.
75      * @throws Exception if super method fails
76      */

77     @BeforeMethod
78     protected void setUp() throws Exception JavaDoc {
79         if (!enhancingDone) {
80             LifeCycleInterceptorsClassesEnhancer.enhance();
81             enhancingDone = true;
82         }
83         statelessBean = new StatelessBean();
84         statelessBean2 = new StatelessBean2();
85         statelessBean3 = new StatelessBean3();
86         statefulBean = new StatefulBean();
87     }
88
89     // =====================
90
// Common methods
91
// =====================
92
/**
93      * @return a lifecycle object for the current stateless bean
94      */

95     private EasyBeansSLSBLifeCycle getSLSBLifeCycle() {
96         if (statelessBean instanceof EasyBeansSLSBLifeCycle) {
97             return (EasyBeansSLSBLifeCycle) statelessBean;
98         }
99         fail("The stateless bean is not an instance of the interface EasyBeansSLSBLifeCycle.");
100         return null;
101     }
102
103     /**
104      * @return a lifecycle object for the current stateless2 bean
105      */

106     private EasyBeansSLSBLifeCycle getSLSB2LifeCycle() {
107         if (statelessBean2 instanceof EasyBeansSLSBLifeCycle) {
108             return (EasyBeansSLSBLifeCycle) statelessBean2;
109         }
110         fail("The stateless bean is not an instance of the interface EasyBeansSLSBLifeCycle.");
111         return null;
112     }
113
114     /**
115      * @return a lifecycle object for the current stateless3 bean
116      */

117     private EasyBeansSLSBLifeCycle getSLSB3LifeCycle() {
118         if (statelessBean3 instanceof EasyBeansSLSBLifeCycle) {
119             return (EasyBeansSLSBLifeCycle) statelessBean3;
120         }
121         fail("The stateless bean is not an instance of the interface EasyBeansSLSBLifeCycle.");
122         return null;
123     }
124
125     /**
126      * @return a lifecycle object for the current stateful bean
127      */

128     private EasyBeansSFSBLifeCycle getSFSBLifeCycle() {
129         if (statefulBean instanceof EasyBeansSFSBLifeCycle) {
130             return (EasyBeansSFSBLifeCycle) statefulBean;
131         }
132         fail("The stateful bean is not an instance of the interface EasyBeansSLSBLifeCycle.");
133         return null;
134     }
135
136     // =====================
137
// Stateless
138
// =====================
139

140     /**
141      * Test the stateless bean callbacks.
142      */

143     @Test
144     public void testStatelessBeanCallbacks() {
145         assertEquals(0, statelessBean.getCounter());
146         EasyBeansSLSBLifeCycle lifeCycle = getSLSBLifeCycle();
147
148         // postConstruct should increment the counter
149
lifeCycle.postConstructEasyBeansLifeCycle();
150         assertEquals(1, statelessBean.getCounter());
151
152         // preDestroy should decrement counter
153
lifeCycle.preDestroyEasyBeansLifeCycle();
154         assertEquals(0, statelessBean.getCounter());
155     }
156
157     /**
158      * Test the postConstruct on stateless bean.
159      */

160     @Test
161     public void testStatelessPostConstruct() {
162         assertFalse(statelessBean.isPostConstructCalled());
163         EasyBeansSLSBLifeCycle lifeCycle = getSLSBLifeCycle();
164         lifeCycle.postConstructEasyBeansLifeCycle();
165
166         assertTrue(statelessBean.isPostConstructCalled());
167     }
168
169     /**
170      * Test the preDestroy on stateless bean.
171      */

172     @Test
173     public void testStatelessPreDestroy() {
174         assertFalse(statelessBean.isPreDestroyCalled());
175         EasyBeansSLSBLifeCycle lifeCycle = getSLSBLifeCycle();
176         lifeCycle.preDestroyEasyBeansLifeCycle();
177
178         assertTrue(statelessBean.isPreDestroyCalled());
179     }
180
181
182     /**
183      * Test the postConstruct on stateless bean.
184      */

185     @Test
186     public void testStateless2PostConstruct() {
187         assertFalse(statelessBean2.isPostConstructCalled());
188         EasyBeansSLSBLifeCycle lifeCycle = getSLSB2LifeCycle();
189         lifeCycle.postConstructEasyBeansLifeCycle();
190
191         assertTrue(statelessBean2.isPostConstructCalled());
192     }
193
194     /**
195      * Test the preDestroy on stateless bean.
196      */

197     @Test
198     public void testStateless2PreDestroy() {
199         assertFalse(statelessBean2.isPreDestroyCalled());
200         EasyBeansSLSBLifeCycle lifeCycle = getSLSB2LifeCycle();
201         lifeCycle.preDestroyEasyBeansLifeCycle();
202
203         assertTrue(statelessBean2.isPreDestroyCalled());
204     }
205
206
207     /**
208      * Test the postConstruct/preDestroy on stateless bean.
209      */

210     @Test
211     public void testStateless3() {
212         assertEquals(0, statelessBean3.getCounter());
213
214         EasyBeansSLSBLifeCycle lifeCycle = getSLSB3LifeCycle();
215
216         // postConstruct should increment the counter
217
lifeCycle.postConstructEasyBeansLifeCycle();
218         assertEquals(1, statelessBean3.getCounter());
219
220         // preDestroy should decrement the counter
221
lifeCycle.preDestroyEasyBeansLifeCycle();
222         assertEquals(0, statelessBean3.getCounter());
223     }
224
225
226     /**
227      * Test the inheritance on lifecycle method.
228      * Super methods should be called.
229      */

230     @Test
231     public void testInheritanceBeanLifeCycleStateless() {
232         assertEquals(0, statelessBean.getCounter());
233         assertEquals(0, statelessBean.getSuperLifeCycleCounter());
234
235         EasyBeansSLSBLifeCycle lifeCycle = getSLSBLifeCycle();
236
237         // postConstruct should increment the counter
238
lifeCycle.postConstructEasyBeansLifeCycle();
239         assertEquals(1, statelessBean.getCounter());
240         // and the counter of the super class
241
assertEquals(1, statelessBean.getSuperLifeCycleCounter());
242
243         // preDestroy should decrement the counter
244
lifeCycle.preDestroyEasyBeansLifeCycle();
245         assertEquals(0, statelessBean.getCounter());
246         assertEquals(0, statelessBean.getSuperLifeCycleCounter());
247     }
248
249
250     // =====================
251
// Stateful
252
// =====================
253

254     /**
255      * Test the stateful bean callbacks.
256      */

257     @Test
258     public void testStatefulBeanCallbacks() {
259         EasyBeansSFSBLifeCycle lifeCycle = getSFSBLifeCycle();
260
261         int internalCounter = 0;
262         assertEquals(internalCounter++, statefulBean.getCounter());
263         if (statefulBean instanceof EasyBeansSFSBLifeCycle) {
264             lifeCycle = (EasyBeansSFSBLifeCycle) statefulBean;
265         }
266
267         // postConstruct should increment the counter
268
lifeCycle.postConstructEasyBeansLifeCycle();
269         assertEquals(internalCounter++, statefulBean.getCounter());
270
271         // preDestroy should increment counter
272
lifeCycle.preDestroyEasyBeansLifeCycle();
273         assertEquals(internalCounter++, statefulBean.getCounter());
274
275         // prePassivate should increment counter
276
lifeCycle.prePassivateEasyBeansLifeCycle();
277         assertEquals(internalCounter++, statefulBean.getCounter());
278
279         // postActivate should increment counter
280
lifeCycle.postActivateEasyBeansLifeCycle();
281         assertEquals(internalCounter++, statefulBean.getCounter());
282
283     }
284
285     /**
286      * Test the postConstruct on stateful bean.
287      */

288     @Test
289     public void testStatefulPostConstruct() {
290         assertFalse(statefulBean.isPostConstructCalled());
291         EasyBeansSFSBLifeCycle lifeCycle = getSFSBLifeCycle();
292         lifeCycle.postConstructEasyBeansLifeCycle();
293         assertTrue(statefulBean.isPostConstructCalled());
294     }
295
296     /**
297      * Test the preDestroy on stateful bean.
298      */

299     @Test
300     public void testStatefulPreDestroy() {
301         assertFalse(statefulBean.isPreDestroyCalled());
302         EasyBeansSFSBLifeCycle lifeCycle = getSFSBLifeCycle();
303         lifeCycle.preDestroyEasyBeansLifeCycle();
304         assertTrue(statefulBean.isPreDestroyCalled());
305     }
306
307     /**
308      * Test the prePassivate on stateful bean.
309      */

310     @Test
311     public void testStatefulPrePassivate() {
312         assertFalse(statefulBean.isprePassivateCalled());
313         EasyBeansSFSBLifeCycle lifeCycle = getSFSBLifeCycle();
314         lifeCycle.prePassivateEasyBeansLifeCycle();
315         assertTrue(statefulBean.isprePassivateCalled());
316     }
317
318     /**
319      * Test the PostActivate on stateful bean.
320      */

321     @Test
322     public void testStatefulPostActivate() {
323         assertFalse(statefulBean.isPostActivateCalled());
324         EasyBeansSFSBLifeCycle lifeCycle = getSFSBLifeCycle();
325         lifeCycle.postActivateEasyBeansLifeCycle();
326         assertTrue(statefulBean.isPostActivateCalled());
327     }
328
329
330     /**
331      * Test the inheritance on lifecycle method.
332      * Super methods should be called.
333      */

334     @Test
335     public void testInheritanceBeanLifeCycleStateful() {
336         assertEquals(0, statefulBean.getCounter());
337         assertEquals(0, statefulBean.getSuperLifeCycleCounter());
338
339         EasyBeansSFSBLifeCycle lifeCycle = getSFSBLifeCycle();
340
341         // postActivate should increment the counter
342
lifeCycle.postActivateEasyBeansLifeCycle();
343         assertEquals(1, statefulBean.getCounter());
344         // and the counter of the super class
345
assertEquals(1, statefulBean.getSuperLifeCycleCounter());
346
347         // increment counter of default class
348
lifeCycle.prePassivateEasyBeansLifeCycle();
349         assertEquals(2, statefulBean.getCounter());
350         // prePassivate should decrement the counter
351
assertEquals(0, statefulBean.getSuperLifeCycleCounter());
352     }
353
354 }
355
Popular Tags