View Javadoc

1   /*
2    * $Id: ExceptionListenerTestCase.java 22409 2011-07-14 05:14:27Z 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  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.assertTrue;
21  
22  /**
23   * Tests using an exception listener to intercept all exceptions on the ibean.  Also test that parsing the ibean will not barf if
24   * the ibean extends {@link org.ibeans.api.ExceptionListenerAware}
25   */
26  public class ExceptionListenerTestCase extends AbstractIBeansTestCase
27  {
28      @IntegrationBean
29      private TestExceptionIBean test;
30  
31      @Test
32      public void testExceptionIsCaughtByListener() throws Exception
33      {
34          final AtomicBoolean exceptionThrown = new AtomicBoolean(false);
35          test.setExceptionListener(new ExceptionListener()
36          {
37              @Override
38              public void exceptionThrown(Exception e)
39              {
40                  exceptionThrown.set(true);
41              }
42          });
43          String data = test.doSomething("blah");
44          //Exception should not be thrown, instead the listener intercepts it
45          assertTrue(exceptionThrown.get());
46          assertNull(data);
47      }
48  
49      @Test
50      public void testExceptionOfDifferentTypeIsCaughtByListener() throws Exception
51      {
52          final AtomicBoolean exceptionThrown = new AtomicBoolean(false);
53          test.setExceptionListener(new ExceptionListener()
54          {
55              @Override
56              public void exceptionThrown(Exception e)
57              {
58                  exceptionThrown.set(true);
59                  assertTrue(e instanceof UnknownHostException);
60              }
61          });
62          String data = test.doSomethingElse();
63          //Exception should not be thrown, instead the listener intercepts it
64          assertTrue(exceptionThrown.get());
65          assertNull(data);
66      }
67  }
68