001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tasklist.trampoline;
043:
044: import java.beans.PropertyVetoException;
045: import java.io.File;
046: import java.io.IOException;
047: import java.net.URL;
048: import java.net.URLStreamHandler;
049: import java.net.URLStreamHandlerFactory;
050: import java.util.Enumeration;
051: import junit.framework.Assert;
052: import org.netbeans.junit.Manager;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileSystem;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.filesystems.MultiFileSystem;
057: import org.openide.filesystems.Repository;
058: import org.openide.filesystems.XMLFileSystem;
059: import org.openide.util.Lookup;
060: import org.openide.util.lookup.Lookups;
061: import org.openide.util.lookup.ProxyLookup;
062:
063: /**
064: * Inspired by org.netbeans.api.project.TestUtil.
065: *
066: * @author Miloslav Metelka, Jan Lahoda
067: */
068: public class IDEInitializer extends ProxyLookup {
069:
070: public static IDEInitializer DEFAULT_LOOKUP = null;
071: private static FileSystem lfs;
072:
073: static {
074: IDEInitializer.class.getClassLoader()
075: .setDefaultAssertionStatus(true);
076: System.setProperty("org.openide.util.Lookup",
077: IDEInitializer.class.getName());
078: Assert.assertEquals(IDEInitializer.class, Lookup.getDefault()
079: .getClass());
080: }
081:
082: public IDEInitializer() {
083: Assert.assertNull(DEFAULT_LOOKUP);
084: DEFAULT_LOOKUP = this ;
085: URL.setURLStreamHandlerFactory(new MyURLHandlerFactory());
086: }
087:
088: /**
089: * Set the global default lookup with the specified content.
090: *
091: * @param layers xml-layer URLs to be present in the system filesystem.
092: * @param instances object instances to be present in the default lookup.
093: */
094: public static void setup(String[] layers, Object[] instances) {
095: ClassLoader classLoader = IDEInitializer.class.getClassLoader();
096: File workDir = new File(Manager.getWorkDirPath());
097: URL[] urls = new URL[layers.length];
098: int i, k = urls.length;
099: for (i = 0; i < k; i++)
100: urls[i] = classLoader.getResource(layers[i]);
101:
102: // 1) create repository
103: XMLFileSystem systemFS = new XMLFileSystem();
104: lfs = FileUtil.createMemoryFileSystem();
105: try {
106: systemFS.setXmlUrls(urls);
107: } catch (Exception ex) {
108: ex.printStackTrace();
109: }
110: MyFileSystem myFileSystem = new MyFileSystem(new FileSystem[] {
111: lfs, systemFS });
112: Repository repository = new Repository(myFileSystem);
113:
114: Object[] lookupContent = new Object[instances.length + 1];
115: lookupContent[0] = repository;
116: System.arraycopy(instances, 0, lookupContent, 1,
117: instances.length);
118:
119: DEFAULT_LOOKUP.setLookups(new Lookup[] {
120: Lookups.fixed(lookupContent),
121: Lookups.metaInfServices(classLoader),
122: Lookups.singleton(classLoader), });
123: Assert.assertEquals(myFileSystem, Repository.getDefault()
124: .getDefaultFileSystem());
125: }
126:
127: public static void cleanWorkDir() {
128: try {
129: Enumeration en = lfs.getRoot().getChildren(false);
130: while (en.hasMoreElements())
131: ((FileObject) en.nextElement()).delete();
132: } catch (IOException ex) {
133: ex.printStackTrace();
134: }
135: }
136:
137: private static class MyFileSystem extends MultiFileSystem {
138: public MyFileSystem(FileSystem[] fileSystems) {
139: super (fileSystems);
140: try {
141: setSystemName("TestFS");
142: } catch (PropertyVetoException ex) {
143: ex.printStackTrace();
144: }
145: }
146: }
147:
148: private static class MyURLHandlerFactory implements
149: URLStreamHandlerFactory {
150: public URLStreamHandler createURLStreamHandler(String protocol) {
151: if (protocol.equals("nbfs")) {
152: return FileUtil.nbfsURLStreamHandler();
153: }
154: return null;
155: }
156: }
157: }
|