001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.mdb.test;
023:
024: import javax.jms.ObjectMessage;
025: import javax.jms.Queue;
026: import javax.jms.QueueConnection;
027: import javax.jms.QueueConnectionFactory;
028: import javax.jms.QueueSender;
029: import javax.jms.QueueSession;
030: import javax.jms.Session;
031: import javax.jms.TextMessage;
032: import javax.jms.Topic;
033: import javax.jms.TopicConnection;
034: import javax.jms.TopicConnectionFactory;
035: import javax.jms.TopicPublisher;
036: import javax.jms.TopicSession;
037: import javax.management.ObjectName;
038: import javax.naming.NamingException;
039:
040: import junit.framework.Test;
041: import junit.framework.TestSuite;
042:
043: import org.jboss.test.JBossTestCase;
044: import org.jboss.test.JBossTestSetup;
045: import org.jboss.test.mdb.bean.CustomMessage;
046:
047: /**
048: * Some simple tests of MDB. These could be much more elaborated.
049: *
050: * In the future at least the following tests should be done some how:
051: * <ol>
052: * <li>Queue
053: * <li>Topic
054: * <li>Durable topic
055: * <li>Bean TX - with AUTO_ACK and DUPS_OK
056: * <li>CMT Required
057: * <li>CMT NotSupported
058: * <li>Selector
059: * <li>User and password login
060: * <li>Al the stuff with the context
061: * </ol>
062: *
063: * <p>Created: Fri Dec 29 16:53:26 2000
064: *
065: * @author <a href="mailto:peter.antman@tim.se">Peter Antman</a>
066: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
067: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
068: * @version <pre>$Revision: 57211 $</pre>
069: */
070: public class MDBUnitTestCase extends JBossTestCase {
071: // Static --------------------------------------------------------
072:
073: // Provider specific
074: static String TOPIC_FACTORY = "ConnectionFactory";
075:
076: static String QUEUE_FACTORY = "ConnectionFactory";
077:
078: QueueConnection queueConnection;
079:
080: TopicConnection topicConnection;
081:
082: // JMSProviderAdapter providerAdapter;
083:
084: String dest;
085:
086: public MDBUnitTestCase(String name, String dest) {
087: super (name);
088: this .dest = dest;
089: // Get JMS JNDI Adapter
090: // Class cls = Class.forName(providerAdapterClass);
091: // providerAdapter = (JMSProviderAdapter)cls.newInstance();
092: // This is not completly clean since it still have to use
093: // provider specific queue and topic names!!
094: }
095:
096: protected void tearDown() throws Exception {
097: if (topicConnection != null) {
098: topicConnection.close();
099: }
100: if (queueConnection != null) {
101: queueConnection.close();
102: }
103: }
104:
105: protected void printHeader() {
106: getLog().info(
107: "\n---- Testing method " + getName()
108: + " for destination " + dest);
109: }
110:
111: private QueueSession getQueueSession() throws Exception {
112: if (queueConnection == null) {
113: QueueConnectionFactory queueFactory = (QueueConnectionFactory) getInitialContext()
114: .lookup(QUEUE_FACTORY);
115:
116: queueConnection = queueFactory.createQueueConnection();
117: }
118: return queueConnection.createQueueSession(false,
119: Session.AUTO_ACKNOWLEDGE);
120: }
121:
122: private TopicSession getTopicSession() throws Exception {
123: if (topicConnection == null) {
124: TopicConnectionFactory topicFactory = (TopicConnectionFactory) getInitialContext()
125: .lookup(TOPIC_FACTORY);
126: topicConnection = topicFactory.createTopicConnection();
127: }
128:
129: // No transaction & auto ack
130: return topicConnection.createTopicSession(false,
131: Session.AUTO_ACKNOWLEDGE);
132: }
133:
134: /**
135: * Test sending messages to Topic testTopic
136: */
137: public void testQueue() throws Exception {
138: printHeader();
139: QueueSession session = getQueueSession();
140: Queue queue = (Queue) getInitialContext().lookup(dest);
141: QueueSender sender = session.createSender(queue);
142:
143: getLog().debug(
144: "TestQueue: " + dest + " Sending 10 messages 1-10");
145: for (int i = 1; i < 11; i++) {
146: TextMessage message = session.createTextMessage();
147: message.setText("Queue Message " + dest + " nr " + i);
148: sender.send(queue, message);
149: }
150:
151: sender.close();
152: }
153:
154: /**
155: * Test sending messages to Queue testQueue
156: */
157: public void testTopic() throws Exception {
158: printHeader();
159: TopicSession session = getTopicSession();
160: Topic topic = (Topic) getInitialContext().lookup(dest);
161: TopicPublisher pub = session.createPublisher(topic);
162:
163: getLog().debug(
164: "TestTopic: " + dest + ": Sending 10st messages 1-10");
165:
166: for (int i = 1; i < 11; i++) {
167: TextMessage message = session.createTextMessage();
168: message.setText("Topic Message " + dest + " nr " + i);
169: pub.publish(topic, message);
170: }
171:
172: pub.close();
173: }
174:
175: /**
176: * Test sending messages to queue testObjectMessage
177: */
178: public void testObjectMessage() throws Exception {
179: printHeader();
180: QueueSession session = getQueueSession();
181: // Non portable!!
182: Queue queue = (Queue) getInitialContext().lookup(
183: "queue/testObjectMessage");
184: QueueSender sender = session.createSender(queue);
185:
186: getLog().debug("TestQueue: Sending 10 messages 1-10");
187: for (int i = 1; i < 11; i++) {
188: ObjectMessage message = session.createObjectMessage();
189: message.setObject(new CustomMessage(i));
190: sender.send(queue, message);
191: }
192:
193: sender.close();
194: session.close();
195: }
196:
197: public void testWaitForCompleation() throws Exception {
198: try {
199: Thread.sleep(1000 * 20);
200: } catch (InterruptedException e) {
201: }
202: }
203:
204: public void testNoQueueConstructionForAlreadyExists()
205: throws Exception {
206: try {
207: getInitialContext()
208: .lookup("queue/QueueInADifferentContext");
209: } catch (NamingException expected) {
210: return;
211: }
212: fail("It should not create queue/QueueInADifferentContext");
213: }
214:
215: public void testNoTopicConstructionForAlreadyExists()
216: throws Exception {
217: try {
218: getInitialContext()
219: .lookup("topic/TopicInADifferentContext");
220: } catch (NamingException expected) {
221: return;
222: }
223: fail("It should not create topic/TopicInADifferentContext");
224: }
225:
226: /**
227: * Setup the test suite.
228: */
229: public static Test suite() throws Exception {
230: TestSuite suite = new TestSuite();
231: suite.addTest(new MDBUnitTestCase(
232: "testNoQueueConstructionForAlreadyExists", ""));
233: suite.addTest(new MDBUnitTestCase(
234: "testNoTopicConstructionForAlreadyExists", ""));
235: suite.addTest(new MDBUnitTestCase("testObjectMessage", ""));
236: suite.addTest(new MDBUnitTestCase("testQueue",
237: "queue/testQueue"));
238: suite.addTest(new MDBUnitTestCase("testTopic",
239: "topic/testTopic"));
240: suite.addTest(new MDBUnitTestCase("testTopic",
241: "topic/testDurableTopic"));
242: suite.addTest(new MDBUnitTestCase("testQueue", "queue/ex"));
243: suite.addTest(new MDBUnitTestCase("testQueue", "queue/A"));
244: suite
245: .addTest(new MDBUnitTestCase("testWaitForCompleation",
246: ""));
247: suite.addTest(new MDBUnitTestCase("testQueue", "queue/B"));
248:
249: return new JBossTestSetup(getDeploySetup(suite, "mdb.jar")) {
250: protected void tearDown() throws Exception {
251: super .tearDown();
252:
253: // Remove the DLQ messages created by the TxTimeout test
254: getServer()
255: .invoke(
256: new ObjectName(
257: "jboss.mq.destination:service=Queue,name=DLQ"),
258: "removeAllMessages", new Object[0],
259: new String[0]);
260:
261: // Remove the durable subscription
262: TopicConnectionFactory topicFactory = (TopicConnectionFactory) getInitialContext()
263: .lookup(TOPIC_FACTORY);
264: TopicConnection topicConnection = topicFactory
265: .createTopicConnection("john", "needle");
266: TopicSession session = topicConnection
267: .createTopicSession(false,
268: Session.AUTO_ACKNOWLEDGE);
269: session.unsubscribe("DurableSubscriberExample");
270: topicConnection.close();
271: }
272: };
273: }
274: }
|