Source code: org/objectweb/jtests/jms/framework/PubSubTestCase.java
1 /*
2 * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3 * Copyright (C) 2002 INRIA
4 * Contact: joram-team@objectweb.org
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA
20 *
21 * Initial developer(s): Jeff Mesnil (jmesnil@inrialpes.fr)
22 * Contributor(s): ______________________________________.
23 */
24
25 package org.objectweb.jtests.jms.framework;
26
27 import org.objectweb.jtests.jms.admin.*;
28
29 import junit.framework.*;
30 import javax.naming.*;
31 import javax.jms.*;
32
33 /**
34 * Creates convenient JMS Publish/Subscribe objects which can be needed for tests.
35 * <br />
36 * This class defines the setUp and tearDown methods so
37 * that JMS administrated objects and other "ready to use" Pub/Sub objects (that is to say topics,
38 * sessions, publishers and subscribers) are available conveniently for the test cases.
39 * <br />
40 * Classes which want that convenience should extend <code>PubSubTestCase</code> instead of
41 * <code>JMSTestCase</code>.
42 *
43 * @author Jeff Mesnil (jmesnil@inrialpes.fr)
44 * @version $Id: PubSubTestCase.java,v 1.1 2002/04/21 21:15:19 chirino Exp $
45 */
46 public class PubSubTestCase extends JMSTestCase {
47
48 private Admin admin;
49 private InitialContext ctx;
50 private static final String TCF_NAME = "testTCF";
51 private static final String TOPIC_NAME = "testTopic";
52
53 /**
54 * Topic used by a publisher
55 */
56 protected Topic publisherTopic;
57
58 /**
59 * Publisher on queue
60 */
61 protected TopicPublisher publisher;
62
63 /**
64 * TopicConnectionFactory of the publisher
65 */
66 protected TopicConnectionFactory publisherTCF;
67
68 /**
69 * TopicConnection of the publisher
70 */
71 protected TopicConnection publisherConnection;
72
73 /**
74 * TopicSession of the publisher (non transacted, AUTO_ACKNOWLEDGE)
75 */
76 protected TopicSession publisherSession;
77
78 /**
79 * Topic used by a subscriber
80 */
81 protected Topic subscriberTopic;
82
83 /**
84 * Subscriber on queue
85 */
86 protected TopicSubscriber subscriber;
87
88 /**
89 * TopicConnectionFactory of the subscriber
90 */
91 protected TopicConnectionFactory subscriberTCF;
92
93 /**
94 * TopicConnection of the subscriber
95 */
96 protected TopicConnection subscriberConnection;
97
98 /**
99 * TopicSession of the subscriber (non transacted, AUTO_ACKNOWLEDGE)
100 */
101 protected TopicSession subscriberSession;
102
103
104 /**
105 * Create all administrated objects connections and sessions ready to use for tests.
106 * <br />
107 * Start connections.
108 */
109 protected void setUp() {
110 try {
111 // Admin step
112 // gets the provider administration wrapper...
113 admin = AdminFactory.getAdmin();
114 // ...and creates administrated objects and binds them
115 admin.createTopicConnectionFactory(TCF_NAME);
116 admin.createTopic(TOPIC_NAME);
117
118 // end of admin step, start of JMS client step
119 ctx = admin.createInitialContext();
120
121 publisherTCF = (TopicConnectionFactory)ctx.lookup(TCF_NAME);
122 publisherTopic = (Topic)ctx.lookup(TOPIC_NAME);
123 publisherConnection = publisherTCF.createTopicConnection();
124 if (publisherConnection.getClientID() == null) {
125 publisherConnection.setClientID("publisherConnection");
126 }
127 publisherSession = publisherConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
128 publisher = publisherSession.createPublisher(publisherTopic);
129
130 subscriberTCF = (TopicConnectionFactory)ctx.lookup(TCF_NAME);
131 subscriberTopic = (Topic)ctx.lookup(TOPIC_NAME);
132 subscriberConnection = subscriberTCF.createTopicConnection();
133 if (subscriberConnection.getClientID() == null) {
134 subscriberConnection.setClientID("subscriberConnection");
135 }
136 subscriberSession = subscriberConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
137 subscriber = subscriberSession.createSubscriber(subscriberTopic);
138
139 publisherConnection.start();
140 subscriberConnection.start();
141 //end of client step
142 } catch (Exception e) {
143 //XXX
144 e.printStackTrace();
145 }
146 }
147
148 /**
149 * Close connections and delete administrated objects
150 */
151 protected void tearDown() {
152 try {
153 publisherConnection.close();
154 subscriberConnection.close();
155
156 admin.deleteTopicConnectionFactory(TCF_NAME);
157 admin.deleteTopic(TOPIC_NAME);
158
159 ctx.close();
160 } catch (Exception e) {
161 //XXX
162 e.printStackTrace();
163 } finally {
164 publisherTopic = null;
165 publisher = null;
166 publisherTCF = null;
167 publisherSession = null;
168 publisherConnection = null;
169
170 subscriberTopic = null;
171 subscriber = null;
172 subscriberTCF = null;
173 subscriberSession = null;
174 subscriberConnection = null;
175 }
176 }
177
178 public PubSubTestCase (String name) {
179 super(name);
180 }
181 }