1 19 20 package org.openide.loaders; 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyVetoException ; 23 import java.beans.VetoableChangeListener ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.util.HashSet ; 26 import org.openide.NotifyDescriptor; 27 import org.openide.cookies.EditorCookie; 28 import org.openide.filesystems.*; 29 import org.openide.nodes.Node; 30 import org.openide.text.DataEditorSupport; 31 import org.openide.util.Lookup; 32 import java.io.IOException ; 33 import java.util.Arrays ; 34 import javax.swing.text.Document ; 35 import org.netbeans.junit.*; 36 import org.openide.cookies.OpenCookie; 37 38 39 42 public class RefusesInvalidationTest extends NbTestCase { 43 44 public RefusesInvalidationTest(String name) { 45 super(name); 46 } 47 48 49 protected void setUp() throws Exception { 50 System.setProperty("org.openide.util.Lookup", RefusesInvalidationTest.class.getName() + "$Lkp"); 51 52 super.setUp(); 53 54 Lookup l = Lookup.getDefault(); 55 if (!(l instanceof Lkp)) { 56 fail("Wrong lookup: " + l); 57 } 58 59 clearWorkDir(); 60 } 61 62 63 64 public void testWhatHappensWhenALoaderBecomesInvalidAndFileIsOpened() throws Exception { 65 final ForgetableLoader l = (ForgetableLoader)DataLoader.getLoader(ForgetableLoader.class); 66 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), new String [] { 67 "folder/f.keep", 68 "folder/f.forget", 69 }); 70 71 FileObject fo = lfs.findResource("folder"); 72 final DataFolder f = DataFolder.findFolder(fo); 73 74 FileObject primary = lfs.findResource("folder/f.keep"); 75 76 final DataObject our = DataObject.find(primary); 77 assertEquals("The right loader", l, our.getLoader()); 78 79 class AddAndRemoveAFile implements FileSystem.AtomicAction { 80 DataObject[] all; 81 82 public void run () throws IOException { 83 FileObject[] two = (FileObject[])our.files().toArray(new FileObject[0]); 84 assertEquals("Two", 2, two.length); 85 86 two[1].delete(); 88 89 90 l.filesInside = new HashSet (); 91 all = f.getChildren(); 92 if (!l.filesInside.contains(two[0])) { 93 fail("We should query our secondary file: " + l.filesInside); 94 } 95 } 96 } 97 98 AddAndRemoveAFile addRemove = new AddAndRemoveAFile(); 99 f.getPrimaryFile().getFileSystem().runAtomicAction(addRemove); 100 101 FileObject[] children = f.getPrimaryFile().getChildren(); 102 assertEquals("One child", 1, children.length); 103 DataObject obj = DataObject.find(children[0]); 104 assertEquals("Object found", children[0], obj.getPrimaryFile()); 105 assertEquals("Object is the same as it tries to prevent to invalidate itself", our, obj); 106 107 assertNotNull("Children computed", addRemove.all); 108 assertEquals("Three of them: " + Arrays.asList(addRemove.all), 1, addRemove.all.length); 109 assertEquals("Object is the same as it tries to prevent to invalidate itself", our, addRemove.all[0]); 110 111 DataObject[] all = f.getChildren(); 112 assertNotNull("Children computed", all); 113 assertEquals("Three of them: " + Arrays.asList(all), 1, all.length); 114 assertEquals("Object is the same as it tries to prevent to invalidate itself", our, all[0]); 115 } 116 117 public static final class ForgetableLoader extends MultiFileLoader 118 implements VetoableChangeListener { 119 PropertyChangeEvent lastEvent; 120 HashSet filesInside; 121 122 public ForgetableLoader () { 123 super(MultiDataObject.class); 124 } 125 protected String displayName() { 126 return "ForgetableLoader"; 127 } 128 130 protected FileObject findPrimaryFile(FileObject fo) { 131 if (filesInside != null) { 132 filesInside.add(fo); 135 } else { 136 assertFalse("We cannot be queried from recognizer thread: ", FolderList.isFolderRecognizerThread()); 137 } 138 139 FileObject forget = FileUtil.findBrother(fo, "forget"); 140 FileObject keep = FileUtil.findBrother(fo, "keep"); 141 if (keep == null || forget == null) { 142 return null; 143 } 144 return fo == keep || fo == forget ? keep : null; 145 } 146 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 147 MultiDataObject m = new MultiDataObject (primaryFile, this); 148 m.addVetoableChangeListener(this); 149 return m; 150 } 151 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 152 return new FileEntry (obj, primaryFile); 153 } 154 protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { 155 return new FileEntry(obj, secondaryFile); 156 } 157 158 public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException { 159 this.lastEvent = evt; 160 throw new PropertyVetoException ("Cannot change isValid", evt); 161 } 162 } 163 164 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 165 public Lkp() { 166 this(new org.openide.util.lookup.InstanceContent()); 167 } 168 169 private Lkp(org.openide.util.lookup.InstanceContent ic) { 170 super(ic); 171 ic.add(new Pool ()); 172 } 173 } 174 175 private static final class Pool extends DataLoaderPool { 176 protected java.util.Enumeration loaders () { 177 DataLoader extra = DataLoader.getLoader(ForgetableLoader.class); 178 return org.openide.util.Enumerations.singleton (extra); 179 } 180 } 181 } 182 | Popular Tags |