KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > messagedriven > support > BasicMessageDrivenUnitTest


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.messagedriven.support;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import javax.jms.Connection JavaDoc;
29 import javax.jms.ConnectionFactory JavaDoc;
30 import javax.jms.Destination JavaDoc;
31 import javax.jms.Message JavaDoc;
32 import javax.jms.MessageProducer JavaDoc;
33 import javax.jms.Session JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35
36 import org.jboss.mx.util.ObjectNameFactory;
37 import org.jboss.naming.Util;
38 import org.jboss.test.JBossTestCase;
39 import org.jboss.test.messagedriven.mbeans.TestMessageDrivenManagementMBean;
40
41 /**
42  * Basic tests of message driven beans
43  *
44  * @author <a HREF="mailto:adrian@jboss.com>Adrian Brock</a>
45  * @version <tt>$Revision: 1.4</tt>
46  */

47 public abstract class BasicMessageDrivenUnitTest extends JBossTestCase
48 {
49    protected static final long WAIT_TIME = 5000L;
50    protected static final long REPEATED_WAIT = 4;
51
52    protected static final ObjectName JavaDoc testQueue = ObjectNameFactory.create("jboss.mq.destination:service=Queue,name=testQueue");
53    protected static final Properties JavaDoc testQueueProps = new Properties JavaDoc();
54    
55    protected static final ObjectName JavaDoc testTopic = ObjectNameFactory.create("jboss.mq.destination:service=Topic,name=testTopic");
56    protected static final Properties JavaDoc testTopicProps = new Properties JavaDoc();
57    
58    protected static final ObjectName JavaDoc testDurableTopic = ObjectNameFactory.create("jboss.mq.destination:service=Topic,name=testDurableTopic");
59    protected static final Properties JavaDoc testDurableTopicProps = new Properties JavaDoc();
60    
61    static
62    {
63       testQueueProps.put("destination", "queue/testQueue");
64       testQueueProps.put("destinationType", "javax.jms.Queue");
65
66       testTopicProps.put("destination", "topic/testTopic");
67       testTopicProps.put("destinationType", "javax.jms.Topic");
68
69       testDurableTopicProps.put("destination", "topic/testDurableTopic");
70       testDurableTopicProps.put("destinationType", "javax.jms.Topic");
71       //testDurableTopicProps.put("clientID", "DurableSubscriberExample");
72
testDurableTopicProps.put("durability", "Durable");
73       testDurableTopicProps.put("subscriptionName", "messagedriven");
74       testDurableTopicProps.put("user", "john");
75       testDurableTopicProps.put("password", "needle");
76    }
77    
78    protected Thread JavaDoc thread;
79    protected boolean running = false;
80
81    protected String JavaDoc mdbjar = "testmessagedriven.jar";
82    protected String JavaDoc mbeansar = "testmessagedriven.sar";
83
84    protected ObjectName JavaDoc jmxDestination = ObjectNameFactory.create("does:not=exist");
85    protected ObjectName JavaDoc dlqJMXDestination = ObjectNameFactory.create("jboss.mq.destination:service=Queue,name=DLQ");
86    protected String JavaDoc connectionFactoryJNDI = "ConnectionFactory";
87    protected Destination JavaDoc destination;
88    protected Destination JavaDoc dlqDestination;
89    protected Properties JavaDoc defaultProps;
90    protected Properties JavaDoc props;
91
92    protected Connection JavaDoc connection;
93    protected Session JavaDoc session;
94    protected HashMap JavaDoc producers = new HashMap JavaDoc();
95    protected ArrayList JavaDoc messages = new ArrayList JavaDoc();
96
97    public BasicMessageDrivenUnitTest(String JavaDoc name, ObjectName JavaDoc jmxDestination, Properties JavaDoc defaultProps)
98    {
99       super(name);
100       this.jmxDestination = jmxDestination;
101       this.defaultProps = defaultProps;
102    }
103    
104    public void runTest(Operation[] ops, Properties JavaDoc props) throws Exception JavaDoc
105    {
106       startTest(props);
107       try
108       {
109          for (int i = 0; i < ops.length; ++i)
110             ops[i].run();
111       }
112       finally
113       {
114          stopTest();
115       }
116    }
117
118    public String JavaDoc getMDBDeployment()
119    {
120       return mdbjar;
121    }
122    
123    public ObjectName JavaDoc getJMXDestination()
124    {
125       return jmxDestination;
126    }
127    
128    public ObjectName JavaDoc getDLQJMXDestination()
129    {
130       return dlqJMXDestination;
131    }
132    
133    public Destination JavaDoc getDestination() throws Exception JavaDoc
134    {
135       if (destination != null)
136          return destination;
137       String JavaDoc jndiName = (String JavaDoc) getAttribute(getJMXDestination(), "JNDIName");
138       destination = (Destination JavaDoc) lookup(jndiName, Destination JavaDoc.class);
139       return destination;
140    }
141    
142    public Destination JavaDoc getDLQDestination() throws Exception JavaDoc
143    {
144       if (dlqDestination != null)
145          return dlqDestination;
146       String JavaDoc jndiName = (String JavaDoc) getAttribute(getDLQJMXDestination(), "JNDIName");
147       dlqDestination = (Destination JavaDoc) lookup(jndiName, Destination JavaDoc.class);
148       return dlqDestination;
149    }
150    
151    public MessageProducer JavaDoc getMessageProducer() throws Exception JavaDoc
152    {
153       return getMessageProducer(getDestination());
154    }
155    
156    public MessageProducer JavaDoc getMessageProducer(Destination JavaDoc destination) throws Exception JavaDoc
157    {
158       MessageProducer JavaDoc producer = (MessageProducer JavaDoc) producers.get(destination);
159       if (producer == null)
160          producer = getSession().createProducer(destination);
161       return producer;
162    }
163
164    public Session JavaDoc getSession() throws Exception JavaDoc
165    {
166       if (session != null)
167          return session;
168       
169       return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
170    }
171    
172    public Connection JavaDoc getConnection() throws Exception JavaDoc
173    {
174       if (connection != null)
175          return connection;
176       
177       ConnectionFactory JavaDoc factory = (ConnectionFactory JavaDoc) lookup(connectionFactoryJNDI, ConnectionFactory JavaDoc.class);
178       connection = factory.createConnection();
179       return connection;
180    }
181    
182    public Connection JavaDoc getConnection(String JavaDoc user, String JavaDoc password) throws Exception JavaDoc
183    {
184       if (connection != null)
185          return connection;
186
187       ConnectionFactory JavaDoc factory = (ConnectionFactory JavaDoc) lookup(connectionFactoryJNDI, ConnectionFactory JavaDoc.class);
188       connection = factory.createConnection(user, password);
189       return connection;
190    }
191    
192    public Message JavaDoc getTestMessage() throws Exception JavaDoc
193    {
194       return getSession().createMessage();
195    }
196
197    protected void setUp() throws Exception JavaDoc
198    {
199       if ("testServerFound".equals(getName()))
200          return;
201       deploy(mbeansar);
202    }
203
204    protected void tearDown() throws Exception JavaDoc
205    {
206       if ("testServerFound".equals(getName()))
207          return;
208       try
209       {
210          undeploy(mbeansar);
211       }
212       catch (Throwable JavaDoc t)
213       {
214          getLog().error("Error undeploying: " + mbeansar, t);
215       }
216    }
217    
218    protected void startTest(Properties JavaDoc props) throws Exception JavaDoc
219    {
220       this.props = props;
221       clearMessages(getJMXDestination());
222       clearMessages(getDLQJMXDestination());
223       tidyup(props);
224       initProperties(props);
225       deploy(getMDBDeployment());
226       try
227       {
228          startReceiverThread();
229       }
230       catch (Exception JavaDoc e)
231       {
232          undeploy(getMDBDeployment());
233          throw e;
234       }
235    }
236
237    protected void stopTest()
238    {
239       if (connection != null)
240       {
241          try
242          {
243             connection.close();
244          }
245          catch (Exception JavaDoc ignored)
246          {
247          }
248          connection = null;
249       }
250       stopReceiverThread();
251       try
252       {
253          undeploy(getMDBDeployment());
254       }
255       catch (Throwable JavaDoc t)
256       {
257          getLog().error("Error undeploying: " + getMDBDeployment(), t);
258       }
259       try
260       {
261          clearMessages(getJMXDestination());
262          tidyup(props);
263       }
264       catch (Throwable JavaDoc t)
265       {
266          getLog().error("Error clearing messages: " + getJMXDestination(), t);
267       }
268       try
269       {
270          clearMessages(getDLQJMXDestination());
271       }
272       catch (Throwable JavaDoc t)
273       {
274          getLog().error("Error clearing messages: " + getDLQJMXDestination(), t);
275       }
276    }
277    
278    protected void clearMessages(ObjectName JavaDoc name) throws Exception JavaDoc
279    {
280       if (name != null)
281       {
282          getLog().info("Clearing messages " + name);
283          getServer().invoke(name, "removeAllMessages", new Object JavaDoc[0], new String JavaDoc[0]);
284       }
285    }
286    
287    protected void tidyup(Properties JavaDoc props) throws Exception JavaDoc
288    {
289       String JavaDoc name = props.getProperty("subscriptionName");
290       if (name != null)
291       {
292          String JavaDoc user = props.getProperty("user");
293          if (user != null)
294          {
295             String JavaDoc password = props.getProperty("password");
296             getConnection(user, password);
297          }
298          else
299             getConnection();
300          try
301          {
302             Session JavaDoc session = getSession();
303             try
304             {
305                session.unsubscribe(name);
306             }
307             catch (Throwable JavaDoc t)
308             {
309                log.debug("Unsubscribe failed: ", t);
310             }
311          }
312          finally
313          {
314             try
315             {
316                connection.close();
317             }
318             catch (Exception JavaDoc ignored)
319             {
320             }
321             connection = null;
322          }
323       }
324    }
325    
326    protected void activate(ObjectName JavaDoc name) throws Exception JavaDoc
327    {
328       getServer().invoke(name, "startDelivery", new Object JavaDoc[0], new String JavaDoc[0]);
329    }
330    
331    protected void deactivate(ObjectName JavaDoc name) throws Exception JavaDoc
332    {
333       getServer().invoke(name, "stopDelivery", new Object JavaDoc[0], new String JavaDoc[0]);
334    }
335    
336    protected void initProperties(Properties JavaDoc props) throws Exception JavaDoc
337    {
338       getLog().info("Init properties " + props);
339       getServer().invoke(TestMessageDrivenManagementMBean.OBJECT_NAME, "initProperties", new Object JavaDoc[] { props }, new String JavaDoc[] { Properties JavaDoc.class.getName() });
340    }
341    
342    protected void waitMessages(int expected, long wait) throws Exception JavaDoc
343    {
344       synchronized (this)
345       {
346          if (wait != 0)
347             wait(wait);
348          
349          for (int i = 0; i < REPEATED_WAIT && messages.size() < expected; ++i)
350             wait(WAIT_TIME);
351       }
352    }
353    
354    protected ArrayList JavaDoc getMessages() throws Exception JavaDoc
355    {
356       synchronized (this)
357       {
358          return new ArrayList JavaDoc(messages);
359       }
360    }
361    
362    protected void startReceiverThread()
363    {
364       synchronized (this)
365       {
366          thread = new Thread JavaDoc(new ReceiverRunnable(), getClass().getName());
367          thread.start();
368          running = true;
369       }
370    }
371    
372    protected void stopReceiverThread()
373    {
374       synchronized (this)
375       {
376          running = false;
377          while (thread != null)
378          {
379             try
380             {
381                this.notifyAll();
382                this.wait();
383             }
384             catch (Throwable JavaDoc t)
385             {
386                getLog().error("Error waiting for receiver thread to stop " + thread, t);
387             }
388          }
389       }
390    }
391
392    protected Object JavaDoc getAttribute(ObjectName JavaDoc name, String JavaDoc attribute) throws Exception JavaDoc
393    {
394       return getServer().getAttribute(name, attribute);
395    }
396    
397    protected Object JavaDoc lookup(String JavaDoc jndiName, Class JavaDoc clazz) throws Exception JavaDoc
398    {
399       return Util.lookup(getInitialContext(), jndiName, clazz);
400    }
401    
402    public class ReceiverRunnable implements Runnable JavaDoc
403    {
404       public void run()
405       {
406          try
407          {
408             while (true)
409             {
410                ArrayList JavaDoc result = (ArrayList JavaDoc) getAttribute(TestMessageDrivenManagementMBean.OBJECT_NAME, "Messages");
411                synchronized (BasicMessageDrivenUnitTest.this)
412                {
413                   if (running == false)
414                      break;
415                   if (result.size() > 0)
416                   {
417                      messages.addAll(result);
418                      BasicMessageDrivenUnitTest.this.notifyAll();
419                   }
420                   BasicMessageDrivenUnitTest.this.wait(WAIT_TIME);
421                }
422             }
423          }
424          catch (Throwable JavaDoc t)
425          {
426             getLog().error("Error in receiver thread " + thread, t);
427          }
428          
429          synchronized (BasicMessageDrivenUnitTest.this)
430          {
431             thread = null;
432             BasicMessageDrivenUnitTest.this.notifyAll();
433          }
434       }
435    }
436 }
437
Popular Tags