1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.integration.providers.jms;
12
13 import org.mule.MuleManager;
14 import org.mule.config.PoolingProfile;
15 import org.mule.config.builders.QuickConfigurationBuilder;
16 import org.mule.impl.endpoint.MuleEndpointURI;
17 import org.mule.impl.model.seda.SedaModel;
18 import org.mule.providers.jms.JmsConnector;
19 import org.mule.tck.AbstractMuleTestCase;
20 import org.mule.tck.functional.EventCallback;
21 import org.mule.tck.functional.FunctionalTestComponent;
22 import org.mule.test.integration.providers.jms.tools.JmsTestUtils;
23 import org.mule.umo.endpoint.MalformedEndpointException;
24 import org.mule.umo.endpoint.UMOEndpointURI;
25 import org.mule.umo.model.UMOModel;
26
27 import java.util.HashMap;
28
29 import javax.jms.Connection;
30 import javax.jms.ConnectionFactory;
31 import javax.jms.JMSException;
32 import javax.jms.Message;
33 import javax.jms.QueueConnection;
34 import javax.jms.TopicConnection;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 public abstract class AbstractJmsFunctionalTestCase extends AbstractMuleTestCase
40 {
41 public static final String DEFAULT_IN_QUEUE = "jms://in.q";
42 public static final String DEFAULT_OUT_QUEUE = "jms://out.q";
43 public static final String DEFAULT_DL_QUEUE = "jms://dlq";
44 public static final String DEFAULT_IN_TOPIC = "jms://topic:in.t";
45 public static final String DEFAULT_OUT_TOPIC = "jms://topic:out.t";
46 public static final String DEFAULT_DL_TOPIC = "jms://topic:dlt";
47 public static final String DEFAULT_MESSAGE = "Test Message";
48 public static final String CONNECTOR_NAME = "testConnector";
49
50 public static final long LOCK_WAIT = 20000;
51
52 protected JmsConnector connector;
53 protected volatile boolean callbackCalled = false;
54 protected Connection cnn;
55 protected volatile Message currentMsg;
56 protected volatile int eventCount = 0;
57 protected UMOModel model;
58
59 protected final transient Log logger = LogFactory.getLog(getClass());
60
61 protected void doSetUp() throws Exception
62 {
63
64
65
66
67 MuleManager.getConfiguration().setSynchronous(true);
68
69 MuleManager.getConfiguration().setServerUrl("tcp://localhost:60504?reuseAddress=true");
70 MuleManager.getConfiguration().getPoolingProfile().setInitialisationPolicy(
71 PoolingProfile.INITIALISE_ONE);
72
73 model = new SedaModel();
74 model.setName("main");
75 MuleManager.getInstance().registerModel(model);
76 callbackCalled = false;
77 ConnectionFactory cf = getConnectionFactory();
78
79 cnn = getSenderConnection();
80 cnn.start();
81 connector = createConnector();
82 connector.setConnectionFactory(cf);
83 MuleManager.getInstance().registerConnector(connector);
84 currentMsg = null;
85 eventCount = 0;
86 }
87
88 protected Connection getSenderConnection() throws Exception
89 {
90 return getConnectionFactory().createConnection();
91 }
92
93 protected void doTearDown() throws Exception
94 {
95
96
97 Thread.sleep(1000);
98 try
99 {
100 if (cnn != null)
101 {
102 cnn.close();
103 }
104 }
105 catch (JMSException e)
106 {
107 }
108 }
109
110 protected void drainDestinations() throws Exception
111 {
112 if (!useTopics())
113 {
114 logger.debug("@@@@ Draining Queues @@@@");
115 JmsTestUtils.drainQueue((QueueConnection) cnn, getInDest().getAddress());
116 assertNull(receive(getInDest().getAddress(), 10));
117 JmsTestUtils.drainQueue((QueueConnection) cnn, getOutDest().getAddress());
118 assertNull(receive(getOutDest().getAddress(), 10));
119 }
120
121 }
122
123 public abstract ConnectionFactory getConnectionFactory() throws Exception;
124
125 public void initialiseComponent(EventCallback callback) throws Exception
126 {
127 QuickConfigurationBuilder builder = new QuickConfigurationBuilder();
128 HashMap props = new HashMap();
129 props.put("eventCallback", callback);
130
131 builder.registerComponent(FunctionalTestComponent.class.getName(), "testComponent", getInDest(),
132 getOutDest(), props);
133 }
134
135 public void afterInitialise() throws Exception
136 {
137
138 }
139
140 protected UMOEndpointURI getInDest()
141 {
142 try
143 {
144 if (!useTopics())
145 {
146 return new MuleEndpointURI(DEFAULT_IN_QUEUE);
147 }
148 else
149 {
150 return new MuleEndpointURI(DEFAULT_IN_TOPIC);
151 }
152 }
153 catch (MalformedEndpointException e)
154 {
155 fail(e.getMessage());
156 return null;
157 }
158 }
159
160 protected UMOEndpointURI getOutDest()
161 {
162 try
163 {
164 if (!useTopics())
165 {
166 return new MuleEndpointURI(DEFAULT_OUT_QUEUE);
167 }
168 else
169 {
170 return new MuleEndpointURI(DEFAULT_OUT_TOPIC);
171 }
172 }
173 catch (Exception e)
174 {
175 fail(e.getMessage());
176 return null;
177 }
178 }
179
180 protected void send(String payload, boolean transacted, int ack, String replyTo) throws JMSException
181 {
182 if (!useTopics())
183 {
184 JmsTestUtils.queueSend((QueueConnection)cnn, getInDest().getAddress(), payload, transacted, ack,
185 replyTo);
186 }
187 else
188 {
189 JmsTestUtils.topicPublish((TopicConnection)cnn, getInDest().getAddress(), payload, transacted,
190 ack, replyTo);
191 }
192 }
193
194 protected Message receive(String dest, long timeout) throws JMSException
195 {
196 Message msg = null;
197 if (!useTopics())
198 {
199 msg = JmsTestUtils.queueReceiver((QueueConnection)cnn, dest, timeout);
200 }
201 else
202 {
203 msg = JmsTestUtils.topicSubscribe((TopicConnection)cnn, dest, timeout);
204 }
205 return msg;
206 }
207
208 public boolean useTopics()
209 {
210 return false;
211 }
212
213 public abstract JmsConnector createConnector() throws Exception;
214 }