001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io.output;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.Writer;
022:
023: import org.apache.commons.io.IOUtils;
024: import org.apache.commons.io.testtools.FileBasedTestCase;
025:
026: /**
027: * Tests that files really lock, although no writing is done as
028: * the locking is tested only on construction.
029: *
030: * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $
031: */
032: public class LockableFileWriterTest extends FileBasedTestCase {
033:
034: private File file;
035: private File lockDir;
036: private File lockFile;
037: private File altLockDir;
038: private File altLockFile;
039:
040: public LockableFileWriterTest(String name) {
041: super (name);
042: }
043:
044: public void setUp() {
045: file = new File(getTestDirectory(), "testlockfile");
046: lockDir = new File(System.getProperty("java.io.tmpdir"));
047: lockFile = new File(lockDir, file.getName() + ".lck");
048: altLockDir = getTestDirectory();
049: altLockFile = new File(altLockDir, file.getName() + ".lck");
050: }
051:
052: public void tearDown() {
053: file.delete();
054: lockFile.delete();
055: altLockFile.delete();
056: }
057:
058: //-----------------------------------------------------------------------
059: public void testFileLocked() throws IOException {
060: LockableFileWriter lfw1 = null;
061: LockableFileWriter lfw2 = null;
062: LockableFileWriter lfw3 = null;
063: try {
064: // open a valid locakable writer
065: lfw1 = new LockableFileWriter(file);
066: assertEquals(true, file.exists());
067: assertEquals(true, lockFile.exists());
068:
069: // try to open a second writer
070: try {
071: lfw2 = new LockableFileWriter(file);
072: fail("Somehow able to open a locked file. ");
073: } catch (IOException ioe) {
074: String msg = ioe.getMessage();
075: assertTrue(
076: "Exception message does not start correctly. ",
077: msg.startsWith("Can't write file, lock "));
078: assertEquals(true, file.exists());
079: assertEquals(true, lockFile.exists());
080: }
081:
082: // try to open a third writer
083: try {
084: lfw3 = new LockableFileWriter(file);
085: fail("Somehow able to open a locked file. ");
086: } catch (IOException ioe) {
087: String msg = ioe.getMessage();
088: assertTrue(
089: "Exception message does not start correctly. ",
090: msg.startsWith("Can't write file, lock "));
091: assertEquals(true, file.exists());
092: assertEquals(true, lockFile.exists());
093: }
094:
095: } finally {
096: IOUtils.closeQuietly(lfw1);
097: IOUtils.closeQuietly(lfw2);
098: IOUtils.closeQuietly(lfw3);
099: }
100: assertEquals(true, file.exists());
101: assertEquals(false, lockFile.exists());
102: }
103:
104: //-----------------------------------------------------------------------
105: public void testAlternateLockDir() throws IOException {
106: LockableFileWriter lfw1 = null;
107: LockableFileWriter lfw2 = null;
108: try {
109: // open a valid locakable writer
110: lfw1 = new LockableFileWriter(file, true, altLockDir
111: .getAbsolutePath());
112: assertEquals(true, file.exists());
113: assertEquals(true, altLockFile.exists());
114:
115: // try to open a second writer
116: try {
117: lfw2 = new LockableFileWriter(file, true, altLockDir
118: .getAbsolutePath());
119: fail("Somehow able to open a locked file. ");
120: } catch (IOException ioe) {
121: String msg = ioe.getMessage();
122: assertTrue(
123: "Exception message does not start correctly. ",
124: msg.startsWith("Can't write file, lock "));
125: assertEquals(true, file.exists());
126: assertEquals(true, altLockFile.exists());
127: }
128:
129: } finally {
130: IOUtils.closeQuietly(lfw1);
131: IOUtils.closeQuietly(lfw2);
132: }
133: assertEquals(true, file.exists());
134: assertEquals(false, altLockFile.exists());
135: }
136:
137: //-----------------------------------------------------------------------
138: public void testFileNotLocked() throws IOException {
139: // open a valid locakable writer
140: LockableFileWriter lfw1 = null;
141: try {
142: lfw1 = new LockableFileWriter(file);
143: assertEquals(true, file.exists());
144: assertEquals(true, lockFile.exists());
145: } finally {
146: IOUtils.closeQuietly(lfw1);
147: }
148: assertEquals(true, file.exists());
149: assertEquals(false, lockFile.exists());
150:
151: // open a second valid writer on the same file
152: LockableFileWriter lfw2 = null;
153: try {
154: lfw2 = new LockableFileWriter(file);
155: assertEquals(true, file.exists());
156: assertEquals(true, lockFile.exists());
157: } finally {
158: IOUtils.closeQuietly(lfw2);
159: }
160: assertEquals(true, file.exists());
161: assertEquals(false, lockFile.exists());
162: }
163:
164: //-----------------------------------------------------------------------
165: public void testConstructor_File_encoding_badEncoding()
166: throws IOException {
167: Writer writer = null;
168: try {
169: writer = new LockableFileWriter(file, "BAD-ENCODE");
170: fail();
171: } catch (IOException ex) {
172: // expected
173: assertEquals(false, file.exists());
174: assertEquals(false, lockFile.exists());
175: } finally {
176: IOUtils.closeQuietly(writer);
177: }
178: assertEquals(false, file.exists());
179: assertEquals(false, lockFile.exists());
180: }
181:
182: //-----------------------------------------------------------------------
183: public void testConstructor_File_directory() throws IOException {
184: Writer writer = null;
185: try {
186: writer = new LockableFileWriter(getTestDirectory());
187: fail();
188: } catch (IOException ex) {
189: // expected
190: assertEquals(false, file.exists());
191: assertEquals(false, lockFile.exists());
192: } finally {
193: IOUtils.closeQuietly(writer);
194: }
195: assertEquals(false, file.exists());
196: assertEquals(false, lockFile.exists());
197: }
198:
199: //-----------------------------------------------------------------------
200: public void testConstructor_File_nullFile() throws IOException {
201: Writer writer = null;
202: try {
203: writer = new LockableFileWriter((File) null);
204: fail();
205: } catch (NullPointerException ex) {
206: // expected
207: assertEquals(false, file.exists());
208: assertEquals(false, lockFile.exists());
209: } finally {
210: IOUtils.closeQuietly(writer);
211: }
212: assertEquals(false, file.exists());
213: assertEquals(false, lockFile.exists());
214: }
215:
216: //-----------------------------------------------------------------------
217: public void testConstructor_fileName_nullFile() throws IOException {
218: Writer writer = null;
219: try {
220: writer = new LockableFileWriter((String) null);
221: fail();
222: } catch (NullPointerException ex) {
223: // expected
224: assertEquals(false, file.exists());
225: assertEquals(false, lockFile.exists());
226: } finally {
227: IOUtils.closeQuietly(writer);
228: }
229: assertEquals(false, file.exists());
230: assertEquals(false, lockFile.exists());
231: }
232:
233: }
|