001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * CustomizationTest.java
043: * JUnit based test
044: *
045: * Created on February 3, 2006, 12:06 PM
046: */
047:
048: package org.netbeans.modules.websvc.customization.model;
049:
050: import java.io.BufferedOutputStream;
051: import java.io.BufferedReader;
052: import java.io.File;
053: import java.io.FileOutputStream;
054: import java.io.InputStream;
055: import java.io.InputStreamReader;
056: import java.io.OutputStream;
057: import java.io.PrintWriter;
058: import java.util.Collection;
059: import java.util.List;
060: import javax.swing.text.Document;
061: import junit.framework.*;
062: import org.netbeans.modules.websvc.customization.model.JAXWSQName;
063: import org.netbeans.modules.xml.wsdl.model.Binding;
064: import org.netbeans.modules.xml.wsdl.model.Definitions;
065: import org.netbeans.modules.xml.wsdl.model.Fault;
066: import org.netbeans.modules.xml.wsdl.model.Operation;
067: import org.netbeans.modules.xml.wsdl.model.Port;
068: import org.netbeans.modules.xml.wsdl.model.PortType;
069: import org.netbeans.modules.xml.wsdl.model.Service;
070: import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory;
071: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
072: import org.netbeans.modules.xml.wsdl.model.impl.WSDLModelImpl;
073: import org.netbeans.modules.xml.xam.ModelSource;
074: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
075: import org.openide.util.Lookup;
076: import org.openide.util.lookup.Lookups;
077:
078: /**
079: *
080: * @author Roderico Cruz
081: */
082: public class CustomizationTest extends TestCase {
083: private final static String TEST_WSDL = "resources/AddNumbers.wsdl";
084:
085: public CustomizationTest(String testName) {
086: super (testName);
087: }
088:
089: protected void setUp() throws Exception {
090: }
091:
092: protected void tearDown() throws Exception {
093: }
094:
095: private WSDLModel getModel() {
096: WSDLModel model = null;
097:
098: try {
099: Document doc = this .getResourceAsDocument(TEST_WSDL);
100: Lookup l = Lookups.fixed(new Object[] { doc });
101: ModelSource source = new ModelSource(l, true);
102: model = new WSDLModelImpl(source);
103: model.sync();
104: } catch (Exception e) {
105: System.out.println("Exception class: "
106: + e.getClass().getName());
107: System.out.println("Unable to load model: "
108: + e.getMessage());
109: }
110: return model;
111: }
112:
113: public void testWrite() throws Exception {
114: WSDLModel model = getModel();
115: model.startTransaction();
116: Definitions d = model.getDefinitions();
117: System.out.println("definitions: " + d);
118: WSDLComponentFactory fact = d.getModel().getFactory();
119: //set global customizations
120: DefinitionsCustomization dc = (DefinitionsCustomization) fact
121: .create(d, JAXWSQName.BINDINGS.getQName());
122: EnableWrapperStyle ews = (EnableWrapperStyle) fact.create(dc,
123: JAXWSQName.ENABLEWRAPPERSTYLE.getQName());
124: ews.setEnabled(true);
125: EnableAsyncMapping eam = (EnableAsyncMapping) fact.create(dc,
126: JAXWSQName.ENABLEASYNCMAPPING.getQName());
127: eam.setEnabled(false);
128: dc.setEnableWrapperStyle(ews);
129: dc.setEnableAsyncMapping(eam);
130: d.addExtensibilityElement(dc);
131:
132: //set PortType customizations
133: Collection<PortType> portTypes = d.getPortTypes();
134: for (PortType p : portTypes) {
135: if (p.getName().equals("AddNumbersImpl")) {
136: PortTypeCustomization pc = (PortTypeCustomization) fact
137: .create(p, JAXWSQName.BINDINGS.getQName());
138: JavaClass jc = (JavaClass) fact.create(pc,
139: JAXWSQName.CLASS.getQName());
140: jc.setName("MathUtil");
141: pc.setJavaClass(jc);
142: ews = (EnableWrapperStyle) fact.create(dc,
143: JAXWSQName.ENABLEWRAPPERSTYLE.getQName());
144: ews.setEnabled(true);
145: pc.setEnableWrapperStyle(ews);
146: eam = (EnableAsyncMapping) fact.create(dc,
147: JAXWSQName.ENABLEASYNCMAPPING.getQName());
148: eam.setEnabled(false);
149: pc.setEnableAsyncMapping(eam);
150: p.addExtensibilityElement(pc);
151:
152: //set PortTypeOperation customizations
153: Collection<Operation> ops = p.getOperations();
154: for (Operation op : ops) {
155: if (op.getName().equals("addNumbers")) {
156: PortTypeOperationCustomization ptoc = (PortTypeOperationCustomization) fact
157: .create(op, JAXWSQName.BINDINGS
158: .getQName());
159:
160: //Customize method name
161: JavaMethod javaMethod = (JavaMethod) fact
162: .create(ptoc, JAXWSQName.METHOD
163: .getQName());
164: javaMethod.setName("add");
165: ptoc.setJavaMethod(javaMethod);
166: //Customize parameters using hard coded values
167: JavaParameter parm1 = (JavaParameter) fact
168: .create(ptoc, JAXWSQName.PARAMETER
169: .getQName());
170: parm1
171: .setPart("wsdl:definitions/wsdl:message[@name='addNumbers']/wsdl:part[@name='parameters']");
172: parm1.setChildElementName("tns:number1");
173: parm1.setName("num1");
174: ptoc.addJavaParameter(parm1);
175:
176: JavaParameter parm2 = (JavaParameter) fact
177: .create(ptoc, JAXWSQName.PARAMETER
178: .getQName());
179: parm2
180: .setPart("wsdl:definitions/wsdl:message[@name='addNumbers']/wsdl:part[@name='parameters']");
181: parm2.setChildElementName("tns:number2");
182: parm2.setName("num2");
183: ptoc.addJavaParameter(parm2);
184: //end of hard coded values
185:
186: ews = (EnableWrapperStyle) fact.create(ptoc,
187: JAXWSQName.ENABLEWRAPPERSTYLE
188: .getQName());
189: ews.setEnabled(true);
190: ptoc.setEnableWrapperStyle(ews);
191: eam = (EnableAsyncMapping) fact.create(ptoc,
192: JAXWSQName.ENABLEASYNCMAPPING
193: .getQName());
194: ptoc.setEnableAsyncMapping(eam);
195: eam.setEnabled(false);
196: op.addExtensibilityElement(ptoc);
197:
198: //Customize the faults
199: Collection<Fault> faults = op.getFaults();
200: for (Fault fault : faults) {
201: if (fault.getName().equals(
202: "AddNumbersException")) {
203: PortTypeOperationFaultCustomization ptofc = (PortTypeOperationFaultCustomization) fact
204: .create(fault,
205: JAXWSQName.BINDINGS
206: .getQName());
207: jc = (JavaClass) fact.create(ptofc,
208: JAXWSQName.CLASS.getQName());
209: jc.setName("MathUtilException");
210: ptofc.setJavaClass(jc);
211: fault.addExtensibilityElement(ptofc);
212: }
213: }
214: }
215: }
216: }
217: }
218:
219: //Customize the service
220: Collection<Service> services = d.getServices();
221: for (Service service : services) {
222: if (service.getName().equals("AddNumbersService")) {
223: ServiceCustomization sc = (ServiceCustomization) fact
224: .create(service, JAXWSQName.BINDINGS.getQName());
225: JavaClass jc = (JavaClass) fact.create(sc,
226: JAXWSQName.CLASS.getQName());
227: jc.setName("MathUtilService");
228: sc.setJavaClass(jc);
229: service.addExtensibilityElement(sc);
230:
231: //Customize the port
232: Collection<Port> ports = service.getPorts();
233: for (Port port : ports) {
234: if (port.getName().equals("AddNumbersPort")) {
235: PortCustomization poc = (PortCustomization) fact
236: .create(port, JAXWSQName.BINDINGS
237: .getQName());
238: JavaMethod jmethod = (JavaMethod) fact.create(
239: poc, JAXWSQName.METHOD.getQName());
240: jmethod.setName("getMathUtil");
241: poc.setJavaMethod(jmethod);
242: port.addExtensibilityElement(poc);
243: }
244: }
245: }
246: }
247: model.endTransaction();
248:
249: File f = dumpToTempFile(((AbstractDocumentModel) model)
250: .getBaseDocument());
251: System.out.println("file: " + f.getAbsolutePath());
252:
253: //now read it back
254:
255: List<DefinitionsCustomization> dces = d
256: .getExtensibilityElements(DefinitionsCustomization.class);
257: System.out.println("###number of DefinitionsCustomizations: "
258: + dces.size());
259:
260: Collection<PortType> pts = d.getPortTypes();
261: for (PortType portType : pts) {
262: if (portType.getName().equals("AddNumbersImpl")) {
263: List<PortTypeCustomization> ptcs = portType
264: .getExtensibilityElements(PortTypeCustomization.class);
265: System.out
266: .println("####number of PortTypeCustomizations: "
267: + ptcs.size());
268: for (PortTypeCustomization ptc : ptcs) {
269: JavaClass jc = ptc.getJavaClass();
270: if (jc != null) {
271: System.out.println("Class name: "
272: + jc.getName());
273: }
274: }
275: }
276: }
277:
278: Collection<Binding> bindings = d.getBindings();
279: for (Binding binding : bindings) {
280: if (binding.getName().equals("AddNumbersImplBinding")) {
281: List<BindingCustomization> bcs = binding
282: .getExtensibilityElements(BindingCustomization.class);
283: System.out
284: .println("####Number of BindingCustomizations: "
285: + bcs.size());
286: }
287: }
288: }
289:
290: public static Document getResourceAsDocument(String path)
291: throws Exception {
292: InputStream in = CustomizationTest.class
293: .getResourceAsStream(path);
294: return loadDocument(in);
295: }
296:
297: public static Document loadDocument(InputStream in)
298: throws Exception {
299: Document sd = new org.netbeans.editor.BaseDocument(
300: org.netbeans.modules.xml.text.syntax.XMLKit.class,
301: false);
302: BufferedReader br = new BufferedReader(
303: new InputStreamReader(in));
304: StringBuffer sbuf = new StringBuffer();
305: try {
306: String line = null;
307: while ((line = br.readLine()) != null) {
308: sbuf.append(line);
309: sbuf.append(System.getProperty("line.separator"));
310: }
311: } finally {
312: br.close();
313: }
314: sd.insertString(0, sbuf.toString(), null);
315: return sd;
316: }
317:
318: public static void dumpToFile(Document doc, File f)
319: throws Exception {
320: if (!f.exists()) {
321: f.createNewFile();
322: }
323: OutputStream out = new BufferedOutputStream(
324: new FileOutputStream(f));
325: PrintWriter w = new PrintWriter(out);
326: w.print(doc.getText(0, doc.getLength()));
327: w.close();
328: out.close();
329: }
330:
331: public static File dumpToTempFile(Document doc) throws Exception {
332: File f = File.createTempFile("xsm", "xsd");
333: dumpToFile(doc, f);
334: return f;
335: }
336:
337: // TODO add test methods here. The name must begin with 'test'. For example:
338: // public void testHello() {}
339:
340: }
|