visualweb.websvcmgr/test/unit/src/org/netbeans/modules/websvc/saas/util/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.saas.util;
    33 
    34 import java.io.File;
    35 import java.io.FileFilter;
    36 import java.io.FileOutputStream;
    37 import java.io.IOException;
    38 import java.io.InputStream;
    39 import java.net.URL;
    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;
    59 
    60 /**
    61  *
    62  * @author quynguyen
    63  */
    64 public class SetupUtil {
    65     
    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";
    68     
    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;
    78         
    79         MockServices.setServices(DialogDisplayerNotifier.class, InstalledFileLocatorImpl.class, TestRepository.class);
    80         
    81         InstalledFileLocatorImpl locator = (InstalledFileLocatorImpl)Lookup.getDefault().lookup(InstalledFileLocator.class);
    82         locator.setUserConfigRoot(userconfig);
    83         
    84         File targetBuildProperties = new File(testuserdir, "build.properties");
    85         generatePropertiesFile(targetBuildProperties);
    86         
    87     }
    88 
    89     public static void commonTearDown() throws Exception {
    90         SaasServicesModel.getWebServiceHome().delete();
    91         MockServices.setServices();
    92     }
    93     
    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();
    98         
    99         FileFilter jarFilter = new FileFilter() {
   100             public boolean accept(File pathname) {
   101                 return pathname.isFile() && pathname.getName().endsWith(".jar");
   102             }
   103         };
   104         
   105         File[] apiJars = apiBase.listFiles(jarFilter);
   106         File[] implJars = jaxWsBase.listFiles(jarFilter);
   107         
   108         Properties result = new Properties();
   109         StringBuffer classpath = new StringBuffer();
   110         
   111         for (int i = 0; i < apiJars.length; i++) {
   112             String pathElement = apiJars[i].getAbsolutePath() + separator;
   113             classpath.append(pathElement);
   114         }
   115         
   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);
   120             }
   121         }
   122         
   123         result.setProperty(JAXWS_LIB_PROPERTY, classpath.toString());
   124         
   125         FileOutputStream fos = new FileOutputStream(target);
   126         result.store(fos, "build.properties file");
   127     }
   128     
   129     public static final class TestRepository extends Repository {
   130         static FileSystem defaultFileSystem = null;
   131         
   132         public TestRepository() {
   133             super(defaultFileSystem);
   134         }
   135     }
   136     
   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();
   144         
   145         private final LocalFileSystem configRoot = new LocalFileSystem();
   146         
   147         public void setConfigRootDir(File root) throws Exception {
   148             configRoot.setRootDirectory(root);
   149         }
   150         
   151         public MainFS() {
   152             ALL.addLookupListener(this);
   153             
   154             List<URL> layerUrls = new ArrayList<URL>();
   155             ClassLoader l = Thread.currentThread().getContextClassLoader();
   156             try {
   157                 for (URL manifest : NbCollections.iterable(l.getResources("META-INF/MANIFEST.MF"))) { // NOI18N
   158                     InputStream is = manifest.openStream();
   159                     try {
   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);
   164                             if (layer != null) {
   165                                 layerUrls.add(layer);
   166                             }
   167                         }
   168                     } finally {
   169                         is.close();
   170                     }
   171                 }
   172                 layers.setXmlUrls(layerUrls.toArray(new URL[layerUrls.size()]));
   173             } catch (Exception x) {
   174             }
   175             resultChanged(null); // run after add listener - see PN1 in #26338
   176         }
   177         
   178         private FileSystem[] computeDelegates() {
   179             List<FileSystem> arr = new ArrayList<FileSystem>();
   180             arr.add(MEMORY);
   181             arr.add(layers);
   182             arr.add(configRoot);
   183             arr.addAll(ALL.allInstances());
   184             return arr.toArray(new FileSystem[0]);
   185         }
   186     
   187         public void resultChanged(LookupEvent ev) {
   188             setDelegates(computeDelegates());
   189         }
   190     }
   191     
   192     
   193 }