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