FileDocCategorySizeDatePackage
JavaTestCaseWriter.javaAPI DocApache Axis 1.411973Sat Apr 22 18:57:28 BST 2006org.apache.axis.wsdl.toJava

JavaTestCaseWriter

public class JavaTestCaseWriter extends JavaClassWriter
This is Wsdl2java's TestCase writer. It writes the TestCase.java file.

Fields Summary
private org.apache.axis.wsdl.symbolTable.ServiceEntry
sEntry
Field sEntry
private org.apache.axis.wsdl.symbolTable.SymbolTable
symbolTable
Field symbolTable
private int
counter
Field counter
Constructors Summary
protected JavaTestCaseWriter(Emitter emitter, org.apache.axis.wsdl.symbolTable.ServiceEntry sEntry, org.apache.axis.wsdl.symbolTable.SymbolTable symbolTable)
Constructor.

param
emitter
param
sEntry
param
symbolTable


        super(emitter, sEntry.getName() + "TestCase", "testCase");

        this.sEntry = sEntry;
        this.symbolTable = symbolTable;
    
Methods Summary
protected java.lang.StringgetExtendsText()
Returns "extends junit.framework.TestCase ".

return

        return "extends junit.framework.TestCase ";
    
public final voidwriteBindingAssignment(java.io.PrintWriter pw, java.lang.String bindingType, java.lang.String portName)
Method writeBindingAssignment

param
pw
param
bindingType
param
portName


        pw.println("        " + bindingType + " binding;");
        pw.println("        try {");
        pw.println("            binding = (" + bindingType + ")");
        pw.print("                          new " + sEntry.getName());
        pw.println("Locator" + "().get" + portName + "();");
        pw.println("        }");
        pw.println("        catch ("
                + javax.xml.rpc.ServiceException.class.getName()
                + " jre) {");
        pw.println("            if(jre.getLinkedCause()!=null)");
        pw.println("                jre.getLinkedCause().printStackTrace();");
        pw.println(
                "            throw new junit.framework.AssertionFailedError(\"JAX-RPC ServiceException caught: \" + jre);");
        pw.println("        }");
        pw.println("        assertNotNull(\""
                + Messages.getMessage("null00", "binding")
                + "\", binding);");
        pw.println();
        pw.println("        // Time out after a minute");
        pw.println("        binding.setTimeout(60000);");
        pw.println();
    
protected voidwriteFileBody(java.io.PrintWriter pw)
Write the body of the TestCase file.

param
pw
throws
IOException


        // Write the constructor
        pw.print("    public ");
        pw.print(getClassName());
        pw.println("(java.lang.String name) {");
        pw.println("        super(name);");
        pw.println("    }");
        pw.println("");
        
        // get ports
        Map portMap = sEntry.getService().getPorts();
        Iterator portIterator = portMap.values().iterator();

        while (portIterator.hasNext()) {
            Port p = (Port) portIterator.next();
            Binding binding = p.getBinding();
            BindingEntry bEntry =
                    symbolTable.getBindingEntry(binding.getQName());

            // If this isn't an SOAP binding, skip it
            if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) {
                continue;
            }

            // JSR 101 indicates that the name of the port used
            // in the java code is the name of the wsdl:port.  It
            // does not indicate what should occur if the
            // wsdl:port name is not a java identifier.  The
            // TCK depends on the case-sensitivity being preserved,
            // and the interop tests have port names that are not
            // valid java identifiers.  Thus the following code.
            String portName = p.getName();

            if (!JavaUtils.isJavaId(portName)) {
                portName = Utils.xmlNameToJavaClass(portName);
            }

            pw.println("    public void test"+portName+"WSDL() throws Exception {");
            pw.println("        javax.xml.rpc.ServiceFactory serviceFactory = javax.xml.rpc.ServiceFactory.newInstance();");
            pw.println("        java.net.URL url = new java.net.URL(new " + sEntry.getName() + "Locator" + "().get" + portName + "Address() + \"?WSDL\");");
            pw.println("        javax.xml.rpc.Service service = serviceFactory.createService(url, new " + sEntry.getName() + "Locator().getServiceName());");
            pw.println("        assertTrue(service != null);");
            pw.println("    }");
            pw.println("");

            PortType portType = binding.getPortType();

            writeComment(pw, p.getDocumentationElement(), true);

            writeServiceTestCode(pw, portName, portType, bEntry);
        }
    
protected final voidwriteServiceTestCode(java.io.PrintWriter pw, java.lang.String portName, javax.wsdl.PortType portType, org.apache.axis.wsdl.symbolTable.BindingEntry bEntry)
Method writeServiceTestCode

param
pw
param
portName
param
portType
param
bEntry


                             
       
                    

        Iterator ops = portType.getOperations().iterator();

        while (ops.hasNext()) {
            Operation op = (Operation) ops.next();
            OperationType type = op.getStyle();
            Parameters params = bEntry.getParameters(op);

            // did we emit a constructor that throws?
            BooleanHolder bThrow = new BooleanHolder(false);

            // These operation types are not supported.  The signature
            // will be a string stating that fact.
            if ((OperationType.NOTIFICATION.equals(type))
                    || (OperationType.SOLICIT_RESPONSE.equals(type))) {
                pw.println("    " + params.signature);

                continue;
            }

            String javaOpName = Utils.xmlNameToJavaClass(op.getName());
            String testMethodName = "test" + counter++ + portName + javaOpName;

            pw.println("    public void " + testMethodName
                    + "() throws Exception {");

            String bindingType = bEntry.getName() + "Stub";

            writeBindingAssignment(pw, bindingType, portName);
            pw.println("        // Test operation");

            String indent = "";
            Map faultMap = op.getFaults();

            if ((faultMap != null) && (faultMap.size() > 0)) {

                // we are going to catch fault Exceptions
                pw.println("        try {");

                indent = "    ";
            }

            Parameter returnParam = params.returnParam;
            if (returnParam != null) {
                TypeEntry returnType = returnParam.getType();

                pw.print("        " + indent);
                pw.print(Utils.getParameterTypeName(returnParam));
                pw.print(" value = ");

                if ((returnParam.getMIMEInfo() == null) &&
                        !returnParam.isOmittable() &&
                        Utils.isPrimitiveType(returnType)) {
                    if ("boolean".equals(returnType.getName())) {
                        pw.println("false;");
                    } else {
                        pw.println("-3;");
                    }
                } else {
                    pw.println("null;");
                }
            }

            pw.print("        " + indent);

            if (returnParam != null) {
                pw.print("value = ");
            }

            pw.print("binding.");
            pw.print(Utils.xmlNameToJava(op.getName()));
            pw.print("(");

            Iterator iparam = params.list.iterator();
            boolean isFirst = true;

            while (iparam.hasNext()) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    pw.print(", ");
                }

                Parameter param = (Parameter) iparam.next();
                String suffix = "";

                // if we have an out or in/out, we are passing in a holder
                if (param.getMode() != Parameter.IN) {
                    pw.print("new " + Utils.holder(param, emitter) + "(");
                    suffix = ")";
                }

                // if we have an in or in/out, write the constructor
                if (param.getMode() != Parameter.OUT) {
                    String constructorString =
                            Utils.getConstructorForParam(param, symbolTable,
                                    bThrow);

                    pw.print(constructorString);
                }

                pw.print(suffix);
            }

            pw.println(");");

            if ((faultMap != null) && (faultMap.size() > 0)) {
                pw.println("        }");
            }

            if (faultMap != null) {
                Iterator i = faultMap.values().iterator();
                int count = 0;

                while (i.hasNext()) {
                    count++;

                    Fault f = (Fault) i.next();

                    pw.print("        catch (");
                    pw.print(Utils.getFullExceptionName(f.getMessage(),
                            symbolTable));
                    pw.println(" e" + count + ") {");
                    pw.print("            ");
                    pw.println(
                            "throw new junit.framework.AssertionFailedError(\""
                            + f.getName() + " Exception caught: \" + e" + count
                            + ");");
                    pw.println("        }");
                }
            }

            pw.println("        " + indent + "// TBD - validate results");

            /*
             * pw.println("        catch (java.rmi.RemoteException re) {");
             * pw.print("            ");
             * pw.println("throw new junit.framework.AssertionFailedError(\"Remote Exception caught: \" + re);");
             * pw.println("        }");
             * if (bThrow.value) {
             *   pw.println("        catch (Exception e) {");
             *   pw.println("            // Unsigned constructors can throw - ignore");
             *   pw.println("        }");
             * }
             */
            pw.println("    }");
            pw.println();
        }