1
2
3
4
5
6
7
8
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.MuleTestUtils;
20 import org.mule.tck.junit4.AbstractMuleContextTestCase;
21
22 import org.junit.Test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29
30
31
32
33 public class ComponentMessageNotificationNoXMLTestCase extends AbstractMuleContextTestCase
34 {
35
36 protected Service service;
37 protected ServerNotificationManager manager;
38 protected ComponentListener componentListener;
39
40 public ComponentMessageNotificationNoXMLTestCase()
41 {
42 setDisposeContextPerClass(true);
43 }
44
45 protected void configureMuleContext(MuleContextBuilder contextBuilder)
46 {
47 ServerNotificationManager notificationManager = new ServerNotificationManager();
48 notificationManager.setNotificationDynamic(true);
49 notificationManager.addInterfaceToType(ComponentMessageNotificationListener.class,
50 ComponentMessageNotification.class);
51 contextBuilder.setNotificationManager(notificationManager);
52 }
53
54 @Override
55 protected void doSetUp() throws Exception
56 {
57 setDisposeContextPerClass(true);
58 componentListener = new ComponentListener();
59 service = getTestService("seda", EchoComponent.class);
60 if(!muleContext.isStarted()) muleContext.start();
61 }
62
63 @Test
64 public void testComponentNotificationNotRegistered() throws Exception
65 {
66 assertFalse(componentListener.isNotified());
67
68 service.sendEvent(MuleTestUtils.getTestEvent("test data", muleContext));
69
70 assertFalse(componentListener.isNotified());
71 assertFalse(componentListener.isBefore());
72 assertFalse(componentListener.isAfter());
73 }
74
75 @Test
76 public void testComponentNotification() throws Exception
77 {
78
79 muleContext.registerListener(componentListener);
80
81 assertFalse(componentListener.isNotified());
82
83 service.sendEvent(MuleTestUtils.getTestEvent("test data", muleContext));
84
85
86 Thread.sleep(100);
87
88 assertTrue(componentListener.isNotified());
89 assertTrue(componentListener.isBefore());
90 assertTrue(componentListener.isAfter());
91 }
92
93 class ComponentListener implements ComponentMessageNotificationListener
94 {
95
96 private ServerNotification notification = null;
97
98 private boolean before = false;
99
100 private boolean after = false;
101
102 public void onNotification(ServerNotification notification)
103 {
104 this.notification = notification;
105 assertEquals(ComponentMessageNotification.class, notification.getClass());
106 assertTrue(notification.getSource() instanceof MuleMessage);
107 assertNotNull(((ComponentMessageNotification) notification).getServiceName());
108
109 if (notification.getAction() == ComponentMessageNotification.COMPONENT_PRE_INVOKE)
110 {
111 before = true;
112 }
113 else if (notification.getAction() == ComponentMessageNotification.COMPONENT_POST_INVOKE)
114 {
115 after = true;
116 }
117 }
118
119 public boolean isNotified()
120 {
121 return null != notification;
122 }
123
124
125
126
127 public boolean isBefore()
128 {
129 return before;
130 }
131
132
133
134
135 public boolean isAfter()
136 {
137 return after;
138 }
139
140 }
141
142 }