KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > filebasedfs > fileobjects > WriteLockTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.masterfs.filebasedfs.fileobjects;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
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 /**
33  * FileLockImplTest.java
34  * JUnit based test
35  *
36  * @author Radek Matous
37  */

38 public class WriteLockTest extends NbTestCase {
39     private File JavaDoc testFile = null;
40     public WriteLockTest(String JavaDoc testName) {
41         super(testName);
42     }
43
44     protected void setUp() throws java.lang.Exception JavaDoc {
45         clearWorkDir();
46         testFile = new File JavaDoc (getWorkDir(), "testLockFile.txt");
47         if (!testFile.exists()) {
48             testFile.createNewFile();
49         }
50
51         assertTrue(testFile.exists());
52     }
53
54     public void testLightWeigtLock() throws Exception JavaDoc {
55         testLightWeigtLockImpl(true);
56         testLightWeigtLockImpl(false);
57     }
58
59     /**
60      * Test of releaseLock method, of class org.netbeans.modules.masterfs.filebasedfs.filelocks.FileLockImpl.
61      */

62     public void testReleaseLock() throws Exception JavaDoc {
63         testReleaseLockImpl(true);
64         testReleaseLockImpl(false);
65     }
66             
67     /**
68      * Test of isValid method, of class org.netbeans.modules.masterfs.filebasedfs.filelocks.FileLockImpl.
69      */

70     public void testIsValid() throws Exception JavaDoc {
71         testIsValidImpl(true);
72         testIsValidImpl(false);
73     }
74     
75     public void testAfterCrash() throws Exception JavaDoc {
76         testAfterCrashImpl(true);
77         testAfterCrashImpl(false);
78     }
79     
80
81     /**
82      * Test of finalize method, of class org.netbeans.modules.masterfs.filebasedfs.filelocks.FileLockImpl.
83      */

84     public void testFinalize() throws Exception JavaDoc{
85         testFinalizeImpl(true);
86         testFinalizeImpl(false);
87     }
88     
89     
90     public void testLightWeigtLockImpl(boolean lightWeight) throws Exception JavaDoc {
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     /**
100      * Test of releaseLock method, of class org.netbeans.modules.masterfs.filebasedfs.filelocks.FileLockImpl.
101      */

102     public void testReleaseLockImpl(boolean lightWeight) throws Exception JavaDoc {
103         FileLock lock = WriteLockFactory.tryLock(testFile, lightWeight);
104         assertNotNull(lock);
105         try {
106             WriteLockFactory.tryLock(testFile, lightWeight);
107             fail ();
108         } catch (IOException JavaDoc iex) {}
109         
110         lock.releaseLock();
111         lock.releaseLock();
112         lock = WriteLockFactory.tryLock(testFile, lightWeight);
113         assertNotNull(lock);
114         lock.releaseLock();
115     }
116             
117     /**
118      * Test of isValid method, of class org.netbeans.modules.masterfs.filebasedfs.filelocks.FileLockImpl.
119      */

120     public void testIsValidImpl(boolean lightWeight) throws Exception JavaDoc {
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 JavaDoc {
130         File JavaDoc 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     /**
148      * Test of finalize method, of class org.netbeans.modules.masterfs.filebasedfs.filelocks.FileLockImpl.
149      */

150     public void testFinalizeImpl(boolean lightWeight) throws Exception JavaDoc{
151         assertGC("",new WeakReference JavaDoc (WriteLockFactory.tryLock(testFile, lightWeight)));
152         FileLock lck = WriteLockFactory.tryLock(testFile, lightWeight);
153         lck.releaseLock();
154                 
155         File JavaDoc lockFile = WriteLockUtils.getAssociatedLockFile(testFile);
156         assertFalse(lockFile.exists());
157     }
158
159     public void testLockingAndReleasingLockAfterRename_82170() throws Exception JavaDoc {
160         File JavaDoc folder = new File JavaDoc(getWorkDir(),"a/b/c/d");
161         if (!folder.exists()) {
162             assertTrue(folder.mkdirs());
163         }
164         File JavaDoc file = new File JavaDoc(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","");//NOI18N
177
} finally {
178             lockForParentOfFileFo.releaseLock();
179         }
180
181         assertTrue(lockForFile.isValid());//after rename is lock still valid
182
file = FileUtil.toFile(fileFo);
183         assertNotNull(file);
184         try {
185             WriteLockFactory.tryLock(file, false);
186             fail();
187         } catch (IOException JavaDoc ex) {
188             //after rename is still locked and there isn't possible to get other lock
189
}
190         File JavaDoc lockFile = WriteLockUtils.getAssociatedLockFile(file);
191         assertTrue(lockFile.exists());//lock file still exists
192
lockForFile.releaseLock();
193         assertFalse(lockFile.exists());//lock file is deleted after releasing lock
194
WriteLockFactory.tryLock(file, false);//there is possible to get lock after releasing lock
195
}
196     
197
198     public java.io.File JavaDoc getWorkDir() throws java.io.IOException JavaDoc {
199         return super.getWorkDir();
200     }
201     
202     // TODO add test methods here. The name must begin with 'test'. For example:
203
// public void testHello() {}
204

205 }
206
Popular Tags