1 19 20 package org.openide.loaders; 21 22 import org.netbeans.junit.NbTestCase; 23 import org.openide.filesystems.FileAttributeEvent; 24 import org.openide.filesystems.FileChangeListener; 25 import org.openide.filesystems.FileEvent; 26 import org.openide.filesystems.FileObject; 27 import org.openide.filesystems.FileRenameEvent; 28 import org.openide.filesystems.FileSystem; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.filesystems.Repository; 31 import org.openide.util.RequestProcessor; 32 33 38 public class Deadlock59522Test extends NbTestCase implements FileChangeListener { 39 FileObject toolbars; 40 FileSystem fs; 41 DataFolder toolbarsFolder; 42 DataFolder anotherFolder; 43 DataObject obj; 44 DataObject anotherObj; 45 46 47 Exception assigned; 48 boolean called; 49 boolean ok; 50 51 private Object BIG_MDR_LOCK = new Object (); 52 53 public Deadlock59522Test(String testName) { 54 super (testName); 55 } 56 57 protected void setUp() throws Exception { 58 fs = Repository.getDefault ().getDefaultFileSystem (); 59 FileObject root = fs.getRoot (); 60 toolbars = FileUtil.createFolder (root, "Toolbars"); 61 toolbarsFolder = DataFolder.findFolder (toolbars); 62 FileObject[] arr = toolbars.getChildren (); 63 for (int i = 0; i < arr.length; i++) { 64 arr[i].delete (); 65 } 66 FileObject fo = FileUtil.createData (root, "Ahoj.txt"); 67 obj = DataObject.find (fo); 68 fo = FileUtil.createFolder (root, "Another"); 69 anotherFolder = DataFolder.findFolder (fo); 70 fo = FileUtil.createData (root, "Another.txt"); 71 anotherObj = DataObject.find (fo); 72 73 fs.addFileChangeListener (this); 74 } 75 76 protected void tearDown() throws Exception { 77 fs.removeFileChangeListener (this); 78 79 assertTrue ("The doRenameAObjectWhileHoldingMDRLock must be called", called); 80 81 FileObject[] arr = toolbars.getChildren (); 82 for (int i = 0; i < arr.length; i++) { 83 arr[i].delete (); 84 } 85 86 if (assigned != null) { 87 throw assigned; 88 } 89 } 90 private static int cnt = 0; 91 private void startRename() throws Exception { 92 synchronized (BIG_MDR_LOCK) { 93 RequestProcessor.getDefault ().post (new Runnable () { 94 public void run () { 95 synchronized (BIG_MDR_LOCK) { 96 try { 97 called = true; 98 BIG_MDR_LOCK.notify(); 99 BIG_MDR_LOCK.wait(); anotherObj.rename ("mynewname" + cnt++); 102 ok = true; 103 } catch (Exception ex) { 104 assigned = ex; 105 } finally { 106 BIG_MDR_LOCK.notifyAll(); 108 } 109 } 110 } 111 }); 112 BIG_MDR_LOCK.wait(); 113 } 114 } 115 116 private boolean wasHereOnce; 117 private void lockMdr() { 118 if (wasHereOnce) { 119 return; 120 } 121 122 wasHereOnce = true; 123 fs.removeFileChangeListener(this); 125 126 synchronized (BIG_MDR_LOCK) { 127 BIG_MDR_LOCK.notify(); try { 129 BIG_MDR_LOCK.wait(); 130 } catch (InterruptedException ex) { 131 ex.printStackTrace(); 132 fail ("No InterruptedExceptions"); 133 } 134 assertTrue ("Rename finished ok", ok); 135 } 136 } 137 138 139 public void testMove () throws Exception { 140 synchronized (BIG_MDR_LOCK) { 141 startRename(); 142 obj.move (anotherFolder); 143 } 144 } 145 146 public void testCopy () throws Exception { 147 synchronized (BIG_MDR_LOCK) { 148 startRename(); 149 obj.copy (anotherFolder); 150 } 151 } 152 153 public void testRename () throws Exception { 154 synchronized (BIG_MDR_LOCK) { 155 startRename(); 156 obj.rename ("NewName.txt"); 157 } 158 } 159 160 public void testCreateShadow () throws Exception { 161 synchronized (BIG_MDR_LOCK) { 162 startRename(); 163 obj.createShadow (anotherFolder); 164 } 165 } 166 167 public void testTemplate () throws Exception { 168 synchronized (BIG_MDR_LOCK) { 169 startRename(); 170 obj.createFromTemplate (anotherFolder); 171 } 172 } 173 174 public void testTemplate2 () throws Exception { 175 synchronized (BIG_MDR_LOCK) { 176 startRename(); 177 obj.createFromTemplate (anotherFolder, "AhojVole.txt"); 178 } 179 } 180 181 185 public void fileRenamed (FileRenameEvent fe) { 186 lockMdr (); 187 } 188 189 public void fileAttributeChanged (FileAttributeEvent fe) { 190 } 191 192 public void fileFolderCreated (FileEvent fe) { 193 lockMdr (); 194 } 195 196 public void fileDeleted (FileEvent fe) { 197 lockMdr (); 198 } 199 200 public void fileDataCreated (FileEvent fe) { 201 lockMdr (); 202 } 203 204 public void fileChanged (FileEvent fe) { 205 lockMdr (); 206 } 207 208 } 209 | Popular Tags |