1
2
3
4
5
6
7 package org.mule.config.spring.parsers.specific;
8
9 import org.mule.DefaultMuleEvent;
10 import org.mule.DefaultMuleMessage;
11 import org.mule.api.MuleContext;
12 import org.mule.api.context.notification.SecurityNotificationListener;
13 import org.mule.api.context.notification.ServerNotification;
14 import org.mule.api.context.notification.ServerNotificationListener;
15 import org.mule.api.security.UnauthorisedException;
16 import org.mule.config.i18n.CoreMessages;
17 import org.mule.context.notification.ListenerSubscriptionPair;
18 import org.mule.context.notification.SecurityNotification;
19 import org.mule.context.notification.ServerNotificationManager;
20 import org.mule.tck.junit4.FunctionalTestCase;
21 import org.mule.transport.NullPayload;
22
23 import java.util.Collection;
24
25 import org.junit.Test;
26
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 public class ServerNotificationManagerTestCase extends FunctionalTestCase
32 {
33
34 @Override
35 protected String getConfigResources()
36 {
37 return "org/mule/config/spring/parsers/specific/server-notification-manager-test.xml";
38 }
39
40 @Test
41 public void testDynamicAttribute()
42 {
43 ServerNotificationManager manager = muleContext.getNotificationManager();
44 assertTrue(manager.isNotificationDynamic());
45 }
46
47 @Test
48 public void testRoutingConfiguration()
49 {
50 ServerNotificationManager manager = muleContext.getNotificationManager();
51 assertTrue(manager.getInterfaceToTypes().size() > 2);
52 Object ifaces = manager.getInterfaceToTypes().get(TestInterface.class);
53 assertNotNull(ifaces);
54 assertTrue(ifaces instanceof Collection);
55 assertTrue(((Collection) ifaces).contains(TestEvent.class));
56 ifaces = manager.getInterfaceToTypes().get(TestInterface2.class);
57 assertNotNull(ifaces);
58 assertTrue(ifaces instanceof Collection);
59 assertTrue(((Collection) ifaces).contains(SecurityNotification.class));
60 }
61
62 @Test
63 public void testSimpleNotification() throws InterruptedException
64 {
65 ServerNotificationManager manager = muleContext.getNotificationManager();
66 Collection listeners = manager.getListeners();
67
68 assertTrue(listeners.size() > 5);
69 TestListener listener = (TestListener) muleContext.getRegistry().lookupObject("listener");
70 assertNotNull(listener);
71 assertFalse(listener.isCalled());
72 manager.fireNotification(new TestEvent());
73 Thread.sleep(1000);
74 assertTrue(listener.isCalled());
75 }
76
77 @Test
78 public void testExplicitlyConiguredNotificationListenerRegistration() throws InterruptedException
79 {
80 ServerNotificationManager manager = muleContext.getNotificationManager();
81 assertTrue(manager.getListeners().contains(
82 new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
83 "listener"), null)));
84 assertTrue(manager.getListeners().contains(
85 new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
86 "listener2"), null)));
87 assertTrue(manager.getListeners().contains(
88 new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
89 "securityListener"), null)));
90 assertTrue(manager.getListeners().contains(
91 new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
92 "listener3"), "*")));
93 }
94
95 @Test
96 public void testAdhocNotificationListenerRegistrations() throws InterruptedException
97 {
98 ServerNotificationManager manager = muleContext.getNotificationManager();
99
100
101
102 assertFalse(manager.getListeners().contains(
103 new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
104 "listener3"), null)));
105
106
107 assertTrue(manager.getListeners().contains(
108 new ListenerSubscriptionPair((ServerNotificationListener) muleContext.getRegistry().lookupObject(
109 "listener4"), null)));
110 }
111
112 @Test
113 public void testDisabledNotification() throws InterruptedException
114 {
115 ServerNotificationManager manager = muleContext.getNotificationManager();
116 Collection listeners = manager.getListeners();
117
118 assertTrue(listeners.size() > 5);
119 TestListener2 listener2 = (TestListener2) muleContext.getRegistry().lookupObject("listener2");
120 assertNotNull(listener2);
121 assertFalse(listener2.isCalled());
122 TestSecurityListener adminListener = (TestSecurityListener) muleContext.getRegistry().lookupObject(
123 "securityListener");
124 assertNotNull(adminListener);
125 assertFalse(adminListener.isCalled());
126 manager.fireNotification(new TestSecurityEvent(muleContext));
127 Thread.sleep(1000);
128 assertTrue(listener2.isCalled());
129 assertFalse(adminListener.isCalled());
130 }
131
132 protected static interface TestInterface extends ServerNotificationListener
133 {
134
135 }
136
137 protected static interface TestInterface2 extends ServerNotificationListener
138 {
139
140 }
141
142 protected static class TestListener implements TestInterface
143 {
144
145 private boolean called = false;
146
147 public boolean isCalled()
148 {
149 return called;
150 }
151
152 public void onNotification(ServerNotification notification)
153 {
154 called = true;
155 }
156
157 }
158
159 protected static class TestListener2 implements TestInterface2
160 {
161
162 private boolean called = false;
163
164 public boolean isCalled()
165 {
166 return called;
167 }
168
169 public void onNotification(ServerNotification notification)
170 {
171 called = true;
172 }
173
174 }
175
176 protected static class TestSecurityListener implements SecurityNotificationListener<SecurityNotification>
177 {
178
179 private boolean called = false;
180
181 public boolean isCalled()
182 {
183 return called;
184 }
185
186 public void onNotification(SecurityNotification notification)
187 {
188 called = true;
189 }
190
191 }
192
193 protected static class TestEvent extends ServerNotification
194 {
195
196 public TestEvent()
197 {
198 super(new Object(), 0);
199 }
200
201 }
202
203 protected static class TestSecurityEvent extends SecurityNotification
204 {
205
206 public TestSecurityEvent(MuleContext muleContext)
207 {
208 super(new UnauthorisedException(CoreMessages.createStaticMessage("dummy"),
209 new DefaultMuleEvent(new DefaultMuleMessage(NullPayload.getInstance(), muleContext),
210 null, null)), 0);
211 }
212
213 }
214
215 }