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