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.
jglick@3213
     1
/*
jglick@3213
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jglick@3213
     3
 *
jglick@3213
     4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
jglick@3213
     5
 *
jglick@3213
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jglick@3213
     7
 * Other names may be trademarks of their respective owners.
jglick@3213
     8
 *
jglick@3213
     9
 * The contents of this file are subject to the terms of either the GNU
jglick@3213
    10
 * General Public License Version 2 only ("GPL") or the Common
jglick@3213
    11
 * Development and Distribution License("CDDL") (collectively, the
jglick@3213
    12
 * "License"). You may not use this file except in compliance with the
jglick@3213
    13
 * License. You can obtain a copy of the License at
jglick@3213
    14
 * http://www.netbeans.org/cddl-gplv2.html
jglick@3213
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jglick@3213
    16
 * specific language governing permissions and limitations under the
jglick@3213
    17
 * License.  When distributing the software, include this License Header
jglick@3213
    18
 * Notice in each file and include the License file at
jglick@3213
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jglick@3213
    20
 * particular file as subject to the "Classpath" exception as provided
jglick@3213
    21
 * by Oracle in the GPL Version 2 section of the License file that
jglick@3213
    22
 * accompanied this code. If applicable, add the following below the
jglick@3213
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jglick@3213
    24
 * your own identifying information:
jglick@3213
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jglick@3213
    26
 * 
jglick@3213
    27
 * Contributor(s):
jglick@3213
    28
 * 
jglick@3213
    29
 * Portions Copyrighted 2007 Sun Microsystems, Inc.
jglick@3213
    30
 */
jglick@3213
    31
jglick@3213
    32
package org.netbeans.modules.websvc.saas.util;
jglick@3213
    33
jglick@3213
    34
import java.io.File;
jglick@3213
    35
import java.io.FileFilter;
jglick@3213
    36
import java.io.FileOutputStream;
jglick@3213
    37
import java.io.IOException;
jglick@3213
    38
import java.io.InputStream;
jglick@3213
    39
import java.net.URL;
jglick@3213
    40
import java.util.ArrayList;
jglick@3213
    41
import java.util.List;
jglick@3213
    42
import java.util.Properties;
jglick@3213
    43
import java.util.jar.Manifest;
jglick@3213
    44
import org.netbeans.junit.MockServices;
jglick@3213
    45
import org.netbeans.modules.websvc.saas.model.SaasServicesModel;
jglick@3213
    46
import org.netbeans.modules.websvc.saas.model.SaasServicesModelTest;
jglick@3213
    47
import org.openide.filesystems.FileObject;
jglick@3213
    48
import org.openide.filesystems.FileSystem;
jglick@3213
    49
import org.openide.filesystems.FileUtil;
jglick@3213
    50
import org.openide.filesystems.LocalFileSystem;
jglick@3213
    51
import org.openide.filesystems.MultiFileSystem;
jglick@3213
    52
import org.openide.filesystems.Repository;
jglick@3213
    53
import org.openide.filesystems.XMLFileSystem;
jglick@3213
    54
import org.openide.modules.InstalledFileLocator;
jglick@3213
    55
import org.openide.util.Lookup;
jglick@3213
    56
import org.openide.util.LookupEvent;
jglick@3213
    57
import org.openide.util.LookupListener;
jglick@3213
    58
import org.openide.util.NbCollections;
jglick@3213
    59
jglick@3213
    60
/**
jglick@3213
    61
 *
jglick@3213
    62
 * @author quynguyen
jglick@3213
    63
 */
jglick@3213
    64
public class SetupUtil {
jglick@3213
    65
    
jglick@3213
    66
    private static final String ENDORSED_REF = "modules/ext/jaxws21/api/jaxws-api.jar";
jglick@3213
    67
    private static final String JAXWS_LIB_PROPERTY = "libs.jaxws21.classpath";
jglick@3213
    68
    
jglick@3213
    69
    public static void commonSetUp(File workingDir) throws Exception {
jglick@3213
    70
        File testuserdir = new File(workingDir.getParentFile(), "testuser");
jglick@3213
    71
        System.getProperties().setProperty("netbeans.user", testuserdir.getAbsolutePath());
jglick@3213
    72
        SaasServicesModelTest.resetSaasServicesModel();
jglick@3213
    73
        FileObject websvcHome = SaasServicesModel.getWebServiceHome();
jglick@3213
    74
        File userconfig = FileUtil.toFile(websvcHome.getParent());
jglick@3213
    75
        MainFS fs = new MainFS();
jglick@3213
    76
        fs.setConfigRootDir(userconfig);
jglick@3213
    77
        TestRepository.defaultFileSystem = fs;
jglick@3213
    78
        
jglick@3213
    79
        MockServices.setServices(DialogDisplayerNotifier.class, InstalledFileLocatorImpl.class, TestRepository.class);
jglick@3213
    80
        
jglick@3213
    81
        InstalledFileLocatorImpl locator = (InstalledFileLocatorImpl)Lookup.getDefault().lookup(InstalledFileLocator.class);
jglick@3213
    82
        locator.setUserConfigRoot(userconfig);
jglick@3213
    83
        
jglick@3213
    84
        File targetBuildProperties = new File(testuserdir, "build.properties");
jglick@3213
    85
        generatePropertiesFile(targetBuildProperties);
jglick@3213
    86
        
jglick@3213
    87
    }
jglick@3213
    88
jglick@3213
    89
    public static void commonTearDown() throws Exception {
jglick@3213
    90
        SaasServicesModel.getWebServiceHome().delete();
jglick@3213
    91
        MockServices.setServices();
jglick@3213
    92
    }
jglick@3213
    93
    
jglick@3213
    94
    private static void generatePropertiesFile(File target) throws IOException {
jglick@3213
    95
        String separator = System.getProperty("path.separator");
jglick@3213
    96
        File apiBase = InstalledFileLocator.getDefault().locate(ENDORSED_REF, null, true).getParentFile();
jglick@3213
    97
        File jaxWsBase = apiBase.getParentFile();
jglick@3213
    98
        
jglick@3213
    99
        FileFilter jarFilter = new FileFilter() {
jglick@3213
   100
            public boolean accept(File pathname) {
jglick@3213
   101
                return pathname.isFile() && pathname.getName().endsWith(".jar");
jglick@3213
   102
            }
jglick@3213
   103
        };
jglick@3213
   104
        
jglick@3213
   105
        File[] apiJars = apiBase.listFiles(jarFilter);
jglick@3213
   106
        File[] implJars = jaxWsBase.listFiles(jarFilter);
jglick@3213
   107
        
jglick@3213
   108
        Properties result = new Properties();
jglick@3213
   109
        StringBuffer classpath = new StringBuffer();
jglick@3213
   110
        
jglick@3213
   111
        for (int i = 0; i < apiJars.length; i++) {
jglick@3213
   112
            String pathElement = apiJars[i].getAbsolutePath() + separator;
jglick@3213
   113
            classpath.append(pathElement);
jglick@3213
   114
        }
jglick@3213
   115
        
jglick@3213
   116
        for (int i = 0; i < implJars.length; i++) {
jglick@3213
   117
            classpath.append(implJars[i].getAbsolutePath());
jglick@3213
   118
            if (i != implJars.length - 1) {
jglick@3213
   119
                classpath.append(separator);
jglick@3213
   120
            }
jglick@3213
   121
        }
jglick@3213
   122
        
jglick@3213
   123
        result.setProperty(JAXWS_LIB_PROPERTY, classpath.toString());
jglick@3213
   124
        
jglick@3213
   125
        FileOutputStream fos = new FileOutputStream(target);
jglick@3213
   126
        result.store(fos, "build.properties file");
jglick@3213
   127
    }
jglick@3213
   128
    
jglick@3213
   129
    public static final class TestRepository extends Repository {
jglick@3213
   130
        static FileSystem defaultFileSystem = null;
jglick@3213
   131
        
jglick@3213
   132
        public TestRepository() {
jglick@3213
   133
            super(defaultFileSystem);
jglick@3213
   134
        }
jglick@3213
   135
    }
jglick@3213
   136
    
jglick@3213
   137
    // Taken from org.openide.filesystems.ExternalUtil to allow layer files to be
jglick@3213
   138
    // loaded into the default filesystem (since core/startup is in the classpath
jglick@3213
   139
    // and registers a default Repository that we do not want)
jglick@3213
   140
    public static final class MainFS extends MultiFileSystem implements LookupListener {
jglick@3213
   141
        private final Lookup.Result<FileSystem> ALL = Lookup.getDefault().lookupResult(FileSystem.class);
jglick@3213
   142
        private final FileSystem MEMORY = FileUtil.createMemoryFileSystem();
jglick@3213
   143
        private final XMLFileSystem layers = new XMLFileSystem();
jglick@3213
   144
        
jglick@3213
   145
        private final LocalFileSystem configRoot = new LocalFileSystem();
jglick@3213
   146
        
jglick@3213
   147
        public void setConfigRootDir(File root) throws Exception {
jglick@3213
   148
            configRoot.setRootDirectory(root);
jglick@3213
   149
        }
jglick@3213
   150
        
jglick@3213
   151
        public MainFS() {
jglick@3213
   152
            ALL.addLookupListener(this);
jglick@3213
   153
            
jglick@3213
   154
            List<URL> layerUrls = new ArrayList<URL>();
jglick@3213
   155
            ClassLoader l = Thread.currentThread().getContextClassLoader();
jglick@3213
   156
            try {
jglick@3213
   157
                for (URL manifest : NbCollections.iterable(l.getResources("META-INF/MANIFEST.MF"))) { // NOI18N
jglick@3213
   158
                    InputStream is = manifest.openStream();
jglick@3213
   159
                    try {
jglick@3213
   160
                        Manifest mani = new Manifest(is);
jglick@3213
   161
                        String layerLoc = mani.getMainAttributes().getValue("OpenIDE-Module-Layer"); // NOI18N
jglick@3213
   162
                        if (layerLoc != null) {
jglick@3213
   163
                            URL layer = l.getResource(layerLoc);
jglick@3213
   164
                            if (layer != null) {
jglick@3213
   165
                                layerUrls.add(layer);
jglick@3213
   166
                            }
jglick@3213
   167
                        }
jglick@3213
   168
                    } finally {
jglick@3213
   169
                        is.close();
jglick@3213
   170
                    }
jglick@3213
   171
                }
jglick@3213
   172
                layers.setXmlUrls(layerUrls.toArray(new URL[layerUrls.size()]));
jglick@3213
   173
            } catch (Exception x) {
jglick@3213
   174
            }
jglick@3213
   175
            resultChanged(null); // run after add listener - see PN1 in #26338
jglick@3213
   176
        }
jglick@3213
   177
        
jglick@3213
   178
        private FileSystem[] computeDelegates() {
jglick@3213
   179
            List<FileSystem> arr = new ArrayList<FileSystem>();
jglick@3213
   180
            arr.add(MEMORY);
jglick@3213
   181
            arr.add(layers);
jglick@3213
   182
            arr.add(configRoot);
jglick@3213
   183
            arr.addAll(ALL.allInstances());
jglick@3213
   184
            return arr.toArray(new FileSystem[0]);
jglick@3213
   185
        }
jglick@3213
   186
    
jglick@3213
   187
        public void resultChanged(LookupEvent ev) {
jglick@3213
   188
            setDelegates(computeDelegates());
jglick@3213
   189
        }
jglick@3213
   190
    }
jglick@3213
   191
    
jglick@3213
   192
    
jglick@3213
   193
}