visualweb.websvcmgr/test/unit/src/org/netbeans/modules/websvc/manager/test/SetupUtil.java
author Jesse Glick <jglick@netbeans.org>
Wed, 23 Mar 2011 16:50:27 -0400
changeset 3213 c85dcf71e424
permissions -rw-r--r--
Tests at least should compile now.
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     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]"
    26  * 
    27  * Contributor(s):
    28  * 
    29  * Portions Copyrighted 2007 Sun Microsystems, Inc.
    30  */
    31 
    32 package org.netbeans.modules.websvc.manager.test;
    33 
    34 import java.io.File;
    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;
    40 import java.net.URL;
    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;
    60 
    61 /**
    62  *
    63  * @author quynguyen
    64  */
    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";
    72     
    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";
    75     
    76     public static SetupData commonSetUp(File workingDir) throws Exception {
    77         SetupData data = new SetupData();
    78         
    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());
    82         
    83         File websvcHome = new File(WebServiceManager.WEBSVC_HOME);
    84         data.setWebsvcHome(websvcHome);
    85         
    86         File websvcUserDir = new File(WebServiceManager.WEBSVC_HOME);
    87         websvcUserDir.mkdirs();
    88         
    89         File wsdlFile = new File(websvcUserDir, WSDL_FILE);
    90         File catalogFile = new File(websvcUserDir, CATALOG_FILE);
    91 
    92         retrieveURL(wsdlFile, SetupUtil.class.getResource(TEST_WSDL));
    93         retrieveURL(catalogFile, SetupUtil.class.getResource(TEST_CATALOG));
    94         
    95         copy(wsdlFile, workingDir);
    96         
    97         data.setLocalWsdlFile(wsdlFile);
    98         data.setLocalCatalogFile(catalogFile);
    99         data.setLocalOriginalWsdl(new File(workingDir, wsdlFile.getName()));
   100         
   101         MainFS fs = new MainFS();
   102         fs.setConfigRootDir(websvcHome.getParentFile());
   103         TestRepository.defaultFileSystem = fs;
   104         
   105         MockServices.setServices(DialogDisplayerNotifier.class, InstalledFileLocatorImpl.class, TestRepository.class);
   106         
   107         InstalledFileLocatorImpl locator = (InstalledFileLocatorImpl)Lookup.getDefault().lookup(InstalledFileLocator.class);
   108         locator.setUserConfigRoot(websvcHome.getParentFile());
   109         
   110         File targetBuildProperties = new File(websvcUserDir.getParentFile().getParentFile(), "build.properties");
   111         generatePropertiesFile(targetBuildProperties);
   112         
   113         return data;
   114     }
   115 
   116     public static void commonTearDown() throws Exception {
   117         DialogDisplayer dd = DialogDisplayer.getDefault();
   118         if (dd instanceof DialogDisplayerNotifier) {
   119             ((DialogDisplayerNotifier)dd).removeAllListeners();
   120         }
   121         
   122         MockServices.setServices();
   123     }
   124     
   125     public static void copy(File src, File target) throws Exception {        
   126         if (src.isFile()) {
   127             File targetFile = new File(target, src.getName());
   128             
   129             FileInputStream is = new FileInputStream(src);
   130             FileOutputStream os = new FileOutputStream(targetFile);
   131             
   132             FileChannel inputChannel = is.getChannel();
   133             FileChannel outputChannel = os.getChannel();
   134             
   135             inputChannel.transferTo(0, inputChannel.size(), outputChannel);
   136             inputChannel.close();
   137             outputChannel.close();
   138         }else {
   139             File newDir = new File(target, src.getName());
   140             newDir.mkdirs();
   141             
   142             File[] dirFiles = src.listFiles();
   143             if (dirFiles != null) {
   144                 for (int i = 0; i < dirFiles.length; i++) {
   145                     copy(dirFiles[i], newDir);
   146                 }
   147             }
   148         }
   149     }
   150     
   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];
   155         
   156         InputStream is = url.openStream();
   157         int bytesRead = 0;
   158         while ( (bytesRead = is.read(readBuffer, 0, 1024)) > 0) {
   159             fos.write(readBuffer, 0, bytesRead);
   160         }
   161         fos.flush();
   162         fos.close();
   163         is.close();
   164     }
   165     
   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();
   170         
   171         FileFilter jarFilter = new FileFilter() {
   172             public boolean accept(File pathname) {
   173                 return pathname.isFile() && pathname.getName().endsWith(".jar");
   174             }
   175         };
   176         
   177         File[] apiJars = apiBase.listFiles(jarFilter);
   178         File[] implJars = jaxWsBase.listFiles(jarFilter);
   179         
   180         Properties result = new Properties();
   181         StringBuffer classpath = new StringBuffer();
   182         
   183         for (int i = 0; i < apiJars.length; i++) {
   184             String pathElement = apiJars[i].getAbsolutePath() + separator;
   185             classpath.append(pathElement);
   186         }
   187         
   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);
   192             }
   193         }
   194         
   195         result.setProperty(JAXWS_LIB_PROPERTY, classpath.toString());
   196         
   197         FileOutputStream fos = new FileOutputStream(target);
   198         result.store(fos, "build.properties file");
   199     }
   200     
   201     public static final class TestRepository extends Repository {
   202         static FileSystem defaultFileSystem = null;
   203         
   204         public TestRepository() {
   205             super(defaultFileSystem);
   206         }
   207     }
   208     
   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();
   216         
   217         private final LocalFileSystem configRoot = new LocalFileSystem();
   218         
   219         public void setConfigRootDir(File root) throws Exception {
   220             configRoot.setRootDirectory(root);
   221         }
   222         
   223         public MainFS() {
   224             ALL.addLookupListener(this);
   225             
   226             List<URL> layerUrls = new ArrayList<URL>();
   227             ClassLoader l = Thread.currentThread().getContextClassLoader();
   228             try {
   229                 for (URL manifest : NbCollections.iterable(l.getResources("META-INF/MANIFEST.MF"))) { // NOI18N
   230                     InputStream is = manifest.openStream();
   231                     try {
   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);
   236                             if (layer != null) {
   237                                 layerUrls.add(layer);
   238                             }
   239                         }
   240                     } finally {
   241                         is.close();
   242                     }
   243                 }
   244                 layers.setXmlUrls(layerUrls.toArray(new URL[layerUrls.size()]));
   245             } catch (Exception x) {
   246             }
   247             resultChanged(null); // run after add listener - see PN1 in #26338
   248         }
   249         
   250         private FileSystem[] computeDelegates() {
   251             List<FileSystem> arr = new ArrayList<FileSystem>();
   252             arr.add(MEMORY);
   253             arr.add(layers);
   254             arr.add(configRoot);
   255             arr.addAll(ALL.allInstances());
   256             return arr.toArray(new FileSystem[0]);
   257         }
   258     
   259         public void resultChanged(LookupEvent ev) {
   260             setDelegates(computeDelegates());
   261         }
   262     }
   263     
   264     
   265 }