View Javadoc

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