KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > mdb > unit > MDBUnitTestCase


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.ejb3.test.mdb.unit;
23
24 import java.util.Enumeration JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.jms.DeliveryMode JavaDoc;
28 import javax.jms.MessageProducer JavaDoc;
29 import javax.jms.Queue JavaDoc;
30 import javax.jms.QueueBrowser JavaDoc;
31 import javax.jms.QueueConnection JavaDoc;
32 import javax.jms.QueueConnectionFactory JavaDoc;
33 import javax.jms.QueueReceiver JavaDoc;
34 import javax.jms.QueueSender JavaDoc;
35 import javax.jms.QueueSession JavaDoc;
36 import javax.jms.TextMessage JavaDoc;
37 import javax.jms.Topic JavaDoc;
38 import javax.jms.TopicConnection JavaDoc;
39 import javax.jms.TopicConnectionFactory JavaDoc;
40 import javax.jms.TopicSession JavaDoc;
41 import javax.management.ObjectName JavaDoc;
42 import javax.naming.InitialContext JavaDoc;
43 import javax.naming.NamingException JavaDoc;
44
45 import junit.framework.Test;
46
47 import org.jboss.ejb3.ClientKernelAbstraction;
48 import org.jboss.ejb3.InitialContextFactory;
49 import org.jboss.ejb3.KernelAbstractionFactory;
50 import org.jboss.ejb3.test.mdb.Stateless;
51 import org.jboss.ejb3.test.mdb.TestStatus;
52 import org.jboss.logging.Logger;
53 import org.jboss.security.SecurityAssociation;
54 import org.jboss.security.SimplePrincipal;
55 import org.jboss.test.JBossTestCase;
56 import org.jboss.util.collection.CollectionsUtil;
57
58 /**
59  * Sample client for the jboss container.
60  *
61  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
62  * @version $Id: MDBUnitTestCase.java 58493 2006-11-17 13:03:03Z wolfc $
63  */

64 public class MDBUnitTestCase extends JBossTestCase
65 {
66    @SuppressWarnings JavaDoc("unused")
67    private static final Logger log = Logger.getLogger(MDBUnitTestCase.class);
68
69    static boolean deployed = false;
70
71    static int test = 0;
72
73    public MDBUnitTestCase(String JavaDoc name)
74    {
75
76       super(name);
77
78    }
79
80    private static void removeAllMessagesFromDLQ() throws Exception JavaDoc
81    {
82       ObjectName JavaDoc dlqName = new ObjectName JavaDoc("jboss.messaging.destination:name=DLQ,service=Queue");
83       ClientKernelAbstraction kernel = KernelAbstractionFactory.getClientInstance();
84       kernel.invoke(dlqName, "removeAllMessages", new Object JavaDoc[0], new String JavaDoc[0]);
85    }
86    
87    public void testOverrideQueue() throws Exception JavaDoc
88    {
89       SecurityAssociation.setPrincipal(new SimplePrincipal("anyone"));
90       SecurityAssociation.setCredential("password".toCharArray());
91
92       TestStatus status = (TestStatus) getInitialContext().lookup(
93             "TestStatusBean/remote");
94       clear(status);
95       QueueConnection JavaDoc cnn = null;
96       QueueSender JavaDoc sender = null;
97       QueueSession JavaDoc session = null;
98
99       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup(
100             "queue/overridequeuetest");
101       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
102       cnn = factory.createQueueConnection();
103       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
104
105       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
106
107       sender = session.createSender(queue);
108       sender.send(msg);
109       session.close();
110       cnn.close();
111
112       Thread.sleep(5000);
113       assertEquals(1, status.overrideQueueFired());
114    }
115
116    public void testNondurableQueue() throws Exception JavaDoc
117    {
118       SecurityAssociation.setPrincipal(new SimplePrincipal("anyone"));
119       SecurityAssociation.setCredential("password".toCharArray());
120
121       TestStatus status = (TestStatus) getInitialContext().lookup(
122             "TestStatusBean/remote");
123       clear(status);
124       QueueConnection JavaDoc cnn = null;
125       QueueSender JavaDoc sender = null;
126       QueueSession JavaDoc session = null;
127
128       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup(
129             "queue/nondurablemdbtest");
130       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
131       cnn = factory.createQueueConnection();
132       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
133
134       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
135       msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
136
137       sender = session.createSender(queue);
138       sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
139       sender.send(msg);
140       session.close();
141       cnn.close();
142
143       Thread.sleep(2000);
144       assertEquals(1, status.nondurableQueueFired());
145    }
146
147    public void testDefaultedQueue() throws Exception JavaDoc
148    {
149       SecurityAssociation.setPrincipal(new SimplePrincipal("anyone"));
150       SecurityAssociation.setCredential("password".toCharArray());
151
152       TestStatus status = (TestStatus) getInitialContext().lookup(
153             "TestStatusBean/remote");
154       clear(status);
155       QueueConnection JavaDoc cnn = null;
156       QueueSender JavaDoc sender = null;
157       QueueSession JavaDoc session = null;
158
159       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext()
160             .lookup("queue/defaultedmdbtest");
161       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
162       cnn = factory.createQueueConnection();
163       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
164
165       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
166
167       sender = session.createSender(queue);
168       sender.send(msg);
169       session.close();
170       cnn.close();
171
172       Thread.sleep(2000);
173       assertEquals(1, status.defaultedQueueFired());
174    }
175
176    public void testOverrideDefaultedQueue() throws Exception JavaDoc
177    {
178       SecurityAssociation.setPrincipal(new SimplePrincipal("anyone"));
179       SecurityAssociation.setCredential("password".toCharArray());
180
181       TestStatus status = (TestStatus) getInitialContext().lookup(
182             "TestStatusBean/remote");
183       clear(status);
184       QueueConnection JavaDoc cnn = null;
185       QueueSender JavaDoc sender = null;
186       QueueSession JavaDoc session = null;
187
188       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup(
189             "queue/overridedefaultedmdbtest");
190       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
191       cnn = factory.createQueueConnection();
192       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
193
194       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
195
196       sender = session.createSender(queue);
197       sender.send(msg);
198       session.close();
199       cnn.close();
200
201       Thread.sleep(2000);
202       assertEquals(1, status.overrideDefaultedQueueFired());
203    }
204
205    public void testQueue() throws Exception JavaDoc
206    {
207       setSecurity("anyone", "password");
208
209       TestStatus status = (TestStatus) getInitialContext().lookup(
210             "TestStatusBean/remote");
211       clear(status);
212       QueueConnection JavaDoc cnn = null;
213       QueueSender JavaDoc sender = null;
214       QueueSession JavaDoc session = null;
215
216       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup("queue/mdbtest");
217       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
218       cnn = factory.createQueueConnection();
219       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
220
221       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
222
223       sender = session.createSender(queue);
224       sender.send(msg);
225       sender.send(msg);
226       sender.send(msg);
227       sender.send(msg);
228       sender.send(msg);
229       session.close();
230       cnn.close();
231
232       Thread.sleep(2000);
233       assertEquals(5, status.queueFired());
234       assertTrue(status.interceptedQueue());
235       assertTrue(status.postConstruct());
236       assertEquals(5, status.messageCount());
237
238       // TODO: Figure out how to test preDestroy gets invoked
239
// assertTrue(status.preDestroy());
240

241       Stateless stateless = (Stateless) getInitialContext().lookup("Stateless");
242       assertNotNull(stateless);
243       String JavaDoc state = stateless.getState();
244       assertEquals("Set", state);
245    }
246
247    public void testTopic() throws Exception JavaDoc
248    {
249       TestStatus status = (TestStatus) getInitialContext().lookup(
250             "TestStatusBean/remote");
251       clear(status);
252       TopicConnection JavaDoc cnn = null;
253       MessageProducer JavaDoc sender = null;
254       TopicSession JavaDoc session = null;
255
256       Topic JavaDoc topic = (Topic JavaDoc) getInitialContext().lookup("topic/mdbtest");
257       TopicConnectionFactory JavaDoc factory = getTopicConnectionFactory();
258       cnn = factory.createTopicConnection();
259       session = cnn.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE);
260
261       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
262
263       sender = session.createProducer(topic);
264       sender.send(msg);
265       session.close();
266       cnn.close();
267
268       Thread.sleep(2000);
269       assertEquals(1, status.topicFired());
270       assertTrue(status.interceptedTopic());
271       assertFalse(status.postConstruct());
272       assertFalse(status.preDestroy());
273    }
274
275    public void testRuntimeException() throws Exception JavaDoc
276    {
277       TestStatus status = (TestStatus) getInitialContext().lookup(
278             "TestStatusBean/remote");
279       clear(status);
280       QueueConnection JavaDoc cnn = null;
281       QueueSender JavaDoc sender = null;
282       QueueSession JavaDoc session = null;
283
284       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup("queue/bmtmdbtest");
285       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
286       cnn = factory.createQueueConnection();
287       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
288
289       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
290       msg.setIntProperty("JMS_JBOSS_REDELIVERY_LIMIT", 1);
291
292       sender = session.createSender(queue);
293       sender.send(msg);
294       session.close();
295       cnn.close();
296
297       Thread.sleep(2000);
298       assertEquals(1, status.bmtQueueRan());
299    }
300
301    public void testRuntimeExceptionCMT() throws Exception JavaDoc
302    {
303       TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote");
304       clear(status);
305       QueueConnection JavaDoc cnn = null;
306       QueueSender JavaDoc sender = null;
307       QueueSession JavaDoc session = null;
308
309       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup("queue/cmtmdbtest");
310       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
311       cnn = factory.createQueueConnection();
312       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
313
314       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
315       msg.setIntProperty("JMS_JBOSS_REDELIVERY_LIMIT", 1);
316
317       sender = session.createSender(queue);
318       sender.send(msg);
319       session.close();
320       cnn.close();
321
322       Thread.sleep(2000);
323       assertEquals(1, status.cmtQueueRan());
324    }
325
326    /**
327     * Test that expired messages are moved to a separate queue.
328     */

329    public void testExpiredMessagesMove() throws Exception JavaDoc
330    {
331       QueueConnection JavaDoc connection = null;
332       QueueSender JavaDoc sender = null;
333       QueueSession JavaDoc session = null;
334
335       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
336       connection = factory.createQueueConnection();
337       connection.start();
338       session = connection.createQueueSession(false,
339             QueueSession.AUTO_ACKNOWLEDGE);
340
341       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup("queue/expirytest");
342       sender = session.createSender(queue);
343
344       Queue JavaDoc dlq = (Queue JavaDoc) getInitialContext().lookup("queue/DLQ");
345
346       removeAllMessagesFromDLQ();
347
348       long now = System.currentTimeMillis();
349
350       TextMessage JavaDoc message = session.createTextMessage();
351       message.setStringProperty("foo", "bar");
352       message.setStringProperty("null", null);
353       String JavaDoc text = "expire on server";
354       message.setText(text);
355
356       System.out.println("!!!! sending ...");
357       sender.send(message, DeliveryMode.NON_PERSISTENT, 4, 1);
358
359       System.out.println("!!!! sleeping ...");
360       Thread.sleep(2000);
361
362       assertSize(session, queue, 0);
363       assertSize(session, dlq, 1);
364
365       QueueReceiver JavaDoc receiver = session.createReceiver(dlq);
366       message = (TextMessage JavaDoc) receiver.receiveNoWait();
367       assertNotNull(message);
368       assertEquals("QUEUE.expirytest", message
369             .getStringProperty("JBOSS_ORIG_DESTINATION"));
370       assertTrue(message.getLongProperty("JBOSS_ORIG_EXPIRATION") > now);
371       assertEquals(0L, message.getJMSExpiration());
372       assertEquals(text, message.getText());
373       assertEquals("bar", message.getStringProperty("foo"));
374       assertNull(message.getStringProperty("null"));
375
376       sender.close();
377       receiver.close();
378
379       session.close();
380       connection.close();
381    }
382
383    public void testDlqMaxResent() throws Exception JavaDoc
384    {
385       QueueConnection JavaDoc connection = null;
386       QueueSender JavaDoc sender = null;
387       QueueSession JavaDoc session = null;
388
389       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
390       connection = factory.createQueueConnection();
391       connection.start();
392       session = connection.createQueueSession(false,
393             QueueSession.AUTO_ACKNOWLEDGE);
394
395       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup("queue/dlqtest");
396       sender = session.createSender(queue);
397
398       Queue JavaDoc dlq = (Queue JavaDoc) getInitialContext().lookup("queue/DLQ");
399
400       removeAllMessagesFromDLQ();
401
402       TextMessage JavaDoc message = session.createTextMessage();
403       message.setStringProperty("foo", "bar");
404       message.setStringProperty("null", null);
405       String JavaDoc text = "expire on server";
406       message.setText(text);
407
408       sender.send(message);
409
410       Thread.sleep(1000);
411
412       assertSize(session, queue, 0);
413       assertSize(session, dlq, 1);
414
415       QueueReceiver JavaDoc receiver = session.createReceiver(dlq);
416       message = (TextMessage JavaDoc) receiver.receiveNoWait();
417       assertNotNull(message);
418       // AS 4.x
419
// assertEquals("QUEUE.dlqtest", message
420
// .getStringProperty("JBOSS_ORIG_DESTINATION"));
421
// AS 5.x
422
assertEquals("JBossQueue[dlqtest]", message
423             .getStringProperty("JBOSS_ORIG_DESTINATION"));
424       assertEquals(0L, message.getJMSExpiration());
425       assertEquals(text, message.getText());
426       assertEquals("bar", message.getStringProperty("foo"));
427       assertNull(message.getStringProperty("null"));
428
429       sender.close();
430       receiver.close();
431
432       session.close();
433       connection.close();
434    }
435
436    public void testRollbackCMT() throws Exception JavaDoc
437    {
438       TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote");
439       clear(status);
440       QueueConnection JavaDoc cnn = null;
441       QueueSender JavaDoc sender = null;
442       QueueSession JavaDoc session = null;
443
444       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup("queue/rollbackcmtmdbtest");
445       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
446       cnn = factory.createQueueConnection();
447       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
448
449       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
450       msg.setIntProperty("JMS_JBOSS_REDELIVERY_LIMIT", 1);
451
452       sender = session.createSender(queue);
453       sender.send(msg);
454       session.close();
455       cnn.close();
456
457       Thread.sleep(2000);
458       assertEquals(1, status.rollbackCmtQueueRan());
459    }
460
461    private List JavaDoc list(QueueSession JavaDoc session, Queue JavaDoc queue) throws Exception JavaDoc
462    {
463       QueueBrowser JavaDoc browser = session.createBrowser(queue);
464       Enumeration JavaDoc e = browser.getEnumeration();
465       List JavaDoc messages = CollectionsUtil.list(e);
466
467       browser.close();
468
469       return messages;
470    }
471
472    private int size(QueueSession JavaDoc session, Queue JavaDoc queue) throws Exception JavaDoc
473    {
474       List JavaDoc l = list(session, queue);
475       return l.size();
476    }
477
478    private void assertSize(QueueSession JavaDoc session, Queue JavaDoc queue, int size)
479          throws Exception JavaDoc
480    {
481       assertEquals(size, size(session, queue));
482    }
483
484    protected QueueConnectionFactory JavaDoc getQueueConnectionFactory()
485          throws Exception JavaDoc
486    {
487       try
488       {
489          return (QueueConnectionFactory JavaDoc) getInitialContext().lookup(
490                "ConnectionFactory");
491       } catch (NamingException JavaDoc e)
492       {
493          return (QueueConnectionFactory JavaDoc) getInitialContext().lookup(
494                "java:/ConnectionFactory");
495       }
496    }
497
498    protected TopicConnectionFactory JavaDoc getTopicConnectionFactory()
499          throws Exception JavaDoc
500    {
501       try
502       {
503          return (TopicConnectionFactory JavaDoc) getInitialContext().lookup(
504                "ConnectionFactory");
505       } catch (NamingException JavaDoc e)
506       {
507          return (TopicConnectionFactory JavaDoc) getInitialContext().lookup(
508                "java:/ConnectionFactory");
509       }
510    }
511
512    protected void clear(TestStatus status)
513    {
514       status.clear();
515       assertEquals(0, status.bmtQueueRan());
516       assertEquals(0, status.defaultedQueueFired());
517       assertEquals(0, status.messageCount());
518       assertEquals(0, status.nondurableQueueFired());
519       assertEquals(0, status.overrideDefaultedQueueFired());
520       assertEquals(0, status.overrideQueueFired());
521       assertEquals(0, status.queueFired());
522       assertEquals(0, status.topicFired());
523       assertFalse(status.interceptedQueue());
524       assertFalse(status.interceptedTopic());
525       assertFalse(status.postConstruct());
526       assertFalse(status.preDestroy());
527    }
528
529    protected void setSecurity(String JavaDoc user, String JavaDoc password)
530    {
531       SecurityAssociation.setPrincipal(new SimplePrincipal(user));
532       SecurityAssociation.setCredential(password.toCharArray());
533
534       InitialContextFactory.setSecurity(user, password);
535    }
536
537    protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
538    {
539       return InitialContextFactory.getInitialContext();
540    }
541
542    public static Test suite() throws Exception JavaDoc
543    {
544       return getDeploySetup(MDBUnitTestCase.class,
545             "mdbtest-service.xml, mdb-test.jar");
546    }
547
548 }
Popular Tags