KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soap > InputPartsTest


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 import junit.framework.Test;
61 import junit.framework.TestCase;
62 import junit.framework.TestSuite;
63
64 import org.apache.wsif.WSIFException;
65 import org.apache.wsif.WSIFMessage;
66 import org.apache.wsif.WSIFOperation;
67 import org.apache.wsif.WSIFPort;
68 import org.apache.wsif.WSIFService;
69 import org.apache.wsif.WSIFServiceFactory;
70 import org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis;
71 import org.apache.wsif.util.WSIFPluggableProviders;
72 import util.TestUtilities;
73
74 import addressbook.wsiftypes.Address;
75 import addressbook.wsiftypes.Phone;
76
77 /**
78  * Junit test to test out input message parts using the AddressBook sample.
79  *
80  * The WSDL input message can define multiple parts, but the parts in the
81  * WSIFMessage passed to the WSIFOperation may not match what the WSDL defines.
82  * Its been decided the parts defined in the WSDL but not in the WSIFMessage
83  * should be sent as null, and parts in the WSIFMessage but not in the WSDL
84  * should be ignored.
85  *
86  * @author Ant Elder <ant.elder@uk.ibm.com>
87  */

88 public class InputPartsTest extends TestCase {
89     static final String JavaDoc WSDL_LOCATION =
90         TestUtilities.getWsdlPath("java\\test\\addressbook\\wsifservice") +
91         "AddressBook.wsdl";
92     static String JavaDoc server = TestUtilities.getSoapServer().toUpperCase();
93
94     public InputPartsTest(String JavaDoc name) {
95         super(name);
96     }
97
98     public static void main(String JavaDoc[] args)
99     {
100         TestUtilities.startListeners(
101             TestUtilities.ADDRESSBOOK_LISTENER
102                 | TestUtilities.NATIVEJMS_LISTENER);
103
104         junit.textui.TestRunner.run(suite());
105         TestUtilities.stopListeners();
106     }
107
108     public static Test suite() {
109         return new TestSuite(InputPartsTest.class);
110     }
111
112     public void setUp() {
113         TestUtilities.setUpExtensionsAndProviders();
114     }
115
116     public void testSoap() {
117         doit(server+"Port", "soap");
118     }
119     public void testJava() {
120         doit("JavaPort", "java");
121     }
122     public void testSoapJms() {
123         doit("SOAPJMSPort", "soap");
124     }
125 //TODO the SOAP service doesn't work with AXIS nulls
126
// public void testAxis() {
127
// doit(server+"Port", "axis");
128
// }
129
// public void testAxisJms() {
130
// doit("SOAPJMSPort", "axis");
131
// }
132
public void testNativeJms() {
133         doit("NativeJmsPort", "" );
134     }
135
136     private void doit(String JavaDoc portName, String JavaDoc protocol) {
137         if (portName.toUpperCase().indexOf("JMS") != -1 && !TestUtilities.areWeTesting("jms"))
138             return;
139
140         TestUtilities.setProviderForProtocol( protocol );
141
142         System.out.println(
143            "\n=== " + this.getClass().getName() + " " + portName + " " + protocol );
144
145         try {
146             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
147                 WSIFService service = factory.getService(WSDL_LOCATION,
148                 null, // serviceNS
149
null, // serviceName
150
"http://wsifservice.addressbook/", // portTypeNS
151
"AddressBook"); // portTypeName
152

153             service.mapType(
154                 new javax.xml.namespace.QName JavaDoc(
155                     "http://wsiftypes.addressbook/",
156                     "address"),
157                 Class.forName("addressbook.wsiftypes.Address"));
158
159             service.mapType(
160                 new javax.xml.namespace.QName JavaDoc(
161                     "http://wsiftypes.addressbook/",
162                     "phone"),
163                 Class.forName("addressbook.wsiftypes.Phone"));
164
165             WSIFPort port = service.getPort( portName );
166
167             Address addr1 = new Address (1, "University Drive",
168                                       "West Lafayette", "IN", 47907,
169                                       new Phone (765, "494", "4900"));
170             Address addr2 = new Address (0, "Somewhere Else",
171                                       "No Where", "NO", 71983,
172                                       new Phone (600, "391", "5682"));
173             Address addr3 = new Address( 0,"","","",0, new Phone() );
174
175             addName( port, "ant", addr1 );
176             Address addressFound = getAddress( port, "ant" );
177             assertTrue( "test 1 addresses are not equal!!", addressFound.equals( addr1 ) );
178
179             addName( port, null, addr2 );
180             addressFound = getAddress( port, null );
181             assertTrue( "test 2 addresses are not equal!!", addressFound.equals( addr2 ) );
182
183             addName( port, "petra", null );
184             addressFound = getAddress( port, "petra" );
185 //TODO Address gets NPE on null address compare
186
// assertTrue( "test 3 addresses are not equal!!", addressFound.equals( addr3 ) );
187

188         } catch (Exception JavaDoc e) {
189             e.printStackTrace();
190             assertTrue( "got exception: " + e, false);
191         } finally {
192             if (protocol.equals("axis")) {
193                WSIFPluggableProviders.overrideDefaultProvider(
194                     "http://schemas.xmlsoap.org/wsdl/soap/",
195                     null);
196             }
197         }
198
199     }
200
201     private void addName(WSIFPort port, String JavaDoc name, Address addr)
202        throws WSIFException {
203         
204        WSIFOperation operation =
205           port.createOperation("addEntry", "AddEntryWholeNameRequest", null);
206        WSIFMessage inputMessage = operation.createInputMessage();
207        WSIFMessage outputMessage = operation.createOutputMessage();
208        WSIFMessage faultMessage = operation.createFaultMessage();
209
210        if ( name != null ) {
211           inputMessage.setObjectPart( "name", name );
212        }
213        if ( addr != null ) {
214           inputMessage.setObjectPart( "address", addr );
215        }
216        inputMessage.setObjectPart( "extra", "junk" );
217
218        boolean ok = operation.executeRequestResponseOperation(
219           inputMessage,
220           outputMessage,
221           faultMessage);
222
223        assertTrue( "addEntry operation returned false!!", ok );
224        
225     }
226     
227     private Address getAddress(WSIFPort port, String JavaDoc name)
228        throws WSIFException {
229
230        WSIFOperation operation = port.createOperation("getAddressFromName");
231        WSIFMessage inputMessage = operation.createInputMessage();
232        WSIFMessage outputMessage = operation.createOutputMessage();
233        WSIFMessage faultMessage = operation.createFaultMessage();
234
235        if ( name != null ) {
236           inputMessage.setObjectPart( "name", name );
237        }
238        inputMessage.setObjectPart( "extra", "junk" );
239
240        boolean ok = operation.executeRequestResponseOperation(
241           inputMessage,
242           outputMessage,
243           faultMessage);
244
245        assertTrue( "getAddressFromName operation returned false!!", ok );
246        
247        return (Address) outputMessage.getObjectPart( "address" );
248
249     }
250     
251 }
252
Popular Tags