1 19 20 package org.netbeans.modules.masterfs.filebasedfs.fileobjects; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.lang.ref.WeakReference ; 25 26 import org.netbeans.junit.NbTestCase; 27 import org.openide.filesystems.FileLock; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileUtil; 30 31 32 38 public class WriteLockTest extends NbTestCase { 39 private File testFile = null; 40 public WriteLockTest(String testName) { 41 super(testName); 42 } 43 44 protected void setUp() throws java.lang.Exception { 45 clearWorkDir(); 46 testFile = new File (getWorkDir(), "testLockFile.txt"); 47 if (!testFile.exists()) { 48 testFile.createNewFile(); 49 } 50 51 assertTrue(testFile.exists()); 52 } 53 54 public void testLightWeigtLock() throws Exception { 55 testLightWeigtLockImpl(true); 56 testLightWeigtLockImpl(false); 57 } 58 59 62 public void testReleaseLock() throws Exception { 63 testReleaseLockImpl(true); 64 testReleaseLockImpl(false); 65 } 66 67 70 public void testIsValid() throws Exception { 71 testIsValidImpl(true); 72 testIsValidImpl(false); 73 } 74 75 public void testAfterCrash() throws Exception { 76 testAfterCrashImpl(true); 77 testAfterCrashImpl(false); 78 } 79 80 81 84 public void testFinalize() throws Exception { 85 testFinalizeImpl(true); 86 testFinalizeImpl(false); 87 } 88 89 90 public void testLightWeigtLockImpl(boolean lightWeight) throws Exception { 91 WriteLock lock = (WriteLock)WriteLockFactory.tryLock(testFile, lightWeight); 92 assertEquals(!lightWeight,lock.getLockFile().exists()); 93 lock.releaseLock(); 94 lock = (WriteLock)WriteLockFactory.tryLock(testFile, lightWeight); 95 assertEquals(!lightWeight,lock.getLockFile().exists()); 96 lock.releaseLock(); 97 } 98 99 102 public void testReleaseLockImpl(boolean lightWeight) throws Exception { 103 FileLock lock = WriteLockFactory.tryLock(testFile, lightWeight); 104 assertNotNull(lock); 105 try { 106 WriteLockFactory.tryLock(testFile, lightWeight); 107 fail (); 108 } catch (IOException iex) {} 109 110 lock.releaseLock(); 111 lock.releaseLock(); 112 lock = WriteLockFactory.tryLock(testFile, lightWeight); 113 assertNotNull(lock); 114 lock.releaseLock(); 115 } 116 117 120 public void testIsValidImpl(boolean lightWeight) throws Exception { 121 FileLock lock = WriteLockFactory.tryLock(testFile, lightWeight); 122 123 assertTrue(lock.isValid()); 124 lock.releaseLock(); 125 assertFalse(lock.isValid()); 126 127 } 128 129 public void testAfterCrashImpl(boolean lightWeight) throws Exception { 130 File lockFile = WriteLockUtils.getAssociatedLockFile(testFile); 131 if (!lockFile.exists()) { 132 assertTrue(lockFile.createNewFile()); 133 } 134 assertTrue(lockFile.exists()); 135 FileLock lock = WriteLockFactory.tryLock(testFile, lightWeight); 136 try { 137 assertNotNull(lock); 138 assertTrue(lock.isValid()); 139 assertTrue(lockFile.exists()); 140 } finally { 141 lock.releaseLock(); 142 } 143 144 } 145 146 147 150 public void testFinalizeImpl(boolean lightWeight) throws Exception { 151 assertGC("",new WeakReference (WriteLockFactory.tryLock(testFile, lightWeight))); 152 FileLock lck = WriteLockFactory.tryLock(testFile, lightWeight); 153 lck.releaseLock(); 154 155 File lockFile = WriteLockUtils.getAssociatedLockFile(testFile); 156 assertFalse(lockFile.exists()); 157 } 158 159 public void testLockingAndReleasingLockAfterRename_82170() throws Exception { 160 File folder = new File (getWorkDir(),"a/b/c/d"); 161 if (!folder.exists()) { 162 assertTrue(folder.mkdirs()); 163 } 164 File file = new File (folder, "file.txt"); 165 if (!file.exists()) { 166 assertTrue(file.createNewFile()); 167 } 168 FileObject fileFo = FileUtil.toFileObject(file); 169 assertNotNull(fileFo); 170 171 FileLock lockForFile = WriteLockFactory.tryLock(file, false); 172 assertTrue(lockForFile.isValid()); 173 FileObject parentOfFileFo = fileFo.getParent().getParent(); 174 FileLock lockForParentOfFileFo = parentOfFileFo.lock(); 175 try { 176 parentOfFileFo.rename(lockForParentOfFileFo, "renamedFile",""); } finally { 178 lockForParentOfFileFo.releaseLock(); 179 } 180 181 assertTrue(lockForFile.isValid()); file = FileUtil.toFile(fileFo); 183 assertNotNull(file); 184 try { 185 WriteLockFactory.tryLock(file, false); 186 fail(); 187 } catch (IOException ex) { 188 } 190 File lockFile = WriteLockUtils.getAssociatedLockFile(file); 191 assertTrue(lockFile.exists()); lockForFile.releaseLock(); 193 assertFalse(lockFile.exists()); WriteLockFactory.tryLock(file, false); } 196 197 198 public java.io.File getWorkDir() throws java.io.IOException { 199 return super.getWorkDir(); 200 } 201 202 205 } 206 | Popular Tags |