visualweb.websvcmgr/test/unit/src/org/netbeans/modules/websvc/saas/util/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.saas.util;
35 import java.io.FileFilter;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Properties;
43 import java.util.jar.Manifest;
44 import org.netbeans.junit.MockServices;
45 import org.netbeans.modules.websvc.saas.model.SaasServicesModel;
46 import org.netbeans.modules.websvc.saas.model.SaasServicesModelTest;
47 import org.openide.filesystems.FileObject;
48 import org.openide.filesystems.FileSystem;
49 import org.openide.filesystems.FileUtil;
50 import org.openide.filesystems.LocalFileSystem;
51 import org.openide.filesystems.MultiFileSystem;
52 import org.openide.filesystems.Repository;
53 import org.openide.filesystems.XMLFileSystem;
54 import org.openide.modules.InstalledFileLocator;
55 import org.openide.util.Lookup;
56 import org.openide.util.LookupEvent;
57 import org.openide.util.LookupListener;
58 import org.openide.util.NbCollections;
64 public class SetupUtil {
66 private static final String ENDORSED_REF = "modules/ext/jaxws21/api/jaxws-api.jar";
67 private static final String JAXWS_LIB_PROPERTY = "libs.jaxws21.classpath";
69 public static void commonSetUp(File workingDir) throws Exception {
70 File testuserdir = new File(workingDir.getParentFile(), "testuser");
71 System.getProperties().setProperty("netbeans.user", testuserdir.getAbsolutePath());
72 SaasServicesModelTest.resetSaasServicesModel();
73 FileObject websvcHome = SaasServicesModel.getWebServiceHome();
74 File userconfig = FileUtil.toFile(websvcHome.getParent());
75 MainFS fs = new MainFS();
76 fs.setConfigRootDir(userconfig);
77 TestRepository.defaultFileSystem = fs;
79 MockServices.setServices(DialogDisplayerNotifier.class, InstalledFileLocatorImpl.class, TestRepository.class);
81 InstalledFileLocatorImpl locator = (InstalledFileLocatorImpl)Lookup.getDefault().lookup(InstalledFileLocator.class);
82 locator.setUserConfigRoot(userconfig);
84 File targetBuildProperties = new File(testuserdir, "build.properties");
85 generatePropertiesFile(targetBuildProperties);
89 public static void commonTearDown() throws Exception {
90 SaasServicesModel.getWebServiceHome().delete();
91 MockServices.setServices();
94 private static void generatePropertiesFile(File target) throws IOException {
95 String separator = System.getProperty("path.separator");
96 File apiBase = InstalledFileLocator.getDefault().locate(ENDORSED_REF, null, true).getParentFile();
97 File jaxWsBase = apiBase.getParentFile();
99 FileFilter jarFilter = new FileFilter() {
100 public boolean accept(File pathname) {
101 return pathname.isFile() && pathname.getName().endsWith(".jar");
105 File[] apiJars = apiBase.listFiles(jarFilter);
106 File[] implJars = jaxWsBase.listFiles(jarFilter);
108 Properties result = new Properties();
109 StringBuffer classpath = new StringBuffer();
111 for (int i = 0; i < apiJars.length; i++) {
112 String pathElement = apiJars[i].getAbsolutePath() + separator;
113 classpath.append(pathElement);
116 for (int i = 0; i < implJars.length; i++) {
117 classpath.append(implJars[i].getAbsolutePath());
118 if (i != implJars.length - 1) {
119 classpath.append(separator);
123 result.setProperty(JAXWS_LIB_PROPERTY, classpath.toString());
125 FileOutputStream fos = new FileOutputStream(target);
126 result.store(fos, "build.properties file");
129 public static final class TestRepository extends Repository {
130 static FileSystem defaultFileSystem = null;
132 public TestRepository() {
133 super(defaultFileSystem);
137 // Taken from org.openide.filesystems.ExternalUtil to allow layer files to be
138 // loaded into the default filesystem (since core/startup is in the classpath
139 // and registers a default Repository that we do not want)
140 public static final class MainFS extends MultiFileSystem implements LookupListener {
141 private final Lookup.Result<FileSystem> ALL = Lookup.getDefault().lookupResult(FileSystem.class);
142 private final FileSystem MEMORY = FileUtil.createMemoryFileSystem();
143 private final XMLFileSystem layers = new XMLFileSystem();
145 private final LocalFileSystem configRoot = new LocalFileSystem();
147 public void setConfigRootDir(File root) throws Exception {
148 configRoot.setRootDirectory(root);
152 ALL.addLookupListener(this);
154 List<URL> layerUrls = new ArrayList<URL>();
155 ClassLoader l = Thread.currentThread().getContextClassLoader();
157 for (URL manifest : NbCollections.iterable(l.getResources("META-INF/MANIFEST.MF"))) { // NOI18N
158 InputStream is = manifest.openStream();
160 Manifest mani = new Manifest(is);
161 String layerLoc = mani.getMainAttributes().getValue("OpenIDE-Module-Layer"); // NOI18N
162 if (layerLoc != null) {
163 URL layer = l.getResource(layerLoc);
165 layerUrls.add(layer);
172 layers.setXmlUrls(layerUrls.toArray(new URL[layerUrls.size()]));
173 } catch (Exception x) {
175 resultChanged(null); // run after add listener - see PN1 in #26338
178 private FileSystem[] computeDelegates() {
179 List<FileSystem> arr = new ArrayList<FileSystem>();
183 arr.addAll(ALL.allInstances());
184 return arr.toArray(new FileSystem[0]);
187 public void resultChanged(LookupEvent ev) {
188 setDelegates(computeDelegates());