1   /*
2    * $Id: ComponentMessageNotificationNoXMLTestCase.java 12252 2008-07-08 01:55:10Z 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.context.notification;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.context.MuleContextBuilder;
15  import org.mule.api.context.notification.ComponentMessageNotificationListener;
16  import org.mule.api.context.notification.ServerNotification;
17  import org.mule.api.service.Service;
18  import org.mule.component.simple.EchoComponent;
19  import org.mule.tck.AbstractMuleTestCase;
20  import org.mule.tck.MuleTestUtils;
21  
22  /**
23   * Test ComponentNotifications/Listeners by sending events to a component. A pre and
24   * post notification should be received by listeners.
25   */
26  public class ComponentMessageNotificationNoXMLTestCase extends AbstractMuleTestCase
27  {
28  
29      protected Service service;
30      protected ServerNotificationManager manager;
31      protected ComponentListener componentListener;
32  
33      protected void configureMuleContext(MuleContextBuilder contextBuilder)
34      {
35          ServerNotificationManager notificationManager = new ServerNotificationManager();
36          notificationManager.setNotificationDynamic(true);
37          notificationManager.addInterfaceToType(ComponentMessageNotificationListener.class,
38              ComponentMessageNotification.class);
39          contextBuilder.setNotificationManager(notificationManager);
40      }
41  
42      protected void doSetUp() throws Exception
43      {
44          componentListener = new ComponentListener();
45          service = getTestService("seda", EchoComponent.class);
46          muleContext.start();
47      }
48  
49      public void testComponentNotificationNotRegistered() throws Exception
50      {
51          assertFalse(componentListener.isNotified());
52  
53          service.sendEvent(MuleTestUtils.getTestInboundEvent("test data", muleContext));
54  
55          assertFalse(componentListener.isNotified());
56          assertFalse(componentListener.isBefore());
57          assertFalse(componentListener.isAfter());
58      }
59  
60      public void testComponentNotification() throws Exception
61      {
62          // Need to configure NotificationManager as "dynamic" in order to do this.
63          muleContext.registerListener(componentListener);
64  
65          assertFalse(componentListener.isNotified());
66  
67          service.sendEvent(MuleTestUtils.getTestInboundEvent("test data", muleContext));
68  
69          // threaded processing, make sure the notifications have time to process
70          Thread.sleep(100);
71  
72          assertTrue(componentListener.isNotified());
73          assertTrue(componentListener.isBefore());
74          assertTrue(componentListener.isAfter());
75      }
76  
77      class ComponentListener implements ComponentMessageNotificationListener
78      {
79  
80          private ServerNotification notification = null;
81  
82          private boolean before = false;
83  
84          private boolean after = false;
85  
86          public void onNotification(ServerNotification notification)
87          {
88              this.notification = notification;
89              assertEquals(ComponentMessageNotification.class, notification.getClass());
90              assertTrue(notification.getSource() instanceof MuleMessage);
91              assertNotNull(((ComponentMessageNotification) notification).getComponent());
92  
93              if (notification.getAction() == ComponentMessageNotification.COMPONENT_PRE_INVOKE)
94              {
95                  before = true;
96              }
97              else if (notification.getAction() == ComponentMessageNotification.COMPONENT_POST_INVOKE)
98              {
99                  after = true;
100             }
101         }
102 
103         public boolean isNotified()
104         {
105             return null != notification;
106         }
107 
108         /**
109          * @return the before
110          */
111         public boolean isBefore()
112         {
113             return before;
114         }
115 
116         /**
117          * @return the after
118          */
119         public boolean isAfter()
120         {
121             return after;
122         }
123 
124     }
125 
126 }