visualweb.websvcmgr/test/unit/src/org/netbeans/modules/websvc/manager/test/SetupUtil.java
changeset 3213 c85dcf71e424
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/visualweb.websvcmgr/test/unit/src/org/netbeans/modules/websvc/manager/test/SetupUtil.java	Wed Mar 23 16:50:27 2011 -0400
     1.3 @@ -0,0 +1,265 @@
     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.manager.test;
    1.36 +
    1.37 +import java.io.File;
    1.38 +import java.io.FileFilter;
    1.39 +import java.io.FileInputStream;
    1.40 +import java.io.FileOutputStream;
    1.41 +import java.io.IOException;
    1.42 +import java.io.InputStream;
    1.43 +import java.net.URL;
    1.44 +import java.nio.channels.FileChannel;
    1.45 +import java.util.ArrayList;
    1.46 +import java.util.List;
    1.47 +import java.util.Properties;
    1.48 +import java.util.jar.Manifest;
    1.49 +import org.netbeans.junit.MockServices;
    1.50 +import org.netbeans.modules.websvc.manager.WebServiceManager;
    1.51 +import org.openide.DialogDisplayer;
    1.52 +import org.openide.filesystems.FileSystem;
    1.53 +import org.openide.filesystems.FileUtil;
    1.54 +import org.openide.filesystems.LocalFileSystem;
    1.55 +import org.openide.filesystems.MultiFileSystem;
    1.56 +import org.openide.filesystems.Repository;
    1.57 +import org.openide.filesystems.XMLFileSystem;
    1.58 +import org.openide.modules.InstalledFileLocator;
    1.59 +import org.openide.util.Lookup;
    1.60 +import org.openide.util.LookupEvent;
    1.61 +import org.openide.util.LookupListener;
    1.62 +import org.openide.util.NbCollections;
    1.63 +
    1.64 +/**
    1.65 + *
    1.66 + * @author quynguyen
    1.67 + */
    1.68 +public class SetupUtil {
    1.69 +    private static final String WORKDIR_SPACES = "user directory";
    1.70 +    private static final String WORKDIR = "userdirectory";
    1.71 +    private static final String TEST_WSDL = "/org/netbeans/modules/websvc/manager/resources/uszip-asmx-catalog/www.webservicemart.com/uszip.asmx.wsdl";
    1.72 +    private static final String TEST_CATALOG = "/org/netbeans/modules/websvc/manager/resources/uszip-asmx-catalog/catalog.xml";
    1.73 +    private static final String CATALOG_FILE = "uszip-asmx-catalog/catalog.xml";
    1.74 +    private static final String WSDL_FILE = "uszip-asmx-catalog/www.webservicemart.com/uszip.asmx.wsdl";
    1.75 +    
    1.76 +    private static final String ENDORSED_REF = "modules/ext/jaxws21/api/jaxws-api.jar";
    1.77 +    private static final String JAXWS_LIB_PROPERTY = "libs.jaxws21.classpath";
    1.78 +    
    1.79 +    public static SetupData commonSetUp(File workingDir) throws Exception {
    1.80 +        SetupData data = new SetupData();
    1.81 +        
    1.82 +        File userDir = new File(workingDir.getParentFile(),
    1.83 +                System.getProperty("os.name").startsWith("Windows") ? WORKDIR_SPACES : WORKDIR);
    1.84 +        System.getProperties().setProperty("netbeans.user", userDir.getAbsolutePath());
    1.85 +        
    1.86 +        File websvcHome = new File(WebServiceManager.WEBSVC_HOME);
    1.87 +        data.setWebsvcHome(websvcHome);
    1.88 +        
    1.89 +        File websvcUserDir = new File(WebServiceManager.WEBSVC_HOME);
    1.90 +        websvcUserDir.mkdirs();
    1.91 +        
    1.92 +        File wsdlFile = new File(websvcUserDir, WSDL_FILE);
    1.93 +        File catalogFile = new File(websvcUserDir, CATALOG_FILE);
    1.94 +
    1.95 +        retrieveURL(wsdlFile, SetupUtil.class.getResource(TEST_WSDL));
    1.96 +        retrieveURL(catalogFile, SetupUtil.class.getResource(TEST_CATALOG));
    1.97 +        
    1.98 +        copy(wsdlFile, workingDir);
    1.99 +        
   1.100 +        data.setLocalWsdlFile(wsdlFile);
   1.101 +        data.setLocalCatalogFile(catalogFile);
   1.102 +        data.setLocalOriginalWsdl(new File(workingDir, wsdlFile.getName()));
   1.103 +        
   1.104 +        MainFS fs = new MainFS();
   1.105 +        fs.setConfigRootDir(websvcHome.getParentFile());
   1.106 +        TestRepository.defaultFileSystem = fs;
   1.107 +        
   1.108 +        MockServices.setServices(DialogDisplayerNotifier.class, InstalledFileLocatorImpl.class, TestRepository.class);
   1.109 +        
   1.110 +        InstalledFileLocatorImpl locator = (InstalledFileLocatorImpl)Lookup.getDefault().lookup(InstalledFileLocator.class);
   1.111 +        locator.setUserConfigRoot(websvcHome.getParentFile());
   1.112 +        
   1.113 +        File targetBuildProperties = new File(websvcUserDir.getParentFile().getParentFile(), "build.properties");
   1.114 +        generatePropertiesFile(targetBuildProperties);
   1.115 +        
   1.116 +        return data;
   1.117 +    }
   1.118 +
   1.119 +    public static void commonTearDown() throws Exception {
   1.120 +        DialogDisplayer dd = DialogDisplayer.getDefault();
   1.121 +        if (dd instanceof DialogDisplayerNotifier) {
   1.122 +            ((DialogDisplayerNotifier)dd).removeAllListeners();
   1.123 +        }
   1.124 +        
   1.125 +        MockServices.setServices();
   1.126 +    }
   1.127 +    
   1.128 +    public static void copy(File src, File target) throws Exception {        
   1.129 +        if (src.isFile()) {
   1.130 +            File targetFile = new File(target, src.getName());
   1.131 +            
   1.132 +            FileInputStream is = new FileInputStream(src);
   1.133 +            FileOutputStream os = new FileOutputStream(targetFile);
   1.134 +            
   1.135 +            FileChannel inputChannel = is.getChannel();
   1.136 +            FileChannel outputChannel = os.getChannel();
   1.137 +            
   1.138 +            inputChannel.transferTo(0, inputChannel.size(), outputChannel);
   1.139 +            inputChannel.close();
   1.140 +            outputChannel.close();
   1.141 +        }else {
   1.142 +            File newDir = new File(target, src.getName());
   1.143 +            newDir.mkdirs();
   1.144 +            
   1.145 +            File[] dirFiles = src.listFiles();
   1.146 +            if (dirFiles != null) {
   1.147 +                for (int i = 0; i < dirFiles.length; i++) {
   1.148 +                    copy(dirFiles[i], newDir);
   1.149 +                }
   1.150 +            }
   1.151 +        }
   1.152 +    }
   1.153 +    
   1.154 +    public static void retrieveURL(File targetFile, URL url) throws IOException {
   1.155 +        targetFile.getParentFile().mkdirs();
   1.156 +        FileOutputStream fos = new FileOutputStream(targetFile);
   1.157 +        byte[] readBuffer = new byte[1024];
   1.158 +        
   1.159 +        InputStream is = url.openStream();
   1.160 +        int bytesRead = 0;
   1.161 +        while ( (bytesRead = is.read(readBuffer, 0, 1024)) > 0) {
   1.162 +            fos.write(readBuffer, 0, bytesRead);
   1.163 +        }
   1.164 +        fos.flush();
   1.165 +        fos.close();
   1.166 +        is.close();
   1.167 +    }
   1.168 +    
   1.169 +    private static void generatePropertiesFile(File target) throws IOException {
   1.170 +        String separator = System.getProperty("path.separator");
   1.171 +        File apiBase = InstalledFileLocator.getDefault().locate(ENDORSED_REF, null, true).getParentFile();
   1.172 +        File jaxWsBase = apiBase.getParentFile();
   1.173 +        
   1.174 +        FileFilter jarFilter = new FileFilter() {
   1.175 +            public boolean accept(File pathname) {
   1.176 +                return pathname.isFile() && pathname.getName().endsWith(".jar");
   1.177 +            }
   1.178 +        };
   1.179 +        
   1.180 +        File[] apiJars = apiBase.listFiles(jarFilter);
   1.181 +        File[] implJars = jaxWsBase.listFiles(jarFilter);
   1.182 +        
   1.183 +        Properties result = new Properties();
   1.184 +        StringBuffer classpath = new StringBuffer();
   1.185 +        
   1.186 +        for (int i = 0; i < apiJars.length; i++) {
   1.187 +            String pathElement = apiJars[i].getAbsolutePath() + separator;
   1.188 +            classpath.append(pathElement);
   1.189 +        }
   1.190 +        
   1.191 +        for (int i = 0; i < implJars.length; i++) {
   1.192 +            classpath.append(implJars[i].getAbsolutePath());
   1.193 +            if (i != implJars.length - 1) {
   1.194 +                classpath.append(separator);
   1.195 +            }
   1.196 +        }
   1.197 +        
   1.198 +        result.setProperty(JAXWS_LIB_PROPERTY, classpath.toString());
   1.199 +        
   1.200 +        FileOutputStream fos = new FileOutputStream(target);
   1.201 +        result.store(fos, "build.properties file");
   1.202 +    }
   1.203 +    
   1.204 +    public static final class TestRepository extends Repository {
   1.205 +        static FileSystem defaultFileSystem = null;
   1.206 +        
   1.207 +        public TestRepository() {
   1.208 +            super(defaultFileSystem);
   1.209 +        }
   1.210 +    }
   1.211 +    
   1.212 +    // Taken from org.openide.filesystems.ExternalUtil to allow layer files to be
   1.213 +    // loaded into the default filesystem (since core/startup is in the classpath
   1.214 +    // and registers a default Repository that we do not want)
   1.215 +    public static final class MainFS extends MultiFileSystem implements LookupListener {
   1.216 +        private final Lookup.Result<FileSystem> ALL = Lookup.getDefault().lookupResult(FileSystem.class);
   1.217 +        private final FileSystem MEMORY = FileUtil.createMemoryFileSystem();
   1.218 +        private final XMLFileSystem layers = new XMLFileSystem();
   1.219 +        
   1.220 +        private final LocalFileSystem configRoot = new LocalFileSystem();
   1.221 +        
   1.222 +        public void setConfigRootDir(File root) throws Exception {
   1.223 +            configRoot.setRootDirectory(root);
   1.224 +        }
   1.225 +        
   1.226 +        public MainFS() {
   1.227 +            ALL.addLookupListener(this);
   1.228 +            
   1.229 +            List<URL> layerUrls = new ArrayList<URL>();
   1.230 +            ClassLoader l = Thread.currentThread().getContextClassLoader();
   1.231 +            try {
   1.232 +                for (URL manifest : NbCollections.iterable(l.getResources("META-INF/MANIFEST.MF"))) { // NOI18N
   1.233 +                    InputStream is = manifest.openStream();
   1.234 +                    try {
   1.235 +                        Manifest mani = new Manifest(is);
   1.236 +                        String layerLoc = mani.getMainAttributes().getValue("OpenIDE-Module-Layer"); // NOI18N
   1.237 +                        if (layerLoc != null) {
   1.238 +                            URL layer = l.getResource(layerLoc);
   1.239 +                            if (layer != null) {
   1.240 +                                layerUrls.add(layer);
   1.241 +                            }
   1.242 +                        }
   1.243 +                    } finally {
   1.244 +                        is.close();
   1.245 +                    }
   1.246 +                }
   1.247 +                layers.setXmlUrls(layerUrls.toArray(new URL[layerUrls.size()]));
   1.248 +            } catch (Exception x) {
   1.249 +            }
   1.250 +            resultChanged(null); // run after add listener - see PN1 in #26338
   1.251 +        }
   1.252 +        
   1.253 +        private FileSystem[] computeDelegates() {
   1.254 +            List<FileSystem> arr = new ArrayList<FileSystem>();
   1.255 +            arr.add(MEMORY);
   1.256 +            arr.add(layers);
   1.257 +            arr.add(configRoot);
   1.258 +            arr.addAll(ALL.allInstances());
   1.259 +            return arr.toArray(new FileSystem[0]);
   1.260 +        }
   1.261 +    
   1.262 +        public void resultChanged(LookupEvent ev) {
   1.263 +            setDelegates(computeDelegates());
   1.264 +        }
   1.265 +    }
   1.266 +    
   1.267 +    
   1.268 +}