Clover coverage report -
Coverage timestamp: Thu Jun 22 2006 14:24:50 CEST
file stats: LOC: 367   Methods: 19
NCLOC: 214   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BusinessInterceptorsTestCase.java 53.6% 80.2% 100% 77.8%
coverage coverage
 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 55 2006-02-28 15:27:10Z benoitf $
 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    * 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  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    * Tests the boolean.
 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    * Tests the bytes.
 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    * Tests the chars.
 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    * Tests the double.
 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    * Tests the float.
 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    * Tests the int.
 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    * Tests the long.
 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    * Tests the short.
 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    * Mix of primitive / object.
 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    * Test a user defined exception.
 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    * Test a user defined exception.
 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    * Test user defined exceptions and exceptions.
 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    * Test that aroundInvoke in the bean has increased a counter.
 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    * Test that interceptor has increased a counter.
 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    * Test that interceptor throw an exception.
 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    * Test that value is increased by interceptor.
 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    * Test that the interceptor is only called on a stateless
 337    * bean and on singleMethodIntercepted method.
 338    */
 339  2 @Test
 340    public void testSingleMethodIntercepted() {
 341  2 int count = statelessBean.getIncrementSingleMethodInterceptedCounter();
 342  2 assertTrue(count == 0);
 343    // Counter will increment
 344  2 statelessBean.singleMethodIntercepted();
 345  2 assertEquals(1, statelessBean.getIncrementSingleMethodInterceptedCounter());
 346    // counter shouldn't increment with another method
 347  2 statelessBean.addInt(1, 2);
 348  2 assertEquals(1, statelessBean.getIncrementSingleMethodInterceptedCounter());
 349    }
 350   
 351    /**
 352    * Test that no interceptors are called on this method.
 353    */
 354  2 @Test
 355    public void testExcludedInterceptorsMethod() {
 356    // start : counter = 0, interceptor not called
 357  2 int count = statelessBean.getOtherInterceptorCounter();
 358  2 assert count == 0;
 359   
 360    // call the business method on the bean
 361  2 statelessBean.excludedInterceptorsMethod();
 362   
 363    // Check that counter = 0 <-- means that it was excluded
 364  2 assert statelessBean.getOtherInterceptorCounter() == 0;
 365    }
 366   
 367    }