1
2
3
4
5
6
7
8
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
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
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
56 InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy(muleContext);
57 strategy.setMuleContext(muleContext);
58 strategy.handleException(new IllegalArgumentException("boom"));
59
60
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 }