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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028:
029: package org.netbeans.modules.websvc.manager;
030:
031: import java.io.File;
032: import java.net.URL;
033: import org.netbeans.junit.NbTestCase;
034: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel;
035: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModeler;
036: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelerFactory;
037: import org.netbeans.modules.websvc.manager.model.WebServiceData;
038: import org.netbeans.modules.websvc.manager.model.WebServiceGroup;
039: import org.netbeans.modules.websvc.manager.model.WebServiceListModel;
040: import org.netbeans.modules.websvc.manager.test.DialogDisplayerNotifier;
041: import org.netbeans.modules.websvc.manager.test.SetupData;
042: import org.netbeans.modules.websvc.manager.test.SetupUtil;
043: import org.openide.DialogDisplayer;
044:
045: /**
046: *
047: * @author quynguyen
048: */
049: public class WebServiceManagerTest extends NbTestCase {
050: private static final String TEST_WSDL_REMOTE = "http://www.webservicemart.com/uszip.asmx.wsdl";
051:
052: private SetupData testData;
053:
054: public WebServiceManagerTest(String testName) {
055: super (testName);
056: }
057:
058: @Override
059: protected void setUp() throws Exception {
060: super .setUp();
061: clearWorkDir();
062: File testRoot = getWorkDir();
063:
064: testData = SetupUtil.commonSetUp(testRoot);
065:
066: assertTrue("Could not set up user directory for test", testData
067: .getWebsvcHome().exists());
068: assertTrue("WSDL copy failed", testData.getLocalWsdlFile()
069: .exists());
070: assertTrue("Catalog copy failed", testData
071: .getLocalCatalogFile().exists());
072:
073: File endorsedDir = null;
074: String endorsedDirs = System.getProperty("java.endorsed.dirs");
075: if (endorsedDirs != null) {
076: endorsedDir = new File(endorsedDirs);
077: }
078:
079: DialogDisplayer dd = DialogDisplayer.getDefault();
080: assertTrue("DialogDisplayer not set correctly",
081: dd instanceof DialogDisplayerNotifier);
082: assertTrue(
083: "java.endorsed.dirs needs to be set to a valid location",
084: endorsedDir != null && endorsedDir.exists());
085: }
086:
087: @Override
088: protected void tearDown() throws Exception {
089: super .tearDown();
090:
091: SetupUtil.commonTearDown();
092: }
093:
094: public void testLocalWebServiceData() throws Exception {
095: System.out.println("localWebServiceAdd");
096: WebServiceListModel listModel = WebServiceListModel
097: .getInstance();
098:
099: URL wsdlUrl = testData.getLocalOriginalWsdl().toURI().toURL();
100:
101: WebServiceData wsData = createWebServiceData(testData
102: .getLocalWsdlFile(), wsdlUrl.toExternalForm(),
103: "websvc", testData.getLocalCatalogFile(),
104: WebServiceListModel.DEFAULT_GROUP);
105:
106: WebServiceGroup defaultGroup = listModel
107: .getWebServiceGroup(WebServiceListModel.DEFAULT_GROUP);
108:
109: assertEquals(
110: "WebServiceData stored in WebServiceListModel not retrieved",
111: wsData, listModel.getWebService(wsData.getId()));
112: assertTrue("WebServiceGroup does not link to WebServiceData",
113: defaultGroup.getWebServiceIds()
114: .contains(wsData.getId()));
115:
116: WebServiceManager.getInstance().removeWebService(wsData);
117: assertFalse("WSDL not deleted as expected", testData
118: .getLocalWsdlFile().exists());
119: assertFalse("Catalog file not deleted as expected", testData
120: .getLocalCatalogFile().exists()
121: || testData.getLocalCatalogFile().getParentFile()
122: .exists());
123: assertNull("WebServiceData not removed from list model",
124: listModel.getWebService(wsData.getId()));
125: assertTrue("Original WSDL deleted improperly", testData
126: .getLocalOriginalWsdl().exists());
127: }
128:
129: public void testRemoteWebServiceData() throws Exception {
130: System.out.println("remoteWebServiceAdd");
131: WebServiceListModel listModel = WebServiceListModel
132: .getInstance();
133:
134: WebServiceData wsData = createWebServiceData(testData
135: .getLocalWsdlFile(), TEST_WSDL_REMOTE, "websvc",
136: testData.getLocalCatalogFile(),
137: WebServiceListModel.DEFAULT_GROUP);
138:
139: WebServiceGroup defaultGroup = listModel
140: .getWebServiceGroup(WebServiceListModel.DEFAULT_GROUP);
141:
142: assertEquals(
143: "WebServiceData stored in WebServiceListModel not retrieved",
144: wsData, listModel.getWebService(wsData.getId()));
145: assertTrue("WebServiceGroup does not link to WebServiceData",
146: defaultGroup.getWebServiceIds()
147: .contains(wsData.getId()));
148:
149: WebServiceManager.getInstance().removeWebService(wsData);
150: assertFalse("WSDL not deleted as expected", testData
151: .getLocalWsdlFile().exists());
152: assertFalse("Catalog file not deleted as expected", testData
153: .getLocalCatalogFile().exists()
154: || testData.getLocalCatalogFile().getParentFile()
155: .exists());
156: assertNull("WebServiceData not removed from list model",
157: listModel.getWebService(wsData.getId()));
158: }
159:
160: public WebServiceData createWebServiceData(File wsdlFile,
161: String original, String packageName, File catalog,
162: String groupId) throws Exception {
163: WebServiceData wsData = new WebServiceData(wsdlFile
164: .getAbsolutePath(), original, groupId);
165:
166: wsData.setPackageName(packageName);
167: wsData.setCatalog(catalog.getAbsolutePath());
168:
169: WebServiceManager.getInstance().addWebService(wsData, false);
170:
171: assertTrue("WebServiceData state not correct", wsData
172: .getState().equals(WebServiceData.State.WSDL_RETRIEVED));
173: return wsData;
174: }
175:
176: /**
177: * Test of addWebService method, of class WebServiceManager.
178: */
179: /*
180: public void testAddWebService() throws Exception {
181: System.out.println("addWebService");
182:
183: File localWsdlFile = testData.getLocalWsdlFile();
184: File localOriginalWsdl = testData.getLocalOriginalWsdl();
185:
186: String packageName = "websvc";
187: String groupId = WebServiceListModel.DEFAULT_GROUP;
188: URL wsdlUrl = localWsdlFile.toURI().toURL();
189: String originalWsdl = localOriginalWsdl.toURI().toURL().toExternalForm();
190:
191: DialogDisplayerNotifier dd = (DialogDisplayerNotifier)DialogDisplayer.getDefault();
192:
193: // Objects used for multi-thread success/failure notification
194: final TestFlag failBeforeCompile = new TestFlag();
195: final TestFlag failAfterAdd = new TestFlag();
196: final TestFlag successAfterCompile = new TestFlag();
197:
198: dd.addListener(new WebServiceManagerListener() {
199: public void eventFired(WebServiceManagerEvent event) {
200: failBeforeCompile.setValue(true);
201: failBeforeCompile.msg = event.getData();
202: }
203: });
204:
205: WebServiceListModel.getInstance().addDefaultGroupListener(new WebServiceGroupListener() {
206:
207: public void webServiceAdded(WebServiceGroupEvent groupEvent) {
208: WebServiceData wsData = WebServiceListModel.getInstance().getWebService(groupEvent.getWebServiceId());
209: wsData.addWebServiceDataListener(new WebServiceDataListener() {
210: public void webServiceCompiled(WebServiceDataEvent evt) {
211: successAfterCompile.setValue(true);
212: }
213: });
214: }
215:
216: public void webServiceRemoved(WebServiceGroupEvent groupEvent) {
217: failAfterAdd.setValue(true);
218: }
219: });
220:
221: File localCatalogFile = testData.getLocalCatalogFile();
222: final int TIMEOUT = 30000;
223:
224: WsdlModelListenerImpl listener = new WsdlModelListenerImpl(localWsdlFile, originalWsdl, packageName, groupId, localCatalogFile);
225: WsdlModeler wsdlModeler = WsdlModelerFactory.getDefault().getWsdlModeler(wsdlUrl);
226: listener.setWsdlModeler(wsdlModeler);
227: wsdlModeler.setPackageName(packageName);
228: wsdlModeler.generateWsdlModel(listener);
229:
230: // hack to wait until the processes are done
231: int counter = 0;
232: do {
233: counter += 1000;
234: Thread.sleep(1000);
235: }while ( ! (failBeforeCompile.getValue() || failAfterAdd.getValue() || successAfterCompile.getValue()) &&
236: counter < TIMEOUT);
237:
238: assertFalse("Failure notified with the following message: " + failBeforeCompile.msg, failBeforeCompile.getValue());
239: assertFalse("Failed during client creation", failAfterAdd.getValue());
240: assertTrue("Test timed out after " + counter + "ms", counter < TIMEOUT);
241: }
242:
243: private static final class TestFlag {
244: private boolean value = false;
245: private String msg = "";
246:
247: public void setValue(boolean v) {
248: this.value = v;
249: }
250:
251: public boolean getValue() {
252: return value;
253: }
254: }
255: */
256: }
|