1 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 46 public class LifeCycleInterceptorsTestCase{ 47 48 51 private StatelessBean statelessBean = null; 52 53 56 private StatelessBean2 statelessBean2 = null; 57 58 61 private StatelessBean3 statelessBean3 = null; 62 63 66 private StatefulBean statefulBean = null; 67 68 71 private static boolean enhancingDone = false; 72 73 77 @BeforeMethod 78 protected void setUp() throws Exception { 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 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 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 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 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 140 143 @Test 144 public void testStatelessBeanCallbacks() { 145 assertEquals(0, statelessBean.getCounter()); 146 EasyBeansSLSBLifeCycle lifeCycle = getSLSBLifeCycle(); 147 148 lifeCycle.postConstructEasyBeansLifeCycle(); 150 assertEquals(1, statelessBean.getCounter()); 151 152 lifeCycle.preDestroyEasyBeansLifeCycle(); 154 assertEquals(0, statelessBean.getCounter()); 155 } 156 157 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 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 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 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 210 @Test 211 public void testStateless3() { 212 assertEquals(0, statelessBean3.getCounter()); 213 214 EasyBeansSLSBLifeCycle lifeCycle = getSLSB3LifeCycle(); 215 216 lifeCycle.postConstructEasyBeansLifeCycle(); 218 assertEquals(1, statelessBean3.getCounter()); 219 220 lifeCycle.preDestroyEasyBeansLifeCycle(); 222 assertEquals(0, statelessBean3.getCounter()); 223 } 224 225 226 230 @Test 231 public void testInheritanceBeanLifeCycleStateless() { 232 assertEquals(0, statelessBean.getCounter()); 233 assertEquals(0, statelessBean.getSuperLifeCycleCounter()); 234 235 EasyBeansSLSBLifeCycle lifeCycle = getSLSBLifeCycle(); 236 237 lifeCycle.postConstructEasyBeansLifeCycle(); 239 assertEquals(1, statelessBean.getCounter()); 240 assertEquals(1, statelessBean.getSuperLifeCycleCounter()); 242 243 lifeCycle.preDestroyEasyBeansLifeCycle(); 245 assertEquals(0, statelessBean.getCounter()); 246 assertEquals(0, statelessBean.getSuperLifeCycleCounter()); 247 } 248 249 250 254 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 lifeCycle.postConstructEasyBeansLifeCycle(); 269 assertEquals(internalCounter++, statefulBean.getCounter()); 270 271 lifeCycle.preDestroyEasyBeansLifeCycle(); 273 assertEquals(internalCounter++, statefulBean.getCounter()); 274 275 lifeCycle.prePassivateEasyBeansLifeCycle(); 277 assertEquals(internalCounter++, statefulBean.getCounter()); 278 279 lifeCycle.postActivateEasyBeansLifeCycle(); 281 assertEquals(internalCounter++, statefulBean.getCounter()); 282 283 } 284 285 288 @Test 289 public void testStatefulPostConstruct() { 290 assertFalse(statefulBean.isPostConstructCalled()); 291 EasyBeansSFSBLifeCycle lifeCycle = getSFSBLifeCycle(); 292 lifeCycle.postConstructEasyBeansLifeCycle(); 293 assertTrue(statefulBean.isPostConstructCalled()); 294 } 295 296 299 @Test 300 public void testStatefulPreDestroy() { 301 assertFalse(statefulBean.isPreDestroyCalled()); 302 EasyBeansSFSBLifeCycle lifeCycle = getSFSBLifeCycle(); 303 lifeCycle.preDestroyEasyBeansLifeCycle(); 304 assertTrue(statefulBean.isPreDestroyCalled()); 305 } 306 307 310 @Test 311 public void testStatefulPrePassivate() { 312 assertFalse(statefulBean.isprePassivateCalled()); 313 EasyBeansSFSBLifeCycle lifeCycle = getSFSBLifeCycle(); 314 lifeCycle.prePassivateEasyBeansLifeCycle(); 315 assertTrue(statefulBean.isprePassivateCalled()); 316 } 317 318 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 334 @Test 335 public void testInheritanceBeanLifeCycleStateful() { 336 assertEquals(0, statefulBean.getCounter()); 337 assertEquals(0, statefulBean.getSuperLifeCycleCounter()); 338 339 EasyBeansSFSBLifeCycle lifeCycle = getSFSBLifeCycle(); 340 341 lifeCycle.postActivateEasyBeansLifeCycle(); 343 assertEquals(1, statefulBean.getCounter()); 344 assertEquals(1, statefulBean.getSuperLifeCycleCounter()); 346 347 lifeCycle.prePassivateEasyBeansLifeCycle(); 349 assertEquals(2, statefulBean.getCounter()); 350 assertEquals(0, statefulBean.getSuperLifeCycleCounter()); 352 } 353 354 } 355 | Popular Tags |