KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > mimelookup > EditorTestLookup


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.mimelookup;
21
22
23 import java.beans.PropertyVetoException JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27 import junit.framework.Assert;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileSystem;
30 import org.openide.filesystems.FileSystem.Status;
31 import org.openide.filesystems.LocalFileSystem;
32 import org.openide.filesystems.MultiFileSystem;
33 import org.openide.filesystems.Repository;
34 import org.openide.filesystems.XMLFileSystem;
35 import org.openide.loaders.DataFolder;
36 import org.openide.loaders.FolderLookup;
37 import org.openide.util.Lookup;
38 import org.openide.util.lookup.Lookups;
39 import org.openide.util.lookup.ProxyLookup;
40
41
42 /**
43  * Inspired by org.netbeans.api.project.TestUtil and FolderLookupTest
44  *
45  * @author Martin Roskanin
46  */

47 public class EditorTestLookup extends ProxyLookup {
48     
49     public static EditorTestLookup DEFAULT_LOOKUP = null;
50     
51     static {
52         EditorTestLookup.class.getClassLoader().setDefaultAssertionStatus(true);
53         System.setProperty("org.openide.util.Lookup", EditorTestLookup.class.getName());
54         Assert.assertEquals(EditorTestLookup.class, Lookup.getDefault().getClass());
55     }
56     
57     public EditorTestLookup() {
58         Assert.assertNull(DEFAULT_LOOKUP);
59         DEFAULT_LOOKUP = this;
60     }
61     
62     public static void setLookup(Object JavaDoc[] instances, ClassLoader JavaDoc cl, FileObject servicesFolder, Class JavaDoc [] exclude) {
63         Lookup metaInfServices = Lookups.metaInfServices(cl);
64         if (exclude != null && exclude.length > 0) {
65             metaInfServices = Lookups.exclude(metaInfServices, exclude);
66         }
67         
68         DEFAULT_LOOKUP.setLookups(new Lookup[] {
69             Lookups.fixed(instances),
70             metaInfServices,
71             Lookups.singleton(cl),
72         });
73         
74         if (servicesFolder != null) {
75             // DataSystems need default repository, which is read from the default lookup.
76
// That's why the lookup is set first without the services lookup and then again
77
// here with the FolderLookup over the Services folder.
78
Lookup services = new FolderLookup(DataFolder.findFolder(servicesFolder)).getLookup();
79             if (exclude != null && exclude.length > 0) {
80                 services = Lookups.exclude(services, exclude);
81             }
82             
83             DEFAULT_LOOKUP.setLookups(new Lookup[] {
84                 Lookups.fixed(instances),
85                 metaInfServices,
86                 Lookups.singleton(cl),
87                 services
88             });
89         }
90     }
91     
92     public static void setLookup(String JavaDoc[] files, File JavaDoc workDir, Object JavaDoc[] instances, ClassLoader JavaDoc cl)
93     throws IOException JavaDoc, PropertyVetoException JavaDoc {
94         setLookup(files, workDir, instances, cl, null);
95     }
96     
97     public static void setLookup(String JavaDoc[] files, File JavaDoc workDir, Object JavaDoc[] instances, ClassLoader JavaDoc cl, Class JavaDoc [] exclude)
98     throws IOException JavaDoc, PropertyVetoException JavaDoc {
99         FileSystem fs = createLocalFileSystem(workDir, files);
100         setLookup(new FileSystem [] { fs }, instances, cl, exclude);
101     }
102     
103     public static void setLookup(URL JavaDoc[] layers, File JavaDoc workDir, Object JavaDoc[] instances, ClassLoader JavaDoc cl)
104     throws IOException JavaDoc, PropertyVetoException JavaDoc {
105         setLookup(layers, workDir, instances, cl, null);
106     }
107     
108     public static void setLookup(URL JavaDoc[] layers, File JavaDoc workDir, Object JavaDoc[] instances, ClassLoader JavaDoc cl, Class JavaDoc [] exclude)
109     throws IOException JavaDoc, PropertyVetoException JavaDoc {
110         FileSystem writeableFs = createLocalFileSystem(workDir, new String JavaDoc[0]);
111         XMLFileSystem layersFs = new XMLFileSystem();
112         layersFs.setXmlUrls(layers);
113         
114         setLookup(new FileSystem [] { writeableFs, layersFs }, instances, cl, exclude);
115     }
116     
117     private static void setLookup(FileSystem [] fs, Object JavaDoc[] instances, ClassLoader JavaDoc cl, Class JavaDoc [] exclude)
118     throws IOException JavaDoc, PropertyVetoException JavaDoc {
119
120         // Remember the tests run in the same VM and repository is singleton.
121
// Once it is created for the first time it will stick around forever.
122
Repository repository = (Repository) Lookup.getDefault().lookup(Repository.class);
123         if (repository == null) {
124             repository = new Repository(new SystemFileSystem(fs));
125         } else {
126             ((SystemFileSystem) repository.getDefaultFileSystem()).setOrig(fs);
127         }
128         
129         Object JavaDoc[] lookupContent = new Object JavaDoc[instances.length + 1];
130         lookupContent[0] = repository;
131         System.arraycopy(instances, 0, lookupContent, 1, instances.length);
132
133         // Create the Services folder (if needed}
134
FileObject services = repository.getDefaultFileSystem().findResource("Services");
135         if (services == null) {
136             services = repository.getDefaultFileSystem().getRoot().createFolder("Services");
137         }
138         
139         DEFAULT_LOOKUP.setLookup(lookupContent, cl, services, exclude);
140     }
141
142     private static FileSystem createLocalFileSystem(File JavaDoc mountPoint, String JavaDoc[] resources) throws IOException JavaDoc {
143         mountPoint.mkdir();
144         
145         for (int i = 0; i < resources.length; i++) {
146             createFileOnPath(mountPoint, resources[i]);
147         }
148         
149         LocalFileSystem lfs = new StatusFileSystem();
150         try {
151         lfs.setRootDirectory(mountPoint);
152         } catch (Exception JavaDoc ex) {}
153         
154         return lfs;
155     }
156
157     private static void createFileOnPath(File JavaDoc mountPoint, String JavaDoc path) throws IOException JavaDoc{
158         mountPoint.mkdir();
159         
160         File JavaDoc f = new File JavaDoc (mountPoint, path);
161         if (f.isDirectory() || path.endsWith("/")) {
162             f.mkdirs();
163         }
164         else {
165             f.getParentFile().mkdirs();
166             try {
167                 f.createNewFile();
168             } catch (IOException JavaDoc iex) {
169                 throw new IOException JavaDoc ("While creating " + path + " in " + mountPoint.getAbsolutePath() + ": " + iex.toString() + ": " + f.getAbsolutePath());
170             }
171         }
172     }
173     
174     private static class StatusFileSystem extends LocalFileSystem {
175         Status status = new Status () {
176             public String JavaDoc annotateName (String JavaDoc name, java.util.Set JavaDoc files) {
177                 return name;
178             }
179
180             public java.awt.Image JavaDoc annotateIcon (java.awt.Image JavaDoc icon, int iconType, java.util.Set JavaDoc files) {
181                 return icon;
182             }
183         };
184         
185         public org.openide.filesystems.FileSystem.Status getStatus() {
186             return status;
187         }
188         
189     }
190     
191     private static class SystemFileSystem extends MultiFileSystem {
192         public SystemFileSystem(FileSystem [] orig) {
193             super(orig);
194         }
195         
196         public void setOrig(FileSystem [] orig) {
197             setDelegates(orig);
198         }
199     }
200 }
201
Popular Tags