KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > dd > 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.dd.mdb.unit;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.jms.ObjectMessage JavaDoc;
28 import javax.jms.Queue JavaDoc;
29 import javax.jms.QueueConnection JavaDoc;
30 import javax.jms.QueueConnectionFactory JavaDoc;
31 import javax.jms.QueueSender JavaDoc;
32 import javax.jms.QueueSession JavaDoc;
33 import javax.jms.Session JavaDoc;
34 import javax.jms.TextMessage JavaDoc;
35 import javax.jms.Topic JavaDoc;
36 import javax.jms.TopicConnection JavaDoc;
37 import javax.jms.TopicConnectionFactory JavaDoc;
38 import javax.jms.TopicPublisher JavaDoc;
39 import javax.jms.TopicSession JavaDoc;
40 import javax.naming.NamingException JavaDoc;
41 import org.jboss.ejb3.test.dd.mdb.CustomMessage;
42 import org.jboss.ejb3.test.dd.mdb.TestStatus;
43 import org.jboss.logging.Logger;
44 import org.jboss.test.JBossTestCase;
45 import org.jboss.test.JBossTestSetup;
46 import junit.framework.Test;
47 import junit.framework.TestSuite;
48
49 /**
50  * Some simple tests of MDB. These could be much more elaborated.
51  *
52  * In the future at least the following tests should be done some how:
53  * <ol>
54  * <li>Queue
55  * <li>Topic
56  * <li>Durable topic
57  * <li>Bean TX - with AUTO_ACK and DUPS_OK
58  * <li>CMT Required
59  * <li>CMT NotSupported
60  * <li>Selector
61  * <li>User and password login
62  * <li>Al the stuff with the context
63  * </ol>
64  *
65  * <p>Created: Fri Dec 29 16:53:26 2000
66  *
67  * @author <a HREF="mailto:peter.antman@tim.se">Peter Antman</a>
68  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
69  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
70  * @version <pre>$Revision: 45712 $</pre>
71  */

72 public class MDBUnitTestCase
73    extends JBossTestCase
74 {
75    private static final Logger log = Logger.getLogger(MDBUnitTestCase.class);
76
77    // Provider specific
78
static String JavaDoc TOPIC_FACTORY = "ConnectionFactory";
79    static String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
80
81    QueueConnection JavaDoc queueConnection;
82    TopicConnection JavaDoc topicConnection;
83    
84    static HashMap JavaDoc jndiToDestName = new HashMap JavaDoc();
85    static
86    {
87       jndiToDestName.put("queue/testQueue", "QUEUE.testQueue");
88       jndiToDestName.put("topic/testTopic", "TOPIC.testTopic");
89       jndiToDestName.put("topic/testDurableTopic", "TOPIC.testDurableTopic");
90       jndiToDestName.put("queue/ex", "QUEUE.ex");
91       jndiToDestName.put("queue/A", "QUEUE.A");
92       jndiToDestName.put("queue/B", "QUEUE.B");
93    }
94    
95
96    // JMSProviderAdapter providerAdapter;
97

98    String JavaDoc dest;
99
100    public MDBUnitTestCase(String JavaDoc name, String JavaDoc dest) {
101       super(name);
102       this.dest = dest;
103       // Get JMS JNDI Adapter
104
// Class cls = Class.forName(providerAdapterClass);
105
// providerAdapter = (JMSProviderAdapter)cls.newInstance();
106
// This is not completly clean since it still have to use
107
// provider specific queue and topic names!!
108
}
109
110
111    protected void tearDown() throws Exception JavaDoc {
112       if (topicConnection != null) {
113          topicConnection.close();
114       }
115       if (queueConnection != null) {
116          queueConnection.close();
117       }
118    }
119
120    protected void printHeader() {
121       log.info("\n---- Testing method " + getName() +
122                          " for destination " +dest);
123    }
124
125    private QueueSession JavaDoc getQueueSession() throws Exception JavaDoc {
126       if (queueConnection == null) {
127          QueueConnectionFactory JavaDoc queueFactory =
128             (QueueConnectionFactory JavaDoc)getInitialContext().lookup(QUEUE_FACTORY);
129
130          queueConnection = queueFactory.createQueueConnection();
131       }
132       return queueConnection.createQueueSession(false,
133                                                 Session.AUTO_ACKNOWLEDGE);
134    }
135
136    private TopicSession JavaDoc getTopicSession() throws Exception JavaDoc {
137       if (topicConnection == null) {
138          TopicConnectionFactory JavaDoc topicFactory =
139             (TopicConnectionFactory JavaDoc)getInitialContext().lookup(TOPIC_FACTORY);
140          topicConnection = topicFactory.createTopicConnection();
141       }
142
143       // No transaction & auto ack
144
return topicConnection.createTopicSession(false,
145                                                 Session.AUTO_ACKNOWLEDGE);
146    }
147
148    /**
149     * Test sending messages to Topic testTopic
150     */

151    public void testQueue() throws Exception JavaDoc {
152       printHeader();
153       TestStatus status = (TestStatus)getInitialContext().lookup("TestStatusBean/remote");
154       status.clear();
155       
156       QueueSession JavaDoc session = getQueueSession();
157       Queue JavaDoc queue = (Queue JavaDoc)getInitialContext().lookup(dest);
158       QueueSender JavaDoc sender = session.createSender(queue);
159
160       log.debug("TestQueue: " + dest + " Sending 10 messages 1-10");
161       for (int i = 1; i < 11; i++) {
162          TextMessage JavaDoc message = session.createTextMessage();
163          message.setText("Queue Message " + dest + " nr " + i);
164          sender.send(queue, message);
165       }
166
167       sender.close();
168       
169       Thread.currentThread().sleep(10000);
170       
171       ArrayList JavaDoc destinations = status.getDestinations();
172       int size = (dest.equals("queue/ex")) ? 110 : 10;
173
174       assertEquals(size, destinations.size());
175       String JavaDoc destinationName = (String JavaDoc)jndiToDestName.get(dest);
176       for (Iterator JavaDoc it = destinations.iterator() ; it.hasNext() ;)
177       {
178          assertEquals(destinationName, it.next());
179       }
180 }
181
182    /**
183     * Test sending messages to Queue testQueue
184     */

185    public void testTopic() throws Exception JavaDoc {
186       printHeader();
187       TestStatus status = (TestStatus)getInitialContext().lookup("TestStatusBean/remote");
188       status.clear();
189
190       TopicSession JavaDoc session = getTopicSession();
191       Topic JavaDoc topic = (Topic JavaDoc)getInitialContext().lookup(dest);
192       TopicPublisher JavaDoc pub = session.createPublisher(topic);
193
194       log.debug("TestTopic: " + dest +
195                          ": Sending 10st messages 1-10");
196
197       for (int i = 1; i < 11; i++) {
198          TextMessage JavaDoc message = session.createTextMessage();
199          message.setText("Topic Message " + dest + " nr " + i);
200          pub.publish(topic, message);
201       }
202
203       pub.close();
204
205       Thread.currentThread().sleep(10000);
206
207       ArrayList JavaDoc destinations = status.getDestinations();
208       
209       assertEquals(10, destinations.size());
210       String JavaDoc destinationName = (String JavaDoc)jndiToDestName.get(dest);
211       for (Iterator JavaDoc it = destinations.iterator() ; it.hasNext() ;)
212       {
213          String JavaDoc name = (String JavaDoc)it.next();
214          assertTrue("Destination name should start with '" + destinationName + "' it was '" + name + "'", name.startsWith(destinationName));
215       }
216      
217 }
218
219    /**
220     * Test sending messages to queue testObjectMessage
221     */

222    public void testObjectMessage() throws Exception JavaDoc {
223       printHeader();
224       QueueSession JavaDoc session = getQueueSession();
225       // Non portable!!
226
Queue JavaDoc queue = (Queue JavaDoc)getInitialContext().lookup("queue/testObjectMessage");
227       QueueSender JavaDoc sender = session.createSender(queue);
228
229       log.debug("TestQueue: Sending 10 messages 1-10");
230       for (int i = 1; i < 11; i++) {
231          ObjectMessage JavaDoc message = session.createObjectMessage();
232          message.setObject(new CustomMessage(i));
233          sender.send(queue, message);
234       }
235
236       sender.close();
237       session.close();
238    }
239
240
241    public void testWaitForCompletion() throws Exception JavaDoc {
242       try { Thread.currentThread().sleep(1000*20);
243       } catch ( InterruptedException JavaDoc e ) {}
244    }
245
246    public void testNoQueueConstructionForAlreadyExists()
247       throws Exception JavaDoc
248    {
249       try
250       {
251          getInitialContext().lookup("queue/QueueInADifferentContext");
252       }
253       catch (NamingException JavaDoc expected)
254       {
255          return;
256       }
257       fail("It should not create queue/QueueInADifferentContext");
258    }
259
260    public void testNoTopicConstructionForAlreadyExists()
261       throws Exception JavaDoc
262    {
263       try
264       {
265          getInitialContext().lookup("topic/TopicInADifferentContext");
266       }
267       catch (NamingException JavaDoc expected)
268       {
269          return;
270       }
271       fail("It should not create topic/TopicInADifferentContext");
272    }
273
274    /**
275     * Setup the test suite.
276     */

277    public static Test suite() throws Exception JavaDoc
278    {
279       TestSuite suite = new TestSuite();
280       suite.addTest(new MDBUnitTestCase("testQueue","queue/testQueue"));
281       suite.addTest(new MDBUnitTestCase("testTopic","topic/testTopic"));
282       suite.addTest(new MDBUnitTestCase("testTopic","topic/testDurableTopic"));
283       suite.addTest(new MDBUnitTestCase("testQueue","queue/ex"));
284       suite.addTest(new MDBUnitTestCase("testQueue","queue/A"));
285       suite.addTest(new MDBUnitTestCase("testWaitForCompletion",""));
286       suite.addTest(new MDBUnitTestCase("testQueue","queue/B"));
287
288      return new JBossTestSetup(getDeploySetup(suite, "dd-mdb-jbossmq-destinations-service.xml, dd-mdb-service.xml, dd-mdb.jar"));
289
290    }
291 }
292
293
294
295
Popular Tags