View Javadoc

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.geronimo.ews.jaxrpcmapping;
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  import org.apache.axis.wsdl.toJava.JavaClassWriter;
27  import org.apache.axis.wsdl.toJava.Utils;
28  
29  import javax.wsdl.Binding;
30  import javax.wsdl.Fault;
31  import javax.wsdl.Operation;
32  import javax.wsdl.OperationType;
33  import javax.wsdl.Port;
34  import javax.wsdl.PortType;
35  import javax.xml.rpc.holders.BooleanHolder;
36  import java.io.IOException;
37  import java.io.PrintWriter;
38  import java.util.Iterator;
39  import java.util.Map;
40  
41  /***
42   * This is Wsdl2java's TestCase writer.  It writes the <serviceName>TestCase.java file.
43   *
44   * @author Ias (iasandcb@tmax.co.kr)
45   * @deprecated no more used by J2eeGeneratorFactory
46   */
47  public class J2eeTestCaseWriter extends JavaClassWriter {
48      private ServiceEntry sEntry;
49      private SymbolTable symbolTable;
50  
51      /***
52       * Constructor.
53       */
54      protected J2eeTestCaseWriter(J2eeEmitter emitter,
55                                   ServiceEntry sEntry,
56                                   SymbolTable symbolTable) {
57          super(emitter, sEntry.getName() + "TestCase", "testCase");
58          this.sEntry = sEntry;
59          this.symbolTable = symbolTable;
60      } // ctor
61  
62      /***
63       * Returns "extends junit.framework.TestCase ".
64       */
65      protected String getExtendsText() {
66          return "extends junit.framework.TestCase ";
67      } // getExtendsText
68  
69      /***
70       * Write the body of the TestCase file.
71       */
72      protected void writeFileBody(PrintWriter pw) throws IOException {
73          // Write the constructor
74          pw.print("    public ");
75          pw.print(getClassName());
76          pw.println("(java.lang.String name) {");
77          pw.println("        super(name);");
78          pw.println("    }");
79  
80          // get ports
81          Map portMap = sEntry.getService().getPorts();
82          Iterator portIterator = portMap.values().iterator();
83          while (portIterator.hasNext()) {
84              Port p = (Port) portIterator.next();
85              Binding binding = p.getBinding();
86              BindingEntry bEntry =
87                      symbolTable.getBindingEntry(binding.getQName());
88  
89              // If this isn't an SOAP binding, skip it
90              if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) {
91                  continue;
92              }
93  
94              // JSR 101 indicates that the name of the port used
95              // in the java code is the name of the wsdl:port.  It
96              // does not indicate what should occur if the 
97              // wsdl:port name is not a java identifier.  The
98              // TCK depends on the case-sensitivity being preserved,
99              // and the interop tests have port names that are not
100             // valid java identifiers.  Thus the following code.
101             String portName = p.getName();
102             if (!JavaUtils.isJavaId(portName)) {
103                 portName = Utils.xmlNameToJavaClass(portName);
104             }
105             PortType portType = binding.getPortType();
106             writeComment(pw, p.getDocumentationElement(), true);
107             writeServiceTestCode(pw, portName, portType, bEntry);
108         }
109     } // writeFileBody
110 
111     // Methods may be overloaded.  If we just grab the method name
112     // for the test method names, we could end up with duplicates.
113     // The quick-and-easy solution is to have a test counter so that
114     // each test method has a number.
115     private int counter = 1;
116 
117     private final void writeServiceTestCode(PrintWriter pw,
118                                             String portName, PortType portType,
119                                             BindingEntry bEntry) throws IOException {
120         Iterator ops = portType.getOperations().iterator();
121         while (ops.hasNext()) {
122             Operation op = (Operation) ops.next();
123             OperationType type = op.getStyle();
124             Parameters params = bEntry.getParameters(op);
125             // did we emit a constructor that throws?
126             BooleanHolder bThrow = new BooleanHolder(false);
127 
128             // These operation types are not supported.  The signature
129             // will be a string stating that fact.
130             if (type == OperationType.NOTIFICATION
131                     || type == OperationType.SOLICIT_RESPONSE) {
132                 pw.println("    " + params.signature);
133                 continue;
134             }
135             String javaOpName = Utils.xmlNameToJavaClass(op.getName());
136             String testMethodName = "test" + counter++ + portName + javaOpName;
137             pw.println("    public void " + testMethodName + "() throws Exception {");
138             String bindingType = bEntry.getName() + "Stub";
139             writeBindingAssignment(pw, bindingType, portName);
140             pw.println("        // Test operation");
141             String indent = "";
142             Map faultMap = op.getFaults();
143             if (faultMap != null && faultMap.size() > 0) {
144                 // we are going to catch fault Exceptions
145                 pw.println("        try {");
146                 indent = "    ";
147             }
148             if (params.returnParam != null) {
149                 TypeEntry returnType = params.returnParam.getType();
150                 pw.print("        " + indent);
151                 pw.print(Utils.getParameterTypeName(params.returnParam));
152                 pw.print(" value = ");
153                 if (params.returnParam.getMIMEInfo() == null &&
154                         Utils.isPrimitiveType(returnType)) {
155                     if ("boolean".equals(returnType.getName())) {
156                         pw.println("false;");
157                     } else {
158                         pw.println("-3;");
159                     }
160                 } else {
161                     pw.println("null;");
162                 }
163             }
164             pw.print("        " + indent);
165             if (params.returnParam != null) {
166                 pw.print("value = ");
167             }
168             pw.print("binding.");
169             pw.print(Utils.xmlNameToJava(op.getName()));
170             pw.print("(");
171             Iterator iparam = params.list.iterator();
172             boolean isFirst = true;
173             while (iparam.hasNext()) {
174                 if (isFirst) {
175                     isFirst = false;
176                 } else {
177                     pw.print(", ");
178                 }
179                 Parameter param = (Parameter) iparam.next();
180                 String suffix = "";
181 
182                 // if we have an out or in/out, we are passing in a holder
183                 if (param.getMode() != Parameter.IN) {
184                     pw.print("new " + Utils.holder(param, emitter)
185                             + "(");
186                     suffix = ")";
187                 }
188 
189                 // if we have an in or in/out, write the constructor
190                 if (param.getMode() != Parameter.OUT) {
191                     String constructorString = Utils.getConstructorForParam(param, symbolTable, bThrow);
192                     pw.print(constructorString);
193                 }
194                 pw.print(suffix);
195             }
196             pw.println(");");
197             if (faultMap != null && faultMap.size() > 0) {
198                 pw.println("        }");
199             }
200             if (faultMap != null) {
201                 Iterator i = faultMap.values().iterator();
202                 int count = 0;
203                 while (i.hasNext()) {
204                     count++;
205                     Fault f = (Fault) i.next();
206                     pw.print("        catch (");
207                     pw.print(Utils.getFullExceptionName(f.getMessage(), symbolTable));
208                     pw.println(" e" + count + ") {");
209                     pw.print("            ");
210                     pw.println("throw new junit.framework.AssertionFailedError(\"" + f.getName() + " Exception caught: \" + e" + count + ");");
211                     pw.println("        }");
212                 }
213             }
214             pw.println("        " + indent + "// TBD - validate results");
215 
216             /*
217             pw.println("        catch (java.rmi.RemoteException re) {");
218             pw.print("            ");
219             pw.println("throw new junit.framework.AssertionFailedError(\"Remote Exception caught: \" + re);");
220             pw.println("        }");
221             if (bThrow.value) {
222                 pw.println("        catch (Exception e) {");
223                 pw.println("            // Unsigned constructors can throw - ignore");
224                 pw.println("        }");
225             }
226             */
227             pw.println("    }");
228             pw.println();
229         }
230     } // writeServiceTestCode
231 
232     public final void writeBindingAssignment(PrintWriter pw,
233                                              String bindingType, String portName) throws IOException {
234         pw.println("        " + bindingType + " binding;");
235         pw.println("        try {");
236         pw.println("            binding = (" + bindingType + ")");
237         pw.print("                          new " + sEntry.getName());
238         pw.println("Locator" + "().get" + portName + "();");
239         pw.println("        }");
240         pw.println("        catch (" + javax.xml.rpc.ServiceException.class.getName() + " jre) {");
241         pw.println("            if(jre.getLinkedCause()!=null)");
242         pw.println("                jre.getLinkedCause().printStackTrace();");
243         pw.println("            throw new junit.framework.AssertionFailedError(\"JAX-RPC ServiceException caught: \" + jre);");
244         pw.println("        }");
245         pw.println("        assertNotNull(\"" +
246                 Messages.getMessage("null00", "binding") +
247                 "\", binding);");
248         pw.println();
249         pw.println("        // Time out after a minute");
250         pw.println("        binding.setTimeout(60000);");
251         pw.println();
252     } // writeBindingAssignment
253 
254 }