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  
23  import org.apache.commons.io.IOUtils;
24  import org.apache.commons.io.testtools.FileBasedTestCase;
25  
26  /**
27   * Tests that files really lock, although no writing is done as 
28   * the locking is tested only on construction. 
29   *
30   * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $
31   */
32  public class LockableFileWriterTest extends FileBasedTestCase {
33  
34      private File file;
35      private File lockDir;
36      private File lockFile;
37      private File altLockDir;
38      private File altLockFile;
39  
40      public LockableFileWriterTest(String name) {
41          super(name);
42      }
43  
44      public void setUp() {
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");
50      }
51  
52      public void tearDown() {
53          file.delete();
54          lockFile.delete();
55          altLockFile.delete();
56      }
57  
58      //-----------------------------------------------------------------------
59      public void testFileLocked() throws IOException {
60          LockableFileWriter lfw1 = null;
61          LockableFileWriter lfw2 = null;
62          LockableFileWriter lfw3 = null;
63          try {
64              // open a valid locakable writer
65              lfw1 = new LockableFileWriter(file);
66              assertEquals(true, file.exists());
67              assertEquals(true, lockFile.exists());
68              
69              // try to open a second writer
70              try {
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());
79              }
80              
81              // try to open a third writer
82              try {
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());
91              }
92              
93          } finally {
94              IOUtils.closeQuietly(lfw1);
95              IOUtils.closeQuietly(lfw2);
96              IOUtils.closeQuietly(lfw3);
97          }
98          assertEquals(true, file.exists());
99          assertEquals(false, lockFile.exists());
100     }
101 
102     //-----------------------------------------------------------------------
103     public void testAlternateLockDir() throws IOException {
104         LockableFileWriter lfw1 = null;
105         LockableFileWriter lfw2 = null;
106         try {
107             // open a valid locakable writer
108             lfw1 = new LockableFileWriter(file, true, altLockDir.getAbsolutePath());
109             assertEquals(true, file.exists());
110             assertEquals(true, altLockFile.exists());
111             
112             // try to open a second writer
113             try {
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());
122             }
123             
124         } finally {
125             IOUtils.closeQuietly(lfw1);
126             IOUtils.closeQuietly(lfw2);
127         }
128         assertEquals(true, file.exists());
129         assertEquals(false, altLockFile.exists());
130     }
131 
132     //-----------------------------------------------------------------------
133     public void testFileNotLocked() throws IOException {
134         // open a valid locakable writer
135         LockableFileWriter lfw1 = null;
136         try {
137             lfw1 = new LockableFileWriter(file);
138             assertEquals(true, file.exists());
139             assertEquals(true, lockFile.exists());
140         } finally {
141             IOUtils.closeQuietly(lfw1);
142         }
143         assertEquals(true, file.exists());
144         assertEquals(false, lockFile.exists());
145         
146         // open a second valid writer on the same file
147         LockableFileWriter lfw2 = null;
148         try {
149             lfw2 = new LockableFileWriter(file);
150             assertEquals(true, file.exists());
151             assertEquals(true, lockFile.exists());
152         } finally {
153             IOUtils.closeQuietly(lfw2);
154         }
155         assertEquals(true, file.exists());
156         assertEquals(false, lockFile.exists());
157     }
158 
159     //-----------------------------------------------------------------------
160     public void testConstructor_File_encoding_badEncoding() throws IOException {
161         Writer writer = null;
162         try {
163             writer = new LockableFileWriter(file, "BAD-ENCODE");
164             fail();
165         } catch (IOException ex) {
166             // expected
167             assertEquals(false, file.exists());
168             assertEquals(false, lockFile.exists());
169         } finally {
170             IOUtils.closeQuietly(writer);
171         }
172         assertEquals(false, file.exists());
173         assertEquals(false, lockFile.exists());
174     }
175 
176     //-----------------------------------------------------------------------
177     public void testConstructor_File_directory() throws IOException {
178         Writer writer = null;
179         try {
180             writer = new LockableFileWriter(getTestDirectory());
181             fail();
182         } catch (IOException ex) {
183             // expected
184             assertEquals(false, file.exists());
185             assertEquals(false, lockFile.exists());
186         } finally {
187             IOUtils.closeQuietly(writer);
188         }
189         assertEquals(false, file.exists());
190         assertEquals(false, lockFile.exists());
191     }
192 
193     //-----------------------------------------------------------------------
194     public void testConstructor_File_nullFile() throws IOException {
195         Writer writer = null;
196         try {
197             writer = new LockableFileWriter((File) null);
198             fail();
199         } catch (NullPointerException ex) {
200             // expected
201             assertEquals(false, file.exists());
202             assertEquals(false, lockFile.exists());
203         } finally {
204             IOUtils.closeQuietly(writer);
205         }
206         assertEquals(false, file.exists());
207         assertEquals(false, lockFile.exists());
208     }
209 
210     //-----------------------------------------------------------------------
211     public void testConstructor_fileName_nullFile() throws IOException {
212         Writer writer = null;
213         try {
214             writer = new LockableFileWriter((String) null);
215             fail();
216         } catch (NullPointerException ex) {
217             // expected
218             assertEquals(false, file.exists());
219             assertEquals(false, lockFile.exists());
220         } finally {
221             IOUtils.closeQuietly(writer);
222         }
223         assertEquals(false, file.exists());
224         assertEquals(false, lockFile.exists());
225     }
226 
227 }