visualweb.websvcmgr/test/unit/src/org/netbeans/modules/websvc/manager/test/SetupUtil.java
Tests at least should compile now.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * Portions Copyrighted 2007 Sun Microsystems, Inc.
32 package org.netbeans.modules.websvc.manager.test;
35 import java.io.FileFilter;
36 import java.io.FileInputStream;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
41 import java.nio.channels.FileChannel;
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Properties;
45 import java.util.jar.Manifest;
46 import org.netbeans.junit.MockServices;
47 import org.netbeans.modules.websvc.manager.WebServiceManager;
48 import org.openide.DialogDisplayer;
49 import org.openide.filesystems.FileSystem;
50 import org.openide.filesystems.FileUtil;
51 import org.openide.filesystems.LocalFileSystem;
52 import org.openide.filesystems.MultiFileSystem;
53 import org.openide.filesystems.Repository;
54 import org.openide.filesystems.XMLFileSystem;
55 import org.openide.modules.InstalledFileLocator;
56 import org.openide.util.Lookup;
57 import org.openide.util.LookupEvent;
58 import org.openide.util.LookupListener;
59 import org.openide.util.NbCollections;
65 public class SetupUtil {
66 private static final String WORKDIR_SPACES = "user directory";
67 private static final String WORKDIR = "userdirectory";
68 private static final String TEST_WSDL = "/org/netbeans/modules/websvc/manager/resources/uszip-asmx-catalog/www.webservicemart.com/uszip.asmx.wsdl";
69 private static final String TEST_CATALOG = "/org/netbeans/modules/websvc/manager/resources/uszip-asmx-catalog/catalog.xml";
70 private static final String CATALOG_FILE = "uszip-asmx-catalog/catalog.xml";
71 private static final String WSDL_FILE = "uszip-asmx-catalog/www.webservicemart.com/uszip.asmx.wsdl";
73 private static final String ENDORSED_REF = "modules/ext/jaxws21/api/jaxws-api.jar";
74 private static final String JAXWS_LIB_PROPERTY = "libs.jaxws21.classpath";
76 public static SetupData commonSetUp(File workingDir) throws Exception {
77 SetupData data = new SetupData();
79 File userDir = new File(workingDir.getParentFile(),
80 System.getProperty("os.name").startsWith("Windows") ? WORKDIR_SPACES : WORKDIR);
81 System.getProperties().setProperty("netbeans.user", userDir.getAbsolutePath());
83 File websvcHome = new File(WebServiceManager.WEBSVC_HOME);
84 data.setWebsvcHome(websvcHome);
86 File websvcUserDir = new File(WebServiceManager.WEBSVC_HOME);
87 websvcUserDir.mkdirs();
89 File wsdlFile = new File(websvcUserDir, WSDL_FILE);
90 File catalogFile = new File(websvcUserDir, CATALOG_FILE);
92 retrieveURL(wsdlFile, SetupUtil.class.getResource(TEST_WSDL));
93 retrieveURL(catalogFile, SetupUtil.class.getResource(TEST_CATALOG));
95 copy(wsdlFile, workingDir);
97 data.setLocalWsdlFile(wsdlFile);
98 data.setLocalCatalogFile(catalogFile);
99 data.setLocalOriginalWsdl(new File(workingDir, wsdlFile.getName()));
101 MainFS fs = new MainFS();
102 fs.setConfigRootDir(websvcHome.getParentFile());
103 TestRepository.defaultFileSystem = fs;
105 MockServices.setServices(DialogDisplayerNotifier.class, InstalledFileLocatorImpl.class, TestRepository.class);
107 InstalledFileLocatorImpl locator = (InstalledFileLocatorImpl)Lookup.getDefault().lookup(InstalledFileLocator.class);
108 locator.setUserConfigRoot(websvcHome.getParentFile());
110 File targetBuildProperties = new File(websvcUserDir.getParentFile().getParentFile(), "build.properties");
111 generatePropertiesFile(targetBuildProperties);
116 public static void commonTearDown() throws Exception {
117 DialogDisplayer dd = DialogDisplayer.getDefault();
118 if (dd instanceof DialogDisplayerNotifier) {
119 ((DialogDisplayerNotifier)dd).removeAllListeners();
122 MockServices.setServices();
125 public static void copy(File src, File target) throws Exception {
127 File targetFile = new File(target, src.getName());
129 FileInputStream is = new FileInputStream(src);
130 FileOutputStream os = new FileOutputStream(targetFile);
132 FileChannel inputChannel = is.getChannel();
133 FileChannel outputChannel = os.getChannel();
135 inputChannel.transferTo(0, inputChannel.size(), outputChannel);
136 inputChannel.close();
137 outputChannel.close();
139 File newDir = new File(target, src.getName());
142 File[] dirFiles = src.listFiles();
143 if (dirFiles != null) {
144 for (int i = 0; i < dirFiles.length; i++) {
145 copy(dirFiles[i], newDir);
151 public static void retrieveURL(File targetFile, URL url) throws IOException {
152 targetFile.getParentFile().mkdirs();
153 FileOutputStream fos = new FileOutputStream(targetFile);
154 byte[] readBuffer = new byte[1024];
156 InputStream is = url.openStream();
158 while ( (bytesRead = is.read(readBuffer, 0, 1024)) > 0) {
159 fos.write(readBuffer, 0, bytesRead);
166 private static void generatePropertiesFile(File target) throws IOException {
167 String separator = System.getProperty("path.separator");
168 File apiBase = InstalledFileLocator.getDefault().locate(ENDORSED_REF, null, true).getParentFile();
169 File jaxWsBase = apiBase.getParentFile();
171 FileFilter jarFilter = new FileFilter() {
172 public boolean accept(File pathname) {
173 return pathname.isFile() && pathname.getName().endsWith(".jar");
177 File[] apiJars = apiBase.listFiles(jarFilter);
178 File[] implJars = jaxWsBase.listFiles(jarFilter);
180 Properties result = new Properties();
181 StringBuffer classpath = new StringBuffer();
183 for (int i = 0; i < apiJars.length; i++) {
184 String pathElement = apiJars[i].getAbsolutePath() + separator;
185 classpath.append(pathElement);
188 for (int i = 0; i < implJars.length; i++) {
189 classpath.append(implJars[i].getAbsolutePath());
190 if (i != implJars.length - 1) {
191 classpath.append(separator);
195 result.setProperty(JAXWS_LIB_PROPERTY, classpath.toString());
197 FileOutputStream fos = new FileOutputStream(target);
198 result.store(fos, "build.properties file");
201 public static final class TestRepository extends Repository {
202 static FileSystem defaultFileSystem = null;
204 public TestRepository() {
205 super(defaultFileSystem);
209 // Taken from org.openide.filesystems.ExternalUtil to allow layer files to be
210 // loaded into the default filesystem (since core/startup is in the classpath
211 // and registers a default Repository that we do not want)
212 public static final class MainFS extends MultiFileSystem implements LookupListener {
213 private final Lookup.Result<FileSystem> ALL = Lookup.getDefault().lookupResult(FileSystem.class);
214 private final FileSystem MEMORY = FileUtil.createMemoryFileSystem();
215 private final XMLFileSystem layers = new XMLFileSystem();
217 private final LocalFileSystem configRoot = new LocalFileSystem();
219 public void setConfigRootDir(File root) throws Exception {
220 configRoot.setRootDirectory(root);
224 ALL.addLookupListener(this);
226 List<URL> layerUrls = new ArrayList<URL>();
227 ClassLoader l = Thread.currentThread().getContextClassLoader();
229 for (URL manifest : NbCollections.iterable(l.getResources("META-INF/MANIFEST.MF"))) { // NOI18N
230 InputStream is = manifest.openStream();
232 Manifest mani = new Manifest(is);
233 String layerLoc = mani.getMainAttributes().getValue("OpenIDE-Module-Layer"); // NOI18N
234 if (layerLoc != null) {
235 URL layer = l.getResource(layerLoc);
237 layerUrls.add(layer);
244 layers.setXmlUrls(layerUrls.toArray(new URL[layerUrls.size()]));
245 } catch (Exception x) {
247 resultChanged(null); // run after add listener - see PN1 in #26338
250 private FileSystem[] computeDelegates() {
251 List<FileSystem> arr = new ArrayList<FileSystem>();
255 arr.addAll(ALL.allInstances());
256 return arr.toArray(new FileSystem[0]);
259 public void resultChanged(LookupEvent ev) {
260 setDelegates(computeDelegates());