KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.tools.ws.processor.model.Model;
23 import com.sun.tools.ws.processor.model.Service;
24 import java.io.File JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import junit.framework.*;
29 import org.netbeans.junit.AssertionFailedErrorException;
30 import org.netbeans.junit.NbTestCase;
31 import java.util.*;
32 import org.openide.util.RequestProcessor;
33
34 /**
35  *
36  * @author mkuchtiak
37  */

38 public class WSDLModelTest extends NbTestCase {
39     
40     private List<String JavaDoc> serviceNames, portNames, opNames, opTypes, paramNames, paramTypes;
41     
42     private Object JavaDoc expectedValue, realValue;
43     private int numberOfEvents;
44     
45     private static final String JavaDoc[][] SERVICE_NAMES={{"Test2WS"},{"AddNumbers"},{"AddNumbers1"}};
46     private static final String JavaDoc[][] PORT_NAMES={{"FooPortTypePort"},{"AddNumbersSEIPort"},{"AddNumbersSEIPort"}};
47     private static final String JavaDoc[][] OP_NAMES={{"FooOperation"},{"add"},{"add"}};
48     private static final String JavaDoc[][] OP_TYPES={{"org.netbeans.xml.examples.targetns.ItemType"},{"int"},{"int"}};
49     private static final String JavaDoc[][] PARAM_NAMES={{"id"},{"x","y"},{"x","y"}};
50     private static final String JavaDoc[][] PARAM_TYPES={{"java.lang.String"},{"int","int"},{"int","int"}};
51     
52     public WSDLModelTest(String JavaDoc testName) {
53         super(testName);
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57     }
58
59     protected void tearDown() throws Exception JavaDoc {
60     }
61     public void testAsynchronousModelCreation() throws java.net.MalformedURLException JavaDoc {
62         System.out.println("Test 1 : Asynchronous Model Creation");
63         initResults();
64         final WsdlModeler wsdlModeler = WsdlModelerFactory.getDefault().getWsdlModeler(getUrl("T2.wsdl"));
65         assertNotNull("WsdlModelerFactory failed to create object", wsdlModeler);
66         for (int i=0;i<1000;i++) {
67             final int j=i+1;
68             wsdlModeler.generateWsdlModel(new WsdlModelListener() {
69                     public void modelCreated(WsdlModel model) {
70                         System.out.println("T2("+String.valueOf(j)+") model Created "+"THREAD:"+Thread.currentThread().getName());
71                         compareWsdl(model,0);
72                     }
73             });
74         }
75
76         wsdlModeler.task.waitFinished();
77         System.out.println("Test 1 : FINISHED");
78         if (expectedValue!=null) assertEquals (expectedValue,realValue);
79         assertEquals(1000,numberOfEvents);
80     }
81     public void testSynchronousModelCreation() throws java.net.MalformedURLException JavaDoc {
82         System.out.println("Test 2 : Synchronous Model Creation");
83         initResults();
84         final WsdlModeler wsdlModeler = WsdlModelerFactory.getDefault().getWsdlModeler(getUrl("AddNumbers.wsdl"));
85         assertNotNull("WsdlModelerFactory failed to create object", wsdlModeler);
86         WsdlModel wsdlModel = wsdlModeler.getAndWaitForWsdlModel();
87         assertNotNull("WsdlModeler failed to create WSDL Model", wsdlModel);
88         compareWsdl(wsdlModel,1);
89         if (expectedValue!=null) assertEquals (expectedValue,realValue);
90     }
91     
92     public void testInvalidWsdl() throws java.net.MalformedURLException JavaDoc {
93         System.out.println("Test 3 : Invalid WSDL");
94         final WsdlModeler wsdlModeler = WsdlModelerFactory.getDefault().getWsdlModeler(getUrl("T2_invalid.wsdl"));
95         assertNotNull("WsdlModelerFactory failed to create object", wsdlModeler);
96         WsdlModel wsdlModel = wsdlModeler.getAndWaitForWsdlModel();
97         assertNull("WsdlModeler should fail for this WSDL",wsdlModel);
98         System.out.println("TEST 3 Exception : "+wsdlModeler.getCreationException());
99         assertNotNull("Exception shouldn't be null", wsdlModeler.getCreationException());
100     }
101     
102     public void testReloadWsdl() throws java.net.MalformedURLException JavaDoc {
103         System.out.println("Test 4 : Reload Test WSDL");
104         initResults();
105         final WsdlModeler wsdlModeler = WsdlModelerFactory.getDefault().getWsdlModeler(getUrl("AddNumbers.wsdl"));
106         assertNotNull("WsdlModelerFactory failed to create object", wsdlModeler);
107         wsdlModeler.generateWsdlModel(new WsdlModelListener() {
108                 public void modelCreated(WsdlModel model) {
109                     System.out.println("AddNumbers(1) model Created, THREAD:"+Thread.currentThread().getName());
110                     compareWsdl(model,1);
111                 }
112         });
113         wsdlModeler.generateWsdlModel(new WsdlModelListener() {
114                 public void modelCreated(WsdlModel model) {
115                     System.out.println("AddNumbers(2) model Created, THREAD:"+Thread.currentThread().getName());
116                     compareWsdl(model,1);
117                 }
118         });
119         wsdlModeler.task.waitFinished();
120         Thread.currentThread().yield();
121         wsdlModeler.setWsdlUrl(getUrl("AddNumbers1.wsdl"));
122         wsdlModeler.generateWsdlModel(new WsdlModelListener() {
123                 public void modelCreated(WsdlModel model) {
124                     System.out.println("AddNumbers(3) model Created, THREAD:"+Thread.currentThread().getName());
125                     compareWsdl(model,2);
126                 }
127         },true);
128         wsdlModeler.task.waitFinished();
129         System.out.println("Test 4 : FINISHED");
130         if (expectedValue!=null) assertEquals (expectedValue,realValue);
131         assertEquals(3,numberOfEvents);
132     }
133
134     public void testGarbageCollection() {
135         System.out.println("Test 4 : Garbage Collection Test");
136         try {Thread.sleep(100);} catch (InterruptedException JavaDoc ex) {}
137         System.gc();System.gc();System.gc();
138         WeakHashMap<URL JavaDoc, WeakReference JavaDoc<WsdlModeler>> modelers = WsdlModelerFactory.getDefault().modelers;
139         assertNotNull(modelers);
140         System.out.println("TEST 4 - modelers.size = "+modelers.size());
141         
142         URL JavaDoc[] urls = new URL JavaDoc[modelers.keySet().size()];
143         
144         modelers.keySet().toArray(urls);
145         for (int i=0;i<urls.length;i++) {
146             if (urls[i]!=null) System.out.println("value = " + modelers.get(urls[i]).get());
147             assertNull(urls[i]);
148         }
149     }
150     
151     private void compareWsdl(WsdlModel model, int testNumber) {
152         if (expectedValue!=null || realValue!=null) return;
153         initLists();
154         List<WsdlService> services = model.getServices();
155         for (Iterator<WsdlService> it = services.iterator(); it.hasNext();) {
156             WsdlService s = it.next();
157             serviceNames.add(s.getName());
158             List<WsdlPort> ports = s.getPorts();
159             for (Iterator<WsdlPort> it1 = ports.iterator(); it1.hasNext();) {
160                 WsdlPort port = it1.next();
161                 portNames.add(port.getName());
162                 List<WsdlOperation> operations = port.getOperations();
163                 for (Iterator<WsdlOperation> it2 = operations.iterator(); it2.hasNext();) {
164                     WsdlOperation op = it2.next();
165                     opNames.add(op.getName());
166                     opTypes.add(op.getReturnTypeName());
167                     List<WsdlParameter> parameters = op.getParameters();
168                     for (Iterator<WsdlParameter> it3 = parameters.iterator(); it3.hasNext();) {
169                         WsdlParameter param = it3.next();
170                         paramNames.add(param.getName());
171                         paramTypes.add(param.getTypeName());
172                     }
173                 }
174             }
175         }
176         compareResults(testNumber);
177         numberOfEvents++;
178     }
179     
180     private URL JavaDoc getUrl(String JavaDoc file) throws MalformedURLException JavaDoc {
181         return new File JavaDoc(getDataDir(),file).toURL();
182     }
183     
184     private void compareResults(int testNumber) {
185         
186         if (!comp(SERVICE_NAMES[testNumber].length,serviceNames.size())) return;
187         if (!comp(PORT_NAMES[testNumber].length,portNames.size())) return;
188         if (!comp(OP_NAMES[testNumber].length,opNames.size())) return;
189         if (!comp(OP_TYPES[testNumber].length,opTypes.size())) return;
190         if (!comp(PARAM_NAMES[testNumber].length,paramNames.size())) return;
191         if (!comp(PARAM_TYPES[testNumber].length,paramTypes.size())) return;
192         for (int i=0;i<SERVICE_NAMES[testNumber].length;i++) {
193             if (!comp(SERVICE_NAMES[testNumber][i],serviceNames.get(i))) return;
194         }
195         for (int i=0;i<PORT_NAMES[testNumber].length;i++) {
196             if (!comp(PORT_NAMES[testNumber][i],portNames.get(i))) return;
197         }
198         for (int i=0;i<OP_NAMES[testNumber].length;i++) {
199             if (!comp(OP_NAMES[testNumber][i],opNames.get(i))) return;
200         }
201         for (int i=0;i<OP_TYPES[testNumber].length;i++) {
202             if (!comp(OP_TYPES[testNumber][i],opTypes.get(i))) return;
203         }
204         for (int i=0;i<PARAM_NAMES[testNumber].length;i++) {
205             if (!comp(PARAM_NAMES[testNumber][i],paramNames.get(i))) return;
206         }
207         for (int i=0;i<PARAM_TYPES[testNumber].length;i++) {
208             if (!comp(PARAM_TYPES[testNumber][i],paramTypes.get(i))) return;
209         }
210     }
211     
212     private void initLists() {
213         serviceNames = new ArrayList<String JavaDoc>();
214         portNames = new ArrayList<String JavaDoc>();
215         opNames = new ArrayList<String JavaDoc>();
216         opTypes = new ArrayList<String JavaDoc>();
217         paramNames = new ArrayList<String JavaDoc>();
218         paramTypes = new ArrayList<String JavaDoc>();
219     }
220     
221     private void initResults() {
222         expectedValue=null;realValue=null;
223         numberOfEvents=0;
224     }
225     
226     private boolean comp(int x, int y) {
227         if (x!=y) {
228             expectedValue = new Integer JavaDoc(x);
229             realValue = new Integer JavaDoc(y);
230             return false;
231         } else {
232             return true;
233         }
234     }
235
236     private boolean comp(Object JavaDoc x, Object JavaDoc y) {
237         if (!x.equals(y)) {
238             expectedValue = x;
239             realValue = y;
240             return false;
241         } else {
242             return true;
243         }
244     }
245 }
246
Popular Tags