1 19 25 26 package org.netbeans.modules.websvc.customization.model; 27 28 import java.io.BufferedOutputStream ; 29 import java.io.BufferedReader ; 30 import java.io.File ; 31 import java.io.FileOutputStream ; 32 import java.io.InputStream ; 33 import java.io.InputStreamReader ; 34 import java.io.OutputStream ; 35 import java.io.PrintWriter ; 36 import java.util.Collection ; 37 import java.util.List ; 38 import javax.swing.text.Document ; 39 import junit.framework.*; 40 import org.netbeans.modules.websvc.customization.model.impl.JAXWSQName; 41 import org.netbeans.modules.xml.wsdl.model.Binding; 42 import org.netbeans.modules.xml.wsdl.model.Definitions; 43 import org.netbeans.modules.xml.wsdl.model.Fault; 44 import org.netbeans.modules.xml.wsdl.model.Operation; 45 import org.netbeans.modules.xml.wsdl.model.Port; 46 import org.netbeans.modules.xml.wsdl.model.PortType; 47 import org.netbeans.modules.xml.wsdl.model.Service; 48 import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory; 49 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 50 import org.netbeans.modules.xml.wsdl.model.impl.WSDLModelImpl; 51 import org.netbeans.modules.xml.xam.ModelSource; 52 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 53 import org.openide.util.Lookup; 54 import org.openide.util.lookup.Lookups; 55 56 60 public class CustomizationTest extends TestCase { 61 private final static String TEST_WSDL = "resources/AddNumbers.wsdl"; 62 63 public CustomizationTest(String testName) { 64 super(testName); 65 } 66 67 protected void setUp() throws Exception { 68 } 69 70 protected void tearDown() throws Exception { 71 } 72 73 private WSDLModel getModel(){ 74 WSDLModel model = null; 75 76 try{ 77 Document doc = this.getResourceAsDocument(TEST_WSDL); 78 Lookup l = Lookups.fixed(new Object [] { doc }); 79 ModelSource source = new ModelSource(l, true); 80 model = new WSDLModelImpl(source); 81 model.sync(); 82 } catch(Exception e){ 83 System.out.println("Exception class: " + e.getClass().getName()); 84 System.out.println("Unable to load model: " + e.getMessage()); 85 } 86 return model; 87 } 88 89 public void testWrite() throws Exception { 90 WSDLModel model = getModel(); 91 model.startTransaction(); 92 Definitions d = model.getDefinitions(); 93 System.out.println("definitions: " + d); 94 WSDLComponentFactory fact = d.getModel().getFactory(); 95 DefinitionsCustomization dc = (DefinitionsCustomization) 97 fact.create(d, JAXWSQName.BINDINGS.getQName()); 98 EnableWrapperStyle ews = (EnableWrapperStyle) fact.create(dc, JAXWSQName.ENABLEWRAPPERSTYLE.getQName()); 99 ews.setEnabled(true); 100 EnableAsyncMapping eam = (EnableAsyncMapping) fact.create(dc, JAXWSQName.ENABLEASYNCMAPPING.getQName()); 101 eam.setEnabled(false); 102 dc.setEnableWrapperStyle(ews); 103 dc.setEnableAsyncMapping(eam); 104 d.addExtensibilityElement(dc); 105 106 Collection <PortType> portTypes = d.getPortTypes(); 108 for(PortType p : portTypes){ 109 if(p.getName().equals("AddNumbersImpl")){ 110 PortTypeCustomization pc = (PortTypeCustomization) 111 fact.create(p, JAXWSQName.BINDINGS.getQName()); 112 JavaClass jc = (JavaClass) fact.create(pc, JAXWSQName.CLASS.getQName()); 113 jc.setName("MathUtil"); 114 pc.setJavaClass(jc); 115 ews = (EnableWrapperStyle) fact.create(dc, JAXWSQName.ENABLEWRAPPERSTYLE.getQName()); 116 ews.setEnabled(true); 117 pc.setEnableWrapperStyle(ews); 118 eam = (EnableAsyncMapping) fact.create(dc, JAXWSQName.ENABLEASYNCMAPPING.getQName()); 119 eam.setEnabled(false); 120 pc.setEnableAsyncMapping(eam); 121 p.addExtensibilityElement(pc); 122 123 Collection <Operation> ops = p.getOperations(); 125 for(Operation op : ops){ 126 if(op.getName().equals("addNumbers")){ 127 PortTypeOperationCustomization ptoc = (PortTypeOperationCustomization) 128 fact.create(op, JAXWSQName.BINDINGS.getQName()); 129 130 JavaMethod javaMethod = (JavaMethod) fact.create(ptoc, JAXWSQName.METHOD.getQName()); 132 javaMethod.setName("add"); 133 ptoc.setJavaMethod(javaMethod); 134 JavaParameter parm1 = (JavaParameter) fact.create(ptoc, 136 JAXWSQName.PARAMETER.getQName()); 137 parm1.setPart("wsdl:definitions/wsdl:message[@name='addNumbers']/wsdl:part[@name='parameters']"); 138 parm1.setChildElementName("tns:number1"); 139 parm1.setName("num1"); 140 ptoc.addJavaParameter(parm1); 141 142 JavaParameter parm2 = (JavaParameter) fact.create(ptoc, 143 JAXWSQName.PARAMETER.getQName()); 144 parm2.setPart("wsdl:definitions/wsdl:message[@name='addNumbers']/wsdl:part[@name='parameters']"); 145 parm2.setChildElementName("tns:number2"); 146 parm2.setName("num2"); 147 ptoc.addJavaParameter(parm2); 148 150 ews = (EnableWrapperStyle) fact.create(ptoc, 151 JAXWSQName.ENABLEWRAPPERSTYLE.getQName()); 152 ews.setEnabled(true); 153 ptoc.setEnableWrapperStyle(ews); 154 eam = (EnableAsyncMapping) fact.create(ptoc, 155 JAXWSQName.ENABLEASYNCMAPPING.getQName()); 156 ptoc.setEnableAsyncMapping(eam); 157 eam.setEnabled(false); 158 op.addExtensibilityElement(ptoc); 159 160 Collection <Fault> faults = op.getFaults(); 162 for(Fault fault : faults){ 163 if(fault.getName().equals("AddNumbersException")){ 164 PortTypeOperationFaultCustomization ptofc = (PortTypeOperationFaultCustomization) 165 fact.create(fault, JAXWSQName.BINDINGS.getQName()); 166 jc = (JavaClass) fact.create(ptofc, JAXWSQName.CLASS.getQName()); 167 jc.setName("MathUtilException"); 168 ptofc.setJavaClass(jc); 169 fault.addExtensibilityElement(ptofc); 170 } 171 } 172 } 173 } 174 } 175 } 176 177 Collection <Service> services = d.getServices(); 179 for(Service service : services){ 180 if(service.getName().equals("AddNumbersService")){ 181 ServiceCustomization sc = (ServiceCustomization) fact.create(service, JAXWSQName.BINDINGS.getQName()); 182 JavaClass jc = (JavaClass) fact.create(sc, JAXWSQName.CLASS.getQName()); 183 jc.setName("MathUtilService"); 184 sc.setJavaClass(jc); 185 service.addExtensibilityElement(sc); 186 187 Collection <Port> ports = service.getPorts(); 189 for(Port port : ports){ 190 if(port.getName().equals("AddNumbersPort")){ 191 PortCustomization poc = (PortCustomization) fact.create(port, JAXWSQName.BINDINGS.getQName()); 192 JavaMethod jmethod = (JavaMethod) fact.create(poc, JAXWSQName.METHOD.getQName()); 193 jmethod.setName("getMathUtil"); 194 poc.setJavaMethod(jmethod); 195 port.addExtensibilityElement(poc); 196 } 197 } 198 } 199 } 200 model.endTransaction(); 201 202 File f = dumpToTempFile(((AbstractDocumentModel)model).getBaseDocument()); 203 System.out.println("file: " +f.getAbsolutePath()); 204 205 207 List <DefinitionsCustomization> dces = d.getExtensibilityElements(DefinitionsCustomization.class); 208 System.out.println("###number of DefinitionsCustomizations: " + dces.size()); 209 210 Collection <PortType> pts = d.getPortTypes(); 211 for(PortType portType : pts){ 212 if(portType.getName().equals("AddNumbersImpl")) { 213 List <PortTypeCustomization> ptcs = portType.getExtensibilityElements(PortTypeCustomization.class); 214 System.out.println("####number of PortTypeCustomizations: " + ptcs.size()); 215 for(PortTypeCustomization ptc : ptcs){ 216 JavaClass jc = ptc.getJavaClass(); 217 if(jc != null){ 218 System.out.println("Class name: " + jc.getName()); 219 } 220 } 221 } 222 } 223 224 Collection <Binding> bindings = d.getBindings(); 225 for(Binding binding : bindings){ 226 if(binding.getName().equals("AddNumbersImplBinding")){ 227 List <BindingCustomization> bcs = binding.getExtensibilityElements(BindingCustomization.class); 228 System.out.println("####Number of BindingCustomizations: " + bcs.size()); 229 } 230 } 231 } 232 233 public static Document getResourceAsDocument(String path) throws Exception { 234 InputStream in = CustomizationTest.class.getResourceAsStream(path); 235 return loadDocument(in); 236 } 237 238 public static Document loadDocument(InputStream in) throws Exception { 239 Document sd = new org.netbeans.editor.BaseDocument( 240 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 241 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 242 StringBuffer sbuf = new StringBuffer (); 243 try { 244 String line = null; 245 while ((line = br.readLine()) != null) { 246 sbuf.append(line); 247 sbuf.append(System.getProperty("line.separator")); 248 } 249 } finally { 250 br.close(); 251 } 252 sd.insertString(0,sbuf.toString(),null); 253 return sd; 254 } 255 256 public static void dumpToFile(Document doc, File f) throws Exception { 257 if (! f.exists()) { 258 f.createNewFile(); 259 } 260 OutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 261 PrintWriter w = new PrintWriter (out); 262 w.print(doc.getText(0, doc.getLength())); 263 w.close(); 264 out.close(); 265 } 266 267 public static File dumpToTempFile(Document doc) throws Exception { 268 File f = File.createTempFile("xsm", "xsd"); 269 dumpToFile(doc, f); 270 return f; 271 } 272 273 274 277 } 278 | Popular Tags |