1 13 14 package org.openide.loaders; 15 16 import java.io.IOException ; 17 import java.util.Enumeration ; 18 import java.util.logging.Level ; 19 import java.util.logging.Logger ; 20 import org.netbeans.junit.MockServices; 21 import org.netbeans.junit.NbTestCase; 22 import org.openide.filesystems.FileObject; 23 import org.openide.filesystems.FileSystem; 24 import org.openide.filesystems.FileUtil; 25 import org.openide.filesystems.Repository; 26 import org.openide.nodes.Children; 27 import org.openide.nodes.Node; 28 import org.openide.util.Enumerations; 29 import org.openide.util.lookup.Lookups; 30 31 34 public class DataShadowLookupTest extends NbTestCase 35 implements java.net.URLStreamHandlerFactory { 36 37 private DataObject original; 38 39 private DataFolder folder; 40 41 private FileSystem lfs; 42 43 private Logger err; 44 45 static { 46 MockServices.setServices(new Class [] { Pool.class }); 49 } 50 51 public DataShadowLookupTest (String name) { 52 super(name); 53 } 54 55 protected Level logLevel() { 56 return Level.INFO; 57 } 58 59 protected void setUp() throws Exception { 60 61 lfs = Repository.getDefault ().getDefaultFileSystem (); 62 63 FileObject[] delete = lfs.getRoot().getChildren(); 64 for (int i = 0; i < delete.length; i++) { 65 delete[i].delete(); 66 } 67 68 69 FileObject fo = FileUtil.createData (lfs.getRoot (), getName () + "/folder/original.string"); 70 assertNotNull(fo); 71 original = DataObject.find (fo); 72 assertFalse ("Just to be sure that this is not shadow", original instanceof DataShadow); 73 assertEquals ("It is the right class", StringObject.class, original.getClass ()); 74 fo = FileUtil.createFolder (lfs.getRoot (), getName () + "/modify"); 75 assertNotNull(fo); 76 assertTrue (fo.isFolder ()); 77 folder = DataFolder.findFolder (fo); 78 79 Repository.getDefault ().addFileSystem (lfs); 80 81 err = Logger.getLogger(getName()); 82 } 83 84 public java.net.URLStreamHandler createURLStreamHandler(String protocol) { 85 if (protocol.equals ("nbfs")) { 86 return FileUtil.nbfsURLStreamHandler (); 87 } 88 return null; 89 } 90 91 public void testStringIsInLookupOfDataShadow() throws Exception { 92 DataShadow shade = original.createShadow(folder); 93 94 { 95 String s = (String )original.getNodeDelegate().getLookup().lookup(String .class); 96 assertNotNull("String is in the original's lookup", s); 97 } 98 99 assertSame(shade.getOriginal(), original); 100 String s = (String )shade.getNodeDelegate().getLookup().lookup(String .class); 101 assertNotNull("String is in the lookup", s); 102 assertEquals("It is the name of the original", original.getName(), s); 103 } 104 105 public static final class Pool extends DataLoaderPool { 106 protected Enumeration loaders() { 107 return Enumerations.singleton(StringLoader.findObject(StringLoader.class, true)); 108 } 109 110 } 111 112 private static final class StringLoader extends UniFileLoader { 113 public StringLoader() { 114 super("org.openide.loaders.StringObject"); 115 getExtensions().addExtension("string"); 116 } 117 118 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 119 return new StringObject(this, primaryFile); 120 } 121 122 } 124 private static final class StringObject extends MultiDataObject { 125 public StringObject(StringLoader l, FileObject fo) throws DataObjectExistsException { 126 super(fo, l); 127 } 128 129 protected Node createNodeDelegate() { 130 return new DataNode(this, Children.LEAF, Lookups.singleton(getName())); 131 } 132 } 134 135 } 136 | Popular Tags |