1
package org.apache.lucene.store;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import org.apache.lucene.util.LuceneTestCase;
21
import org.apache.lucene.util._TestUtil;
24
import java.io.IOException;
25
import java.util.Arrays;
27
public class TestDirectory extends LuceneTestCase {
29
public void testDetectClose() throws Throwable {
30
Directory[] dirs = new Directory[] { new RAMDirectory(), new SimpleFSDirectory(TEMP_DIR), new NIOFSDirectory(TEMP_DIR) };
31
for (Directory dir : dirs) {
34
dir.createOutput("test");
35
fail("did not hit expected exception");
36
} catch (AlreadyClosedException ace) {
42
// Test that different instances of FSDirectory can coexist on the same
43
// path, can read, write, and lock files.
44
public void testDirectInstantiation() throws Exception {
45
File path = _TestUtil.getTempDir("testDirectInstantiation");
48
Directory[] dirs = new Directory[sz];
50
dirs[0] = new SimpleFSDirectory(path, null);
51
dirs[1] = new NIOFSDirectory(path, null);
52
dirs[2] = new MMapDirectory(path, null);
54
for (int i=0; i<sz; i++) {
55
Directory dir = dirs[i];
57
String fname = "foo." + i;
58
String lockname = "foo" + i + ".lck";
59
IndexOutput out = dir.createOutput(fname);
60
out.writeByte((byte)i);
63
for (int j=0; j<sz; j++) {
64
Directory d2 = dirs[j];
66
assertTrue(d2.fileExists(fname));
67
assertEquals(1, d2.fileLength(fname));
69
// don't test read on MMapDirectory, since it can't really be
70
// closed and will cause a failure to delete the file.
71
if (d2 instanceof MMapDirectory) continue;
73
IndexInput input = d2.openInput(fname);
74
assertEquals((byte)i, input.readByte());
78
// delete with a different dir
79
dirs[(i+1)%sz].deleteFile(fname);
81
for (int j=0; j<sz; j++) {
82
Directory d2 = dirs[j];
83
assertFalse(d2.fileExists(fname));
86
Lock lock = dir.makeLock(lockname);
87
assertTrue(lock.obtain());
89
for (int j=0; j<sz; j++) {
90
Directory d2 = dirs[j];
91
Lock lock2 = d2.makeLock(lockname);
93
assertFalse(lock2.obtain(1));
94
} catch (LockObtainFailedException e) {
101
// now lock with different dir
102
lock = dirs[(i+1)%sz].makeLock(lockname);
103
assertTrue(lock.obtain());
107
for (int i=0; i<sz; i++) {
108
Directory dir = dirs[i];
111
assertFalse(dir.isOpen);
114
_TestUtil.rmDir(path);
118
public void testDontCreate() throws Throwable {
119
File path = new File(TEMP_DIR, "doesnotexist");
121
assertTrue(!path.exists());
122
Directory dir = new SimpleFSDirectory(path, null);
123
assertTrue(!path.exists());
126
_TestUtil.rmDir(path);
131
public void testRAMDirectoryFilter() throws IOException {
132
checkDirectoryFilter(new RAMDirectory());
136
public void testFSDirectoryFilter() throws IOException {
137
checkDirectoryFilter(newFSDirectory(_TestUtil.getTempDir("test")));
141
private void checkDirectoryFilter(Directory dir) throws IOException {
142
String name = "file";
144
dir.createOutput(name).close();
145
assertTrue(dir.fileExists(name));
146
assertTrue(Arrays.asList(dir.listAll()).contains(name));
153
public void testCopySubdir() throws Throwable {
154
File path = _TestUtil.getTempDir("testsubdir");
157
new File(path, "subdir").mkdirs();
158
Directory fsDir = new SimpleFSDirectory(path, null);
159
assertEquals(0, new RAMDirectory(fsDir).listAll().length);
161
_TestUtil.rmDir(path);
166
public void testNotDirectory() throws Throwable {
167
File path = _TestUtil.getTempDir("testnotdir");
168
Directory fsDir = new SimpleFSDirectory(path, null);
170
IndexOutput out = fsDir.createOutput("afile");
172
assertTrue(fsDir.fileExists("afile"));
174
new SimpleFSDirectory(new File(path, "afile"), null);
175
fail("did not hit expected exception");
176
} catch (NoSuchDirectoryException nsde) {
181
_TestUtil.rmDir(path);