View Javadoc

1   /*
2    * $Id: DefaultExceptionStrategyTestCase.java 22772 2011-08-27 15:20:15Z dfeist $
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  
11  package org.mule;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  import org.mule.api.MuleContext;
16  import org.mule.api.context.notification.ExceptionNotificationListener;
17  import org.mule.api.context.notification.ServerNotification;
18  import org.mule.context.notification.ExceptionNotification;
19  import org.mule.exception.DefaultSystemExceptionStrategy;
20  import org.mule.tck.junit4.AbstractMuleContextTestCase;
21  
22  import java.util.concurrent.CountDownLatch;
23  import java.util.concurrent.TimeUnit;
24  import java.util.concurrent.atomic.AtomicInteger;
25  
26  import org.junit.Test;
27  
28  public class DefaultExceptionStrategyTestCase extends AbstractMuleContextTestCase
29  {
30      // MULE-1404
31      @Test
32      public void testExceptions() throws Exception
33      {
34          InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy(muleContext);
35          strategy.setMuleContext(muleContext);
36          strategy.handleException(new IllegalArgumentException("boom"));
37          assertEquals(1, strategy.getCount());
38      }
39  
40      // MULE-1627
41      @Test
42      public void testExceptionNotifications() throws Exception
43      {
44          final CountDownLatch latch = new CountDownLatch(1);
45          final AtomicInteger notificationCount = new AtomicInteger(0);
46  
47          muleContext.registerListener(new ExceptionNotificationListener<ExceptionNotification>()
48          {
49              public void onNotification(ExceptionNotification notification)
50              {
51                  if (notification.getAction() == ExceptionNotification.EXCEPTION_ACTION)
52                  {
53                      assertEquals("exception", notification.getActionName());
54                      assertEquals("Wrong info type", ServerNotification.TYPE_ERROR, notification.getType());
55                      notificationCount.incrementAndGet();
56                      latch.countDown();
57                  }
58              }
59          });
60  
61          // throwing exception
62          InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy(muleContext);
63          strategy.setMuleContext(muleContext);
64          strategy.handleException(new IllegalArgumentException("boom"));
65  
66          // Wait for the notifcation event to be fired as they are queue
67          latch.await(2000, TimeUnit.MILLISECONDS);
68          assertEquals(1, notificationCount.get());
69  
70      }
71  
72      private static class InstrumentedExceptionStrategy extends DefaultSystemExceptionStrategy
73      {
74          private volatile int count = 0;
75  
76          public InstrumentedExceptionStrategy(MuleContext muleContext)
77          {
78              super(muleContext);
79          }
80  
81          @Override
82          public void handleException(Exception exception)
83          {
84              count++;
85              super.handleException(exception);
86          }
87  
88          public int getCount()
89          {
90              return count;
91          }
92      }
93  }