KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > api > jaxws > wsdlmodel > WSDLBindingTest


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 package org.netbeans.modules.websvc.api.jaxws.wsdlmodel;
21
22 import java.io.File JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28
29 /**
30  *
31  * @author mkuchtiak
32  */

33 public class WSDLBindingTest extends NbTestCase {
34     
35     private List JavaDoc<String JavaDoc> serviceNames, serviceJavaNames, portNames, portJavaNames, portGetters, opNames, opJavaNames, opTypes, paramNames, paramTypes;
36     private static final String JavaDoc BINDING_1="custom-client.xml";
37     private static final String JavaDoc BINDING_11="custom-client1.xml";
38     private static final String JavaDoc BINDING_2="custom-schema.xml";
39     private static final String JavaDoc PACKAGE_NAME="netbeans.org.ws";
40     
41     
42     private Object JavaDoc expectedValue, realValue;
43     private int numberOfEvents;
44     
45     private static final String JavaDoc[][] SERVICE_NAMES={{"AddNumbersService"},{"AddNumbersService"}};
46     private static final String JavaDoc[][] SERVICE_JAVA_NAMES={{"external_customize.client.MathUtilService"},{PACKAGE_NAME+".MathUtilService"}};
47     private static final String JavaDoc[][] PORT_NAMES={{"AddNumbersImplPort"},{"AddNumbersImplPort"}};
48     private static final String JavaDoc[][] PORT_JAVA_NAMES={{"external_customize.client.MathUtil"},{PACKAGE_NAME+".MathUtil"}};
49     private static final String JavaDoc[][] PORT_GETTERS={{"getMathUtil"},{"getMathUtil"}};
50     private static final String JavaDoc[][] OP_NAMES={{"addNumbers"},{"addNumbers"}};
51     private static final String JavaDoc[][] OP_JAVA_NAMES={{"add"},{"add"}};
52     private static final String JavaDoc[][] OP_TYPES={{"external_customize.schema.AddNumbersResponse"},{"int"}};
53     private static final String JavaDoc[][] PARAM_NAMES={{"num1"},{"arg0","arg1"}};
54     private static final String JavaDoc[][] PARAM_TYPES={{"external_customize.schema.AddNumbers"},{"int","int"}};
55     
56     public WSDLBindingTest(String JavaDoc testName) {
57         super(testName);
58     }
59
60     protected void setUp() throws Exception JavaDoc {
61     }
62
63     protected void tearDown() throws Exception JavaDoc {
64     }
65     
66     /** Testing both wsdl and schema bindings
67      * Used external files that customize binding process
68      */

69     public void testExternalBindings() throws java.net.MalformedURLException JavaDoc {
70         System.out.println("Test 1 : External Bindings");
71         initResults();
72         final WsdlModeler wsdlModeler = WsdlModelerFactory.getDefault().getWsdlModeler(getUrl("AddNumbers2.wsdl"));
73         assertNotNull("WsdlModelerFactory failed to create object", wsdlModeler);
74         URL JavaDoc binding1 = getUrl(BINDING_1);
75         URL JavaDoc binding2 = getUrl(BINDING_2);
76         wsdlModeler.setJAXBBindings(new URL JavaDoc[]{binding1,binding2});
77         //wsdlModeler.setPackageName("netbeans.ws.org");
78
wsdlModeler.generateWsdlModel(new WsdlModelListener() {
79                 public void modelCreated(WsdlModel model) {
80                     compareWsdl(model,0);
81                 }
82         });
83         wsdlModeler.task.waitFinished();
84         Thread.currentThread().yield();
85         URL JavaDoc binding11 = getUrl(BINDING_11);
86         wsdlModeler.setJAXBBindings(new URL JavaDoc[]{binding11});
87         wsdlModeler.setPackageName(PACKAGE_NAME);
88         wsdlModeler.generateWsdlModel(new WsdlModelListener() {
89                 public void modelCreated(WsdlModel model) {
90                     compareWsdl(model,1);
91                 }
92         },true);
93         wsdlModeler.task.waitFinished();
94         System.out.println("Test 1 : FINISHED "+expectedValue+":"+realValue);
95         if (expectedValue!=null) assertEquals (expectedValue,realValue);
96         assertEquals(2,numberOfEvents);
97     }
98     
99     private void compareWsdl(WsdlModel model, int testNumber) {
100         if (expectedValue!=null || realValue!=null) return;
101         initLists();
102         List JavaDoc<WsdlService> services = model.getServices();
103         for (Iterator JavaDoc<WsdlService> it = services.iterator(); it.hasNext();) {
104             WsdlService s = it.next();
105             serviceNames.add(s.getName());
106             serviceJavaNames.add(s.getJavaName());
107             List JavaDoc<WsdlPort> ports = s.getPorts();
108             for (Iterator JavaDoc<WsdlPort> it1 = ports.iterator(); it1.hasNext();) {
109                 WsdlPort port = it1.next();
110                 portNames.add(port.getName());
111                 portJavaNames.add(port.getJavaName());
112                 portGetters.add(port.getPortGetter());
113                 List JavaDoc<WsdlOperation> operations = port.getOperations();
114                 for (Iterator JavaDoc<WsdlOperation> it2 = operations.iterator(); it2.hasNext();) {
115                     WsdlOperation op = it2.next();
116                     opNames.add(op.getName());
117                     opJavaNames.add(op.getJavaName());
118                     opTypes.add(op.getReturnTypeName());
119                     List JavaDoc<WsdlParameter> parameters = op.getParameters();
120                     for (Iterator JavaDoc<WsdlParameter> it3 = parameters.iterator(); it3.hasNext();) {
121                         WsdlParameter param = it3.next();
122                         paramNames.add(param.getName());
123                         paramTypes.add(param.getTypeName());
124                     }
125                 }
126             }
127         }
128         compareResults(testNumber);
129         numberOfEvents++;
130     }
131     
132     private URL JavaDoc getUrl(String JavaDoc file) throws java.net.MalformedURLException JavaDoc {
133         return new File JavaDoc(getDataDir(),file).toURL();
134     }
135     
136     private File JavaDoc getFile(String JavaDoc file) {
137         return new File JavaDoc(getDataDir(),file);
138     }
139     
140     private void compareResults(int testNumber) {
141         
142         if (!comp(SERVICE_NAMES[testNumber].length,serviceNames.size())) return;
143         if (!comp(SERVICE_JAVA_NAMES[testNumber].length,serviceJavaNames.size())) return;
144         
145         if (!comp(PORT_NAMES[testNumber].length,portNames.size())) return;
146         if (!comp(PORT_JAVA_NAMES[testNumber].length,portJavaNames.size())) return;
147         if (!comp(PORT_GETTERS[testNumber].length,portGetters.size())) return;
148         
149         if (!comp(OP_NAMES[testNumber].length,opNames.size())) return;
150         if (!comp(OP_JAVA_NAMES[testNumber].length,opJavaNames.size())) return;
151         if (!comp(OP_TYPES[testNumber].length,opTypes.size())) return;
152         
153         if (!comp(PARAM_NAMES[testNumber].length,paramNames.size())) return;
154         if (!comp(PARAM_TYPES[testNumber].length,paramTypes.size())) return;
155         
156         for (int i=0;i<SERVICE_NAMES[testNumber].length;i++) {
157             if (!comp(SERVICE_NAMES[testNumber][i],serviceNames.get(i))) return;
158         }
159         for (int i=0;i<SERVICE_JAVA_NAMES[testNumber].length;i++) {
160             if (!comp(SERVICE_JAVA_NAMES[testNumber][i],serviceJavaNames.get(i))) return;
161         }
162         
163         for (int i=0;i<PORT_NAMES[testNumber].length;i++) {
164             if (!comp(PORT_NAMES[testNumber][i],portNames.get(i))) return;
165         }
166         for (int i=0;i<PORT_JAVA_NAMES[testNumber].length;i++) {
167             if (!comp(PORT_JAVA_NAMES[testNumber][i],portJavaNames.get(i))) return;
168         }
169         for (int i=0;i<PORT_GETTERS[testNumber].length;i++) {
170             if (!comp(PORT_GETTERS[testNumber][i],portGetters.get(i))) return;
171         }
172         
173         for (int i=0;i<OP_NAMES[testNumber].length;i++) {
174             if (!comp(OP_NAMES[testNumber][i],opNames.get(i))) return;
175         }
176         for (int i=0;i<OP_JAVA_NAMES[testNumber].length;i++) {
177             if (!comp(OP_JAVA_NAMES[testNumber][i],opJavaNames.get(i))) return;
178         }
179         for (int i=0;i<OP_TYPES[testNumber].length;i++) {
180             if (!comp(OP_TYPES[testNumber][i],opTypes.get(i))) return;
181         }
182         
183         for (int i=0;i<PARAM_NAMES[testNumber].length;i++) {
184             if (!comp(PARAM_NAMES[testNumber][i],paramNames.get(i))) return;
185         }
186         for (int i=0;i<PARAM_TYPES[testNumber].length;i++) {
187             if (!comp(PARAM_TYPES[testNumber][i],paramTypes.get(i))) return;
188         }
189     }
190     
191     private void initLists() {
192         serviceNames = new ArrayList JavaDoc<String JavaDoc>();
193         serviceJavaNames = new ArrayList JavaDoc<String JavaDoc>();
194         portNames = new ArrayList JavaDoc<String JavaDoc>();
195         portJavaNames = new ArrayList JavaDoc<String JavaDoc>();
196         portGetters = new ArrayList JavaDoc<String JavaDoc>();
197         opNames = new ArrayList JavaDoc<String JavaDoc>();
198         opJavaNames = new ArrayList JavaDoc<String JavaDoc>();
199         opTypes = new ArrayList JavaDoc<String JavaDoc>();
200         paramNames = new ArrayList JavaDoc<String JavaDoc>();
201         paramTypes = new ArrayList JavaDoc<String JavaDoc>();
202     }
203     
204     private void initResults() {
205         expectedValue=null;realValue=null;
206         numberOfEvents=0;
207     }
208     
209     private boolean comp(int x, int y) {
210         if (x!=y) {
211             expectedValue = new Integer JavaDoc(x);
212             realValue = new Integer JavaDoc(y);
213             Thread.dumpStack();
214             return false;
215         } else {
216             return true;
217         }
218     }
219
220     private boolean comp(Object JavaDoc x, Object JavaDoc y) {
221         if (!x.equals(y)) {
222             expectedValue = x;
223             realValue = y;
224             return false;
225         } else {
226             return true;
227         }
228     }
229 }
230
Popular Tags