1
2
3
4
5
6
7
8
9
10
11 package org.mule.context.notification;
12
13 import org.mule.api.context.notification.ServerNotification;
14 import org.mule.tck.FunctionalTestCase;
15
16 import java.util.Iterator;
17
18
19
20
21 public abstract class AbstractNotificationTestCase extends FunctionalTestCase
22 {
23
24 private AbstractNotificationLogger notifications;
25
26 public AbstractNotificationTestCase()
27 {
28 super();
29 setDisposeManagerPerSuite(true);
30 }
31
32 public final void testNotifications() throws Exception
33 {
34 doTest();
35 notifications = (AbstractNotificationLogger) muleContext.getRegistry().lookupObject("notificationLogger");
36 }
37
38 public abstract void doTest() throws Exception;
39
40 public abstract RestrictedNode getSpecification();
41
42 public abstract void validateSpecification(RestrictedNode spec) throws Exception;
43
44 protected void suitePostTearDown() throws Exception
45 {
46
47 muleContext.dispose();
48
49 Thread.sleep(2000L);
50 logNotifications();
51 RestrictedNode spec = getSpecification();
52 validateSpecification(spec);
53 assertExpectedNotifications(spec);
54 }
55
56 protected void logNotifications()
57 {
58 logger.info("Number of notifications: " + notifications.getNotifications().size());
59 for (Iterator iterator = notifications.getNotifications().iterator(); iterator.hasNext();)
60 {
61 ServerNotification notification = (ServerNotification) iterator.next();
62 logger.info(notification);
63 }
64 }
65
66
67
68
69 protected void assertExpectedNotifications(RestrictedNode spec)
70 {
71 for (Iterator iterator = notifications.getNotifications().iterator(); iterator.hasNext();)
72 {
73 ServerNotification notification = (ServerNotification) iterator.next();
74 switch (spec.match(notification))
75 {
76 case Node.SUCCESS:
77 break;
78 case Node.FAILURE:
79 fail("Could not match " + notification);
80 break;
81 case Node.EMPTY:
82 fail("Extra notification: " + notification);
83 }
84 }
85 if (!spec.isExhausted())
86 {
87 fail("Specification not exhausted: " + spec.getAnyRemaining());
88 }
89 }
90
91 protected void verifyAllNotifications(RestrictedNode spec, Class clazz, int from, int to)
92 {
93 for (int action = from; action <= to; ++action)
94 {
95 if (!spec.contains(clazz, action))
96 {
97 fail("Specification missed action " + action + " for class " + clazz);
98 }
99 }
100 }
101
102 }