1   /*
2    * $Id: DefaultExceptionStrategyTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.impl;
12  
13  import org.mule.impl.internal.notifications.ExceptionNotification;
14  import org.mule.impl.internal.notifications.ExceptionNotificationListener;
15  import org.mule.tck.AbstractMuleTestCase;
16  import org.mule.umo.manager.UMOManager;
17  import org.mule.umo.manager.UMOServerNotification;
18  
19  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
20  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
21  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
22  
23  public class DefaultExceptionStrategyTestCase extends AbstractMuleTestCase
24  {
25      private UMOManager manager;
26  
27      // @Override
28      protected void doSetUp() throws Exception
29      {
30          manager = getManager(true);
31          manager.start();
32      }
33  
34      // @Override
35      protected void doTearDown() throws Exception
36      {
37          manager.stop();
38      }
39  
40      // MULE-1404
41      public void testExceptions() throws Exception
42      {
43          InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy();
44          strategy.exceptionThrown(new IllegalArgumentException("boom"));
45          assertEquals(1, strategy.getCount());
46      }
47  
48      // MULE-1627
49      public void testExceptionNotifications() throws Exception
50      {
51          final CountDownLatch latch = new CountDownLatch(1);
52          final AtomicInteger notificationCount = new AtomicInteger(0);
53  
54          manager.registerListener(new ExceptionNotificationListener()
55          {
56              public void onNotification(UMOServerNotification notification)
57              {
58                  if (notification.getAction() == ExceptionNotification.EXCEPTION_ACTION)
59                  {
60                      assertEquals("exception", notification.getActionName());
61                      assertEquals("Wrong info type", UMOServerNotification.TYPE_ERROR, notification.getType());
62                      notificationCount.incrementAndGet();
63                      latch.countDown();
64                  }
65              }
66          });
67  
68          // throwing exception
69          DefaultExceptionStrategy listener = new DefaultExceptionStrategy();
70          listener.exceptionThrown(new IllegalArgumentException("boom"));
71  
72          // Wait for the notifcation event to be fired as they are queue
73          latch.await(2000, TimeUnit.MILLISECONDS);
74          assertEquals(1, notificationCount.get());
75  
76      }
77  
78      private class InstrumentedExceptionStrategy extends DefaultExceptionStrategy
79      {
80          private volatile int count = 0;
81  
82          // @Override
83          protected void defaultHandler(Throwable t)
84          {
85              count++;
86              super.defaultHandler(t);
87          }
88  
89          public int getCount()
90          {
91              return count;
92          }
93  
94      }
95  
96  }