1 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 ; 34 import java.io.IOException ; 35 import java.io.PrintWriter ; 36 import java.util.Iterator ; 37 import java.util.Map ; 38 39 42 public class JavaTestCaseWriter extends JavaClassWriter { 43 44 45 private ServiceEntry sEntry; 46 47 48 private SymbolTable symbolTable; 49 50 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 } 66 71 protected String getExtendsText() { 72 return "extends junit.framework.TestCase "; 73 } 75 81 protected void writeFileBody(PrintWriter pw) throws IOException { 82 83 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 Map portMap = sEntry.getService().getPorts(); 93 Iterator 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 (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) { 103 continue; 104 } 105 106 String 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 } 135 140 141 private int counter = 1; 142 143 151 protected final void writeServiceTestCode( 152 PrintWriter pw, String portName, PortType portType, BindingEntry bEntry) { 153 154 Iterator 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 BooleanHolder bThrow = new BooleanHolder (false); 163 164 if ((OperationType.NOTIFICATION.equals(type)) 167 || (OperationType.SOLICIT_RESPONSE.equals(type))) { 168 pw.println(" " + params.signature); 169 170 continue; 171 } 172 173 String javaOpName = Utils.xmlNameToJavaClass(op.getName()); 174 String testMethodName = "test" + counter++ + portName + javaOpName; 175 176 pw.println(" public void " + testMethodName 177 + "() throws Exception {"); 178 179 String bindingType = bEntry.getName() + "Stub"; 180 181 writeBindingAssignment(pw, bindingType, portName); 182 pw.println(" // Test operation"); 183 184 String indent = ""; 185 Map faultMap = op.getFaults(); 186 187 if ((faultMap != null) && (faultMap.size() > 0)) { 188 189 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 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 suffix = ""; 238 239 if (param.getMode() != Parameter.IN) { 241 pw.print("new " + Utils.holder(param, emitter) + "("); 242 suffix = ")"; 243 } 244 245 if (param.getMode() != Parameter.OUT) { 247 String 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 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 298 pw.println(" }"); 299 pw.println(); 300 } 301 } 303 310 public final void writeBindingAssignment( 311 PrintWriter pw, String bindingType, String 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 .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 } } | Popular Tags |