1   /*
2    * $Id: ServerNotificationManagerTestCase.java 10787 2008-02-12 18:51:50Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.spring.parsers.specific;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.context.notification.SecurityNotificationListener;
15  import org.mule.api.context.notification.ServerNotification;
16  import org.mule.api.context.notification.ServerNotificationListener;
17  import org.mule.api.security.UnauthorisedException;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.context.notification.SecurityNotification;
20  import org.mule.context.notification.ServerNotificationManager;
21  import org.mule.tck.FunctionalTestCase;
22  import org.mule.transport.NullPayload;
23  
24  import java.util.Collection;
25  
26  public class ServerNotificationManagerTestCase extends FunctionalTestCase
27  {
28  
29      protected String getConfigResources()
30      {
31          return "org/mule/config/spring/parsers/specific/server-notification-manager-test.xml";
32      }
33  
34      public void testDynamicAttribute()
35      {
36          ServerNotificationManager manager = muleContext.getNotificationManager();
37          assertTrue(manager.isNotificationDynamic());
38      }
39  
40      public void testRoutingConfiguration()
41      {
42          ServerNotificationManager manager = muleContext.getNotificationManager();
43          assertTrue(manager.getInterfaceToTypes().size() > 2);
44          Object ifaces = manager.getInterfaceToTypes().get(TestInterface.class);
45          assertNotNull(ifaces);
46          assertTrue(ifaces instanceof Collection);
47          assertTrue(((Collection) ifaces).contains(TestEvent.class));
48          ifaces = manager.getInterfaceToTypes().get(TestInterface2.class);
49          assertNotNull(ifaces);
50          assertTrue(ifaces instanceof Collection);
51          assertTrue(((Collection) ifaces).contains(SecurityNotification.class));
52      }
53  
54      public void testSimpleNotification() throws InterruptedException
55      {
56          ServerNotificationManager manager = muleContext.getNotificationManager();
57          Collection listeners = manager.getListeners();
58          assertEquals(3, listeners.size());
59          TestListener listener = (TestListener) muleContext.getRegistry().lookupObject("listener");
60          assertNotNull(listener);
61          assertFalse(listener.isCalled());
62          manager.fireNotification(new TestEvent());
63          Thread.sleep(1000); // asynch events
64          assertTrue(listener.isCalled());
65      }
66  
67      public void testDisabledNotification() throws InterruptedException
68      {
69          ServerNotificationManager manager = muleContext.getNotificationManager();
70          Collection listeners = manager.getListeners();
71          assertEquals(3, listeners.size());
72          TestListener2 listener2 =
73                  (TestListener2) muleContext.getRegistry().lookupObject("listener2");
74          assertNotNull(listener2);
75          assertFalse(listener2.isCalled());
76          TestSecurityListener adminListener =
77                  (TestSecurityListener) muleContext.getRegistry().lookupObject("securityListener");
78          assertNotNull(adminListener);
79          assertFalse(adminListener.isCalled());
80          manager.fireNotification(new TestSecurityEvent());
81          Thread.sleep(1000); // asynch events
82          assertTrue(listener2.isCalled());
83          assertFalse(adminListener.isCalled());
84      }
85  
86      protected static interface TestInterface extends ServerNotificationListener
87      {
88          // empty
89      }
90  
91      protected static interface TestInterface2 extends ServerNotificationListener
92      {
93          // empty
94      }
95  
96      protected static class TestListener implements TestInterface
97      {
98  
99          private boolean called = false;
100 
101         public boolean isCalled()
102         {
103             return called;
104         }
105 
106         public void onNotification(ServerNotification notification)
107         {
108             called = true;
109         }
110 
111     }
112 
113     protected static class TestListener2 implements TestInterface2
114     {
115 
116         private boolean called = false;
117 
118         public boolean isCalled()
119         {
120             return called;
121         }
122 
123         public void onNotification(ServerNotification notification)
124         {
125             called = true;
126         }
127 
128     }
129 
130     protected static class TestSecurityListener implements SecurityNotificationListener
131     {
132 
133         private boolean called = false;
134 
135         public boolean isCalled()
136         {
137             return called;
138         }
139 
140         public void onNotification(ServerNotification notification)
141         {
142             called = true;
143         }
144 
145     }
146 
147     protected static class TestEvent extends ServerNotification
148     {
149 
150         public TestEvent()
151         {
152             super(new Object(), 0);
153         }
154 
155     }
156 
157     protected static class TestSecurityEvent extends SecurityNotification
158     {
159 
160         public TestSecurityEvent()
161         {
162             super(new UnauthorisedException(CoreMessages.createStaticMessage("dummy"),
163                     new DefaultMuleMessage(NullPayload.getInstance())), 0);
164         }
165 
166     }
167 
168 
169 }