1 22 package org.netbeans.modules.editor.errorstripe; 23 24 25 import java.beans.PropertyVetoException ; 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.net.URL ; 29 import javax.swing.text.Utilities ; 30 import junit.framework.Assert; 31 import org.netbeans.junit.NbTestCase; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.filesystems.LocalFileSystem; 35 import org.openide.filesystems.Repository; 36 import org.openide.filesystems.XMLFileSystem; 37 import org.openide.util.Lookup; 38 import org.openide.util.lookup.Lookups; 39 import org.openide.util.lookup.ProxyLookup; 40 import org.xml.sax.SAXException ; 41 42 46 public class UnitUtilities extends ProxyLookup { 47 48 public static UnitUtilities DEFAULT_LOOKUP = null; 49 50 public UnitUtilities() { 51 Assert.assertNull(DEFAULT_LOOKUP); 52 DEFAULT_LOOKUP = this; 53 } 54 55 58 public static void setLookup(Object [] instances, ClassLoader cl) { 59 DEFAULT_LOOKUP.setLookups(new Lookup[] { 61 Lookups.fixed(instances), 62 Lookups.metaInfServices(cl), 63 Lookups.singleton(cl), 64 }); 65 } 66 67 public static void prepareTest(String [] additionalLayers, Object [] additionalLookupContent) throws IOException , SAXException , PropertyVetoException { 68 URL [] layers = new URL [additionalLayers.length + 1]; 69 70 layers[0] = Utilities .class.getResource("/org/netbeans/modules/editor/errorstripe/resources/layer.xml"); 71 72 for (int cntr = 0; cntr < additionalLayers.length; cntr++) { 73 layers[cntr + 1] = Utilities .class.getResource(additionalLayers[cntr]); 74 } 75 76 for (int cntr = 0; cntr < layers.length; cntr++) { 77 if (layers[cntr] == null) { 78 throw new IllegalStateException ("layers[" + cntr + "]=null"); 79 } 80 } 81 82 XMLFileSystem system = new XMLFileSystem(); 83 system.setXmlUrls(layers); 84 85 Repository repository = new Repository(system); 86 Object [] lookupContent = new Object [additionalLookupContent.length + 1]; 87 88 System.arraycopy(additionalLookupContent, 0, lookupContent, 1, additionalLookupContent.length); 89 90 lookupContent[0] = repository; 91 92 DEFAULT_LOOKUP.setLookup(lookupContent, UnitUtilities.class.getClassLoader()); 93 } 94 95 static { 96 UnitUtilities.class.getClassLoader().setDefaultAssertionStatus(true); 97 System.setProperty("org.openide.util.Lookup", UnitUtilities.class.getName()); 98 Assert.assertEquals(UnitUtilities.class, Lookup.getDefault().getClass()); 99 } 100 101 public static void initLookup() { 102 } 104 105 110 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 111 test.clearWorkDir(); 112 File root = test.getWorkDir(); 113 assert root.isDirectory() && root.list().length == 0; 114 FileObject fo = FileUtil.toFileObject(root); 115 if (fo != null) { 116 return fo; 118 } else { 119 LocalFileSystem lfs = new LocalFileSystem(); 121 try { 122 lfs.setRootDirectory(root); 123 } catch (PropertyVetoException e) { 124 assert false : e; 125 } 126 Repository.getDefault().addFileSystem(lfs); 127 return lfs.getRoot(); 128 } 129 } 130 131 } 132 | Popular Tags |