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