KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > util > TestUtilities


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package util;
59
60 /**
61  * A mish-mash of various utilities useful in our JUnit tests
62  * @author Mark Whitlock
63  */

64
65 import java.io.InputStream JavaDoc;
66 import java.util.Properties JavaDoc;
67 import java.util.StringTokenizer JavaDoc;
68
69 import javax.jms.JMSException JavaDoc;
70 import javax.jms.Message JavaDoc;
71 import javax.jms.Queue JavaDoc;
72 import javax.jms.QueueConnection JavaDoc;
73 import javax.jms.QueueConnectionFactory JavaDoc;
74 import javax.jms.QueueReceiver JavaDoc;
75 import javax.jms.QueueSession JavaDoc;
76 import javax.jms.Session JavaDoc;
77
78 import org.apache.wsif.WSIFException;
79 import org.apache.wsif.base.WSIFDefaultCorrelationService;
80 import org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis;
81 import org.apache.wsif.providers.soap.apachesoap.WSIFDynamicProvider_ApacheSOAP;
82 import org.apache.wsif.spi.WSIFProvider;
83 import org.apache.wsif.util.WSIFCorrelationServiceLocator;
84 import org.apache.wsif.util.WSIFPluggableProviders;
85 import org.apache.wsif.util.WSIFProperties;
86 import org.apache.wsif.util.jms.JMSAsyncListener;
87 import org.apache.wsif.util.jms.NativeJMSRequestListener;
88 import org.apache.wsif.util.jms.WSIFJMSFinder;
89 import org.apache.wsif.util.jms.WSIFJMSFinderForJndi;
90
91 public class TestUtilities {
92     private static final String JavaDoc WSIF_TEST_PROPERTIES = "wsif.test.properties";
93     private static final String JavaDoc WSIF_PATH = "wsif.path";
94     private static final String JavaDoc WSIF_TEST_COMPONENTS = "wsif.test.components";
95     private static final String JavaDoc WSIF_SOAP_SERVER = "wsif.soapserver";
96
97     private static final String JavaDoc SOAP = "soap";
98     private static final String JavaDoc AXIS = "axis";
99
100     private static BridgeThread jmsAb = null;
101     private static BridgeThread jmsSq = null;
102     private static JMSAsyncListener asyncListener = null;
103     private static NativeJMSRequestListener nativeReqListener = null;
104
105     public static final int ADDRESSBOOK_LISTENER = 1;
106     public static final int STOCKQUOTE_LISTENER = 2;
107     public static final int ASYNC_LISTENER = 4;
108     public static final int NATIVEJMS_LISTENER = 8;
109     public static final int ALL_LISTENERS = 15;
110
111     public static final Class JavaDoc DEFAULT_SOAP_PROVIDER_CLASS =
112        WSIFDynamicProvider_ApacheAxis.class;
113     private static final String JavaDoc DEFAULT_SOAP_PROTOCOL =
114        "axis";
115     public static final Class JavaDoc NON_DEFAULT_SOAP_PROVIDER_CLASS =
116        WSIFDynamicProvider_ApacheSOAP.class;
117     private static final String JavaDoc NON_DEFAULT_SOAP_PROTOCOL =
118        "soap";
119 /*
120     public static final Class DEFAULT_SOAP_PROVIDER_CLASS =
121        WSIFDynamicProvider_ApacheSOAP.class;
122     private static final String DEFAULT_SOAP_PROTOCOL =
123        "soap";
124     public static final Class NON_DEFAULT_SOAP_PROVIDER_CLASS =
125        WSIFDynamicProvider_ApacheAxis.class;
126     private static final String NON_DEFAULT_SOAP_PROTOCOL =
127        "axis";
128 */

129     public static String JavaDoc getWsdlPath(String JavaDoc relativePath) {
130         String JavaDoc wsdlPath = getWsifProperty(WSIF_PATH);
131         if (wsdlPath == null)
132             wsdlPath = new String JavaDoc("");
133
134         String JavaDoc slash = System.getProperty("file.separator");
135
136         if (!wsdlPath.equals("") && !wsdlPath.endsWith(slash))
137             wsdlPath += slash;
138
139         if (relativePath != null && relativePath.length() != 0) {
140             StringBuffer JavaDoc relativeBuff = new StringBuffer JavaDoc(relativePath);
141             for (int i = 0; i < relativeBuff.length(); i++) {
142                 if (relativeBuff.charAt(i) == '\\' || relativeBuff.charAt(i) == '/')
143                     relativeBuff.setCharAt(i, slash.charAt(0));
144             }
145
146             wsdlPath += relativeBuff;
147
148             if (!wsdlPath.endsWith(slash))
149                 wsdlPath += slash;
150         }
151         return wsdlPath;
152     }
153
154     /**
155      * Gets a property from the wsif.test.properties file
156      */

157     public static String JavaDoc getWsifProperty(String JavaDoc property) {
158         try {
159             Properties JavaDoc prop = new Properties JavaDoc();
160             InputStream JavaDoc in = ClassLoader.getSystemResourceAsStream(WSIF_TEST_PROPERTIES);
161             prop.load(in);
162             return prop.getProperty(property);
163         } catch (Exception JavaDoc ignored) {
164             return null;
165         }
166     }
167
168     /**
169      * Finds out from the wsif.test.properties file whether we are testing
170      * a particular component (EJBs, JMS, etc) of WSIF. If not mentioned
171      * in wsif.test.properties, the default is to test everything.
172      */

173     public static boolean areWeTesting(String JavaDoc component) {
174         String JavaDoc prop = getWsifProperty(WSIF_TEST_COMPONENTS);
175         if (prop == null)
176             return true; // test everything by default
177

178         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(prop, ",");
179         String JavaDoc token;
180         int eqPos;
181         while (tokenizer.hasMoreTokens()) {
182             token = tokenizer.nextToken();
183             eqPos = token.indexOf('=');
184             if (eqPos == -1)
185                 continue;
186             if (token.substring(0, eqPos).equals(component)
187                 && token.substring(eqPos + 1).equals("off"))
188                 return false;
189         }
190         return true;
191     }
192
193     /**
194      * Queries the soap server being used. Can be either Apache Soap or Apache Axis.
195      * Default is soap.
196      */

197     public static String JavaDoc getSoapServer() {
198         String JavaDoc prop = getWsifProperty(WSIF_SOAP_SERVER);
199         if (prop == null)
200             return SOAP;
201         if (SOAP.equals(prop) || AXIS.equals(prop))
202             return prop;
203         return SOAP;
204     }
205
206     public static boolean isJmsVerbose() {
207         String JavaDoc strVerbose = TestUtilities.getWsifProperty("wsif.jms.output");
208         if ("verbose".equals(strVerbose))
209             return true;
210         return false;
211     }
212
213     public static void setProviderForProtocol(String JavaDoc protocol) {
214        if ( protocol.equals( NON_DEFAULT_SOAP_PROTOCOL ) ) {
215           try {
216              WSIFPluggableProviders.overrideDefaultProvider(
217                 "http://schemas.xmlsoap.org/wsdl/soap/",
218                 (WSIFProvider) NON_DEFAULT_SOAP_PROVIDER_CLASS.newInstance() );
219           } catch (Exception JavaDoc ex) {
220              ex.printStackTrace();
221           }
222        } else {
223           resetDefaultProviders();
224        }
225     }
226
227     public static void resetDefaultProviders() {
228        WSIFPluggableProviders.setAutoLoadProviders( false );
229        WSIFPluggableProviders.setAutoLoadProviders( true );
230     }
231
232     public static void setUpExtensionsAndProviders() {
233         //WSIFServiceImpl.addExtensionRegistry(new JavaExtensionRegistry()) ;
234
//WSIFServiceImpl.addExtensionRegistry(new FormatExtensionRegistry()) ;
235
//WSIFServiceImpl.addExtensionRegistry(new JmsExtensionRegistry()) ;
236
//WSIFServiceImpl.addExtensionRegistry(new EJBExtensionRegistry());
237
resetDefaultProviders();
238     }
239     /**
240      * This starts what listeners are required to run the testcases.
241      * What listeners are needed depends on settings in the
242      * wsif.test.properties file.
243      * Possible listeners are:
244      * JMS2HTTPBridge - for SOAP/JMS tests
245      * NativeJMSRequestListener - for the native JMS provider
246      * JMSAsynListener - for asynchronous operation tests
247      */

248     public static void startListeners() {
249         startListeners(ALL_LISTENERS);
250     }
251
252     public static void startListeners(int which) {
253         if (TestUtilities.areWeTesting("jms")) {
254             if ((which & ADDRESSBOOK_LISTENER) > 0) {
255                 jmsAb = new BridgeThread("AddressBook");
256                 jmsAb.start();
257             }
258
259             if ((which & STOCKQUOTE_LISTENER) > 0) {
260                 jmsSq = new BridgeThread("Stockquote");
261                 jmsSq.start();
262             }
263
264             if ((which & NATIVEJMS_LISTENER) > 0)
265                 try {
266                     nativeReqListener =
267                         new NativeJMSRequestListener(
268                             TestUtilities.getWsifProperty(
269                                 "wsif.nativejms.requestq"));
270                 } catch (WSIFException ex) {
271                     ex.printStackTrace();
272                 }
273         }
274
275         if ((which & ASYNC_LISTENER) > 0
276             && TestUtilities.areWeTesting("async"))
277             try {
278                 asyncListener =
279                     new JMSAsyncListener(
280                         TestUtilities.getWsifProperty("wsif.async.replytoq"));
281             } catch (Exception JavaDoc ex) {
282                 ex.printStackTrace();
283             }
284     }
285
286     /**
287      * This starts whatever listeners have been started.
288      * Possible listeners are:
289      * JMS2HTTPBridge - for SOAP/JMS tests
290      * NativeJMSRequestListener - for the native JMS provider
291      * JMSAsynListener - for asynchronous operation tests
292      */

293     public static void stopListeners()
294     {
295        if ( jmsAb != null ) jmsAb.interrupt();
296        if ( jmsSq != null ) jmsSq.interrupt();
297        if ( asyncListener != null ) asyncListener.stop();
298        if ( nativeReqListener != null ) nativeReqListener.stop();
299        ((WSIFDefaultCorrelationService)WSIFCorrelationServiceLocator.
300           getCorrelationService()).shutdown();
301     }
302
303     public static Message JavaDoc getJMSAsyncResponse(String JavaDoc id, String JavaDoc queueName)
304         throws WSIFException, JMSException JavaDoc {
305
306         WSIFJMSFinder finder =
307             new WSIFJMSFinderForJndi(
308                 null,
309                 TestUtilities.getWsifProperty(
310                         "wsif.jms2httpbridge.initialcontextfactory"),
311                 TestUtilities.getWsifProperty(
312                         "wsif.jms2httpbridge.jndiproviderurl"),
313                 WSIFJMSFinder.STYLE_QUEUE,
314                 TestUtilities.getWsifProperty(
315                         "wsif.jms2httpbridge.jndiconnectionfactoryname"),
316                 null,
317                 null);
318
319         QueueConnectionFactory JavaDoc factory = finder.getFactory();
320         QueueConnection JavaDoc connection = factory.createQueueConnection();
321         connection.start();
322         QueueSession JavaDoc session =
323             connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
324         Queue JavaDoc readQ = session.createQueue(queueName);
325         QueueReceiver JavaDoc receiver =
326             session.createReceiver(readQ, "JMSCorrelationID='" + id + "'");
327         return receiver.receive(WSIFProperties.getAsyncTimeout());
328     }
329 }
330
Popular Tags