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