~ubuntu-branches/ubuntu/trusty/commons-io/trusty

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/io/output/LockableFileWriterTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2007-04-13 08:20:49 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070413082049-2hd3s5ixtxsgvnvm
Tags: 1.3.1.dfsg.1-1
* New upstream (closes: #418973)
* java-gcj-compat-dev and cdbs transition
* Removed junit at the moment (closes: #397567)
* No javadoc at the moment

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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
 
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
7
8
 * 
8
9
 *      http://www.apache.org/licenses/LICENSE-2.0
9
10
 * 
13
14
 * See the License for the specific language governing permissions and
14
15
 * limitations under the License.
15
16
 */
16
 
 
17
 
 
18
17
package org.apache.commons.io.output;
19
18
 
20
 
 
 
19
import java.io.File;
21
20
import java.io.IOException;
22
 
import java.io.File;
 
21
import java.io.Writer;
23
22
 
24
 
import junit.framework.TestCase;
 
23
import org.apache.commons.io.IOUtils;
 
24
import org.apache.commons.io.testtools.FileBasedTestCase;
25
25
 
26
26
/**
27
27
 * Tests that files really lock, although no writing is done as 
28
28
 * the locking is tested only on construction. 
29
29
 *
30
 
 * @author Henri Yandell (bayard at apache dot org)
31
 
 * @version $Revision: 1.2 $ $Date: 2004/02/29 21:07:14 $
 
30
 * @version $Revision: 471628 $ $Date: 2006-11-05 20:06:45 -0800 (Sun, 05 Nov 2006) $
32
31
 */
33
 
 
34
 
public class LockableFileWriterTest extends TestCase {
 
32
public class LockableFileWriterTest extends FileBasedTestCase {
35
33
 
36
34
    private File file;
 
35
    private File lockDir;
 
36
    private File lockFile;
 
37
    private File altLockDir;
 
38
    private File altLockFile;
37
39
 
38
40
    public LockableFileWriterTest(String name) {
39
41
        super(name);
40
42
    }
41
43
 
42
44
    public void setUp() {
43
 
        this.file = new File("testlockfile");
 
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");
44
50
    }
45
51
 
46
52
    public void tearDown() {
47
 
        this.file.delete();
 
53
        file.delete();
 
54
        lockFile.delete();
 
55
        altLockFile.delete();
48
56
    }
49
57
 
 
58
    //-----------------------------------------------------------------------
50
59
    public void testFileLocked() throws IOException {
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 ioe) {
56
 
            String 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
 
 
 
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
    //-----------------------------------------------------------------------
64
133
    public void testFileNotLocked() throws IOException {
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 ioe) {
71
 
            String msg = ioe.getMessage();
72
 
            if( msg.startsWith("Can't write file, lock ") ) {
73
 
                fail("Somehow unable to open a unlocked file. ");
74
 
            }
75
 
        }
 
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());
76
225
    }
77
226
 
78
227
}