KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > mdb > test > 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.test.mdb.test;
23
24 import javax.management.ObjectName JavaDoc;
25 import javax.jms.*;
26 import javax.naming.*;
27
28 import junit.framework.Test;
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
32 import org.jboss.test.mdb.bean.CustomMessage;
33
34 import org.jboss.test.JBossTestCase;
35 import org.jboss.test.JBossTestSetup;
36 import org.jboss.test.jbossmq.test.SecurityUnitTestCase;
37
38 /**
39  * Some simple tests of MDB. These could be much more elaborated.
40  *
41  * In the future at least the following tests should be done some how:
42  * <ol>
43  * <li>Queue
44  * <li>Topic
45  * <li>Durable topic
46  * <li>Bean TX - with AUTO_ACK and DUPS_OK
47  * <li>CMT Required
48  * <li>CMT NotSupported
49  * <li>Selector
50  * <li>User and password login
51  * <li>Al the stuff with the context
52  * </ol>
53  *
54  * <p>Created: Fri Dec 29 16:53:26 2000
55  *
56  * @author <a HREF="mailto:peter.antman@tim.se">Peter Antman</a>
57  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
58  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
59  * @version <pre>$Revision: 37406 $</pre>
60  */

61 public class MDBUnitTestCase
62    extends JBossTestCase
63 {
64    // Static --------------------------------------------------------
65

66    // Provider specific
67
static String JavaDoc TOPIC_FACTORY = "ConnectionFactory";
68    static String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
69
70    QueueConnection queueConnection;
71    TopicConnection topicConnection;
72
73    // JMSProviderAdapter providerAdapter;
74

75    String JavaDoc dest;
76
77    public MDBUnitTestCase(String JavaDoc name, String JavaDoc dest) {
78       super(name);
79       this.dest = dest;
80       // Get JMS JNDI Adapter
81
// Class cls = Class.forName(providerAdapterClass);
82
// providerAdapter = (JMSProviderAdapter)cls.newInstance();
83
// This is not completly clean since it still have to use
84
// provider specific queue and topic names!!
85
}
86
87
88    protected void tearDown() throws Exception JavaDoc {
89       if (topicConnection != null) {
90          topicConnection.close();
91       }
92       if (queueConnection != null) {
93          queueConnection.close();
94       }
95    }
96
97    protected void printHeader() {
98       getLog().info("\n---- Testing method " + getName() +
99                          " for destination " +dest);
100    }
101
102    private QueueSession getQueueSession() throws Exception JavaDoc {
103       if (queueConnection == null) {
104          QueueConnectionFactory queueFactory =
105             (QueueConnectionFactory)getInitialContext().lookup(QUEUE_FACTORY);
106
107          queueConnection = queueFactory.createQueueConnection();
108       }
109       return queueConnection.createQueueSession(false,
110                                                 Session.AUTO_ACKNOWLEDGE);
111    }
112
113    private TopicSession getTopicSession() throws Exception JavaDoc {
114       if (topicConnection == null) {
115          TopicConnectionFactory topicFactory =
116             (TopicConnectionFactory)getInitialContext().lookup(TOPIC_FACTORY);
117          topicConnection = topicFactory.createTopicConnection();
118       }
119
120       // No transaction & auto ack
121
return topicConnection.createTopicSession(false,
122                                                 Session.AUTO_ACKNOWLEDGE);
123    }
124
125    /**
126     * Test sending messages to Topic testTopic
127     */

128    public void testQueue() throws Exception JavaDoc {
129       printHeader();
130       QueueSession session = getQueueSession();
131       Queue queue = (Queue)getInitialContext().lookup(dest);
132       QueueSender sender = session.createSender(queue);
133
134       getLog().debug("TestQueue: " + dest + " Sending 10 messages 1-10");
135       for (int i = 1; i < 11; i++) {
136          TextMessage message = session.createTextMessage();
137          message.setText("Queue Message " + dest + " nr " + i);
138          sender.send(queue, message);
139       }
140
141       sender.close();
142    }
143
144    /**
145     * Test sending messages to Queue testQueue
146     */

147    public void testTopic() throws Exception JavaDoc {
148       printHeader();
149       TopicSession session = getTopicSession();
150       Topic topic = (Topic)getInitialContext().lookup(dest);
151       TopicPublisher pub = session.createPublisher(topic);
152
153       getLog().debug("TestTopic: " + dest +
154                          ": Sending 10st messages 1-10");
155
156       for (int i = 1; i < 11; i++) {
157          TextMessage message = session.createTextMessage();
158          message.setText("Topic Message " + dest + " nr " + i);
159          pub.publish(topic, message);
160       }
161
162       pub.close();
163    }
164
165    /**
166     * Test sending messages to queue testObjectMessage
167     */

168    public void testObjectMessage() throws Exception JavaDoc {
169       printHeader();
170       QueueSession session = getQueueSession();
171       // Non portable!!
172
Queue queue = (Queue)getInitialContext().lookup("queue/testObjectMessage");
173       QueueSender sender = session.createSender(queue);
174
175       getLog().debug("TestQueue: Sending 10 messages 1-10");
176       for (int i = 1; i < 11; i++) {
177          ObjectMessage message = session.createObjectMessage();
178          message.setObject(new CustomMessage(i));
179          sender.send(queue, message);
180       }
181
182       sender.close();
183       session.close();
184    }
185
186
187    public void testWaitForCompleation() throws Exception JavaDoc {
188       try { Thread.currentThread().sleep(1000*20);
189       } catch ( InterruptedException JavaDoc e ) {}
190    }
191
192    public void testNoQueueConstructionForAlreadyExists()
193       throws Exception JavaDoc
194    {
195       try
196       {
197          getInitialContext().lookup("queue/QueueInADifferentContext");
198       }
199       catch (NamingException expected)
200       {
201          return;
202       }
203       fail("It should not create queue/QueueInADifferentContext");
204    }
205
206    public void testNoTopicConstructionForAlreadyExists()
207       throws Exception JavaDoc
208    {
209       try
210       {
211          getInitialContext().lookup("topic/TopicInADifferentContext");
212       }
213       catch (NamingException expected)
214       {
215          return;
216       }
217       fail("It should not create topic/TopicInADifferentContext");
218    }
219
220    /**
221     * Setup the test suite.
222     */

223    public static Test suite() throws Exception JavaDoc
224    {
225       TestSuite suite = new TestSuite();
226
227       //suite.addTest(new MDBUnitTestCase("testServerFound",""));
228
suite.addTest(new MDBUnitTestCase("testNoQueueConstructionForAlreadyExists",""));
229       suite.addTest(new MDBUnitTestCase("testNoTopicConstructionForAlreadyExists",""));
230       suite.addTest(new MDBUnitTestCase("testObjectMessage",""));
231       suite.addTest(new MDBUnitTestCase("testQueue","queue/testQueue"));
232       suite.addTest(new MDBUnitTestCase("testTopic","topic/testTopic"));
233       suite.addTest(new MDBUnitTestCase("testTopic","topic/testDurableTopic"));
234       suite.addTest(new MDBUnitTestCase("testQueue","queue/ex"));
235       suite.addTest(new MDBUnitTestCase("testQueue","queue/A"));
236       suite.addTest(new MDBUnitTestCase("testWaitForCompleation",""));
237       suite.addTest(new MDBUnitTestCase("testQueue","queue/B"));
238
239       Test wrapper = new JBossTestSetup(suite)
240          {
241          protected void setUp() throws Exception JavaDoc
242          {
243             super.setUp();
244             ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
245             deploy (loader.getResource("messaging/test-destinations-full-service.xml").toString());
246             deploy ("mdb.jar");
247          }
248              protected void tearDown() throws Exception JavaDoc
249              {
250                 super.tearDown();
251
252                 // Remove the DLQ messages created by the TxTimeout test
253
getServer().invoke
254                 (
255                    new ObjectName JavaDoc("jboss.mq.destination:service=Queue,name=DLQ"),
256                    "removeAllMessages",
257                    new Object JavaDoc[0],
258                    new String JavaDoc[0]
259                 );
260
261                 ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
262                 undeploy ("mdb.jar");
263
264                 // Remove the durable subscription
265
TopicConnectionFactory topicFactory = (TopicConnectionFactory) getInitialContext().lookup(TOPIC_FACTORY);
266                 TopicConnection topicConnection = topicFactory.createTopicConnection("john", "needle");
267                 TopicSession session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
268                 session.unsubscribe("DurableSubscriberExample");
269                 topicConnection.close();
270
271                 undeploy (loader.getResource("messaging/test-destinations-full-service.xml").toString());
272
273              }
274           };
275
276       return wrapper;
277    }
278 }
279
280
281
282
Popular Tags