KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soap > OutputPartsTest


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 soap;
59
60
61 import java.util.Iterator JavaDoc;
62
63 import javax.jms.JMSException JavaDoc;
64 import javax.jms.Message JavaDoc;
65 import javax.jms.Queue JavaDoc;
66 import javax.jms.QueueConnection JavaDoc;
67 import javax.jms.QueueConnectionFactory JavaDoc;
68 import javax.jms.QueueReceiver JavaDoc;
69 import javax.jms.QueueSession JavaDoc;
70 import javax.jms.Session JavaDoc;
71 import javax.jms.TextMessage JavaDoc;
72 import junit.framework.Test;
73 import junit.framework.TestCase;
74 import junit.framework.TestSuite;
75
76 import org.apache.wsif.WSIFConstants;
77 import org.apache.wsif.WSIFCorrelationId;
78 import org.apache.wsif.WSIFCorrelationService;
79 import org.apache.wsif.WSIFException;
80 import org.apache.wsif.WSIFMessage;
81 import org.apache.wsif.WSIFOperation;
82 import org.apache.wsif.WSIFPort;
83 import org.apache.wsif.WSIFService;
84 import org.apache.wsif.WSIFServiceFactory;
85 import org.apache.wsif.base.WSIFServiceImpl;
86 import org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis;
87 import org.apache.wsif.util.WSIFCorrelationServiceLocator;
88 import org.apache.wsif.util.WSIFPluggableProviders;
89 import org.apache.wsif.util.WSIFProperties;
90 import org.apache.wsif.util.jms.WSIFJMSFinder;
91 import org.apache.wsif.util.jms.WSIFJMSFinderForJndi;
92 import util.TestUtilities;
93
94 import async.AsyncResponseHandler;
95
96 /**
97  * Junit test to test out output message parts.
98  *
99  * The WSDL output message can define multiple parts, but the response
100  * recieved from the remote service may not match what is defined in the WSDL.
101  * Its been decided that parts defined in the WSDL but not received should be
102  * set in the output WSIFMessage with a null value, and that parts in the
103  * response but not in the WSDL should be added to the ouput WSIFMessage.
104  *
105  * @author Ant Elder <ant.elder@uk.ibm.com>
106  */

107 public class OutputPartsTest extends TestCase {
108
109   static final String JavaDoc WSDL_LOCATION =
110       TestUtilities.getWsdlPath( "java\\test\\soap\\wsifservice" ) +
111       "FakeStockQuote.wsdl";
112
113   static final String JavaDoc FAKE_SOAP_MSG_FILENAME1 =
114       TestUtilities.getWsdlPath( "java\\test\\soap" ) +
115       "FakeSQRespOK.txt";
116   static final String JavaDoc FAKE_SOAP_MSG_FILENAME2 =
117       TestUtilities.getWsdlPath( "java\\test\\soap" ) +
118       "FakeSQRespNull.txt";
119   static final String JavaDoc FAKE_SOAP_MSG_FILENAME3 =
120       TestUtilities.getWsdlPath( "java\\test\\soap" ) +
121       "FakeSQRespMissingReturn.txt";
122   static final String JavaDoc FAKE_SOAP_MSG_FILENAME4 =
123       TestUtilities.getWsdlPath( "java\\test\\soap" ) +
124       "FakeSQRespMissingPart.txt";
125   static final String JavaDoc FAKE_SOAP_MSG_FILENAME5 =
126       TestUtilities.getWsdlPath( "java\\test\\soap" ) +
127       "FakeSQRespMissingAllParts.txt";
128   static final String JavaDoc FAKE_SOAP_MSG_FILENAME6 =
129       TestUtilities.getWsdlPath( "java\\test\\soap" ) +
130       "FakeSQRespRtnTypeErr.txt";
131   static final String JavaDoc FAKE_SOAP_MSG_FILENAME7 =
132       TestUtilities.getWsdlPath( "java\\test\\soap" ) +
133       "FakeSQRespExtraParts.txt";
134       
135   public OutputPartsTest(String JavaDoc name) {
136     super(name);
137   }
138
139   public static void main(String JavaDoc[] args) {
140      TestUtilities.startListeners();
141      junit.textui.TestRunner.run(suite());
142      TestUtilities.stopListeners();
143   }
144
145   public static Test suite() {
146     return new TestSuite(OutputPartsTest.class);
147   }
148
149   public void setUp() {
150     TestUtilities.setUpExtensionsAndProviders();
151   }
152   
153   public void tearDown() {
154      WSIFPluggableProviders.overrideDefaultProvider(
155         "http://schemas.xmlsoap.org/wsdl/soap/",
156         null);
157   }
158
159   public void testSOAP1() {
160      doitStockquote( "SOAPJMSPort", "soap", FAKE_SOAP_MSG_FILENAME1, null );
161   }
162   public void testAxis1() {
163      doitStockquote( "SOAPJMSPort", "axis", FAKE_SOAP_MSG_FILENAME1, null );
164   }
165   public void testSOAP2() {
166      doitStockquote( "SOAPJMSPort", "soap", FAKE_SOAP_MSG_FILENAME2, "return value not found in response message" );
167   }
168   public void testAxis2() {
169      doitStockquote( "SOAPJMSPort", "axis", FAKE_SOAP_MSG_FILENAME2, "<null>" );
170   }
171   public void testSOAP3() {
172      doitStockquote( "SOAPJMSPort", "soap", FAKE_SOAP_MSG_FILENAME3, "java.lang.String" );
173   }
174   public void testAxis3() {
175      doitStockquote( "SOAPJMSPort", "axis", FAKE_SOAP_MSG_FILENAME3, "java.lang.String" );
176   }
177   public void testSOAP4() {
178      doitStockquote( "SOAPJMSPort", "soap", FAKE_SOAP_MSG_FILENAME4, "p1=null" );
179   }
180   public void testAxis4() {
181      doitStockquote( "SOAPJMSPort", "axis", FAKE_SOAP_MSG_FILENAME4, "p1=null" );
182   }
183   public void testSOAP5() {
184      doitStockquote( "SOAPJMSPort", "soap", FAKE_SOAP_MSG_FILENAME5, "parms=null" );
185   }
186   public void testAxis5() {
187      doitStockquote( "SOAPJMSPort", "axis", FAKE_SOAP_MSG_FILENAME5, "parms=null" );
188   }
189   public void testSOAP6() {
190      doitStockquote( "SOAPJMSPort", "soap", FAKE_SOAP_MSG_FILENAME6, "java.lang.String" );
191   }
192
193   // AXIS doesn't work with incorect return type
194
// public void testAxis6() {
195
// doitStockquote( "SOAPJMSPort", "axis", FAKE_SOAP_MSG_FILENAME6, "java.lang.String" );
196
// }
197
public void testSOAP7() {
198      doitStockquote( "SOAPJMSPort", "soap", FAKE_SOAP_MSG_FILENAME7, null );
199   }
200   
201   // AXIS doesn't like unknown parts coming in
202
//public void testAxis7() {
203
// doitStockquote( "SOAPJMSPort", "axis", FAKE_SOAP_MSG_FILENAME7, null );
204
//}
205

206   /**
207    * Query Stockquote sample
208    */

209   public void doitStockquote(String JavaDoc portName, String JavaDoc protocol, String JavaDoc fakeFile, String JavaDoc error) {
210     float value;
211     WSIFPort port;
212     WSIFOperation operation;
213     WSIFMessage input, output, fault, context;
214
215     if (portName.toUpperCase().indexOf("JMS") != -1
216         && !TestUtilities.areWeTesting("jms")) {
217         return;
218     }
219
220     TestUtilities.setProviderForProtocol( protocol );
221
222     System.out.println("\n=== StockQuote");
223     try {
224       WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
225         WSIFService service = factory.getService( WSDL_LOCATION,
226             null, // serviceNS
227
null, // serviceName
228
"http://wsifservice.stockquote/", // portTypeNS
229
"StockquotePT" ); // portTypeName
230

231       port = service.getPort(portName);
232
233       // Executing executeRequestResponseAsync(input)
234
operation = port.createOperation( "getQuote" );
235
236       context = operation.getContext();
237       context.setObjectPart(
238          WSIFConstants.CONTEXT_JMS_PREFIX + "WSIF_FAKE",
239          "\"" + fakeFile + "\"" );
240
241       input = operation.createInputMessage();
242       input.setName("GetQuoteInput");
243       input.setObjectPart("symbol", "" );
244       output = operation.createOutputMessage();
245
246       //output = doAsyncOpNoHandler( operation, input, context );
247
doSyncOp( operation, input, output, context );
248       
249       Object JavaDoc o;
250       
251       o = output.getObjectPart( "quote" );
252       if ( o != null ) {
253          value = ((Float JavaDoc)o).floatValue();
254          assertTrue( "doAsyncOpNoHandler stockquote value incorrect!",
255             value == -1.0F );
256       } else if (error.equals("<null>")) {
257             assertNull("return value not null!!", o);
258       } else {
259          assertTrue( "return value null!!",
260             error.equals( "return=null" )
261             || error.equals( "all=null" ) );
262       }
263         
264
265       System.out.println( "out message contains:" );
266       String JavaDoc partName;
267       for (Iterator JavaDoc i = output.getPartNames(); i.hasNext(); ) {
268          partName = (String JavaDoc) i.next();
269          o = output.getObjectPart( partName );
270          System.out.println( "part=" + partName + " value=" + o );
271       }
272       
273       o = output.getObjectPart( "p1" );
274       if ( o != null ) {
275          assertTrue( "error on part p1!!", o instanceof String JavaDoc );
276       } else if (error.equals("<null>")) {
277             assertNull("return value not null!!", o);
278       } else {
279         assertTrue( "p1 is null!!",
280            error.equals( "p1=null" )
281            || error.equals( "parms=null" )
282            || error.equals( "all=null" ) );
283       }
284       o = output.getObjectPart( "p2" );
285       if ( o != null ) {
286          assertTrue( "error on part p2!!", o instanceof Float JavaDoc );
287       } else if (error.equals("<null>")) {
288             assertNull("return value not null!!", o);
289       } else {
290         assertTrue( "p2 is null!!",
291            error.equals( "p2=null" )
292            || error.equals( "parms=null" )
293            || error.equals( "all=null" ) );
294       }
295       o = output.getObjectPart( "p3" );
296       if ( o != null ) {
297          assertTrue( "error on part p3!!", o instanceof String JavaDoc );
298       } else if (error.equals("<null>")) {
299             assertNull("return value not null!!", o);
300       } else {
301         assertTrue( "p3 is null!!",
302            error.equals( "p3=null" )
303            || error.equals( "parms=null" )
304            || error.equals( "all=null" ) );
305       }
306
307     } catch (Exception JavaDoc ex) {
308         if ( error == null || !error.equals(ex.getMessage()) ) {
309            ex.printStackTrace();
310            assertTrue("exception during stockquote test: " + ex.getMessage(), false);
311         }
312     }
313   }
314
315   private void doSyncOp(WSIFOperation op,
316                          WSIFMessage input,
317                          WSIFMessage output,
318                          WSIFMessage context) throws WSIFException {
319       //try {
320
op.setContext( context );
321          WSIFMessage fault = op.createFaultMessage();
322          boolean ok = op.executeRequestResponseOperation(input, output, fault );
323          assertTrue( "executeRequestResponseOperation returned false!", ok );
324       //} catch (Exception ex) {
325
// ex.printStackTrace();
326
// assertTrue( "exception executing request: " + ex.getMessage(), false );
327
//}
328
}
329
330   private WSIFMessage doAsyncOp(WSIFOperation op, WSIFMessage input, WSIFMessage context) throws WSIFException{
331       AsyncResponseHandler handler = new AsyncResponseHandler(1); // 2 async calls
332
context.setObjectPart( WSIFConstants.CONTEXT_JMS_PREFIX + "JMSReplyTo",
333                              TestUtilities.getWsifProperty("wsif.async.replytoq") );
334       op.setContext( context );
335       WSIFCorrelationId id = op.executeRequestResponseAsync(input, handler);
336
337       int i = 5; // 15 seconds timout
338
while ( i-- > 0 && !handler.isDone() ) {
339          System.out.println( "async requests sent, waiting for responses - " + i );
340          try {
341             Thread.sleep(3000);
342          } catch (InterruptedException JavaDoc ex) {}
343       }
344       assertTrue( "no response to async operation!", i > 0 ); // no responses in time
345
return handler.getOutputs()[0];
346   }
347
348   private WSIFMessage doAsyncOpNoHandler(WSIFOperation op, WSIFMessage input, WSIFMessage context)
349                                                               throws WSIFException, JMSException JavaDoc {
350       WSIFMessage output = null;
351
352          context.setObjectPart( WSIFConstants.CONTEXT_JMS_PREFIX + "JMSReplyTo",
353                                 TestUtilities.getWsifProperty("wsif.async.replytoq2") );
354          context.setObjectPart( "testJMSnoHandler", "true" );
355          op.setContext( context );
356          WSIFCorrelationId id = op.executeRequestResponseAsync( input );
357          System.out.println( "async operation done, correlation id=" + id.getCorrelationId() );
358          op = null;
359          
360         Object JavaDoc jmsResponse =
361             TestUtilities.getJMSAsyncResponse(
362                 id.getCorrelationId(),
363                 TestUtilities.getWsifProperty("wsif.async.replytoq2"));
364          
365          WSIFCorrelationService cs =
366             WSIFCorrelationServiceLocator.getCorrelationService();
367          Object JavaDoc o;
368          synchronized( cs ) {
369             o = cs.get( id );
370          }
371          if ( o != null && o instanceof WSIFOperation ) {
372             cs.remove( id );
373             op = (WSIFOperation)o;
374          } else {
375             assertTrue( "stored correlation object not as expected: " + o, false );
376          }
377          output = op.createOutputMessage();
378          WSIFMessage fault = op.createFaultMessage();
379          op.processAsyncResponse( jmsResponse, output, fault );
380
381       return output;
382   }
383 }
Popular Tags