visualweb.websvcmgr/test/unit/src/org/netbeans/modules/websvc/saas/util/SetupUtil.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/visualweb.websvcmgr/test/unit/src/org/netbeans/modules/websvc/saas/util/SetupUtil.java Wed Mar 23 16:50:27 2011 -0400
1.3 @@ -0,0 +1,193 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * Contributor(s):
1.31 + *
1.32 + * Portions Copyrighted 2007 Sun Microsystems, Inc.
1.33 + */
1.34 +
1.35 +package org.netbeans.modules.websvc.saas.util;
1.36 +
1.37 +import java.io.File;
1.38 +import java.io.FileFilter;
1.39 +import java.io.FileOutputStream;
1.40 +import java.io.IOException;
1.41 +import java.io.InputStream;
1.42 +import java.net.URL;
1.43 +import java.util.ArrayList;
1.44 +import java.util.List;
1.45 +import java.util.Properties;
1.46 +import java.util.jar.Manifest;
1.47 +import org.netbeans.junit.MockServices;
1.48 +import org.netbeans.modules.websvc.saas.model.SaasServicesModel;
1.49 +import org.netbeans.modules.websvc.saas.model.SaasServicesModelTest;
1.50 +import org.openide.filesystems.FileObject;
1.51 +import org.openide.filesystems.FileSystem;
1.52 +import org.openide.filesystems.FileUtil;
1.53 +import org.openide.filesystems.LocalFileSystem;
1.54 +import org.openide.filesystems.MultiFileSystem;
1.55 +import org.openide.filesystems.Repository;
1.56 +import org.openide.filesystems.XMLFileSystem;
1.57 +import org.openide.modules.InstalledFileLocator;
1.58 +import org.openide.util.Lookup;
1.59 +import org.openide.util.LookupEvent;
1.60 +import org.openide.util.LookupListener;
1.61 +import org.openide.util.NbCollections;
1.62 +
1.63 +/**
1.64 + *
1.65 + * @author quynguyen
1.66 + */
1.67 +public class SetupUtil {
1.68 +
1.69 + private static final String ENDORSED_REF = "modules/ext/jaxws21/api/jaxws-api.jar";
1.70 + private static final String JAXWS_LIB_PROPERTY = "libs.jaxws21.classpath";
1.71 +
1.72 + public static void commonSetUp(File workingDir) throws Exception {
1.73 + File testuserdir = new File(workingDir.getParentFile(), "testuser");
1.74 + System.getProperties().setProperty("netbeans.user", testuserdir.getAbsolutePath());
1.75 + SaasServicesModelTest.resetSaasServicesModel();
1.76 + FileObject websvcHome = SaasServicesModel.getWebServiceHome();
1.77 + File userconfig = FileUtil.toFile(websvcHome.getParent());
1.78 + MainFS fs = new MainFS();
1.79 + fs.setConfigRootDir(userconfig);
1.80 + TestRepository.defaultFileSystem = fs;
1.81 +
1.82 + MockServices.setServices(DialogDisplayerNotifier.class, InstalledFileLocatorImpl.class, TestRepository.class);
1.83 +
1.84 + InstalledFileLocatorImpl locator = (InstalledFileLocatorImpl)Lookup.getDefault().lookup(InstalledFileLocator.class);
1.85 + locator.setUserConfigRoot(userconfig);
1.86 +
1.87 + File targetBuildProperties = new File(testuserdir, "build.properties");
1.88 + generatePropertiesFile(targetBuildProperties);
1.89 +
1.90 + }
1.91 +
1.92 + public static void commonTearDown() throws Exception {
1.93 + SaasServicesModel.getWebServiceHome().delete();
1.94 + MockServices.setServices();
1.95 + }
1.96 +
1.97 + private static void generatePropertiesFile(File target) throws IOException {
1.98 + String separator = System.getProperty("path.separator");
1.99 + File apiBase = InstalledFileLocator.getDefault().locate(ENDORSED_REF, null, true).getParentFile();
1.100 + File jaxWsBase = apiBase.getParentFile();
1.101 +
1.102 + FileFilter jarFilter = new FileFilter() {
1.103 + public boolean accept(File pathname) {
1.104 + return pathname.isFile() && pathname.getName().endsWith(".jar");
1.105 + }
1.106 + };
1.107 +
1.108 + File[] apiJars = apiBase.listFiles(jarFilter);
1.109 + File[] implJars = jaxWsBase.listFiles(jarFilter);
1.110 +
1.111 + Properties result = new Properties();
1.112 + StringBuffer classpath = new StringBuffer();
1.113 +
1.114 + for (int i = 0; i < apiJars.length; i++) {
1.115 + String pathElement = apiJars[i].getAbsolutePath() + separator;
1.116 + classpath.append(pathElement);
1.117 + }
1.118 +
1.119 + for (int i = 0; i < implJars.length; i++) {
1.120 + classpath.append(implJars[i].getAbsolutePath());
1.121 + if (i != implJars.length - 1) {
1.122 + classpath.append(separator);
1.123 + }
1.124 + }
1.125 +
1.126 + result.setProperty(JAXWS_LIB_PROPERTY, classpath.toString());
1.127 +
1.128 + FileOutputStream fos = new FileOutputStream(target);
1.129 + result.store(fos, "build.properties file");
1.130 + }
1.131 +
1.132 + public static final class TestRepository extends Repository {
1.133 + static FileSystem defaultFileSystem = null;
1.134 +
1.135 + public TestRepository() {
1.136 + super(defaultFileSystem);
1.137 + }
1.138 + }
1.139 +
1.140 + // Taken from org.openide.filesystems.ExternalUtil to allow layer files to be
1.141 + // loaded into the default filesystem (since core/startup is in the classpath
1.142 + // and registers a default Repository that we do not want)
1.143 + public static final class MainFS extends MultiFileSystem implements LookupListener {
1.144 + private final Lookup.Result<FileSystem> ALL = Lookup.getDefault().lookupResult(FileSystem.class);
1.145 + private final FileSystem MEMORY = FileUtil.createMemoryFileSystem();
1.146 + private final XMLFileSystem layers = new XMLFileSystem();
1.147 +
1.148 + private final LocalFileSystem configRoot = new LocalFileSystem();
1.149 +
1.150 + public void setConfigRootDir(File root) throws Exception {
1.151 + configRoot.setRootDirectory(root);
1.152 + }
1.153 +
1.154 + public MainFS() {
1.155 + ALL.addLookupListener(this);
1.156 +
1.157 + List<URL> layerUrls = new ArrayList<URL>();
1.158 + ClassLoader l = Thread.currentThread().getContextClassLoader();
1.159 + try {
1.160 + for (URL manifest : NbCollections.iterable(l.getResources("META-INF/MANIFEST.MF"))) { // NOI18N
1.161 + InputStream is = manifest.openStream();
1.162 + try {
1.163 + Manifest mani = new Manifest(is);
1.164 + String layerLoc = mani.getMainAttributes().getValue("OpenIDE-Module-Layer"); // NOI18N
1.165 + if (layerLoc != null) {
1.166 + URL layer = l.getResource(layerLoc);
1.167 + if (layer != null) {
1.168 + layerUrls.add(layer);
1.169 + }
1.170 + }
1.171 + } finally {
1.172 + is.close();
1.173 + }
1.174 + }
1.175 + layers.setXmlUrls(layerUrls.toArray(new URL[layerUrls.size()]));
1.176 + } catch (Exception x) {
1.177 + }
1.178 + resultChanged(null); // run after add listener - see PN1 in #26338
1.179 + }
1.180 +
1.181 + private FileSystem[] computeDelegates() {
1.182 + List<FileSystem> arr = new ArrayList<FileSystem>();
1.183 + arr.add(MEMORY);
1.184 + arr.add(layers);
1.185 + arr.add(configRoot);
1.186 + arr.addAll(ALL.allInstances());
1.187 + return arr.toArray(new FileSystem[0]);
1.188 + }
1.189 +
1.190 + public void resultChanged(LookupEvent ev) {
1.191 + setDelegates(computeDelegates());
1.192 + }
1.193 + }
1.194 +
1.195 +
1.196 +}