View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  import org.junit.After;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.assertFalse;
32  import static org.junit.Assert.assertTrue;
33  import static org.junit.Assert.fail;
34  
35  /**
36   * Tests that files really lock, although no writing is done as
37   * the locking is tested only on construction.
38   *
39   * @version $Id: LockableFileWriterTest.java 1718944 2015-12-09 19:50:30Z krosenvold $
40   */
41  public class LockableFileWriterTest extends FileBasedTestCase {
42  
43      private File file;
44      private File lockDir;
45      private File lockFile;
46      private File altLockDir;
47      private File altLockFile;
48  
49      @Before
50      public void setUp() {
51          file = new File(getTestDirectory(), "testlockfile");
52          lockDir = new File(System.getProperty("java.io.tmpdir"));
53          lockFile = new File(lockDir, file.getName() + ".lck");
54          altLockDir = getTestDirectory();
55          altLockFile = new File(altLockDir, file.getName() + ".lck");
56      }
57  
58      @After
59      public void tearDown() {
60          file.delete();
61          lockFile.delete();
62          altLockFile.delete();
63      }
64  
65      //-----------------------------------------------------------------------
66      @Test public void testFileLocked() throws IOException {
67          LockableFileWriter lfw1 = null;
68          LockableFileWriter lfw2 = null;
69          LockableFileWriter lfw3 = null;
70          try {
71              // open a valid locakable writer
72              lfw1 = new LockableFileWriter(file);
73              assertTrue(file.exists());
74              assertTrue(lockFile.exists());
75  
76              // try to open a second writer
77              try {
78                  lfw2 = new LockableFileWriter(file);
79                  fail("Somehow able to open a locked file. ");
80              } catch(final IOException ioe) {
81                  final String msg = ioe.getMessage();
82                  assertTrue( "Exception message does not start correctly. ",
83                              msg.startsWith("Can't write file, lock ") );
84                  assertTrue(file.exists());
85                  assertTrue(lockFile.exists());
86              }
87  
88              // try to open a third writer
89              try {
90                  lfw3 = new LockableFileWriter(file);
91                  fail("Somehow able to open a locked file. ");
92              } catch(final IOException ioe) {
93                  final String msg = ioe.getMessage();
94                  assertTrue( "Exception message does not start correctly. ",
95                              msg.startsWith("Can't write file, lock ") );
96                  assertTrue(file.exists());
97                  assertTrue(lockFile.exists());
98              }
99  
100         } finally {
101             IOUtils.closeQuietly(lfw1);
102             IOUtils.closeQuietly(lfw2);
103             IOUtils.closeQuietly(lfw3);
104         }
105         assertTrue(file.exists());
106         assertFalse(lockFile.exists());
107     }
108 
109     //-----------------------------------------------------------------------
110     @SuppressWarnings("deprecation") // unavoidable until Java 7
111     @Test public void testAlternateLockDir() throws IOException {
112         LockableFileWriter lfw1 = null;
113         LockableFileWriter lfw2 = null;
114         try {
115             // open a valid locakable writer
116             lfw1 = new LockableFileWriter(file, "UTF-8" ,true, altLockDir.getAbsolutePath());
117             assertTrue(file.exists());
118             assertTrue(altLockFile.exists());
119 
120             // try to open a second writer
121             try {
122                 lfw2 = new LockableFileWriter(file, Charsets.UTF_8, true, altLockDir.getAbsolutePath());
123                 fail("Somehow able to open a locked file. ");
124             } catch(final IOException ioe) {
125                 final String msg = ioe.getMessage();
126                 assertTrue( "Exception message does not start correctly. ",
127                             msg.startsWith("Can't write file, lock ") );
128                 assertTrue(file.exists());
129                 assertTrue(altLockFile.exists());
130             }
131 
132         } finally {
133             IOUtils.closeQuietly(lfw1);
134             IOUtils.closeQuietly(lfw2);
135         }
136         assertTrue(file.exists());
137         assertFalse(altLockFile.exists());
138     }
139 
140     //-----------------------------------------------------------------------
141     @Test public void testFileNotLocked() throws IOException {
142         // open a valid locakable writer
143         LockableFileWriter lfw1 = null;
144         try {
145             lfw1 = new LockableFileWriter(file);
146             assertTrue(file.exists());
147             assertTrue(lockFile.exists());
148         } finally {
149             IOUtils.closeQuietly(lfw1);
150         }
151         assertTrue(file.exists());
152         assertFalse(lockFile.exists());
153 
154         // open a second valid writer on the same file
155         LockableFileWriter lfw2 = null;
156         try {
157             lfw2 = new LockableFileWriter(file);
158             assertTrue(file.exists());
159             assertTrue(lockFile.exists());
160         } finally {
161             IOUtils.closeQuietly(lfw2);
162         }
163         assertTrue(file.exists());
164         assertFalse(lockFile.exists());
165     }
166 
167     //-----------------------------------------------------------------------
168     @Test public void testConstructor_File_encoding_badEncoding() throws IOException {
169         Writer writer = null;
170         try {
171             writer = new LockableFileWriter(file, "BAD-ENCODE");
172             fail();
173         } catch (final UnsupportedCharsetException ex) {
174             // expected
175             assertFalse(file.exists());
176             assertFalse(lockFile.exists());
177         } finally {
178             IOUtils.closeQuietly(writer);
179         }
180         assertFalse(file.exists());
181         assertFalse(lockFile.exists());
182     }
183 
184     //-----------------------------------------------------------------------
185     @Test public void testConstructor_File_directory() {
186         Writer writer = null;
187         try {
188             writer = new LockableFileWriter(getTestDirectory());
189             fail();
190         } catch (final IOException ex) {
191             // expected
192             assertFalse(file.exists());
193             assertFalse(lockFile.exists());
194         } finally {
195             IOUtils.closeQuietly(writer);
196         }
197         assertFalse(file.exists());
198         assertFalse(lockFile.exists());
199     }
200 
201     //-----------------------------------------------------------------------
202     @Test public void testConstructor_File_nullFile() throws IOException {
203         Writer writer = null;
204         try {
205             writer = new LockableFileWriter((File) null);
206             fail();
207         } catch (final NullPointerException ex) {
208             // expected
209             assertFalse(file.exists());
210             assertFalse(lockFile.exists());
211         } finally {
212             IOUtils.closeQuietly(writer);
213         }
214         assertFalse(file.exists());
215         assertFalse(lockFile.exists());
216     }
217 
218     //-----------------------------------------------------------------------
219     @Test public void testConstructor_fileName_nullFile() throws IOException {
220         Writer writer = null;
221         try {
222             writer = new LockableFileWriter((String) null);
223             fail();
224         } catch (final NullPointerException ex) {
225             // expected
226             assertFalse(file.exists());
227             assertFalse(lockFile.exists());
228         } finally {
229             IOUtils.closeQuietly(writer);
230         }
231         assertFalse(file.exists());
232         assertFalse(lockFile.exists());
233     }
234 
235 }