13
14
* See the License for the specific language governing permissions and
14
15
* limitations under the License.
18
17
package org.apache.commons.io.output;
21
20
import java.io.IOException;
21
import java.io.Writer;
24
import junit.framework.TestCase;
23
import org.apache.commons.io.IOUtils;
24
import org.apache.commons.io.testtools.FileBasedTestCase;
27
27
* Tests that files really lock, although no writing is done as
28
28
* the locking is tested only on construction.
30
* @author Henri Yandell (bayard at apache dot org)
31
* @version $Revision: 1.2 $ $Date: 2004/02/29 21:07:14 $
30
* @version $Revision: 471628 $ $Date: 2006-11-05 20:06:45 -0800 (Sun, 05 Nov 2006) $
34
public class LockableFileWriterTest extends TestCase {
32
public class LockableFileWriterTest extends FileBasedTestCase {
36
private File lockFile;
37
private File altLockDir;
38
private File altLockFile;
38
40
public LockableFileWriterTest(String name) {
42
44
public void setUp() {
43
this.file = new File("testlockfile");
45
file = new File(getTestDirectory(), "testlockfile");
46
lockDir = new File(System.getProperty("java.io.tmpdir"));
47
lockFile = new File(lockDir, file.getName() + ".lck");
48
altLockDir = getTestDirectory();
49
altLockFile = new File(altLockDir, file.getName() + ".lck");
46
52
public void tearDown() {
58
//-----------------------------------------------------------------------
50
59
public void testFileLocked() throws IOException {
51
LockableFileWriter lfw = new LockableFileWriter(this.file);
53
LockableFileWriter lfw2 = new LockableFileWriter(this.file);
54
fail("Somehow able to open a locked file. ");
55
} catch(IOException ioe) {
56
String msg = ioe.getMessage();
57
assertTrue( "Exception message does not start correctly. ",
58
msg.startsWith("Can't write file, lock ") );
60
LockableFileWriter lfw1 = null;
61
LockableFileWriter lfw2 = null;
62
LockableFileWriter lfw3 = null;
64
// open a valid locakable writer
65
lfw1 = new LockableFileWriter(file);
66
assertEquals(true, file.exists());
67
assertEquals(true, lockFile.exists());
69
// try to open a second writer
71
lfw2 = new LockableFileWriter(file);
72
fail("Somehow able to open a locked file. ");
73
} catch(IOException ioe) {
74
String msg = ioe.getMessage();
75
assertTrue( "Exception message does not start correctly. ",
76
msg.startsWith("Can't write file, lock ") );
77
assertEquals(true, file.exists());
78
assertEquals(true, lockFile.exists());
81
// try to open a third writer
83
lfw3 = new LockableFileWriter(file);
84
fail("Somehow able to open a locked file. ");
85
} catch(IOException ioe) {
86
String msg = ioe.getMessage();
87
assertTrue( "Exception message does not start correctly. ",
88
msg.startsWith("Can't write file, lock ") );
89
assertEquals(true, file.exists());
90
assertEquals(true, lockFile.exists());
94
IOUtils.closeQuietly(lfw1);
95
IOUtils.closeQuietly(lfw2);
96
IOUtils.closeQuietly(lfw3);
98
assertEquals(true, file.exists());
99
assertEquals(false, lockFile.exists());
102
//-----------------------------------------------------------------------
103
public void testAlternateLockDir() throws IOException {
104
LockableFileWriter lfw1 = null;
105
LockableFileWriter lfw2 = null;
107
// open a valid locakable writer
108
lfw1 = new LockableFileWriter(file, true, altLockDir.getAbsolutePath());
109
assertEquals(true, file.exists());
110
assertEquals(true, altLockFile.exists());
112
// try to open a second writer
114
lfw2 = new LockableFileWriter(file, true, altLockDir.getAbsolutePath());
115
fail("Somehow able to open a locked file. ");
116
} catch(IOException ioe) {
117
String msg = ioe.getMessage();
118
assertTrue( "Exception message does not start correctly. ",
119
msg.startsWith("Can't write file, lock ") );
120
assertEquals(true, file.exists());
121
assertEquals(true, altLockFile.exists());
125
IOUtils.closeQuietly(lfw1);
126
IOUtils.closeQuietly(lfw2);
128
assertEquals(true, file.exists());
129
assertEquals(false, altLockFile.exists());
132
//-----------------------------------------------------------------------
64
133
public void testFileNotLocked() throws IOException {
65
LockableFileWriter lfw = new LockableFileWriter(this.file);
68
LockableFileWriter lfw2 = new LockableFileWriter(this.file);
70
} catch(IOException ioe) {
71
String msg = ioe.getMessage();
72
if( msg.startsWith("Can't write file, lock ") ) {
73
fail("Somehow unable to open a unlocked file. ");
134
// open a valid locakable writer
135
LockableFileWriter lfw1 = null;
137
lfw1 = new LockableFileWriter(file);
138
assertEquals(true, file.exists());
139
assertEquals(true, lockFile.exists());
141
IOUtils.closeQuietly(lfw1);
143
assertEquals(true, file.exists());
144
assertEquals(false, lockFile.exists());
146
// open a second valid writer on the same file
147
LockableFileWriter lfw2 = null;
149
lfw2 = new LockableFileWriter(file);
150
assertEquals(true, file.exists());
151
assertEquals(true, lockFile.exists());
153
IOUtils.closeQuietly(lfw2);
155
assertEquals(true, file.exists());
156
assertEquals(false, lockFile.exists());
159
//-----------------------------------------------------------------------
160
public void testConstructor_File_encoding_badEncoding() throws IOException {
161
Writer writer = null;
163
writer = new LockableFileWriter(file, "BAD-ENCODE");
165
} catch (IOException ex) {
167
assertEquals(false, file.exists());
168
assertEquals(false, lockFile.exists());
170
IOUtils.closeQuietly(writer);
172
assertEquals(false, file.exists());
173
assertEquals(false, lockFile.exists());
176
//-----------------------------------------------------------------------
177
public void testConstructor_File_directory() throws IOException {
178
Writer writer = null;
180
writer = new LockableFileWriter(getTestDirectory());
182
} catch (IOException ex) {
184
assertEquals(false, file.exists());
185
assertEquals(false, lockFile.exists());
187
IOUtils.closeQuietly(writer);
189
assertEquals(false, file.exists());
190
assertEquals(false, lockFile.exists());
193
//-----------------------------------------------------------------------
194
public void testConstructor_File_nullFile() throws IOException {
195
Writer writer = null;
197
writer = new LockableFileWriter((File) null);
199
} catch (NullPointerException ex) {
201
assertEquals(false, file.exists());
202
assertEquals(false, lockFile.exists());
204
IOUtils.closeQuietly(writer);
206
assertEquals(false, file.exists());
207
assertEquals(false, lockFile.exists());
210
//-----------------------------------------------------------------------
211
public void testConstructor_fileName_nullFile() throws IOException {
212
Writer writer = null;
214
writer = new LockableFileWriter((String) null);
216
} catch (NullPointerException ex) {
218
assertEquals(false, file.exists());
219
assertEquals(false, lockFile.exists());
221
IOUtils.closeQuietly(writer);
223
assertEquals(false, file.exists());
224
assertEquals(false, lockFile.exists());