KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > output > LockableFileWriterTest


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.io.output;
19
20
21 import java.io.IOException JavaDoc;
22 import java.io.File JavaDoc;
23
24 import junit.framework.TestCase;
25
26 /**
27  * Tests that files really lock, although no writing is done as
28  * the locking is tested only on construction.
29  *
30  * @author Henri Yandell (bayard at apache dot org)
31  * @version $Revision: 1.2 $ $Date: 2004/02/29 21:07:14 $
32  */

33
34 public class LockableFileWriterTest extends TestCase {
35
36     private File JavaDoc file;
37
38     public LockableFileWriterTest(String JavaDoc name) {
39         super(name);
40     }
41
42     public void setUp() {
43         this.file = new File JavaDoc("testlockfile");
44     }
45
46     public void tearDown() {
47         this.file.delete();
48     }
49
50     public void testFileLocked() throws IOException JavaDoc {
51         LockableFileWriter lfw = new LockableFileWriter(this.file);
52         try {
53             LockableFileWriter lfw2 = new LockableFileWriter(this.file);
54             fail("Somehow able to open a locked file. ");
55         } catch(IOException JavaDoc ioe) {
56             String JavaDoc msg = ioe.getMessage();
57             assertTrue( "Exception message does not start correctly. ",
58                         msg.startsWith("Can't write file, lock ") );
59         } finally {
60             lfw.close();
61         }
62     }
63
64     public void testFileNotLocked() throws IOException JavaDoc {
65         LockableFileWriter lfw = new LockableFileWriter(this.file);
66         lfw.close();
67         try {
68             LockableFileWriter lfw2 = new LockableFileWriter(this.file);
69             lfw2.close();
70         } catch(IOException JavaDoc ioe) {
71             String JavaDoc msg = ioe.getMessage();
72             if( msg.startsWith("Can't write file, lock ") ) {
73                 fail("Somehow unable to open a unlocked file. ");
74             }
75         }
76     }
77
78 }
79
Popular Tags