1
2
3
4
5
6
7 package org.mule.construct;
8
9 import org.mule.api.MuleEvent;
10 import org.mule.api.MuleException;
11 import org.mule.api.construct.FlowConstruct;
12 import org.mule.api.processor.MessageProcessor;
13 import org.mule.api.source.MessageSource;
14 import org.mule.tck.junit4.AbstractMuleContextTestCase;
15 import org.mule.util.ObjectUtils;
16
17 import org.junit.Test;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 public abstract class AbstractFlowConstuctTestCase extends AbstractMuleContextTestCase
25 {
26 public static class DirectInboundMessageSource implements MessageSource
27 {
28 private MessageProcessor listener;
29
30 public void setListener(MessageProcessor listener)
31 {
32 this.listener = listener;
33 }
34
35 public MuleEvent process(MuleEvent event) throws MuleException
36 {
37 return listener.process(event);
38 }
39
40 @Override
41 public String toString()
42 {
43 return ObjectUtils.toString(this);
44 }
45 }
46
47 protected DirectInboundMessageSource directInboundMessageSource;
48
49 @Override
50 protected void doSetUp() throws Exception
51 {
52 super.doSetUp();
53
54 directInboundMessageSource = new DirectInboundMessageSource();
55 }
56
57 protected abstract AbstractFlowConstruct getFlowConstruct() throws Exception;
58
59 @Test
60 public void testStart() throws Exception
61 {
62 try
63 {
64 getFlowConstruct().start();
65 fail("Exception expected: Cannot start an uninitialised service");
66 }
67 catch (final Exception e)
68 {
69
70 }
71
72 getFlowConstruct().initialise();
73 getFlowConstruct().start();
74
75 try
76 {
77 getFlowConstruct().initialise();
78 fail("Exception expected: Cannot initialise an already initialised service");
79 }
80 catch (final IllegalStateException e)
81 {
82
83 }
84 getFlowConstruct().dispose();
85
86 }
87
88 @Test
89 public void testStop() throws Exception
90 {
91 assertFalse(getFlowConstruct().isStarted());
92
93 try
94 {
95 getFlowConstruct().stop();
96 fail("Exception expected: Cannot stop an uninitialised service");
97 }
98 catch (final IllegalStateException e)
99 {
100
101 }
102
103 getFlowConstruct().initialise();
104 assertFalse(getFlowConstruct().isStarted());
105
106
107 getFlowConstruct().stop();
108
109 assertFalse(getFlowConstruct().isStarted());
110 getFlowConstruct().start();
111 assertTrue(getFlowConstruct().isStarted());
112 getFlowConstruct().stop();
113 assertFalse(getFlowConstruct().isStarted());
114 try
115 {
116 getFlowConstruct().stop();
117 fail("Exception expected: Cannot stop a service that is not started");
118 }
119 catch (final IllegalStateException e)
120 {
121
122 }
123 assertFalse(getFlowConstruct().isStarted());
124 getFlowConstruct().dispose();
125
126 }
127
128 @Test
129 public void testDispose() throws Exception
130 {
131 assertFalse(getFlowConstruct().isStarted());
132 getFlowConstruct().dispose();
133
134 try
135 {
136 getFlowConstruct().dispose();
137 fail("Exception expected: Cannot dispose a service that is already disposed");
138 }
139 catch (final IllegalStateException e)
140 {
141
142 }
143
144 try
145 {
146 getFlowConstruct().initialise();
147 fail("Exception expected: Cannot invoke initialise (or any lifecycle) on an object once it is disposed");
148 }
149 catch (final IllegalStateException e)
150 {
151
152 }
153 }
154
155 @Test
156 public void testRegisterUnregister() throws MuleException, Exception
157 {
158 FlowConstruct construct = getFlowConstruct();
159 muleContext.getRegistry().registerFlowConstruct(construct);
160 assertNotNull(muleContext.getRegistry().lookupFlowConstruct(construct.getName()));
161 }
162
163 @Test
164 public void testInitialStateStopped() throws Exception
165 {
166 AbstractFlowConstruct flow = getFlowConstruct();
167 flow.setInitialState(AbstractFlowConstruct.INITIAL_STATE_STOPPED);
168 assertFalse(flow.isStarted());
169 assertFalse(flow.isStopped());
170
171 flow.initialise();
172 assertFalse(flow.isStarted());
173 assertFalse(flow.isStopped());
174
175
176 flow.start();
177 assertFalse(flow.isStarted());
178 assertTrue(flow.isStopped());
179
180
181 flow.start();
182 assertTrue(flow.isStarted());
183 assertFalse(flow.isStopped());
184
185
186 try
187 {
188 flow.start();
189 fail("Exception expected: Cannot start an already started flow");
190 }
191 catch (final IllegalStateException e)
192 {
193
194 }
195 }
196
197 }