KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > faults > FaultMsgTest


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 faults;
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.WSIFUtils;
74 import util.TestUtilities;
75
76 /**
77  * Junit test to test out Faults.
78  * Tests faults using the stockquote sample
79  */

80 public class FaultMsgTest extends TestCase {
81     String JavaDoc wsdlLocation =
82         TestUtilities.getWsdlPath("java\\test\\faults") + "Stockquote.wsdl";
83     static String JavaDoc server = TestUtilities.getSoapServer().toUpperCase();
84
85     public FaultMsgTest(String JavaDoc name) {
86         super(name);
87     }
88
89     public static void main(String JavaDoc[] args) {
90        TestUtilities.startListeners(TestUtilities.STOCKQUOTE_LISTENER);
91        junit.textui.TestRunner.run (suite());
92        TestUtilities.stopListeners();
93     }
94
95     public static Test suite() {
96         return new TestSuite(FaultMsgTest.class);
97     }
98
99     public void setUp() {
100         TestUtilities.setUpExtensionsAndProviders();
101     }
102
103     //public void testJava () { doit("JavaPort" ,"java"); }
104
public void testAxis() {
105         doit(server+"Port", "axis");
106     }
107     public void testSoap() {
108         doit(server+"Port", "soap");
109     }
110     public void testSoapJms() {
111         if (TestUtilities.areWeTesting("jms"))
112             doit("SOAPJMSPort", "soap");
113     }
114     public void testAxisJms() {
115         if (TestUtilities.areWeTesting("jms"))
116             doit("SOAPJMSPort", "axis");
117     }
118
119     private void doit(String JavaDoc portName, String JavaDoc protocol) {
120
121         try {
122             invokeMethod(
123                 wsdlLocation,
124                 "getQuote",
125                 null,
126                 null,
127                 portName,
128                 protocol,
129                 new String JavaDoc[] { "" },
130                 0);
131         } catch (Exception JavaDoc e) {
132             System.err.println("AsyncTest(" + portName + ") caught exception " + e);
133             e.printStackTrace();
134             assertTrue(false);
135         } finally {
136         }
137
138     }
139
140     public static void invokeMethod(
141         String JavaDoc wsdlLocation,
142         String JavaDoc operationName,
143         String JavaDoc inputName,
144         String JavaDoc outputName,
145         String JavaDoc portName,
146         String JavaDoc protocol,
147         String JavaDoc[] args,
148         int argShift)
149         throws Exception JavaDoc {
150
151         String JavaDoc serviceNS = null;
152         String JavaDoc serviceName = null;
153         String JavaDoc portTypeNS = null;
154         String JavaDoc portTypeName = null;
155
156         TestUtilities.setProviderForProtocol( protocol );
157
158         try {
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             // 1st a call that should work
170
WSIFOperation op1 = port.createOperation("getQuote", inputName, outputName);
171             WSIFMessage input1 = op1.createInputMessage();
172             WSIFMessage output1 = op1.createOutputMessage();
173             WSIFMessage fault1 = op1.createFaultMessage();
174             input1.setObjectPart("symbol", "");
175             boolean ok = op1.executeRequestResponseOperation(input1, output1, fault1);
176
177             assertEquals("ok getQuote response", true, ok);
178             float q1 = ((Float JavaDoc) output1.getObjectPart("quote")).floatValue();
179             assertEquals("getQuote value", -1.0F, q1, 0F);
180
181             // now a call that is defined in WSDL but not on server
182
op1 = port.createOperation("XXXgetQuote", inputName, outputName);
183             input1 = op1.createInputMessage();
184             output1 = op1.createOutputMessage();
185             fault1 = op1.createFaultMessage();
186             input1.setObjectPart("symbol", "");
187             ok = op1.executeRequestResponseOperation(input1, output1, fault1);
188
189             assertEquals("ok getQuote response", false, ok);
190             String JavaDoc name = fault1.getName();
191             Object JavaDoc fobject = fault1.getObjectPart(WSIFConstants.SOAP_FAULT_OBJECT);
192
193             assertEquals("fault message name", WSIFConstants.SOAP_FAULT_MSG_NAME, name);
194             if ("axis".equals(protocol)) {
195                 assertTrue("fault obj type", fobject instanceof org.apache.axis.AxisFault);
196                 //System.out.println( "AxisFault=" + fobject );
197
} else {
198                 assertTrue("fault obj type", fobject instanceof org.apache.soap.Fault);
199                 //System.out.println( "SOAPFault=" + fobject );
200
}
201
202             // now a call where the remote service returns a fault;
203
// doesn't seem any diff?
204

205         } catch (Exception JavaDoc ex) {
206             ex.printStackTrace();
207             assertTrue(false);
208         } finally {
209             TestUtilities.resetDefaultProviders();
210         }
211
212     }
213 }
214
Popular Tags