KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jtests > jms > framework > UnifiedTestCase


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 Unified JMS 1.1 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" JMS objects (that is to say destinations,
38  * sessions, producers and consumers) are available conveniently for the test cases.
39  * <br />
40  * Classes which want that convenience should extend <code>UnifiedTestCase</code> instead of
41  * <code>JMSTestCase</code>.
42  *
43  * @author Jeff Mesnil (jmesnil@inrialpes.fr)
44  * @version $Id: UnifiedTestCase.java,v 1.1 2002/06/20 11:07:18 jmesnil Exp $
45  * @since JMS 1.1
46  */

47 public class UnifiedTestCase extends JMSTestCase {
48
49     protected Admin admin;
50     protected InitialContext ctx;
51     private static final String JavaDoc CF_NAME = "testCF";
52     private static final String JavaDoc TCF_NAME = "testTCF";
53     private static final String JavaDoc QCF_NAME = "testQCF";
54     private static final String JavaDoc DESTINATION_NAME = "testDestination";
55     private static final String JavaDoc QUEUE_NAME = "testQueue";
56     private static final String JavaDoc TOPIC_NAME = "testTopic";
57
58     ////////////////////
59
// Unified Domain //
60
////////////////////
61

62     /**
63      * Destination used by a producer
64      */

65     protected Destination producerDestination;
66     
67     /**
68      * Producer
69      */

70     protected MessageProducer producer;
71     
72     /**
73      * ConnectionFactory of the producer
74      */

75     protected ConnectionFactory producerCF;
76     
77     /**
78      * Connection of the producer
79      */

80     protected Connection producerConnection;
81     
82     /**
83      * Session of the producer (non transacted, AUTO_ACKNOWLEDGE)
84      */

85     protected Session producerSession;
86     
87     /**
88      * Destination used by a consumer
89      */

90     protected Destination consumerDestination;
91     
92     /**
93      * Consumer on destination
94      */

95     protected MessageConsumer consumer;
96     
97     /**
98      * ConnectionFactory of the consumer
99      */

100     protected ConnectionFactory consumerCF;
101     
102     /**
103      * Connection of the consumer
104      */

105     protected Connection consumerConnection;
106     
107     /**
108      * Session of the consumer (non transacted, AUTO_ACKNOWLEDGE)
109      */

110     protected Session consumerSession;
111
112     ////////////////
113
// PTP Domain //
114
////////////////
115

116     /**
117      * QueueConnectionFactory
118      */

119     protected QueueConnectionFactory queueConnectionFactory;
120     
121     /**
122      * Queue
123      */

124     protected Queue queue;
125
126     ////////////////////
127
// Pub/Sub Domain //
128
////////////////////
129

130     /**
131      * TopicConnectionFactory
132      */

133     protected TopicConnectionFactory topicConnectionFactory;
134     
135     /**
136      * Topic
137      */

138     protected Topic topic;
139     
140     /**
141      * Create all administrated objects connections and sessions ready to use for tests.
142      * <br />
143      * Start connections.
144      */

145     protected void setUp() {
146     try {
147         // Admin step
148
// gets the provider administration wrapper...
149
admin = AdminFactory.getAdmin();
150         // ...and creates administrated objects and binds them
151
admin.createConnectionFactory(CF_NAME);
152         admin.createQueueConnectionFactory(QCF_NAME);
153         admin.createTopicConnectionFactory(TCF_NAME);
154             // destination for unified domain is a queue
155
admin.createQueue(DESTINATION_NAME);
156         admin.createQueue(QUEUE_NAME);
157         admin.createTopic(TOPIC_NAME);
158             
159         // end of admin step, start of JMS client step
160
ctx = admin.createInitialContext();
161             
162         producerCF = (ConnectionFactory)ctx.lookup(CF_NAME);
163             // we see destination of the unified domain as a javax.jms.Destination
164
// instead of a javax.jms.Queue to be more generic
165
producerDestination = (Destination)ctx.lookup(DESTINATION_NAME);
166         producerConnection = producerCF.createConnection();
167         producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
168         producer = producerSession.createProducer(producerDestination);
169
170         consumerCF = (ConnectionFactory)ctx.lookup(CF_NAME);
171             // we see destination of the unified domain as a javax.jms.Destination
172
// instead of a javax.jms.Queue to be more generic
173
consumerDestination = (Destination)ctx.lookup(DESTINATION_NAME);
174         consumerConnection = consumerCF.createConnection();
175         consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
176         consumer = consumerSession.createConsumer(consumerDestination);
177
178             queueConnectionFactory = (QueueConnectionFactory)ctx.lookup(QCF_NAME);
179             queue = (Queue)ctx.lookup(QUEUE_NAME);
180
181             topicConnectionFactory = (TopicConnectionFactory)ctx.lookup(TCF_NAME);
182             topic = (Topic)ctx.lookup(TOPIC_NAME);
183             
184         producerConnection.start();
185         consumerConnection.start();
186         //end of client step
187
} catch (Exception JavaDoc e) {
188         //XXX
189
e.printStackTrace();
190     }
191     }
192     
193     /**
194      * Close connections and delete administrated objects
195      */

196     protected void tearDown() {
197     try {
198         consumerConnection.close();
199         producerConnection.close();
200             
201         admin.deleteConnectionFactory(CF_NAME);
202         admin.deleteQueueConnectionFactory(QCF_NAME);
203         admin.deleteTopicConnectionFactory(TCF_NAME);
204         admin.deleteQueue(DESTINATION_NAME);
205         admin.deleteQueue(QUEUE_NAME);
206         admin.deleteTopic(TOPIC_NAME);
207
208         ctx.close();
209     } catch (Exception JavaDoc e) {
210         //XXX
211
e.printStackTrace();
212     } finally {
213         producerDestination = null;
214         producer = null;
215         producerCF = null;
216         producerSession = null;
217         producerConnection = null;
218
219         consumerDestination = null;
220         consumer = null;
221         consumerCF = null;
222         consumerSession = null;
223         consumerConnection = null;
224
225             queueConnectionFactory = null;
226             queue = null;
227
228             topicConnectionFactory = null;
229             topic = null;
230     }
231     }
232     
233     public UnifiedTestCase(String JavaDoc name) {
234     super(name);
235     }
236 }
237
Popular Tags