View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.context.notification.ExceptionNotificationListener;
11  import org.mule.api.context.notification.ServerNotification;
12  import org.mule.context.notification.ExceptionNotification;
13  import org.mule.exception.DefaultSystemExceptionStrategy;
14  import org.mule.tck.junit4.AbstractMuleContextTestCase;
15  
16  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
17  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
18  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  public class DefaultExceptionStrategyTestCase extends AbstractMuleContextTestCase
24  {
25      // MULE-1404
26      @Test
27      public void testExceptions() throws Exception
28      {
29          InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy(muleContext);
30          strategy.setMuleContext(muleContext);
31          strategy.handleException(new IllegalArgumentException("boom"));
32          assertEquals(1, strategy.getCount());
33      }
34  
35      // MULE-1627
36      @Test
37      public void testExceptionNotifications() throws Exception
38      {
39          final CountDownLatch latch = new CountDownLatch(1);
40          final AtomicInteger notificationCount = new AtomicInteger(0);
41  
42          muleContext.registerListener(new ExceptionNotificationListener<ExceptionNotification>()
43          {
44              public void onNotification(ExceptionNotification notification)
45              {
46                  if (notification.getAction() == ExceptionNotification.EXCEPTION_ACTION)
47                  {
48                      assertEquals("exception", notification.getActionName());
49                      assertEquals("Wrong info type", ServerNotification.TYPE_ERROR, notification.getType());
50                      notificationCount.incrementAndGet();
51                      latch.countDown();
52                  }
53              }
54          });
55  
56          // throwing exception
57          InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy(muleContext);
58          strategy.setMuleContext(muleContext);
59          strategy.handleException(new IllegalArgumentException("boom"));
60  
61          // Wait for the notifcation event to be fired as they are queue
62          latch.await(2000, TimeUnit.MILLISECONDS);
63          assertEquals(1, notificationCount.get());
64  
65      }
66  
67      private static class InstrumentedExceptionStrategy extends DefaultSystemExceptionStrategy
68      {
69          private volatile int count = 0;
70  
71          public InstrumentedExceptionStrategy(MuleContext muleContext)
72          {
73              super(muleContext);
74          }
75          
76          @Override
77          public void handleException(Exception exception)
78          {
79              count++;
80              super.handleException(exception);
81          }
82  
83          public int getCount()
84          {
85              return count;
86          }
87      }
88  }