1 19 20 package org.openide.loaders; 21 22 import java.lang.ref.WeakReference ; 23 import javax.swing.event.ChangeEvent ; 24 import junit.textui.TestRunner; 25 26 import org.openide.filesystems.*; 27 import java.io.IOException ; 28 import java.util.*; 29 import org.netbeans.junit.*; 30 import org.openide.util.Lookup; 31 32 35 public class DataLoaderPoolTest extends NbTestCase { 36 private FileSystem lfs; 37 private DataLoader loaderA; 38 private DataLoader loaderB; 39 private Pool pool; 40 41 static { 42 System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.DataLoaderPoolTest$Lkp"); } 44 45 public DataLoaderPoolTest(String name) { 46 super(name); 47 } 48 49 protected void setUp() throws Exception { 50 DataLoaderPool p = DataLoaderPool.getDefault (); 51 assertNotNull (p); 52 assertEquals (Pool.class, p.getClass ()); 53 pool = (Pool)p; 54 pool.clear(false); 55 56 loaderA = DataLoader.getLoader(SimpleAUniFileLoader.class); 57 loaderB = DataLoader.getLoader(SimpleBUniFileLoader.class); 58 59 clearWorkDir(); 60 lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String [] { 61 "folder/file.simple", 62 }); 63 64 } 65 66 protected void tearDown() throws Exception { 67 WeakReference ref = new WeakReference (lfs); 68 lfs = null; 69 assertGC("File system can disappear", ref); 70 } 71 72 75 protected void doSetPreferredLoader (FileObject fo, DataLoader loader) throws IOException { 76 pool.setPreferredLoader (fo, loader); 77 } 78 79 82 public void testSetPrefferedloader () throws Exception { 83 assertTrue(Arrays.asList(pool.toArray()).contains(loaderA)); 84 assertTrue(Arrays.asList(pool.toArray()).contains(loaderB)); 85 86 FileObject fo = lfs.findResource("folder/file.simple"); 87 assertNotNull(fo); 88 89 doSetPreferredLoader (fo, loaderA); 90 DataObject doa = DataObject.find (fo); 91 assertSame (loaderA, doa.getLoader ()); 92 93 doSetPreferredLoader (fo, loaderB); 94 DataObject dob = DataObject.find (fo); 95 assertTrue ("DataObject wasn't refreshed after the prefered loader has been changed.", dob != doa); 96 assertSame (loaderB, dob.getLoader ()); 97 } 98 99 102 public void testClearPrefferedloader() throws Exception { 103 int indxA = Arrays.asList(pool.toArray()).indexOf(loaderA); 104 int indxB = Arrays.asList(pool.toArray()).indexOf(loaderB); 105 106 assertTrue ("It is there", indxA != -1); 107 assertTrue ("It is there", indxB != -1); 108 assertTrue (indxB + " is before " + indxA, indxB < indxA); 109 110 FileObject fo = lfs.findResource("folder/file.simple"); 111 assertNotNull(fo); 112 113 DataObject initial = DataObject.find(fo); 114 assertSame("Loader B is now before loaderA", loaderB, initial.getLoader ()); 115 116 doSetPreferredLoader(fo, loaderA); 117 DataObject doa = DataObject.find(fo); 118 assertTrue("DataObject should refresh itself", initial != doa); 119 assertSame("Loader A took over the object", loaderA, doa.getLoader()); 120 121 doSetPreferredLoader(fo, null); 122 DataObject dob = DataObject.find(fo); 123 assertTrue("DataObject should refresh itself", dob != doa); 124 assertSame("Againg loader B takes over", loaderB, dob.getLoader()); 125 } 126 127 public void testChangeIsAlsoReflectedInNodes () throws Exception { 128 FileObject fo = lfs.findResource("folder"); 129 assertNotNull(fo); 130 131 DataFolder folder = DataFolder.findFolder(fo); 132 133 org.openide.nodes.Node n = folder.getNodeDelegate(); 134 org.openide.nodes.Node[] arr = n.getChildren().getNodes (true); 135 136 assertEquals ("One child is there", 1, arr.length); 137 DataObject initial = (DataObject)arr[0].getCookie (DataObject.class); 138 assertNotNull ("DataObject is cookie of the node", initial); 139 assertSame("Loader B is now before loaderA", loaderB, initial.getLoader()); 140 141 fo = lfs.findResource("folder/file.simple"); 142 assertNotNull(fo); 143 doSetPreferredLoader(fo, loaderA); 144 145 assertSame ("Changes is reflected on data object level", loaderA, DataObject.find (fo).getLoader ()); 146 DataObject[] children = folder.getChildren(); 147 assertEquals ("There is one", 1, children.length); 148 assertSame ("Change is reflected on DataFolder level", loaderA, children[0].getLoader ()); 149 150 arr = n.getChildren().getNodes(true); 151 assertEquals("One child is there", 1, arr.length); 152 DataObject newOne = (DataObject)arr[0].getCookie(DataObject.class); 153 assertNotNull("DataObject is cookie of the node", newOne); 154 assertSame("There has been a change", loaderA, newOne.getLoader()); 155 } 156 157 public void testHowManyTimesWeCallDLPloaders() throws Exception { 158 FileObject fo = lfs.findResource("folder/file.simple"); 159 assertNotNull(fo); 160 161 FileObject f1 = FileUtil.createData(fo.getParent(), "f1.simple"); 162 FileObject f2 = FileUtil.createData(fo.getParent(), "f2.simple"); 163 FileObject f3 = FileUtil.createData(fo.getParent(), "f3.simple"); 164 165 166 FileObject[] all = fo.getParent().getChildren(); 167 assertEquals("No calls to pool yet", 0, pool.cnt); 168 pool.clear(true); 169 170 for (int i = 0; i < all.length; i++) { 171 DataObject o = DataObject.find(all[i]); 172 assertEquals("Only one call even for " + all[i], 1, pool.cnt); 173 assertEquals("loaderB is first for " + all[i], loaderB, o.getLoader()); 174 } 175 176 pool.loaders.remove(loaderB); 177 pool.fireChangeEvent(new ChangeEvent (pool)); 178 179 for (int i = 0; i < all.length; i++) { 180 DataObject o = DataObject.find(all[i]); 181 assertEquals("One more call - " + all[i], 2, pool.cnt); 182 assertEquals("loaderA is the only one " + all[i], loaderA, o.getLoader()); 183 } 184 185 } 186 187 public static final class SimpleAUniFileLoader extends UniFileLoader { 188 public SimpleAUniFileLoader() { 189 super(SimpleDataObject.class.getName()); 190 } 191 protected void initialize() { 192 super.initialize(); 193 getExtensions().addExtension("simple"); 194 } 195 protected String displayName() { 196 return "SimpleA"; 197 } 198 protected MultiDataObject createMultiObject(FileObject pf) throws IOException { 199 return new SimpleDataObject(pf, this); 200 } 201 } 202 public static final class SimpleBUniFileLoader extends UniFileLoader { 203 public SimpleBUniFileLoader() { 204 super(SimpleDataObject.class.getName()); 205 } 206 protected void initialize() { 207 super.initialize(); 208 getExtensions().addExtension("simple"); 209 } 210 protected String displayName() { 211 return "SimpleB"; 212 } 213 protected MultiDataObject createMultiObject(FileObject pf) throws IOException { 214 return new SimpleDataObject(pf, this); 215 } 216 } 217 public static final class SimpleDataObject extends MultiDataObject { 218 public SimpleDataObject(FileObject pf, MultiFileLoader loader) throws IOException { 219 super(pf, loader); 220 } 221 } 222 223 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 224 public Lkp () { 225 this (new org.openide.util.lookup.InstanceContent ()); 226 } 227 228 private Lkp (org.openide.util.lookup.InstanceContent ic) { 229 super (ic); 230 ic.add (new Pool ()); 231 } 232 } 233 234 private static final class Pool extends DataLoaderPool { 235 List loaders; 236 int cnt; 237 238 public Pool () { 239 } 240 241 public void clear(boolean ass) { 242 loaders = null; 243 cnt = 0; 244 fireChangeEvent(new ChangeEvent (this)); 245 if (ass) { 246 assertEquals("No call to loaders", 0, cnt); 247 } 248 cnt = 0; 249 } 250 251 public java.util.Enumeration loaders () { 252 cnt++; 253 254 if (loaders == null) { 255 loaders = new ArrayList (); 256 DataLoader loaderA = DataLoader.getLoader(SimpleAUniFileLoader.class); 257 DataLoader loaderB = DataLoader.getLoader(SimpleBUniFileLoader.class); 258 loaders.add (loaderB); 259 loaders.add (loaderA); 260 } 261 return Collections.enumeration (loaders); 262 } 263 } 264 } 265 | Popular Tags |