KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > customization > model > CustomizationTest


1 /*
2 * The contents of this file are subject to the terms of the Common Development
3 * and Distribution License (the License). You may not use this file except in
4 * compliance with the License.
5 *
6 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7 * or http://www.netbeans.org/cddl.txt.
8 *
9 * When distributing Covered Code, include this CDDL Header Notice in each file
10 * and include the License file at http://www.netbeans.org/cddl.txt.
11 * If applicable, add the following below the CDDL Header, with the fields
12 * enclosed by brackets [] replaced by your own identifying information:
13 * "Portions Copyrighted [year] [name of copyright owner]"
14 *
15 * The Original Software is NetBeans. The Initial Developer of the Original
16 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17 * Microsystems, Inc. All Rights Reserved.
18 */

19 /*
20  * CustomizationTest.java
21  * JUnit based test
22  *
23  * Created on February 3, 2006, 12:06 PM
24  */

25
26 package org.netbeans.modules.websvc.customization.model;
27
28 import java.io.BufferedOutputStream JavaDoc;
29 import java.io.BufferedReader JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.List JavaDoc;
38 import javax.swing.text.Document JavaDoc;
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 /**
57  *
58  * @author Roderico Cruz
59  */

60 public class CustomizationTest extends TestCase {
61     private final static String JavaDoc TEST_WSDL = "resources/AddNumbers.wsdl";
62     
63     public CustomizationTest(String JavaDoc testName) {
64         super(testName);
65     }
66     
67     protected void setUp() throws Exception JavaDoc {
68     }
69     
70     protected void tearDown() throws Exception JavaDoc {
71     }
72     
73     private WSDLModel getModel(){
74         WSDLModel model = null;
75        
76         try{
77              Document JavaDoc doc = this.getResourceAsDocument(TEST_WSDL);
78              Lookup l = Lookups.fixed(new Object JavaDoc[] { doc });
79              ModelSource source = new ModelSource(l, true);
80              model = new WSDLModelImpl(source);
81              model.sync();
82         } catch(Exception JavaDoc 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 JavaDoc {
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         //set global customizations
96
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         //set PortType customizations
107
Collection JavaDoc<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                 //set PortTypeOperation customizations
124
Collection JavaDoc<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                         //Customize method name
131
JavaMethod javaMethod = (JavaMethod) fact.create(ptoc, JAXWSQName.METHOD.getQName());
132                         javaMethod.setName("add");
133                         ptoc.setJavaMethod(javaMethod);
134                         //Customize parameters using hard coded values
135
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                         //end of hard coded values
149

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                         //Customize the faults
161
Collection JavaDoc<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         //Customize the service
178
Collection JavaDoc<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                 //Customize the port
188
Collection JavaDoc<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 JavaDoc f = dumpToTempFile(((AbstractDocumentModel)model).getBaseDocument());
203         System.out.println("file: " +f.getAbsolutePath());
204         
205         //now read it back
206

207         List JavaDoc<DefinitionsCustomization> dces = d.getExtensibilityElements(DefinitionsCustomization.class);
208         System.out.println("###number of DefinitionsCustomizations: " + dces.size());
209         
210         Collection JavaDoc<PortType> pts = d.getPortTypes();
211         for(PortType portType : pts){
212             if(portType.getName().equals("AddNumbersImpl")) {
213                 List JavaDoc<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 JavaDoc<Binding> bindings = d.getBindings();
225         for(Binding binding : bindings){
226             if(binding.getName().equals("AddNumbersImplBinding")){
227                 List JavaDoc<BindingCustomization> bcs = binding.getExtensibilityElements(BindingCustomization.class);
228                 System.out.println("####Number of BindingCustomizations: " + bcs.size());
229             }
230         }
231     }
232     
233     public static Document JavaDoc getResourceAsDocument(String JavaDoc path) throws Exception JavaDoc {
234         InputStream JavaDoc in = CustomizationTest.class.getResourceAsStream(path);
235         return loadDocument(in);
236     }
237     
238     public static Document JavaDoc loadDocument(InputStream JavaDoc in) throws Exception JavaDoc {
239     Document JavaDoc sd = new org.netbeans.editor.BaseDocument(
240             org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
241         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
242         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
243         try {
244             String JavaDoc 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 JavaDoc doc, File JavaDoc f) throws Exception JavaDoc {
257         if (! f.exists()) {
258             f.createNewFile();
259         }
260         OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
261         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
262         w.print(doc.getText(0, doc.getLength()));
263         w.close();
264         out.close();
265     }
266     
267     public static File JavaDoc dumpToTempFile(Document JavaDoc doc) throws Exception JavaDoc {
268         File JavaDoc f = File.createTempFile("xsm", "xsd");
269         dumpToFile(doc, f);
270         return f;
271     }
272     
273     
274     // TODO add test methods here. The name must begin with 'test'. For example:
275
// public void testHello() {}
276

277 }
278
Popular Tags