1 19 20 package org.netbeans.modules.editor.mimelookup; 21 22 23 import java.beans.PropertyVetoException ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.URL ; 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 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 [] instances, ClassLoader cl, FileObject servicesFolder, Class [] 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 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 [] files, File workDir, Object [] instances, ClassLoader cl) 93 throws IOException , PropertyVetoException { 94 setLookup(files, workDir, instances, cl, null); 95 } 96 97 public static void setLookup(String [] files, File workDir, Object [] instances, ClassLoader cl, Class [] exclude) 98 throws IOException , PropertyVetoException { 99 FileSystem fs = createLocalFileSystem(workDir, files); 100 setLookup(new FileSystem [] { fs }, instances, cl, exclude); 101 } 102 103 public static void setLookup(URL [] layers, File workDir, Object [] instances, ClassLoader cl) 104 throws IOException , PropertyVetoException { 105 setLookup(layers, workDir, instances, cl, null); 106 } 107 108 public static void setLookup(URL [] layers, File workDir, Object [] instances, ClassLoader cl, Class [] exclude) 109 throws IOException , PropertyVetoException { 110 FileSystem writeableFs = createLocalFileSystem(workDir, new String [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 [] instances, ClassLoader cl, Class [] exclude) 118 throws IOException , PropertyVetoException { 119 120 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 [] lookupContent = new Object [instances.length + 1]; 130 lookupContent[0] = repository; 131 System.arraycopy(instances, 0, lookupContent, 1, instances.length); 132 133 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 mountPoint, String [] resources) throws IOException { 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 ex) {} 153 154 return lfs; 155 } 156 157 private static void createFileOnPath(File mountPoint, String path) throws IOException { 158 mountPoint.mkdir(); 159 160 File f = new File (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 iex) { 169 throw new IOException ("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 annotateName (String name, java.util.Set files) { 177 return name; 178 } 179 180 public java.awt.Image annotateIcon (java.awt.Image icon, int iconType, java.util.Set 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 |