1   /*
2    * $Id: DefaultExceptionStrategyTestCase.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
11  package org.mule;
12  
13  import org.mule.api.context.notification.ExceptionNotificationListener;
14  import org.mule.api.context.notification.ServerNotification;
15  import org.mule.context.notification.ExceptionNotification;
16  import org.mule.tck.AbstractMuleTestCase;
17  
18  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
19  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
20  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
21  
22  public class DefaultExceptionStrategyTestCase extends AbstractMuleTestCase
23  {
24      // MULE-1404
25      public void testExceptions() throws Exception
26      {
27          InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy();
28          strategy.setMuleContext(muleContext);
29          strategy.exceptionThrown(new IllegalArgumentException("boom"));
30          assertEquals(1, strategy.getCount());
31      }
32  
33      // MULE-1627
34      public void testExceptionNotifications() throws Exception
35      {
36          final CountDownLatch latch = new CountDownLatch(1);
37          final AtomicInteger notificationCount = new AtomicInteger(0);
38  
39          muleContext.registerListener(new ExceptionNotificationListener()
40          {
41              public void onNotification(ServerNotification notification)
42              {
43                  if (notification.getAction() == ExceptionNotification.EXCEPTION_ACTION)
44                  {
45                      assertEquals("exception", notification.getActionName());
46                      assertEquals("Wrong info type", ServerNotification.TYPE_ERROR, notification.getType());
47                      notificationCount.incrementAndGet();
48                      latch.countDown();
49                  }
50              }
51          });
52  
53          // throwing exception
54          InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy();
55          strategy.setMuleContext(muleContext);
56          strategy.exceptionThrown(new IllegalArgumentException("boom"));
57  
58          // Wait for the notifcation event to be fired as they are queue
59          latch.await(2000, TimeUnit.MILLISECONDS);
60          assertEquals(1, notificationCount.get());
61  
62      }
63  
64      private class InstrumentedExceptionStrategy extends DefaultExceptionStrategy
65      {
66          private volatile int count = 0;
67  
68          // @Override
69          protected void defaultHandler(Throwable t)
70          {
71              count++;
72              super.defaultHandler(t);
73          }
74  
75          // @Override
76          protected void logException(Throwable t)
77          {
78              // do not log anything here, we're running as part of a unit test
79          }
80  
81          public int getCount()
82          {
83              return count;
84          }
85      }
86  
87  }