View Javadoc

1   /*
2    * $Id: ExceptionListenerTestCase.java 19026 2010-08-16 07:30:47Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.module.ibeans.annotations;
11  
12  import java.beans.ExceptionListener;
13  import java.net.UnknownHostException;
14  import java.util.concurrent.atomic.AtomicBoolean;
15  
16  import org.ibeans.annotation.IntegrationBean;
17  import org.junit.Test;
18  
19  /**
20   * Tests using an exception listener to intercept all exceptions on the ibean.  Also test that parsing the ibean will not barf if
21   * the ibean extends {@link org.ibeans.api.ExceptionListenerAware}
22   */
23  public class ExceptionListenerTestCase extends AbstractIBeansTestCase
24  {
25      @SuppressWarnings("unused")    
26      @IntegrationBean
27      private TestExceptionIBean test;
28  
29  
30      @Test
31      public void testExceptionIsCaughtByListener() throws Exception
32      {
33          final AtomicBoolean exceptionThrown = new AtomicBoolean(false);
34          test.setExceptionListener(new ExceptionListener()
35          {
36              public void exceptionThrown(Exception e)
37              {
38                  exceptionThrown.set(true);
39              }
40          });
41          String data = test.doSomething("blah");
42          //Exception should not be thrown, instead the listener intercepts it
43          assertTrue(exceptionThrown.get());
44          assertNull(data);
45      }
46  
47      @Test
48      public void testExceptionOfDifferentTypeIsCaughtByListener() throws Exception
49      {
50          final AtomicBoolean exceptionThrown = new AtomicBoolean(false);
51          test.setExceptionListener(new ExceptionListener()
52          {
53              public void exceptionThrown(Exception e)
54              {
55                  exceptionThrown.set(true);
56                  assertTrue(e instanceof UnknownHostException);
57              }
58          });
59          String data = test.doSomethingElse();
60          //Exception should not be thrown, instead the listener intercepts it
61          assertTrue(exceptionThrown.get());
62          assertNull(data);
63      }
64  }
65