KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > wsdl > toJava > JavaTestCaseWriter


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.wsdl.toJava;
17
18 import org.apache.axis.utils.JavaUtils;
19 import org.apache.axis.utils.Messages;
20 import org.apache.axis.wsdl.symbolTable.BindingEntry;
21 import org.apache.axis.wsdl.symbolTable.Parameter;
22 import org.apache.axis.wsdl.symbolTable.Parameters;
23 import org.apache.axis.wsdl.symbolTable.ServiceEntry;
24 import org.apache.axis.wsdl.symbolTable.SymbolTable;
25 import org.apache.axis.wsdl.symbolTable.TypeEntry;
26
27 import javax.wsdl.Binding;
28 import javax.wsdl.Fault;
29 import javax.wsdl.Operation;
30 import javax.wsdl.OperationType;
31 import javax.wsdl.Port;
32 import javax.wsdl.PortType;
33 import javax.xml.rpc.holders.BooleanHolder JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Map JavaDoc;
38
39 /**
40  * This is Wsdl2java's TestCase writer. It writes the <serviceName>TestCase.java file.
41  */

42 public class JavaTestCaseWriter extends JavaClassWriter {
43
44     /** Field sEntry */
45     private ServiceEntry sEntry;
46
47     /** Field symbolTable */
48     private SymbolTable symbolTable;
49
50     /**
51      * Constructor.
52      *
53      * @param emitter
54      * @param sEntry
55      * @param symbolTable
56      */

57     protected JavaTestCaseWriter(Emitter emitter, ServiceEntry sEntry,
58                                  SymbolTable symbolTable) {
59
60         super(emitter, sEntry.getName() + "TestCase", "testCase");
61
62         this.sEntry = sEntry;
63         this.symbolTable = symbolTable;
64     } // ctor
65

66     /**
67      * Returns "extends junit.framework.TestCase ".
68      *
69      * @return
70      */

71     protected String JavaDoc getExtendsText() {
72         return "extends junit.framework.TestCase ";
73     } // getExtendsText
74

75     /**
76      * Write the body of the TestCase file.
77      *
78      * @param pw
79      * @throws IOException
80      */

81     protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc {
82
83         // Write the constructor
84
pw.print(" public ");
85         pw.print(getClassName());
86         pw.println("(java.lang.String name) {");
87         pw.println(" super(name);");
88         pw.println(" }");
89         pw.println("");
90         
91         // get ports
92
Map JavaDoc portMap = sEntry.getService().getPorts();
93         Iterator JavaDoc portIterator = portMap.values().iterator();
94
95         while (portIterator.hasNext()) {
96             Port p = (Port) portIterator.next();
97             Binding binding = p.getBinding();
98             BindingEntry bEntry =
99                     symbolTable.getBindingEntry(binding.getQName());
100
101             // If this isn't an SOAP binding, skip it
102
if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) {
103                 continue;
104             }
105
106             // JSR 101 indicates that the name of the port used
107
// in the java code is the name of the wsdl:port. It
108
// does not indicate what should occur if the
109
// wsdl:port name is not a java identifier. The
110
// TCK depends on the case-sensitivity being preserved,
111
// and the interop tests have port names that are not
112
// valid java identifiers. Thus the following code.
113
String JavaDoc portName = p.getName();
114
115             if (!JavaUtils.isJavaId(portName)) {
116                 portName = Utils.xmlNameToJavaClass(portName);
117             }
118
119             pw.println(" public void test"+portName+"WSDL() throws Exception {");
120             pw.println(" javax.xml.rpc.ServiceFactory serviceFactory = javax.xml.rpc.ServiceFactory.newInstance();");
121             pw.println(" java.net.URL url = new java.net.URL(new " + sEntry.getName() + "Locator" + "().get" + portName + "Address() + \"?WSDL\");");
122             pw.println(" javax.xml.rpc.Service service = serviceFactory.createService(url, new " + sEntry.getName() + "Locator().getServiceName());");
123             pw.println(" assertTrue(service != null);");
124             pw.println(" }");
125             pw.println("");
126
127             PortType portType = binding.getPortType();
128
129             writeComment(pw, p.getDocumentationElement(), true);
130
131             writeServiceTestCode(pw, portName, portType, bEntry);
132         }
133     } // writeFileBody
134

135     // Methods may be overloaded. If we just grab the method name
136
// for the test method names, we could end up with duplicates.
137
// The quick-and-easy solution is to have a test counter so that
138
// each test method has a number.
139

140     /** Field counter */
141     private int counter = 1;
142
143     /**
144      * Method writeServiceTestCode
145      *
146      * @param pw
147      * @param portName
148      * @param portType
149      * @param bEntry
150      */

151     protected final void writeServiceTestCode(
152             PrintWriter JavaDoc pw, String JavaDoc portName, PortType portType, BindingEntry bEntry) {
153
154         Iterator JavaDoc ops = portType.getOperations().iterator();
155
156         while (ops.hasNext()) {
157             Operation op = (Operation) ops.next();
158             OperationType type = op.getStyle();
159             Parameters params = bEntry.getParameters(op);
160
161             // did we emit a constructor that throws?
162
BooleanHolder JavaDoc bThrow = new BooleanHolder JavaDoc(false);
163
164             // These operation types are not supported. The signature
165
// will be a string stating that fact.
166
if ((OperationType.NOTIFICATION.equals(type))
167                     || (OperationType.SOLICIT_RESPONSE.equals(type))) {
168                 pw.println(" " + params.signature);
169
170                 continue;
171             }
172
173             String JavaDoc javaOpName = Utils.xmlNameToJavaClass(op.getName());
174             String JavaDoc testMethodName = "test" + counter++ + portName + javaOpName;
175
176             pw.println(" public void " + testMethodName
177                     + "() throws Exception {");
178
179             String JavaDoc bindingType = bEntry.getName() + "Stub";
180
181             writeBindingAssignment(pw, bindingType, portName);
182             pw.println(" // Test operation");
183
184             String JavaDoc indent = "";
185             Map JavaDoc faultMap = op.getFaults();
186
187             if ((faultMap != null) && (faultMap.size() > 0)) {
188
189                 // we are going to catch fault Exceptions
190
pw.println(" try {");
191
192                 indent = " ";
193             }
194
195             Parameter returnParam = params.returnParam;
196             if (returnParam != null) {
197                 TypeEntry returnType = returnParam.getType();
198
199                 pw.print(" " + indent);
200                 pw.print(Utils.getParameterTypeName(returnParam));
201                 pw.print(" value = ");
202
203                 if ((returnParam.getMIMEInfo() == null) &&
204                         !returnParam.isOmittable() &&
205                         Utils.isPrimitiveType(returnType)) {
206                     if ("boolean".equals(returnType.getName())) {
207                         pw.println("false;");
208                     } else {
209                         pw.println("-3;");
210                     }
211                 } else {
212                     pw.println("null;");
213                 }
214             }
215
216             pw.print(" " + indent);
217
218             if (returnParam != null) {
219                 pw.print("value = ");
220             }
221
222             pw.print("binding.");
223             pw.print(Utils.xmlNameToJava(op.getName()));
224             pw.print("(");
225
226             Iterator JavaDoc iparam = params.list.iterator();
227             boolean isFirst = true;
228
229             while (iparam.hasNext()) {
230                 if (isFirst) {
231                     isFirst = false;
232                 } else {
233                     pw.print(", ");
234                 }
235
236                 Parameter param = (Parameter) iparam.next();
237                 String JavaDoc suffix = "";
238
239                 // if we have an out or in/out, we are passing in a holder
240
if (param.getMode() != Parameter.IN) {
241                     pw.print("new " + Utils.holder(param, emitter) + "(");
242                     suffix = ")";
243                 }
244
245                 // if we have an in or in/out, write the constructor
246
if (param.getMode() != Parameter.OUT) {
247                     String JavaDoc constructorString =
248                             Utils.getConstructorForParam(param, symbolTable,
249                                     bThrow);
250
251                     pw.print(constructorString);
252                 }
253
254                 pw.print(suffix);
255             }
256
257             pw.println(");");
258
259             if ((faultMap != null) && (faultMap.size() > 0)) {
260                 pw.println(" }");
261             }
262
263             if (faultMap != null) {
264                 Iterator JavaDoc i = faultMap.values().iterator();
265                 int count = 0;
266
267                 while (i.hasNext()) {
268                     count++;
269
270                     Fault f = (Fault) i.next();
271
272                     pw.print(" catch (");
273                     pw.print(Utils.getFullExceptionName(f.getMessage(),
274                             symbolTable));
275                     pw.println(" e" + count + ") {");
276                     pw.print(" ");
277                     pw.println(
278                             "throw new junit.framework.AssertionFailedError(\""
279                             + f.getName() + " Exception caught: \" + e" + count
280                             + ");");
281                     pw.println(" }");
282                 }
283             }
284
285             pw.println(" " + indent + "// TBD - validate results");
286
287             /*
288              * pw.println(" catch (java.rmi.RemoteException re) {");
289              * pw.print(" ");
290              * pw.println("throw new junit.framework.AssertionFailedError(\"Remote Exception caught: \" + re);");
291              * pw.println(" }");
292              * if (bThrow.value) {
293              * pw.println(" catch (Exception e) {");
294              * pw.println(" // Unsigned constructors can throw - ignore");
295              * pw.println(" }");
296              * }
297              */

298             pw.println(" }");
299             pw.println();
300         }
301     } // writeServiceTestCode
302

303     /**
304      * Method writeBindingAssignment
305      *
306      * @param pw
307      * @param bindingType
308      * @param portName
309      */

310     public final void writeBindingAssignment(
311             PrintWriter JavaDoc pw, String JavaDoc bindingType, String JavaDoc portName) {
312
313         pw.println(" " + bindingType + " binding;");
314         pw.println(" try {");
315         pw.println(" binding = (" + bindingType + ")");
316         pw.print(" new " + sEntry.getName());
317         pw.println("Locator" + "().get" + portName + "();");
318         pw.println(" }");
319         pw.println(" catch ("
320                 + javax.xml.rpc.ServiceException JavaDoc.class.getName()
321                 + " jre) {");
322         pw.println(" if(jre.getLinkedCause()!=null)");
323         pw.println(" jre.getLinkedCause().printStackTrace();");
324         pw.println(
325                 " throw new junit.framework.AssertionFailedError(\"JAX-RPC ServiceException caught: \" + jre);");
326         pw.println(" }");
327         pw.println(" assertNotNull(\""
328                 + Messages.getMessage("null00", "binding")
329                 + "\", binding);");
330         pw.println();
331         pw.println(" // Time out after a minute");
332         pw.println(" binding.setTimeout(60000);");
333         pw.println();
334     } // writeBindingAssignment
335
} // class JavaTestCasepw
336
Popular Tags