1 19 20 package org.openide.loaders; 21 22 import junit.textui.TestRunner; 23 import org.openide.ErrorManager; 24 25 import org.openide.filesystems.*; 26 import org.openide.util.Lookup; 27 import java.io.IOException ; 28 import java.util.*; 29 import org.netbeans.junit.*; 30 import java.beans.PropertyChangeListener ; 31 32 35 public class Deadlock35847Test extends LoggingTestCaseHid { 36 private ErrorManager err; 37 38 public Deadlock35847Test(String name) { 39 super(name); 40 } 41 42 protected void setUp() throws Exception { 43 clearWorkDir(); 44 err = ErrorManager.getDefault().getInstance("TEST-" + getName()); 45 registerIntoLookup(new Pool()); 46 } 47 48 public void testLoaderThatStopsToRecognizeWhatItHasRecognized () throws Exception { 49 ForgetableLoader l = (ForgetableLoader)ForgetableLoader.getLoader(ForgetableLoader.class); 50 51 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), new String [] { 52 "folder/f.forget", 53 "folder/f.keep" 54 }); 55 56 l.forget = true; 58 59 FileObject fo = lfs.findResource("folder"); 60 DataFolder f = DataFolder.findFolder(fo); 61 62 63 DataObject[] arr = f.getChildren (); 64 assertEquals ("Two child there", 2, arr.length); 65 66 DataObject keep; 67 java.lang.ref.WeakReference forget; 68 if (arr[0].getPrimaryFile().hasExt ("keep")) { 69 keep = arr[0]; 70 forget = new java.lang.ref.WeakReference (arr[1]); 71 } else { 72 keep = arr[1]; 73 forget = new java.lang.ref.WeakReference (arr[0]); 74 } 75 76 org.openide.nodes.Node theDelegate = new org.openide.nodes.FilterNode (keep.getNodeDelegate()); 77 78 arr = null; 79 assertGC ("Forgetable object can be forgeted", forget); 80 81 class P extends org.openide.nodes.NodeAdapter 82 implements java.beans.PropertyChangeListener { 83 int cnt; 84 String name; 85 86 public void propertyChange (java.beans.PropertyChangeEvent ev) { 87 name = ev.getPropertyName(); 88 cnt++; 89 err.log("Event arrived: " + ev.getPropertyName()); 90 } 91 } 92 P listener = new P (); 93 keep.addPropertyChangeListener (listener); 94 theDelegate.addNodeListener(listener); 96 97 l.forget = false; 99 100 err.log("Beging to get children"); 102 DataObject[] newArr = f.getChildren (); 103 err.log("End of get children"); 104 105 assertEquals ("Keep is Invalidated", 1, listener.cnt); 106 assertEquals ("Property is PROP_VALID", DataObject.PROP_VALID, listener.name); 107 } 108 109 public void testLoaderThatStopsToRecognizeWhatItHasRecognizedAndDoesItWhileHoldingChildrenMutex () throws Exception { 110 org.openide.nodes.Children.MUTEX.readAccess (new org.openide.util.Mutex.ExceptionAction () { 111 public Object run () throws Exception { 112 testLoaderThatStopsToRecognizeWhatItHasRecognized (); 113 return null; 114 } 115 }); 116 } 117 118 119 public static final class ForgetableLoader extends MultiFileLoader { 120 public boolean forget; 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 (forget) { 132 return null; 133 } 134 if (fo.hasExt ("forget")) { 135 return FileUtil.findBrother (fo, "keep"); 136 } 137 if (fo.hasExt ("keep")) { 138 return fo; 139 } 140 return null; 141 } 142 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 143 return new MultiDataObject (primaryFile, this); 144 } 145 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 146 return new FileEntry (obj, primaryFile); 147 } 148 protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { 149 return new FileEntry(obj, secondaryFile); 150 } 151 } 152 private static final class Pool extends DataLoaderPool { 153 public Pool() { 154 } 155 156 public Enumeration loaders() { 157 ForgetableLoader l = (ForgetableLoader)ForgetableLoader.getLoader(ForgetableLoader.class); 158 return org.openide.util.Enumerations.singleton(l); 159 } 160 } 161 } 162 | Popular Tags |