1 19 20 package org.netbeans.core.startup.layers; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.URL ; 25 import java.util.Arrays ; 26 import java.util.Collections ; 27 import java.util.List ; 28 import java.util.Map ; 29 import org.netbeans.junit.NbTestCase; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileSystem; 32 import org.openide.filesystems.MultiFileSystem; 33 36 public abstract class CacheManagerTestBaseHid extends NbTestCase { 37 private long initTime = System.currentTimeMillis (); 38 39 42 public static Object method(FileObject fo, String attr) { 43 return String.valueOf(fo.getAttribute("x")) + "/" + attr; 45 } 46 47 public static Object map1(Map map) { 48 return String.valueOf(map.get("x")); 49 } 50 public static Object map2(Map map, String attr) { 51 return String.valueOf(map.get("x")) + "/" + attr; 52 } 53 54 protected CacheManagerTestBaseHid(String name) { 55 super(name); 56 } 57 58 protected static interface ManagerFactory { 59 LayerCacheManager createManager() throws Exception ; 60 boolean supportsTimestamps (); 61 } 62 63 public void testCacheManager() throws Exception { 64 ManagerFactory mf = (ManagerFactory)this; 65 66 clearWorkDir(); 67 LayerCacheManager m = mf.createManager(); 68 assertFalse(m.cacheExists()); 69 List urls = Arrays.asList(new URL [] { 71 CacheManagerTestBaseHid.class.getResource("data/layer2.xml"), 72 CacheManagerTestBaseHid.class.getResource("data/layer1.xml"), 73 }); 74 FileSystem f; 75 if (m.supportsLoad()) { 76 f = m.createEmptyFileSystem(); 77 assertEquals(Collections.EMPTY_LIST, Arrays.asList(f.getRoot().getChildren())); 78 m.store(f, urls); 79 } else { 80 f = m.store(urls); 81 } 82 checkStruct(f); 84 if (mf.supportsTimestamps ()) { 85 checkLastModified (f, "foo/test2", "data/test2a"); 86 checkLastModified (f, "bar/test5", "data/layer2.xml"); 87 checkLastModified (f, "baz/thingy", "data/layer1.xml"); 88 checkLastModified (f, "foo/test1", "data/layer1.xml"); 89 checkLastModified (f, "bug39210/inline.txt", "data/layer1.xml"); 90 } 91 if (m.cacheExists()) { 92 m = mf.createManager(); 94 f = m.createLoadedFileSystem(); 95 checkStruct(f); 96 if (m.supportsLoad()) { 97 f = m.createEmptyFileSystem(); 99 assertEquals(Collections.EMPTY_LIST, Arrays.asList(f.getRoot().getChildren())); 100 m.load(f); 101 checkStruct(f); 102 } 103 } 104 } 105 106 private void checkLastModified (FileSystem f, String file, String resource) throws Exception { 107 FileObject obj = f.findResource (file); 108 assertNotNull (file + " found", obj); 109 110 long time = obj.lastModified ().getTime (); 111 URL url = CacheManagerTestBaseHid.class.getResource(resource); 112 long resourceTime = url.openConnection ().getLastModified (); 113 114 if (initTime < resourceTime) { 115 fail ("The time of the resource " + file + " (" + resourceTime + ") is likely older than iniciation of this class (" + initTime + ")"); 116 } 117 118 assertEquals ("Time of " + file + " is the same as URL time of " + resource, resourceTime, time); 119 120 121 } 122 123 private void checkStruct(FileSystem f) throws Exception { 124 assertEquals("Root has 5 children", 5, f.getRoot().getChildren().length); 125 assertEquals(1, f.findResource("bar").getChildren().length); 126 assertEquals("", slurp(f, "bar/test5")); 127 assertEquals(5, f.findResource("foo").getChildren().length); 128 assertEquals("two", attr(f, "foo/test1", "y")); 131 assertEquals("rara!", slurp(f, "foo/test2")); 132 assertEquals("hi!", slurp(f, "foo/test3")); 133 assertEquals("three", attr(f, "foo/test3", "x")); 134 assertEquals("one too", attr(f, "foo/test3", "y")); 135 assertEquals("", slurp(f, "foo/test4")); 136 FixedFileSystem ffs = new FixedFileSystem("ffs", "FFS"); 138 FixedFileSystem.Instance i = new FixedFileSystem.Instance(false, null, null, null, (URL )null); 139 i.writeAttribute("x", "val"); 140 ffs.add("foo/29356", i); 141 MultiFileSystem mfs = new MultiFileSystem(new FileSystem[] {f, ffs}); 142 assertEquals("val", attr(ffs, "foo/29356", "x")); 143 assertEquals("val", attr(mfs, "foo/29356", "x")); 144 assertEquals("val/a", attr(mfs, "foo/29356", "a")); 145 assertEquals("val", attr(mfs, "foo/29356", "map1")); 146 assertEquals("val/map2", attr(mfs, "foo/29356", "map2")); 147 } 148 149 private static String slurp(FileSystem f, String path) throws IOException { 150 FileObject fo = f.findResource(path); 151 if (fo == null) return null; 152 InputStream is = fo.getInputStream(); 153 StringBuffer text = new StringBuffer ((int)fo.getSize()); 154 byte[] buf = new byte[1024]; 155 int read; 156 while ((read = is.read(buf)) != -1) { 157 text.append(new String (buf, 0, read, "US-ASCII")); 158 } 159 return text.toString(); 160 } 161 162 private static Object attr(FileSystem f, String path, String a) throws IOException { 163 FileObject fo = f.findResource(path); 164 if (fo == null) return null; 165 return fo.getAttribute(a); 166 } 167 168 } 169 | Popular Tags |