001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package docStyle;
059:
060: import java.io.StringReader;
061: import java.io.StringWriter;
062:
063: import junit.framework.Test;
064: import junit.framework.TestCase;
065: import junit.framework.TestSuite;
066:
067: import org.apache.wsif.WSIFMessage;
068: import org.apache.wsif.WSIFOperation;
069: import org.apache.wsif.WSIFPort;
070: import org.apache.wsif.WSIFService;
071: import org.apache.wsif.WSIFServiceFactory;
072: import org.apache.xerces.parsers.DOMParser;
073: import org.apache.xml.serialize.OutputFormat;
074: import org.apache.xml.serialize.XMLSerializer;
075: import org.w3c.dom.Element;
076: import org.w3c.dom.Node;
077: import org.w3c.dom.NodeList;
078: import org.xml.sax.InputSource;
079:
080: import docStyle.wsifservice.ZipCodeResolverSoap;
081: import docStyle.zipCodeNW.ShortZipCode;
082: import docStyle.zipCodeNW.ShortZipCodeResponse;
083:
084: import util.TestUtilities;
085:
086: /**
087: * Junit test to test out the AXIS provider docstyle support
088: */
089: public class ZipCodeAxisTest extends TestCase {
090: String wsdlLocation = TestUtilities
091: .getWsdlPath("java\\test\\docStyle\\wsifservice")
092: + "zipCodeResolver.wsdl";
093:
094: // + "zipCode.wsdl";
095: // + "zipCodeLocal.wsdl";
096: // "http://webservices.eraserver.net/zipcoderesolver/zipcoderesolver.asmx?WSDL";
097:
098: public ZipCodeAxisTest(String name) {
099: super (name);
100: }
101:
102: public static void main(String[] args) {
103: TestUtilities.startListeners(TestUtilities.ADDRESSBOOK_LISTENER
104: | TestUtilities.ASYNC_LISTENER
105: | TestUtilities.NATIVEJMS_LISTENER);
106: junit.textui.TestRunner.run(suite());
107: TestUtilities.stopListeners();
108: }
109:
110: public static Test suite() {
111: return new TestSuite(ZipCodeAxisTest.class);
112: }
113:
114: public void setUp() {
115: TestUtilities.setUpExtensionsAndProviders();
116: }
117:
118: public void testDynamicAxis() {
119: doitDyn("ZipCodeResolverSoap", "axis");
120: }
121:
122: public void testDynamicAxisWrapped() {
123: doitDynWrapped("ZipCodeResolverSoap", "axis");
124: }
125:
126: public void testStubsAxis() {
127: doitStub("ZipCodeResolverSoap", "axis");
128: }
129:
130: public void testStubsAxisWrapped() {
131: doitStubWrapped("ZipCodeResolverSoap", "axis");
132: }
133:
134: public void testMessagingAxis() {
135: doitMessaging("ZipCodeResolverSoap", "axis");
136: }
137:
138: public void testDyn() {
139: doitStub("ZipCodeResolverSoapJMS", "axis");
140: }
141:
142: private void doitDyn(String portName, String protocol) {
143: if (portName.toUpperCase().indexOf("JMS") != -1
144: && !TestUtilities.areWeTesting("jms"))
145: return;
146:
147: TestUtilities.setProviderForProtocol(protocol);
148:
149: try {
150: WSIFServiceFactory factory = WSIFServiceFactory
151: .newInstance();
152: WSIFService service = factory.getService(wsdlLocation,
153: null, null, "http://webservices.eraserver.net/",
154: "ZipCodeResolverSoap");
155:
156: WSIFPort port = service.getPort(portName);
157:
158: WSIFOperation operation = port
159: .createOperation("ShortZipCode");
160:
161: WSIFMessage inMsg = operation.createInputMessage();
162: WSIFMessage outMsg = operation.createOutputMessage();
163: WSIFMessage faultMsg = operation.createFaultMessage();
164: inMsg.setObjectPart("accessCode", "9999");
165: inMsg.setObjectPart("address", "607 Trinity");
166: inMsg.setObjectPart("city", "Austin");
167: inMsg.setObjectPart("state", "TX");
168:
169: boolean ok = operation.executeRequestResponseOperation(
170: inMsg, outMsg, faultMsg);
171:
172: assertTrue("operation returned false!!", ok);
173: String s = (String) outMsg
174: .getObjectPart("ShortZipCodeResult");
175: assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s));
176:
177: } catch (Exception ex) {
178: ex.printStackTrace();
179: assertTrue("AddressBookTest(" + portName
180: + ") caught exception " + ex.getLocalizedMessage(),
181: false);
182: }
183: }
184:
185: private void doitDynWrapped(String portName, String protocol) {
186: if (portName.toUpperCase().indexOf("JMS") != -1
187: && !TestUtilities.areWeTesting("jms"))
188: return;
189:
190: TestUtilities.setProviderForProtocol(protocol);
191:
192: try {
193: WSIFServiceFactory factory = WSIFServiceFactory
194: .newInstance();
195: WSIFService service = factory.getService(wsdlLocation,
196: null, null, "http://webservices.eraserver.net/",
197: "ZipCodeResolverSoap");
198:
199: service.mapType(
200: new javax.xml.namespace.QName(
201: "http://webservices.eraserver.net/",
202: "ShortZipCode"), ShortZipCode.class);
203: service
204: .mapType(new javax.xml.namespace.QName(
205: "http://webservices.eraserver.net/",
206: "ShortZipCodeResponse"),
207: ShortZipCodeResponse.class);
208:
209: WSIFPort port = service.getPort(portName);
210:
211: WSIFOperation operation = port
212: .createOperation("ShortZipCode");
213:
214: WSIFMessage inMsg = operation.createInputMessage();
215: WSIFMessage outMsg = operation.createOutputMessage();
216: WSIFMessage faultMsg = operation.createFaultMessage();
217:
218: ShortZipCode zc = new ShortZipCode();
219: zc.setAccessCode("9999");
220: zc.setAddress("607 Trinity");
221: zc.setCity("Austin");
222: zc.setState("TX");
223:
224: inMsg.setObjectPart("parameters", zc);
225:
226: boolean ok = operation.executeRequestResponseOperation(
227: inMsg, outMsg, faultMsg);
228:
229: assertTrue("operation returned false!!", ok);
230:
231: ShortZipCodeResponse zcResp = (ShortZipCodeResponse) outMsg
232: .getObjectPart("parameters");
233:
234: String s = zcResp.getShortZipCodeResult();
235: assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s));
236:
237: } catch (Exception ex) {
238: ex.printStackTrace();
239: assertTrue("AddressBookTest(" + portName
240: + ") caught exception " + ex.getLocalizedMessage(),
241: false);
242: }
243: }
244:
245: private void doitStub(String portName, String protocol) {
246: if (portName.toUpperCase().indexOf("JMS") != -1
247: && !TestUtilities.areWeTesting("jms"))
248: return;
249:
250: TestUtilities.setProviderForProtocol(protocol);
251:
252: try {
253: WSIFServiceFactory factory = WSIFServiceFactory
254: .newInstance();
255: WSIFService service = factory.getService(wsdlLocation,
256: null, null, "http://webservices.eraserver.net/",
257: "ZipCodeResolverSoap");
258:
259: ZipCodeResolverSoap stub = (ZipCodeResolverSoap) service
260: .getStub(portName, ZipCodeResolverSoap.class);
261:
262: String zipcode = stub.ShortZipCode("9999", "607 Trinity",
263: "Austin", "TX");
264: assertTrue("wrong zipcode: " + zipcode + "!!", "78701"
265: .equals(zipcode));
266:
267: } catch (Exception ex) {
268: ex.printStackTrace();
269: assertTrue("AddressBookTest(" + portName
270: + ") caught exception " + ex.getLocalizedMessage(),
271: false);
272: }
273: }
274:
275: private void doitStubWrapped(String portName, String protocol) {
276: if (portName.toUpperCase().indexOf("JMS") != -1
277: && !TestUtilities.areWeTesting("jms"))
278: return;
279:
280: TestUtilities.setProviderForProtocol(protocol);
281:
282: try {
283: WSIFServiceFactory factory = WSIFServiceFactory
284: .newInstance();
285: WSIFService service = factory.getService(wsdlLocation,
286: null, null, "http://webservices.eraserver.net/",
287: "ZipCodeResolverSoap");
288:
289: //TODO: its a bug that these mapTypes are needed with wrapped operation stubs
290: service.mapType(
291: new javax.xml.namespace.QName(
292: "http://webservices.eraserver.net/",
293: "ShortZipCode"), ShortZipCode.class);
294: service
295: .mapType(new javax.xml.namespace.QName(
296: "http://webservices.eraserver.net/",
297: "ShortZipCodeResponse"),
298: ShortZipCodeResponse.class);
299:
300: ShortZipCode zc = new ShortZipCode();
301: zc.setAccessCode("9999");
302: zc.setAddress("607 Trinity");
303: zc.setCity("Austin");
304: zc.setState("TX");
305:
306: docStyle.zipCodeNW.ZipCodeResolverSoap stub = (docStyle.zipCodeNW.ZipCodeResolverSoap) service
307: .getStub(
308: portName,
309: docStyle.zipCodeNW.ZipCodeResolverSoap.class);
310:
311: ShortZipCodeResponse zcResp = stub.shortZipCode(zc);
312:
313: String s = zcResp.getShortZipCodeResult();
314: assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s));
315:
316: } catch (Exception ex) {
317: ex.printStackTrace();
318: assertTrue("AddressBookTest(" + portName
319: + ") caught exception " + ex.getLocalizedMessage(),
320: false);
321: }
322: }
323:
324: private void doitMessaging(String portName, String protocol) {
325: if (portName.toUpperCase().indexOf("JMS") != -1
326: && !TestUtilities.areWeTesting("jms"))
327: return;
328:
329: TestUtilities.setProviderForProtocol(protocol);
330:
331: try {
332: WSIFServiceFactory factory = WSIFServiceFactory
333: .newInstance();
334: WSIFService service = factory.getService(wsdlLocation,
335: null, null, "http://webservices.eraserver.net/",
336: "ZipCodeResolverSoap");
337:
338: WSIFPort port = service.getPort(portName);
339: WSIFOperation operation = port
340: .createOperation("ShortZipCode");
341: WSIFMessage inMsg = operation.createInputMessage();
342: WSIFMessage outMsg = operation.createOutputMessage();
343: WSIFMessage faultMsg = operation.createFaultMessage();
344:
345: String inputDocument = "<ShortZipCode xmlns=\"http://webservices.eraserver.net/\">"
346: + "<accessCode>9999</accessCode>"
347: + "<address>607 Trinity</address>"
348: + "<city>Austin</city>"
349: + "<state>TX</state>"
350: + "</ShortZipCode>";
351:
352: DOMParser parser = new DOMParser();
353: String xmlString = "<?xml version=\"1.0\"?>\n"
354: + inputDocument;
355: parser.parse(new InputSource(new StringReader(xmlString)));
356: Element element = parser.getDocument().getDocumentElement();
357: //printElement(element);
358:
359: inMsg.setObjectPart("parameters", element);
360:
361: boolean ok = operation.executeRequestResponseOperation(
362: inMsg, outMsg, faultMsg);
363: assertTrue("operation returned false!!", ok);
364:
365: Element responseElement = (Element) outMsg
366: .getObjectPart("parameters");
367: assertTrue("return element is null!!",
368: responseElement != null);
369: // printElement(responseElement);
370:
371: NodeList nl = responseElement.getChildNodes();
372: Node n = nl.item(0); // "ShortZipCodeResponse"
373: nl = n.getChildNodes();
374: n = nl.item(0); // "ShortZipCodeResult"
375: String s = n.getNodeValue();
376: if (!"78701".equals(s)) {
377: printElement(responseElement);
378: }
379: assertTrue("wrong zipcode: " + s + "!!", "78701".equals(s));
380:
381: } catch (Exception ex) {
382: ex.printStackTrace();
383: assertTrue("AddressBookTest(" + portName
384: + ") caught exception " + ex.getLocalizedMessage(),
385: false);
386: }
387: }
388:
389: private void printElement(Element e) throws Exception {
390: OutputFormat of = new OutputFormat(e.getOwnerDocument(),
391: "UTF-8", true);
392: XMLSerializer xmls = new XMLSerializer(of);
393: StringWriter sw = new StringWriter();
394: xmls.setOutputCharStream(sw);
395: xmls.serialize(e);
396: System.err.println("element=" + sw.toString());
397: }
398: }
|