01: /*
02: This source file is part of Smyle, a database library.
03: For up-to-date information, see http://www.drjava.de/smyle
04: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
05:
06: This library is free software; you can redistribute it and/or
07: modify it under the terms of the GNU Lesser General Public
08: License as published by the Free Software Foundation; either
09: version 2.1 of the License, or (at your option) any later version.
10:
11: This library is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: Lesser General Public License for more details.
15:
16: You should have received a copy of the GNU Lesser General Public
17: License along with this library; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19:
20: For full license text, see doc/license/lgpl.txt in this distribution
21: */
22:
23: package drjava.smyle.tests;
24:
25: import junit.framework.*;
26: import java.io.*;
27:
28: /** tests how VM and OS handle file access conflicts */
29: public class FileHandlingTest extends TestCase {
30: public FileHandlingTest(String name) {
31: super (name);
32: }
33:
34: static final String filename = "temp/somefile";
35:
36: /** last writer/closer wins */
37: public void testInterleavingFileOutputStreams() throws Exception {
38: FileOutputStream fos1 = new FileOutputStream(filename);
39: fos1.write(1);
40: FileOutputStream fos2 = new FileOutputStream(filename);
41: fos2.write(2);
42: fos1.close();
43: fos2.close();
44:
45: assertContents(filename, 2);
46: }
47:
48: /** write order matters, not close order */
49: public void testInterleavingFileOutputStreamsReverseClose()
50: throws Exception {
51: FileOutputStream fos1 = new FileOutputStream(filename);
52: fos1.write(1);
53: FileOutputStream fos2 = new FileOutputStream(filename);
54: fos2.write(2);
55: fos2.close();
56: fos1.close();
57:
58: assertContents(filename, 2);
59: }
60:
61: void assertContents(String filename, int theByte) throws Exception {
62: FileInputStream fis = new FileInputStream(filename);
63: assertEquals(theByte, fis.read());
64: fis.close();
65: }
66:
67: public void testRandomAccessFiles() throws Exception {
68: RandomAccessFile raf1 = new RandomAccessFile(filename, "rw");
69: RandomAccessFile raf2 = new RandomAccessFile(filename, "rw");
70:
71: try {
72: raf1.writeLong(123L);
73: assertEquals(123L, raf2.readLong());
74:
75: raf2.seek(0);
76: raf2.writeLong(456L);
77: raf1.seek(0);
78: assertEquals(456L, raf1.readLong());
79: } finally {
80: raf1.close();
81: raf2.close();
82: }
83: }
84: }
|