1 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 ; 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 44 public class BusinessInterceptorsTestCase{ 45 46 49 private StatelessBean statelessBean = null; 50 51 54 private static boolean enhancingDone = false; 55 56 60 @BeforeMethod 61 protected void setUp() throws Exception { 62 if (!enhancingDone) { 63 BusinessInterceptorsClassesEnhancer.enhance(); 64 enhancingDone = true; 65 } 66 statelessBean = new StatelessBean(); 67 } 68 69 70 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 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 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 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 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 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 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 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 176 @Test 177 @SuppressWarnings ("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 o = new Object (); 187 188 Object [] returnObject = new Object [] {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 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 e) { 206 fail("Not the expected type of exception"); 207 } 208 } 209 210 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 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 e) { 234 fail("Not the expected type of exception"); 235 } 236 } 237 238 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 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 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 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 e) { 280 if (!e.getMessage().equals("someCustomizedExceptions3.RuntimeException")) { 281 fail("Exception doesn't contain the expected value"); 282 } 283 } catch (Exception e) { 284 fail("Not the expected type of exception"); 285 } 286 287 } 288 289 290 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 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 315 @Test 316 public void testInterceptorThrowException() { 317 try { 318 statelessBean.throwExceptionByInterceptor(); 319 fail("Should have thrown an exception by the interceptor."); 320 } catch (RuntimeException e) { 321 assertEquals(e.getCause().getMessage(), "Throw an exception on throwExceptionByInterceptor"); 322 } 323 } 324 325 326 329 @Test 330 public void testValueDoubledByInterceptor() { 331 int i = 1; 332 assertEquals(i * 2, statelessBean.valueDoubledByInterceptor(i)); 333 } 334 335 339 @Test 340 public void testSingleMethodIntercepted() { 341 int count = statelessBean.getIncrementSingleMethodInterceptedCounter(); 342 assertTrue(count == 0); 343 statelessBean.singleMethodIntercepted(); 345 assertEquals(1, statelessBean.getIncrementSingleMethodInterceptedCounter()); 346 statelessBean.addInt(1, 2); 348 assertEquals(1, statelessBean.getIncrementSingleMethodInterceptedCounter()); 349 } 350 351 354 @Test 355 public void testExcludedInterceptorsMethod() { 356 int count = statelessBean.getOtherInterceptorCounter(); 358 assert count == 0; 359 360 statelessBean.excludedInterceptorsMethod(); 362 363 assert statelessBean.getOtherInterceptorCounter() == 0; 365 } 366 367 } 368 | Popular Tags |