1 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 ; 73 import java.io.IOException ; 74 import java.io.PrintWriter ; 75 import java.util.Iterator ; 76 import java.util.Map ; 77 78 81 public class JavaTestCaseWriter extends JavaClassWriter 82 { 83 private ServiceEntry sEntry; 84 private SymbolTable symbolTable; 85 86 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 } 98 101 protected String getExtendsText() 102 { 103 return "extends junit.framework.TestCase "; 104 } 106 109 protected void writeFileBody(PrintWriter pw) throws IOException 110 { 111 pw.print(" public "); 113 pw.print(getClassName()); 114 pw.println("(java.lang.String name) {"); 115 pw.println(" super(name);"); 116 pw.println(" }"); 117 118 Map portMap = sEntry.getService().getPorts(); 120 Iterator 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 (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) 131 { 132 continue; 133 } 134 135 String 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 } 155 private int counter = 1; 160 161 private final void writeServiceTestCode(PrintWriter pw, 162 String portName, PortType portType, 163 BindingEntry bEntry) throws IOException 164 { 165 Iterator 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 BooleanHolder bThrow = new BooleanHolder (false); 173 174 if (type == OperationType.NOTIFICATION 177 || type == OperationType.SOLICIT_RESPONSE) 178 { 179 pw.println(" " + params.signature); 180 continue; 181 } 182 183 String javaOpName = Utils.xmlNameToJavaClass(op.getName()); 184 String testMethodName = "test" + counter++ + portName + javaOpName; 185 pw.println(" public void " + testMethodName + "() throws Exception {"); 186 187 String bindingType = bEntry.getName() + "Stub"; 188 writeBindingAssignment(pw, bindingType, portName); 189 190 191 pw.println(" // Test operation"); 192 String indent = ""; 193 Map faultMap = op.getFaults(); 194 if (faultMap != null && faultMap.size() > 0) 195 { 196 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 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 suffix = ""; 252 253 if (param.getMode() != Parameter.IN) 255 { 256 pw.print("new " + Utils.holder(param.getMIMEInfo(), param.getType(), emitter) 257 + "("); 258 suffix = ")"; 259 } 260 261 if (param.getMode() != Parameter.OUT) 263 { 264 String 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 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 308 pw.println(" }"); 309 pw.println(); 310 } 311 } 313 public final void writeBindingAssignment(PrintWriter pw, 314 String bindingType, String portName) throws IOException 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 .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 } 337 } | Popular Tags |