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.StandardCharsets;
23  import java.nio.charset.UnsupportedCharsetException;
24  
25  import org.apache.commons.io.FileUtils;
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   */
40  public class LockableFileWriterTest extends FileBasedTestCase {
41  
42      private File file;
43      private File lockDir;
44      private File lockFile;
45      private File altLockDir;
46      private File altLockFile;
47  
48      @Before
49      public void setUp() {
50          file = new File(getTestDirectory(), "testlockfile");
51          lockDir = new File(System.getProperty("java.io.tmpdir"));
52          lockFile = new File(lockDir, file.getName() + ".lck");
53          altLockDir = getTestDirectory();
54          altLockFile = new File(altLockDir, file.getName() + ".lck");
55      }
56  
57      @After
58      public void tearDown() throws IOException {
59          file.delete();
60          lockFile.delete();
61          altLockFile.delete();
62          FileUtils.deleteDirectory(altLockDir);
63      }
64  
65      //-----------------------------------------------------------------------
66      @Test public void testFileLocked() throws IOException {
67  
68          // open a valid locakable writer
69          try (LockableFileWriter lfw1 = new LockableFileWriter(file)) {
70              assertTrue(file.exists());
71              assertTrue(lockFile.exists());
72  
73              // try to open a second writer
74              try (LockableFileWriter lfw2 = new LockableFileWriter(file)) {
75                  fail("Somehow able to open a locked file. ");
76              } catch(final IOException ioe) {
77                  final String msg = ioe.getMessage();
78                  assertTrue( "Exception message does not start correctly. ",
79                              msg.startsWith("Can't write file, lock ") );
80                  assertTrue(file.exists());
81                  assertTrue(lockFile.exists());
82              }
83  
84              // try to open a third writer
85              try (LockableFileWriter lfw3 = new LockableFileWriter(file)) {
86                  fail("Somehow able to open a locked file. ");
87              } catch(final IOException ioe) {
88                  final String msg = ioe.getMessage();
89                  assertTrue( "Exception message does not start correctly. ",
90                              msg.startsWith("Can't write file, lock ") );
91                  assertTrue(file.exists());
92                  assertTrue(lockFile.exists());
93              }
94          }
95          assertTrue(file.exists());
96          assertFalse(lockFile.exists());
97      }
98  
99      //-----------------------------------------------------------------------
100     @Test public void testAlternateLockDir() throws IOException {
101         // open a valid locakable writer
102         try (LockableFileWriter lfw1 = new LockableFileWriter(file, "UTF-8" ,true, altLockDir.getAbsolutePath())){
103             assertTrue(file.exists());
104             assertTrue(altLockFile.exists());
105 
106             // try to open a second writer
107             try (LockableFileWriter lfw2 = new LockableFileWriter(file, StandardCharsets.UTF_8, true, altLockDir.getAbsolutePath())){
108                 fail("Somehow able to open a locked file. ");
109             } catch(final IOException ioe) {
110                 final String msg = ioe.getMessage();
111                 assertTrue( "Exception message does not start correctly. ",
112                             msg.startsWith("Can't write file, lock ") );
113                 assertTrue(file.exists());
114                 assertTrue(altLockFile.exists());
115             }
116         }
117         assertTrue(file.exists());
118         assertFalse(altLockFile.exists());
119     }
120 
121     //-----------------------------------------------------------------------
122     @Test public void testFileNotLocked() throws IOException {
123         // open a valid lockable writer
124         try (LockableFileWriter lfw1 = new LockableFileWriter(file)) {
125             assertTrue(file.exists());
126             assertTrue(lockFile.exists());
127         }
128         assertTrue(file.exists());
129         assertFalse(lockFile.exists());
130 
131         // open a second valid writer on the same file
132         try (LockableFileWriter lfw2 = new LockableFileWriter(file)) {
133             assertTrue(file.exists());
134             assertTrue(lockFile.exists());
135         }
136         assertTrue(file.exists());
137         assertFalse(lockFile.exists());
138     }
139 
140     //-----------------------------------------------------------------------
141     @Test public void testConstructor_File_encoding_badEncoding() throws IOException {
142         try (Writer writer = new LockableFileWriter(file, "BAD-ENCODE")) {
143             fail();
144         } catch (final UnsupportedCharsetException ex) {
145             // expected
146             assertFalse(file.exists());
147             assertFalse(lockFile.exists());
148         }
149         assertFalse(file.exists());
150         assertFalse(lockFile.exists());
151     }
152 
153     //-----------------------------------------------------------------------
154     @Test public void testConstructor_File_directory() {
155         try (Writer writer = new LockableFileWriter(getTestDirectory())) {
156             fail();
157         } catch (final IOException ex) {
158             // expected
159             assertFalse(file.exists());
160             assertFalse(lockFile.exists());
161         }
162         assertFalse(file.exists());
163         assertFalse(lockFile.exists());
164     }
165 
166     //-----------------------------------------------------------------------
167     @Test public void testConstructor_File_nullFile() throws IOException {
168         try (Writer writer = new LockableFileWriter((File) null)) {
169             fail();
170         } catch (final NullPointerException ex) {
171             // expected
172             assertFalse(file.exists());
173             assertFalse(lockFile.exists());
174         }
175         assertFalse(file.exists());
176         assertFalse(lockFile.exists());
177     }
178 
179     //-----------------------------------------------------------------------
180     @Test public void testConstructor_fileName_nullFile() throws IOException {
181         try (Writer writer = new LockableFileWriter((String) null)) {
182             fail();
183         } catch (final NullPointerException ex) {
184             // expected
185             assertFalse(file.exists());
186             assertFalse(lockFile.exists());
187         }
188         assertFalse(file.exists());
189         assertFalse(lockFile.exists());
190     }
191 
192 }