1
2
3
4
5
6
7
8
9
10
11 package org.mule.source;
12
13 import org.mule.api.MuleEvent;
14 import org.mule.api.MuleException;
15 import org.mule.api.lifecycle.Startable;
16 import org.mule.api.lifecycle.Stoppable;
17 import org.mule.api.processor.MessageProcessor;
18 import org.mule.api.source.MessageSource;
19 import org.mule.tck.SensingNullMessageProcessor;
20 import org.mule.tck.junit4.AbstractMuleContextTestCase;
21 import org.mule.util.ObjectUtils;
22
23 import org.junit.Test;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.fail;
28
29 public class StartableCompositeMessageSourceTestCase extends AbstractMuleContextTestCase
30 {
31 protected SensingNullMessageProcessor listener;
32 protected SensingNullMessageProcessor listener2;
33 protected StartableCompositeMessageSource compositeSource;
34 protected MuleEvent testEvent;
35 protected NullMessageSource source;
36
37 @Override
38 protected void doSetUp() throws Exception
39 {
40 super.doSetUp();
41 listener = getSensingNullMessageProcessor();
42 listener2 = getSensingNullMessageProcessor();
43 compositeSource = getCompositeSource();
44 testEvent = getTestEvent(TEST_MESSAGE);
45 source = new NullMessageSource(testEvent);
46 }
47
48 protected StartableCompositeMessageSource getCompositeSource()
49 {
50 return new StartableCompositeMessageSource();
51 }
52
53 @Test
54 public void testAddSourceStopped() throws MuleException
55 {
56 compositeSource.setListener(listener);
57 compositeSource.addSource(source);
58
59 source.triggerSource();
60 assertNull(listener.event);
61
62 source.start();
63 try
64 {
65 source.triggerSource();
66 fail("Exception expected");
67 }
68 catch (Exception e)
69 {
70 }
71 assertNull(listener.event);
72
73 compositeSource.start();
74 source.triggerSource();
75 assertEquals(testEvent, listener.event);
76 }
77
78 @Test
79 public void testAddSourceStarted() throws MuleException
80 {
81 compositeSource.setListener(listener);
82 compositeSource.start();
83
84 compositeSource.addSource(source);
85
86 source.triggerSource();
87 assertEquals(testEvent, listener.event);
88 }
89
90 @Test
91 public void testRemoveSource() throws MuleException
92 {
93 compositeSource.setListener(listener);
94 compositeSource.addSource(source);
95 compositeSource.start();
96
97 source.triggerSource();
98 assertEquals(testEvent, listener.event);
99 listener.clear();
100
101 compositeSource.removeSource(source);
102 source.triggerSource();
103 assertNull(listener.event);
104 }
105
106 @Test
107 public void testSetListenerStarted() throws MuleException
108 {
109 compositeSource.addSource(source);
110 compositeSource.setListener(listener);
111 compositeSource.start();
112
113 source.triggerSource();
114 assertEquals(testEvent, listener.event);
115
116 listener.clear();
117 compositeSource.setListener(listener2);
118
119 source.triggerSource();
120 assertNull(listener.event);
121 assertEquals(testEvent, listener2.event);
122 }
123
124 @Test
125 public void testStart() throws MuleException
126 {
127 compositeSource.setListener(listener);
128 compositeSource.addSource(source);
129
130 source.triggerSource();
131 assertNull(listener.event);
132
133 compositeSource.start();
134 source.triggerSource();
135 assertEquals(testEvent, listener.event);
136 }
137
138 @Test
139 public void testStartNoListener() throws MuleException
140 {
141 compositeSource.addSource(source);
142 try
143 {
144 compositeSource.start();
145 fail("Exception excepted");
146 }
147 catch (Exception e)
148 {
149 }
150
151 }
152
153 @Test
154 public void testStop() throws MuleException
155 {
156 compositeSource.setListener(listener);
157 compositeSource.addSource(source);
158 compositeSource.start();
159
160 compositeSource.stop();
161 source.triggerSource();
162 assertNull(listener.event);
163 }
164
165 protected class NullMessageSource implements MessageSource, Startable, Stoppable
166 {
167 MuleEvent event;
168 MessageProcessor listener;
169 boolean started = false;
170
171 public NullMessageSource(MuleEvent event)
172 {
173 this.event = event;
174 }
175
176 public void setListener(MessageProcessor listener)
177 {
178 this.listener = listener;
179 }
180
181 public void triggerSource() throws MuleException
182 {
183 if (started && listener != null)
184 {
185 listener.process(event);
186 }
187 }
188
189 public void start() throws MuleException
190 {
191 started = true;
192 }
193
194 public void stop() throws MuleException
195 {
196 started = false;
197 }
198
199 @Override
200 public String toString()
201 {
202 return ObjectUtils.toString(this);
203 }
204 }
205 }