KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmessaging > JMSTestCase


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.jbossmessaging;
23
24 import java.net.URL JavaDoc;
25 import java.util.Properties JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingEnumeration JavaDoc;
28 import javax.naming.NameClassPair JavaDoc;
29
30 import org.objectweb.jtests.jms.admin.Admin;
31 import org.objectweb.jtests.jms.admin.AdminFactory;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.util.NestedRuntimeException;
35 import org.jboss.test.JBossTestCase;
36
37 /**
38  * JMSTestCase.
39  * A base test case for all JMS generic test cases.
40  *
41  * Warning:
42  * If you override setUp() or tearDown(), rememeber to call the superclass
43  * versions of these methods within your overriding methods.
44  * AbstractTestCase uses setUp() and tearDown() to initialise logging.
45  *
46  * @author <a HREF="richard.achmatowicz@jboss.com">Richard Achmatowicz</a>
47  * @version $Revision: 38213 $
48  */

49 public class JMSTestCase extends JBossTestCase
50 {
51     private static String JavaDoc PROP_FILE_NAME = "provider.properties" ;
52     private static String JavaDoc PROP_NAME = "jms.provider.resources.dir" ;
53
54     protected Admin admin;
55
56     /**
57      * Constructor for JMSTestCase object
58      * @param name test case name
59      */

60     public JMSTestCase(String JavaDoc name)
61     {
62     super(name);
63     }
64
65     /**
66      * Create the Admin object to perform all JMS adminsitrative functions
67      * in a JMS provider-independent manner
68      */

69     protected void setUp() throws Exception JavaDoc
70     {
71     // perform any setUp required in the superclass
72
super.setUp() ;
73
74         try {
75         log.info("setting up Admin") ;
76         // get the Admin implementation for the current JMS provider
77
// specified in provider.properties
78
admin = AdminFactory.getAdmin() ;
79     } catch (Exception JavaDoc e) {
80             throw new NestedRuntimeException("getAdmin() operation failed", e) ;
81     }
82     }
83
84     /**
85      * Create a JMS Queue.
86      *
87      * The Queue is created dynamically, in a JMS provider-specific manner,
88      * according to the instance of the Admin interface currently in use.
89      *
90      * @param name The name of the Queue to be created.
91      */

92     public void createQueue(String JavaDoc name)
93     {
94         try {
95             admin.createQueue(name) ;
96         } catch (Exception JavaDoc e) {
97             throw new NestedRuntimeException("createQueue() operation failed", e) ;
98         }
99     }
100
101     /**
102      * Delete a JMS Queue.
103      *
104      * The Queue is deleted dynamically, in a JMS provider-specific manner,
105      * according to the instance of the Admin interface currently in use.
106      *
107      * @param name The name of the Queue to be deleted.
108      */

109     public void deleteQueue(String JavaDoc name)
110     {
111         try {
112             admin.deleteQueue(name) ;
113         } catch (Exception JavaDoc e) {
114             throw new NestedRuntimeException("deleteQueue() operation failed", e) ;
115         }
116     }
117
118     /**
119      * Create a JMS Topic.
120      *
121      * The Topic is created dynamically, in a JMS provider-specific manner,
122      * according to the instance of the Admin interface currently in use.
123      *
124      * @param name The name of the Topic to be created.
125      */

126     public void createTopic(String JavaDoc name)
127     {
128         try {
129             admin.createTopic(name) ;
130         } catch (Exception JavaDoc e) {
131             throw new NestedRuntimeException("createTopic() operation failed", e) ;
132         }
133     }
134
135     /**
136      * Delete a JMS Topic.
137      *
138      * The Topic is deleted dynamically, in a JMS provider-specific manner,
139      * according to the instance of the Admin interface currently in use.
140      *
141      * @param name The name of the Topic to be deleted.
142      */

143     public void deleteTopic(String JavaDoc name)
144     {
145         try {
146             admin.deleteTopic(name) ;
147         } catch (Exception JavaDoc e) {
148             throw new NestedRuntimeException("deleteTopic() operation failed", e) ;
149         }
150     }
151
152     /**
153      * Create a JMS ConnectionFactory.
154      *
155      * The ConnectionFactory is created dynamically, in a JMS provider-specific manner,
156      * according to the instance of the Admin interface currently in use.
157      *
158      * @param name The name of the ConnectionFactory to be created.
159      */

160     public void createConnectionFactory(String JavaDoc name)
161     {
162     try {
163         admin.createConnectionFactory(name) ;
164     } catch (Exception JavaDoc e) {
165             throw new NestedRuntimeException("createConnectionFactory() operation failed", e) ;
166     }
167     }
168
169     /**
170      * Delete a JMS ConnectionFactory.
171      *
172      * The ConnectionFactory is deleted dynamically, in a JMS provider-specific manner,
173      * according to the instance of the Admin interface currently in use.
174      *
175      * @param name The name of the ConnectionFactory to be deleted.
176      */

177     public void deleteConnectionFactory(String JavaDoc name)
178     {
179         try {
180             admin.deleteConnectionFactory(name) ;
181         } catch (Exception JavaDoc e) {
182             throw new NestedRuntimeException("deleteConnectionFactory() operation failed", e) ;
183         }
184     }
185
186     protected void dumpJNDIContext(String JavaDoc context)
187     {
188         try
189         {
190         log.info("Dumping JNDI context:" + context) ;
191
192         // dump out the context name-value bindings
193
InitialContext JavaDoc ic = getInitialContext() ;
194         NamingEnumeration JavaDoc list = ic.list(context);
195
196         while (list.hasMore()) {
197             NameClassPair JavaDoc nc = (NameClassPair JavaDoc)list.next();
198             log.info(nc.toString());
199         }
200         log.info("Dumped JNDI context") ;
201         } catch (Exception JavaDoc e) {
202         throw new NestedRuntimeException("error dumping JNDI context", e) ;
203         }
204     }
205
206     /**
207      * Given a resource file name, prepend a directory qualifier to that name,
208      * according to the instance of the JMS provider currently in use.
209      *
210      * The directory name prepended is determined by the value of the property
211      * jms.provider.resources.dir in the provider.properties resources file.
212      *
213      * @param name The name of the resources file.
214      */

215     public static String JavaDoc getJMSResourceRelativePathname(String JavaDoc name)
216     {
217     String JavaDoc directoryName ;
218
219     // not our problem - let someone else handle it
220
if (name == null)
221         return name ;
222
223         try {
224         // get the JMS provider specific directory name
225
Properties JavaDoc props = new Properties JavaDoc() ;
226             ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
227             URL JavaDoc propsURL = loader.getResource(PROP_FILE_NAME);
228             System.err.println("using provider.properties: "+propsURL);
229             props.load(propsURL.openStream()) ;
230             directoryName = props.getProperty(PROP_NAME) ;
231         } catch (Exception JavaDoc e) {
232             throw new NestedRuntimeException("error getting JMS provider directory name", e) ;
233         }
234
235     if (directoryName == null) {
236         throw new NestedRuntimeException("Property " + PROP_NAME + " has not been found in the file "
237                          + PROP_FILE_NAME + ".") ;
238     }
239
240     // return the resource name with directory prepended
241
return (String JavaDoc) (directoryName + "/" + name) ;
242     }
243 }
244
Popular Tags