KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 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 "Axis" 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. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.wsdl.toJava;
56
57 import org.jboss.axis.utils.JavaUtils;
58 import org.jboss.axis.utils.Messages;
59 import org.jboss.axis.wsdl.symbolTable.BindingEntry;
60 import org.jboss.axis.wsdl.symbolTable.Parameter;
61 import org.jboss.axis.wsdl.symbolTable.Parameters;
62 import org.jboss.axis.wsdl.symbolTable.ServiceEntry;
63 import org.jboss.axis.wsdl.symbolTable.SymbolTable;
64 import org.jboss.axis.wsdl.symbolTable.TypeEntry;
65
66 import javax.wsdl.Binding;
67 import javax.wsdl.Fault;
68 import javax.wsdl.Operation;
69 import javax.wsdl.OperationType;
70 import javax.wsdl.Port;
71 import javax.wsdl.PortType;
72 import javax.xml.rpc.holders.BooleanHolder JavaDoc;
73 import java.io.IOException JavaDoc;
74 import java.io.PrintWriter JavaDoc;
75 import java.util.Iterator JavaDoc;
76 import java.util.Map JavaDoc;
77
78 /**
79  * This is Wsdl2java's TestCase writer. It writes the <serviceName>TestCase.java file.
80  */

81 public class JavaTestCaseWriter extends JavaClassWriter
82 {
83    private ServiceEntry sEntry;
84    private SymbolTable symbolTable;
85
86    /**
87     * Constructor.
88     */

89    protected JavaTestCaseWriter(Emitter emitter,
90                                 ServiceEntry sEntry,
91                                 SymbolTable symbolTable)
92    {
93       super(emitter, sEntry.getName() + "TestCase", "testCase");
94       this.sEntry = sEntry;
95       this.symbolTable = symbolTable;
96    } // ctor
97

98    /**
99     * Returns "extends junit.framework.TestCase ".
100     */

101    protected String JavaDoc getExtendsText()
102    {
103       return "extends junit.framework.TestCase ";
104    } // getExtendsText
105

106    /**
107     * Write the body of the TestCase file.
108     */

109    protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc
110    {
111       // Write the constructor
112
pw.print(" public ");
113       pw.print(getClassName());
114       pw.println("(java.lang.String name) {");
115       pw.println(" super(name);");
116       pw.println(" }");
117
118       // get ports
119
Map JavaDoc portMap = sEntry.getService().getPorts();
120       Iterator JavaDoc portIterator = portMap.values().iterator();
121
122       while (portIterator.hasNext())
123       {
124          Port p = (Port)portIterator.next();
125          Binding binding = p.getBinding();
126          BindingEntry bEntry =
127                  symbolTable.getBindingEntry(binding.getQName());
128
129          // If this isn't an SOAP binding, skip it
130
if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP)
131          {
132             continue;
133          }
134
135          // JSR 101 indicates that the name of the port used
136
// in the java code is the name of the wsdl:port. It
137
// does not indicate what should occur if the
138
// wsdl:port name is not a java identifier. The
139
// TCK depends on the case-sensitivity being preserved,
140
// and the interop tests have port names that are not
141
// valid java identifiers. Thus the following code.
142
String JavaDoc portName = p.getName();
143          if (!JavaUtils.isJavaId(portName))
144          {
145             portName = Utils.xmlNameToJavaClass(portName);
146          }
147
148          PortType portType = binding.getPortType();
149
150          writeComment(pw, p.getDocumentationElement());
151          writeServiceTestCode(pw, portName, portType, bEntry);
152       }
153    } // writeFileBody
154

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

308          pw.println(" }");
309          pw.println();
310       }
311    } // writeServiceTestCode
312

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

337 } // class JavaTestCasepw
338
Popular Tags