View Javadoc

1   /*
2    * $Id: ComponentMessageNotificationNoXMLTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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      public ComponentMessageNotificationNoXMLTestCase()
34      {
35          setDisposeManagerPerSuite(true);
36      }
37  
38      protected void configureMuleContext(MuleContextBuilder contextBuilder)
39      {
40          ServerNotificationManager notificationManager = new ServerNotificationManager();
41          notificationManager.setNotificationDynamic(true);
42          notificationManager.addInterfaceToType(ComponentMessageNotificationListener.class,
43              ComponentMessageNotification.class);
44          contextBuilder.setNotificationManager(notificationManager);
45      }
46  
47      protected void doSetUp() throws Exception
48      {
49          setDisposeManagerPerSuite(true);
50          componentListener = new ComponentListener();
51          service = getTestService("seda", EchoComponent.class);
52          if(!muleContext.isStarted()) muleContext.start();
53      }
54  
55      public void testComponentNotificationNotRegistered() throws Exception
56      {
57          assertFalse(componentListener.isNotified());
58  
59          service.sendEvent(MuleTestUtils.getTestInboundEvent("test data", muleContext));
60  
61          assertFalse(componentListener.isNotified());
62          assertFalse(componentListener.isBefore());
63          assertFalse(componentListener.isAfter());
64      }
65  
66      public void testComponentNotification() throws Exception
67      {
68          // Need to configure NotificationManager as "dynamic" in order to do this.
69          muleContext.registerListener(componentListener);
70  
71          assertFalse(componentListener.isNotified());
72  
73          service.sendEvent(MuleTestUtils.getTestInboundEvent("test data", muleContext));
74  
75          // threaded processing, make sure the notifications have time to process
76          Thread.sleep(100);
77  
78          assertTrue(componentListener.isNotified());
79          assertTrue(componentListener.isBefore());
80          assertTrue(componentListener.isAfter());
81      }
82  
83      class ComponentListener implements ComponentMessageNotificationListener
84      {
85  
86          private ServerNotification notification = null;
87  
88          private boolean before = false;
89  
90          private boolean after = false;
91  
92          public void onNotification(ServerNotification notification)
93          {
94              this.notification = notification;
95              assertEquals(ComponentMessageNotification.class, notification.getClass());
96              assertTrue(notification.getSource() instanceof MuleMessage);
97              assertNotNull(((ComponentMessageNotification) notification).getServiceName());
98  
99              if (notification.getAction() == ComponentMessageNotification.COMPONENT_PRE_INVOKE)
100             {
101                 before = true;
102             }
103             else if (notification.getAction() == ComponentMessageNotification.COMPONENT_POST_INVOKE)
104             {
105                 after = true;
106             }
107         }
108 
109         public boolean isNotified()
110         {
111             return null != notification;
112         }
113 
114         /**
115          * @return the before
116          */
117         public boolean isBefore()
118         {
119             return before;
120         }
121 
122         /**
123          * @return the after
124          */
125         public boolean isAfter()
126         {
127             return after;
128         }
129 
130     }
131 
132 }