1
2
3
4
5
6
7
8
9
10
11 package org.mule;
12
13 import org.mule.api.context.notification.ExceptionNotificationListener;
14 import org.mule.api.context.notification.ServerNotification;
15 import org.mule.context.notification.ExceptionNotification;
16 import org.mule.tck.AbstractMuleTestCase;
17
18 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
19 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
20 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
21
22 public class DefaultExceptionStrategyTestCase extends AbstractMuleTestCase
23 {
24
25 public void testExceptions() throws Exception
26 {
27 InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy();
28 strategy.setMuleContext(muleContext);
29 strategy.exceptionThrown(new IllegalArgumentException("boom"));
30 assertEquals(1, strategy.getCount());
31 }
32
33
34 public void testExceptionNotifications() throws Exception
35 {
36 final CountDownLatch latch = new CountDownLatch(1);
37 final AtomicInteger notificationCount = new AtomicInteger(0);
38
39 muleContext.registerListener(new ExceptionNotificationListener()
40 {
41 public void onNotification(ServerNotification notification)
42 {
43 if (notification.getAction() == ExceptionNotification.EXCEPTION_ACTION)
44 {
45 assertEquals("exception", notification.getActionName());
46 assertEquals("Wrong info type", ServerNotification.TYPE_ERROR, notification.getType());
47 notificationCount.incrementAndGet();
48 latch.countDown();
49 }
50 }
51 });
52
53
54 InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy();
55 strategy.setMuleContext(muleContext);
56 strategy.exceptionThrown(new IllegalArgumentException("boom"));
57
58
59 latch.await(2000, TimeUnit.MILLISECONDS);
60 assertEquals(1, notificationCount.get());
61
62 }
63
64 private class InstrumentedExceptionStrategy extends DefaultExceptionStrategy
65 {
66 private volatile int count = 0;
67
68
69 protected void defaultHandler(Throwable t)
70 {
71 count++;
72 super.defaultHandler(t);
73 }
74
75
76 protected void logException(Throwable t)
77 {
78
79 }
80
81 public int getCount()
82 {
83 return count;
84 }
85 }
86
87 }