1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.output;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.Writer;
22 import java.nio.charset.UnsupportedCharsetException;
23
24 import org.apache.commons.io.Charsets;
25 import org.apache.commons.io.IOUtils;
26 import org.apache.commons.io.testtools.FileBasedTestCase;
27
28
29
30
31
32
33
34 public class LockableFileWriterTest extends FileBasedTestCase {
35
36 private File file;
37 private File lockDir;
38 private File lockFile;
39 private File altLockDir;
40 private File altLockFile;
41
42 public LockableFileWriterTest(final String name) {
43 super(name);
44 }
45
46 @Override
47 public void setUp() {
48 file = new File(getTestDirectory(), "testlockfile");
49 lockDir = new File(System.getProperty("java.io.tmpdir"));
50 lockFile = new File(lockDir, file.getName() + ".lck");
51 altLockDir = getTestDirectory();
52 altLockFile = new File(altLockDir, file.getName() + ".lck");
53 }
54
55 @Override
56 public void tearDown() {
57 file.delete();
58 lockFile.delete();
59 altLockFile.delete();
60 }
61
62
63 public void testFileLocked() throws IOException {
64 LockableFileWriter lfw1 = null;
65 LockableFileWriter lfw2 = null;
66 LockableFileWriter lfw3 = null;
67 try {
68
69 lfw1 = new LockableFileWriter(file);
70 assertTrue(file.exists());
71 assertTrue(lockFile.exists());
72
73
74 try {
75 lfw2 = new LockableFileWriter(file);
76 fail("Somehow able to open a locked file. ");
77 } catch(final IOException ioe) {
78 final String msg = ioe.getMessage();
79 assertTrue( "Exception message does not start correctly. ",
80 msg.startsWith("Can't write file, lock ") );
81 assertTrue(file.exists());
82 assertTrue(lockFile.exists());
83 }
84
85
86 try {
87 lfw3 = new LockableFileWriter(file);
88 fail("Somehow able to open a locked file. ");
89 } catch(final IOException ioe) {
90 final String msg = ioe.getMessage();
91 assertTrue( "Exception message does not start correctly. ",
92 msg.startsWith("Can't write file, lock ") );
93 assertTrue(file.exists());
94 assertTrue(lockFile.exists());
95 }
96
97 } finally {
98 IOUtils.closeQuietly(lfw1);
99 IOUtils.closeQuietly(lfw2);
100 IOUtils.closeQuietly(lfw3);
101 }
102 assertTrue(file.exists());
103 assertFalse(lockFile.exists());
104 }
105
106
107 public void testAlternateLockDir() throws IOException {
108 LockableFileWriter lfw1 = null;
109 LockableFileWriter lfw2 = null;
110 try {
111
112 lfw1 = new LockableFileWriter(file, "UTF-8" ,true, altLockDir.getAbsolutePath());
113 assertTrue(file.exists());
114 assertTrue(altLockFile.exists());
115
116
117 try {
118 lfw2 = new LockableFileWriter(file, Charsets.UTF_8, true, altLockDir.getAbsolutePath());
119 fail("Somehow able to open a locked file. ");
120 } catch(final IOException ioe) {
121 final String msg = ioe.getMessage();
122 assertTrue( "Exception message does not start correctly. ",
123 msg.startsWith("Can't write file, lock ") );
124 assertTrue(file.exists());
125 assertTrue(altLockFile.exists());
126 }
127
128 } finally {
129 IOUtils.closeQuietly(lfw1);
130 IOUtils.closeQuietly(lfw2);
131 }
132 assertTrue(file.exists());
133 assertFalse(altLockFile.exists());
134 }
135
136
137 public void testFileNotLocked() throws IOException {
138
139 LockableFileWriter lfw1 = null;
140 try {
141 lfw1 = new LockableFileWriter(file);
142 assertTrue(file.exists());
143 assertTrue(lockFile.exists());
144 } finally {
145 IOUtils.closeQuietly(lfw1);
146 }
147 assertTrue(file.exists());
148 assertFalse(lockFile.exists());
149
150
151 LockableFileWriter lfw2 = null;
152 try {
153 lfw2 = new LockableFileWriter(file);
154 assertTrue(file.exists());
155 assertTrue(lockFile.exists());
156 } finally {
157 IOUtils.closeQuietly(lfw2);
158 }
159 assertTrue(file.exists());
160 assertFalse(lockFile.exists());
161 }
162
163
164 public void testConstructor_File_encoding_badEncoding() throws IOException {
165 Writer writer = null;
166 try {
167 writer = new LockableFileWriter(file, "BAD-ENCODE");
168 fail();
169 } catch (final UnsupportedCharsetException ex) {
170
171 assertFalse(file.exists());
172 assertFalse(lockFile.exists());
173 } finally {
174 IOUtils.closeQuietly(writer);
175 }
176 assertFalse(file.exists());
177 assertFalse(lockFile.exists());
178 }
179
180
181 public void testConstructor_File_directory() {
182 Writer writer = null;
183 try {
184 writer = new LockableFileWriter(getTestDirectory());
185 fail();
186 } catch (final IOException ex) {
187
188 assertFalse(file.exists());
189 assertFalse(lockFile.exists());
190 } finally {
191 IOUtils.closeQuietly(writer);
192 }
193 assertFalse(file.exists());
194 assertFalse(lockFile.exists());
195 }
196
197
198 public void testConstructor_File_nullFile() throws IOException {
199 Writer writer = null;
200 try {
201 writer = new LockableFileWriter((File) null);
202 fail();
203 } catch (final NullPointerException ex) {
204
205 assertFalse(file.exists());
206 assertFalse(lockFile.exists());
207 } finally {
208 IOUtils.closeQuietly(writer);
209 }
210 assertFalse(file.exists());
211 assertFalse(lockFile.exists());
212 }
213
214
215 public void testConstructor_fileName_nullFile() throws IOException {
216 Writer writer = null;
217 try {
218 writer = new LockableFileWriter((String) null);
219 fail();
220 } catch (final NullPointerException ex) {
221
222 assertFalse(file.exists());
223 assertFalse(lockFile.exists());
224 } finally {
225 IOUtils.closeQuietly(writer);
226 }
227 assertFalse(file.exists());
228 assertFalse(lockFile.exists());
229 }
230
231 }