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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.visualweb.websvcmgr.consumer;
041:
042: import java.io.File;
043: import java.io.FileInputStream;
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.LinkedList;
047: import java.util.List;
048: import java.util.Properties;
049: import org.netbeans.junit.NbTestCase;
050: import org.netbeans.modules.visualweb.websvcmgr.test.RunCommand;
051: import org.netbeans.modules.websvc.manager.WebServiceManager;
052: import org.netbeans.modules.websvc.manager.api.WebServiceDescriptor;
053: import org.netbeans.modules.websvc.manager.model.WebServiceData;
054: import org.netbeans.modules.websvc.manager.model.WebServiceListModel;
055: import org.netbeans.modules.websvc.manager.test.SetupData;
056: import org.netbeans.modules.websvc.manager.test.SetupUtil;
057: import org.openide.modules.InstalledFileLocator;
058:
059: /**
060: *
061: * @author quynguyen
062: */
063: public class DesignerWebServiceExtImplTest extends NbTestCase {
064: static final String CLIENT_JAR_PATH = "/org/netbeans/modules/visualweb/websvcmgr/test/resources/USZip.jar";
065: static final String SRC_JAR_PATH = "/org/netbeans/modules/visualweb/websvcmgr/test/resources/USZip-src.jar";
066:
067: private SetupData data;
068: private WebServiceData wsData;
069:
070: public DesignerWebServiceExtImplTest(String testName) {
071: super (testName);
072: }
073:
074: @Override
075: protected void setUp() throws Exception {
076: super .setUp();
077:
078: String javaHome = System.getProperty("java.home");
079: assertTrue(
080: "This unit test will not complete successfully without the Java SDK",
081: new File(javaHome, "../lib/tools.jar").exists());
082:
083: data = SetupUtil.commonSetUp(getWorkDir());
084: wsData = new WebServiceData(data.getLocalWsdlFile()
085: .getAbsolutePath(), data.getLocalOriginalWsdl().toURI()
086: .toURL().toExternalForm(),
087: WebServiceListModel.DEFAULT_GROUP);
088:
089: wsData.setPackageName("websvc");
090: wsData.setCatalog(data.getLocalCatalogFile().getAbsolutePath());
091: WebServiceManager.getInstance().addWebService(wsData, false);
092:
093: wsData.setCompiled(true);
094: wsData.setState(WebServiceData.State.WSDL_SERVICE_COMPILED);
095:
096: File jarsDir = new File(data.getWebsvcHome(), "USZip/jaxws");
097: jarsDir.mkdirs();
098:
099: File clientJar = new File(jarsDir, "USZip.jar");
100: File srcJar = new File(jarsDir, "USZip-src.jar");
101:
102: SetupUtil.retrieveURL(clientJar,
103: DesignerWebServiceExtImplTest.class
104: .getResource(CLIENT_JAR_PATH));
105: SetupUtil.retrieveURL(srcJar,
106: DesignerWebServiceExtImplTest.class
107: .getResource(SRC_JAR_PATH));
108:
109: assertTrue("JAX-WS proxy jar was not copied", clientJar
110: .exists());
111: assertTrue("JAX-WS source jar was not copied", srcJar.exists());
112:
113: WebServiceDescriptor descriptor = new WebServiceDescriptor(
114: wsData.getName(), wsData.getPackageName(),
115: WebServiceDescriptor.JAX_WS_TYPE, new File(wsData
116: .getWsdlFile()).toURI().toURL(), new File(
117: jarsDir, "descriptor.xml"), wsData
118: .getWsdlService());
119:
120: descriptor.addJar("USZip.jar",
121: WebServiceDescriptor.JarEntry.PROXY_JAR_TYPE);
122: descriptor.addJar("USZip-src.jar",
123: WebServiceDescriptor.JarEntry.SRC_JAR_TYPE);
124:
125: wsData.setJaxWsDescriptor(descriptor);
126: wsData.setJaxWsDescriptorPath(descriptor.getXmlDescriptor());
127: }
128:
129: @Override
130: protected void tearDown() throws Exception {
131: super .tearDown();
132:
133: WebServiceManager.getInstance().removeWebService(wsData);
134: SetupUtil.commonTearDown();
135: }
136:
137: public void testCreateClientClasses() throws Exception {
138: DesignerWebServiceExtImpl impl = new DesignerWebServiceExtImpl();
139: impl.createClientClasses(wsData.getJaxWsDescriptor());
140:
141: Properties antProps = impl.createAntProperties(wsData
142: .getJaxWsDescriptor());
143: Properties buildProps = new Properties();
144: buildProps.load(new FileInputStream(new File(System
145: .getProperty("netbeans.user"), "build.properties")));
146:
147: File proxyJar = new File(data.getWebsvcHome(),
148: "USZip/jaxws/USZip.jar");
149: List<String> cp = buildCompileClasspath(proxyJar, buildProps,
150: antProps);
151: File endorsedDir = new File(System
152: .getProperty("java.endorsed.dirs"));
153: File srcDir = new File(data.getWebsvcHome(), "USZip/jaxws/src");
154:
155: boolean result = compile(srcDir, cp, endorsedDir);
156: assertTrue(
157: "Web service client and designtime classes failed to compile",
158: result);
159:
160: File soap12Client = new File(srcDir,
161: "websvc/USZipSoap12Client.class");
162: File validateZip12 = new File(srcDir,
163: "websvc/USZipSoap12ValidateZip.class");
164:
165: File soapClient = new File(srcDir,
166: "websvc/USZipSoapClient.class");
167: File validateZip = new File(srcDir,
168: "websvc/USZipSoapValidateZip.class");
169:
170: assertTrue("USZipSoap12Client.java not compiled", soap12Client
171: .exists());
172: assertTrue("USZipSoapClient.java not compiled", soapClient
173: .exists());
174: assertTrue("USZipSoap12ValidateZip.java not compiled",
175: validateZip12.exists());
176: assertTrue("USZipSoapValidateZip.java not compiled",
177: validateZip.exists());
178: }
179:
180: private List<String> buildCompileClasspath(File proxyJar,
181: Properties... props) {
182: ArrayList<String> cp = new ArrayList<String>();
183:
184: // ${java.home}/../lib/tools.jar
185: cp.add(new File(System.getProperty("java.home"),
186: "../lib/tools.jar").getAbsolutePath());
187: cp.add(getProperty("libs.jaxws21.classpath", props));
188: cp.add(getProperty("designtime.classpath", props));
189:
190: File dp = InstalledFileLocator.getDefault().locate(
191: "modules/ext/dataprovider.jar", null, false);
192: cp.add(dp.getAbsolutePath());
193: cp.add(proxyJar.getAbsolutePath());
194: return cp;
195:
196: }
197:
198: private String getProperty(String propName, Properties... props) {
199: for (int i = 0; i < props.length; i++) {
200: String prop = props[i].getProperty(propName);
201: if (prop != null && prop.length() > 0) {
202: return prop;
203: }
204: }
205:
206: throw new IllegalArgumentException("Property " + propName
207: + " not found");
208: }
209:
210: private boolean compile(File srcPath, List<String> classpath,
211: File endorsedDir) {
212: ArrayList<String> cmdArray = new ArrayList<String>();
213:
214: cmdArray.add(new File(System.getProperty("java.home"),
215: "../bin/javac").getAbsolutePath());
216: cmdArray.add("-endorseddirs");
217: cmdArray.add(endorsedDir.getAbsolutePath());
218:
219: cmdArray.add("-classpath");
220:
221: StringBuffer cpBuffer = new StringBuffer();
222: String sep = System.getProperty("path.separator");
223: for (String cpElement : classpath) {
224: cpBuffer.append(cpElement + sep);
225: }
226: cpBuffer.append(srcPath.getAbsolutePath());
227:
228: cmdArray.add(cpBuffer.toString());
229:
230: List<String> javaFiles = new LinkedList<String>();
231: getJavaFiles(srcPath, javaFiles);
232:
233: for (String javaFile : javaFiles) {
234: cmdArray.add(javaFile);
235: }
236:
237: try {
238: RunCommand runCommand = new RunCommand();
239: runCommand.execute(cmdArray.toArray(new String[cmdArray
240: .size()]));
241: int returnStatus = runCommand.getReturnStatus();
242: return returnStatus == 0;
243: } catch (IOException ex) {
244: return false;
245: }
246: }
247:
248: private void getJavaFiles(File srcDir, List<String> files) {
249: if (!srcDir.isDirectory()) {
250: return;
251: } else {
252: File[] subFiles = srcDir.listFiles();
253: for (int i = 0; i < subFiles.length; i++) {
254: if (subFiles[i].isDirectory()) {
255: getJavaFiles(subFiles[i], files);
256: } else if (subFiles[i].getName().endsWith(".java")) {
257: files.add(subFiles[i].getAbsolutePath());
258: }
259: }
260: }
261: }
262:
263: }
|