KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chartype > CharTest


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 chartype;
59
60 import java.util.Collection JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.Hashtable JavaDoc;
63 import java.util.Iterator JavaDoc;
64
65 import junit.framework.Test;
66 import junit.framework.TestCase;
67 import junit.framework.TestSuite;
68
69 import org.apache.wsif.WSIFMessage;
70 import org.apache.wsif.WSIFOperation;
71 import org.apache.wsif.WSIFPort;
72 import org.apache.wsif.WSIFService;
73 import org.apache.wsif.WSIFServiceFactory;
74 import util.TestUtilities;
75
76 /**
77  * JUnit test to verify that char arguments to Java servies can be sent as parts containing
78  * Strings of length 1 and that chars/Characters returned from a Java service are converted
79  * to Strings before being added to the output message. These conversions allow Java/EJB
80  * services to be more interoperable with SOAP services since XML does not define a char
81  * type.
82  *
83  * @author Owen Burroughs <owenb@apache.org>
84  */

85 public class CharTest extends TestCase {
86
87     private boolean debugMode = true;
88     private String JavaDoc wsdl = TestUtilities.getWsdlPath("java\\test\\chartype") + "CharService.wsdl";;
89
90
91     public static void main(java.lang.String JavaDoc[] args) {
92        junit.textui.TestRunner.run (suite());
93     }
94
95     public static Test suite() {
96         return new TestSuite(CharTest.class);
97     }
98
99     public CharTest(String JavaDoc arg0) {
100         super(arg0);
101     }
102
103     protected void setUp() {
104     }
105
106     public void testJava() throws Exception JavaDoc {
107         WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
108         WSIFService service = factory.getService(wsdl, null, null, null, null);
109
110         WSIFPort port = service.getPort();
111
112         WSIFOperation operation;
113         WSIFMessage inputMessage;
114         WSIFMessage outputMessage;
115         WSIFMessage faultMessage;
116
117         String JavaDoc tempString;
118
119         Object JavaDoc part;
120         boolean operationSucceeded;
121
122         /**********************************************************
123          * Test calling a service that takes a char as an argument
124          **********************************************************/

125         operation = port.createOperation("setCharOperation");
126         inputMessage = operation.createInputMessage();
127         outputMessage = operation.createOutputMessage();
128         faultMessage = operation.createFaultMessage();
129
130         inputMessage.setObjectPart("in", "a");
131
132         operationSucceeded =
133             operation.executeRequestResponseOperation(
134                 inputMessage,
135                 outputMessage,
136                 faultMessage);
137
138         assertTrue("setChar failed!!", operationSucceeded);
139
140         if (operationSucceeded) {
141             assertTrue(
142                 "setChar did not return a boolean",
143                 outputMessage.getObjectPart("out") instanceof Boolean JavaDoc);
144             assertTrue("setChar did not return true", ((Boolean JavaDoc) outputMessage.getObjectPart("out")).booleanValue());
145             debug("setChar returned "+ outputMessage.getObjectPart("out"));
146         } else {
147             debug("operation failed");
148         }
149
150         /**********************************************************
151          * Test calling a service that returns a char
152          **********************************************************/

153         operation = port.createOperation("getCharOperation");
154         inputMessage = operation.createInputMessage();
155         outputMessage = operation.createOutputMessage();
156         faultMessage = operation.createFaultMessage();
157
158         inputMessage.setBooleanPart("in", true);
159
160         operationSucceeded =
161             operation.executeRequestResponseOperation(
162                 inputMessage,
163                 outputMessage,
164                 faultMessage);
165
166         assertTrue("getChar failed!!", operationSucceeded);
167
168         if (operationSucceeded) {
169             assertTrue(
170                 "getChar did not return a String",
171                 outputMessage.getObjectPart("out") instanceof String JavaDoc);
172             debug("getChar returned "+ outputMessage.getObjectPart("out"));
173         } else {
174             debug("operation failed");
175         }
176         
177         /*************************************************************
178          * Test calling a service that takes a char[][] as an argument
179          *************************************************************/

180         operation = port.createOperation("setCharArrayOperation");
181         inputMessage = operation.createInputMessage();
182         outputMessage = operation.createOutputMessage();
183         faultMessage = operation.createFaultMessage();
184
185         String JavaDoc[][] sa = new String JavaDoc[][] {{"a", "b"},{"d", "e", "g"}};
186
187         inputMessage.setObjectPart("in", sa);
188
189         operationSucceeded =
190             operation.executeRequestResponseOperation(
191                 inputMessage,
192                 outputMessage,
193                 faultMessage);
194
195         assertTrue("setCharArray failed!!", operationSucceeded);
196
197         if (operationSucceeded) {
198             assertTrue(
199                 "setCharArray did not return a boolean",
200                 outputMessage.getObjectPart("out") instanceof Boolean JavaDoc);
201             assertTrue("getCharArray did not return true", ((Boolean JavaDoc) outputMessage.getObjectPart("out")).booleanValue());
202             debug("setCharArray returned "+ outputMessage.getObjectPart("out"));
203         } else {
204             debug("operation failed");
205         }
206         
207         /*************************************************************
208          * Test calling a service that returns a char[][]
209          *************************************************************/

210         operation = port.createOperation("getCharArrayOperation");
211         inputMessage = operation.createInputMessage();
212         outputMessage = operation.createOutputMessage();
213         faultMessage = operation.createFaultMessage();
214
215         inputMessage.setBooleanPart("in", true);
216
217         operationSucceeded =
218             operation.executeRequestResponseOperation(
219                 inputMessage,
220                 outputMessage,
221                 faultMessage);
222
223         assertTrue("getCharArray failed!!", operationSucceeded);
224
225         if (operationSucceeded) {
226             assertTrue(
227                 "getCharArray did not return a String array",
228                 outputMessage.getObjectPart("out") instanceof String JavaDoc[][]);
229             String JavaDoc[][] out = (String JavaDoc[][]) outputMessage.getObjectPart("out");
230             debug("getCharArray returned ");
231             for (int i=0; i<out.length; i++) {
232                 for (int j=0; j<out[i].length; j++) {
233                     char c = 'X';
234                     debug(" out["+i+"]["+j+"] = "+out[i][j]);
235                 }
236             }
237         } else {
238             debug("operation failed");
239         }
240     }
241     
242     private void debug(Object JavaDoc s) {
243         if (debugMode)
244             System.out.println(s);
245     }
246 }
Popular Tags