KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > headers > HeadersTest


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 headers;
59
60 import javax.wsdl.Definition;
61 import javax.wsdl.PortType;
62 import javax.wsdl.Service;
63 import junit.framework.Test;
64 import junit.framework.TestCase;
65 import junit.framework.TestSuite;
66
67 import org.apache.wsif.WSIFConstants;
68 import org.apache.wsif.WSIFMessage;
69 import org.apache.wsif.WSIFOperation;
70 import org.apache.wsif.WSIFPort;
71 import org.apache.wsif.WSIFService;
72 import org.apache.wsif.WSIFServiceFactory;
73 import org.apache.wsif.util.WSIFPluggableProviders;
74 import org.apache.wsif.util.WSIFUtils;
75 import util.TestUtilities;
76
77 /**
78  * Junit test to test out the Asynchronous requests.
79  *
80  * Tests the get/setContext methods using the stockquote sample
81  * the context allows seting HTTP and SOAP headers, HTTP headers
82  * are currently only uid/pswd for basic authentication.
83  * Currently all this does is set them so you can see thme in
84  * the packets using TCPMON.
85  * You should see this at the top of the out going packet:
86  * Authorization: Basic cGV0cmE6d2FzaGVyZQ==
87  */

88 public class HeadersTest extends TestCase {
89
90     String JavaDoc wsdlLocation =
91         TestUtilities.getWsdlPath("java\\test\\stockquote\\wsifservice") + "Stockquote.wsdl";
92     static String JavaDoc server = TestUtilities.getSoapServer().toUpperCase();
93
94     public HeadersTest(String JavaDoc name) {
95         super(name);
96     }
97
98     public static void main(String JavaDoc[] args) {
99         junit.textui.TestRunner.run(suite());
100     }
101
102     public static Test suite() {
103         return new TestSuite(HeadersTest.class);
104     }
105
106     public void setUp() {
107         TestUtilities.setUpExtensionsAndProviders();
108     }
109
110     public void testAxis() {
111         doit(server+"Port", "axis");
112     }
113     public void testSoap() {
114         doit(server+"Port" ,"soap");
115     }
116     //public void testJava () { doit("JavaPort" ,"java"); }
117
//public void testSoapJms () { if (TestUtilities.areWeTesting("jms")) doit("SOAPJMSPort","soap"); }
118
//public void testAxisJms () { if (TestUtilities.areWeTesting("jms")) doit("SOAPJMSPort","axis"); }
119

120     private void doit(String JavaDoc portName, String JavaDoc protocol) {
121
122         try {
123             invokeMethod(
124                 wsdlLocation,
125                 "getQuote",
126                 null,
127                 null,
128                 portName,
129                 protocol,
130                 new String JavaDoc[] { "" },
131                 0);
132         } catch (Exception JavaDoc e) {
133             System.err.println("AsyncTest(" + portName + ") caught exception " + e);
134             e.printStackTrace();
135             assertTrue(false);
136         } finally {
137         }
138
139     }
140
141     public static void invokeMethod(
142         String JavaDoc wsdlLocation,
143         String JavaDoc operationName,
144         String JavaDoc inputName,
145         String JavaDoc outputName,
146         String JavaDoc portName,
147         String JavaDoc protocol,
148         String JavaDoc[] args,
149         int argShift)
150         throws Exception JavaDoc {
151
152         String JavaDoc serviceNS = null;
153         String JavaDoc serviceName = null;
154         String JavaDoc portTypeNS = null;
155         String JavaDoc portTypeName = null;
156
157         TestUtilities.setProviderForProtocol( protocol );
158
159         System.out.println("Reading WSDL document from '" + wsdlLocation + "'");
160         Definition def = WSIFUtils.readWSDL(null, wsdlLocation);
161
162         Service service = WSIFUtils.selectService(def, serviceNS, serviceName);
163         PortType portType = WSIFUtils.selectPortType(def, portTypeNS, portTypeName);
164
165         WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
166         WSIFService dpf = factory.getService(def, service, portType);
167         WSIFPort port = (portName == null) ? dpf.getPort() : dpf.getPort(portName);
168
169         System.out.println("HeadersTest executing getQuote for \"\"" + operationName);
170         WSIFOperation operation =
171             port.createOperation("getQuote", inputName, outputName);
172         WSIFMessage input = operation.createInputMessage();
173         WSIFMessage output = operation.createOutputMessage();
174         WSIFMessage fault = operation.createFaultMessage();
175         input.setObjectPart("symbol", "");
176
177         // set a basic authentication header
178
WSIFMessage headers = operation.getContext();
179         headers.setObjectPart(WSIFConstants.CONTEXT_HTTP_USER, "petra");
180         headers.setObjectPart(WSIFConstants.CONTEXT_HTTP_PSWD, "washere");
181         operation.setContext(headers);
182
183         boolean ok = operation.executeRequestResponseOperation(input, output, fault);
184         System.out.println("operation returned " + ok);
185
186         float q = ((Float JavaDoc) output.getObjectPart("quote")).floatValue();
187
188         if (q == -1.0F) {
189             assertTrue(true);
190         } else {
191             assertTrue(false);
192         }
193
194         // do it agian without context so you can see the difference
195
System.out.println(
196             "HeadersTest no headers executing getQuote for \"\"" + operationName);
197         operation = port.createOperation("getQuote", inputName, outputName);
198         input = operation.createInputMessage();
199         output = operation.createOutputMessage();
200         fault = operation.createFaultMessage();
201         input.setObjectPart("symbol", "");
202         ok = operation.executeRequestResponseOperation(input, output, fault);
203         q = ((Float JavaDoc) output.getObjectPart("quote")).floatValue();
204         if (q == -1.0F) {
205             assertTrue(true);
206         } else {
207             assertTrue(false);
208         }
209
210         TestUtilities.resetDefaultProviders();
211
212     }
213
214 }
Popular Tags