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.api.options;
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.LocalFileSystem;
057: import org.openide.filesystems.MultiFileSystem;
058: import org.openide.filesystems.Repository;
059: import org.openide.filesystems.XMLFileSystem;
060: import org.openide.util.Lookup;
061: import org.openide.util.lookup.Lookups;
062: import org.openide.util.lookup.ProxyLookup;
063:
064: /**
065: * Inspired by org.netbeans.api.project.TestUtil.
066: *
067: * @author Miloslav Metelka, Jan Lahoda
068: */
069: public class IDEInitializer extends ProxyLookup {
070:
071: public static IDEInitializer DEFAULT_LOOKUP = null;
072: private static FileSystem lfs;
073:
074: static {
075: IDEInitializer.class.getClassLoader()
076: .setDefaultAssertionStatus(true);
077: System.setProperty("org.openide.util.Lookup",
078: IDEInitializer.class.getName());
079: Assert.assertEquals(IDEInitializer.class, Lookup.getDefault()
080: .getClass());
081: }
082:
083: public IDEInitializer() {
084: Assert.assertNull(DEFAULT_LOOKUP);
085: DEFAULT_LOOKUP = this ;
086: URL.setURLStreamHandlerFactory(new MyURLHandlerFactory());
087: }
088:
089: /**
090: * Set the global default lookup with the specified content.
091: *
092: * @param layers xml-layer URLs to be present in the system filesystem.
093: * @param instances object instances to be present in the default lookup.
094: */
095: public static void setup(String[] layers, Object[] instances) {
096: ClassLoader classLoader = IDEInitializer.class.getClassLoader();
097: File workDir = new File(Manager.getWorkDirPath());
098: URL[] urls = new URL[layers.length];
099: int i, k = urls.length;
100: for (i = 0; i < k; i++)
101: urls[i] = classLoader.getResource(layers[i]);
102:
103: // 1) create repository
104: XMLFileSystem systemFS = new XMLFileSystem();
105: lfs = FileUtil.createMemoryFileSystem();
106: try {
107: systemFS.setXmlUrls(urls);
108: } catch (Exception ex) {
109: ex.printStackTrace();
110: }
111: MyFileSystem myFileSystem = new MyFileSystem(new FileSystem[] {
112: lfs, systemFS });
113: Repository repository = new Repository(myFileSystem);
114:
115: Object[] lookupContent = new Object[instances.length + 1];
116: lookupContent[0] = repository;
117: System.arraycopy(instances, 0, lookupContent, 1,
118: instances.length);
119:
120: DEFAULT_LOOKUP.setLookups(new Lookup[] {
121: Lookups.fixed(lookupContent),
122: Lookups.metaInfServices(classLoader),
123: Lookups.singleton(classLoader), });
124: Assert.assertEquals(myFileSystem, Repository.getDefault()
125: .getDefaultFileSystem());
126: }
127:
128: public static void cleanWorkDir() {
129: try {
130: Enumeration en = lfs.getRoot().getChildren(false);
131: while (en.hasMoreElements())
132: ((FileObject) en.nextElement()).delete();
133: } catch (IOException ex) {
134: ex.printStackTrace();
135: }
136: }
137:
138: private static class MyFileSystem extends MultiFileSystem {
139: public MyFileSystem(FileSystem[] fileSystems) {
140: super (fileSystems);
141: try {
142: setSystemName("TestFS");
143: } catch (PropertyVetoException ex) {
144: ex.printStackTrace();
145: }
146: }
147: }
148:
149: private static class MyURLHandlerFactory implements
150: URLStreamHandlerFactory {
151: public URLStreamHandler createURLStreamHandler(String protocol) {
152: if (protocol.equals("nbfs")) {
153: return FileUtil.nbfsURLStreamHandler();
154: }
155: return null;
156: }
157: }
158: }
|