1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  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
17package org.apache.harmony.nio.tests.java.nio.channels;
18
19import java.io.File;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.io.RandomAccessFile;
23import java.nio.channels.ClosedChannelException;
24import java.nio.channels.FileChannel;
25import java.nio.channels.FileLock;
26
27import junit.framework.TestCase;
28
29/**
30 * Tests class FileLock.
31 */
32public class FileLockTest extends TestCase {
33
34	private FileChannel readWriteChannel;
35
36	private MockFileLock mockLock;
37
38	class MockFileLock extends FileLock {
39
40		boolean isValid = true;
41
42		protected MockFileLock(FileChannel channel, long position, long size,
43				boolean shared) {
44			super(channel, position, size, shared);
45		}
46
47		public boolean isValid() {
48			return isValid;
49		}
50
51		public void release() throws IOException {
52			isValid = false;
53		}
54	}
55
56	protected void setUp() throws Exception {
57		super.setUp();
58		File tempFile = File.createTempFile("testing", "tmp");
59		tempFile.deleteOnExit();
60		RandomAccessFile randomAccessFile = new RandomAccessFile(tempFile, "rw");
61		readWriteChannel = randomAccessFile.getChannel();
62		mockLock = new MockFileLock(readWriteChannel, 10, 100, false);
63	}
64
65	protected void tearDown() throws IOException {
66	    if (readWriteChannel != null) {
67	        readWriteChannel.close();
68	    }
69	}
70
71	/**
72	 * @tests java.nio.channels.FileLock#FileLock(FileChannel, long, long,
73	 *        boolean)
74	 */
75	public void test_Constructor_Ljava_nio_channels_FileChannelJJZ() {
76		FileLock fileLock1 = new MockFileLock(null, 0, 0, false);
77		assertNull(fileLock1.channel());
78
79		try {
80			new MockFileLock(readWriteChannel, -1, 0, false);
81			fail("should throw IllegalArgumentException.");
82		} catch (IllegalArgumentException ex) {
83			// expected
84		}
85		try {
86			new MockFileLock(readWriteChannel, 0, -1, false);
87			fail("should throw IllegalArgumentException.");
88		} catch (IllegalArgumentException ex) {
89			// expected
90		}
91        // Harmony-682 regression test
92        try {
93            new MockFileLock(readWriteChannel, Long.MAX_VALUE, 1, false);
94            fail("should throw IllegalArgumentException.");
95        } catch (IllegalArgumentException ex) {
96            // expected
97        }
98	}
99
100	/**
101	 * @tests java.nio.channels.FileLock#channel()
102	 */
103	public void test_channel() {
104		assertSame(readWriteChannel, mockLock.channel());
105		FileLock lock = new MockFileLock(null, 0, 10, true);
106		assertNull(lock.channel());
107	}
108
109	/**
110	 * @tests java.nio.channels.FileLock#position()
111	 */
112	public void test_position() {
113		FileLock fileLock1 = new MockFileLock(readWriteChannel, 20, 100, true);
114		assertEquals(20, fileLock1.position());
115
116		final long position = ((long) Integer.MAX_VALUE + 1);
117		FileLock fileLock2 = new MockFileLock(readWriteChannel, position, 100,
118				true);
119		assertEquals(position, fileLock2.position());
120	}
121
122	/**
123	 * @tests java.nio.channels.FileLock#size()
124	 */
125	public void test_size() {
126		FileLock fileLock1 = new MockFileLock(readWriteChannel, 20, 100, true);
127		assertEquals(100, fileLock1.size());
128
129		final long position = 0x0FFFFFFFFFFFFFFFL;
130		final long size = ((long) Integer.MAX_VALUE + 1);
131		FileLock fileLock2 = new MockFileLock(readWriteChannel, position, size,
132				true);
133		assertEquals(size, fileLock2.size());
134	}
135
136	/**
137	 * @tests java.nio.channels.FileLock#isShared()
138	 */
139	public void test_isShared() {
140		assertFalse(mockLock.isShared());
141		FileLock lock = new MockFileLock(null, 0, 10, true);
142		assertTrue(lock.isShared());
143	}
144
145	/**
146	 * @tests java.nio.channels.FileLock#overlaps(long, long)
147	 */
148	public void test_overlaps_JJ() {
149		assertTrue(mockLock.overlaps(0, 11));
150		assertFalse(mockLock.overlaps(0, 10));
151		assertTrue(mockLock.overlaps(100, 110));
152		assertTrue(mockLock.overlaps(99, 110));
153		assertFalse(mockLock.overlaps(-1, 10));
154        //Harmony-671 regression test
155        assertTrue(mockLock.overlaps(1, 120));
156        assertTrue(mockLock.overlaps(20, 50));
157	}
158
159	/**
160	 * @tests java.nio.channels.FileLock#isValid()
161	 */
162	public void test_isValid() throws IOException {
163		FileLock fileLock = readWriteChannel.lock();
164		assertTrue(fileLock.isValid());
165		fileLock.release();
166		assertFalse(fileLock.isValid());
167	}
168
169    /**
170     * @tests java.nio.channels.FileLock#release()
171     */
172    public void test_release() throws Exception {
173        File file = File.createTempFile("test", "tmp");
174        file.deleteOnExit();
175        FileOutputStream fout = new FileOutputStream(file);
176        FileChannel fileChannel = fout.getChannel();
177        FileLock fileLock = fileChannel.lock();
178        fileChannel.close();
179        try {
180            fileLock.release();
181            fail("should throw ClosedChannelException");
182        } catch (ClosedChannelException e) {
183            // expected
184        }
185
186        // release after release
187        fout = new FileOutputStream(file);
188        fileChannel = fout.getChannel();
189        fileLock = fileChannel.lock();
190        fileLock.release();
191        fileChannel.close();
192        try {
193            fileLock.release();
194            fail("should throw ClosedChannelException");
195        } catch (ClosedChannelException e) {
196            //expected
197        }
198    }
199}
200