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