add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / store / TestDirectory.java
1 package org.apache.lucene.store;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import org.apache.lucene.util.LuceneTestCase;
21 import org.apache.lucene.util._TestUtil;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.Arrays;
26
27 public class TestDirectory extends LuceneTestCase {
28
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) {
32       dir.close();
33       try {
34         dir.createOutput("test");
35         fail("did not hit expected exception");
36       } catch (AlreadyClosedException ace) {
37       }
38     }
39   }
40
41
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");
46
47     int sz = 3;
48     Directory[] dirs = new Directory[sz];
49
50     dirs[0] = new SimpleFSDirectory(path, null);
51     dirs[1] = new NIOFSDirectory(path, null);
52     dirs[2] = new MMapDirectory(path, null);
53
54     for (int i=0; i<sz; i++) {
55       Directory dir = dirs[i];
56       dir.ensureOpen();
57       String fname = "foo." + i;
58       String lockname = "foo" + i + ".lck";
59       IndexOutput out = dir.createOutput(fname);
60       out.writeByte((byte)i);
61       out.close();
62
63       for (int j=0; j<sz; j++) {
64         Directory d2 = dirs[j];
65         d2.ensureOpen();
66         assertTrue(d2.fileExists(fname));
67         assertEquals(1, d2.fileLength(fname));
68
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;
72         
73         IndexInput input = d2.openInput(fname);
74         assertEquals((byte)i, input.readByte());
75         input.close();
76       }
77
78       // delete with a different dir
79       dirs[(i+1)%sz].deleteFile(fname);
80
81       for (int j=0; j<sz; j++) {
82         Directory d2 = dirs[j];
83         assertFalse(d2.fileExists(fname));
84       }
85
86       Lock lock = dir.makeLock(lockname);
87       assertTrue(lock.obtain());
88
89       for (int j=0; j<sz; j++) {
90         Directory d2 = dirs[j];
91         Lock lock2 = d2.makeLock(lockname);
92         try {
93           assertFalse(lock2.obtain(1));
94         } catch (LockObtainFailedException e) {
95           // OK
96         }
97       }
98
99       lock.release();
100       
101       // now lock with different dir
102       lock = dirs[(i+1)%sz].makeLock(lockname);
103       assertTrue(lock.obtain());
104       lock.release();
105     }
106
107     for (int i=0; i<sz; i++) {
108       Directory dir = dirs[i];
109       dir.ensureOpen();
110       dir.close();
111       assertFalse(dir.isOpen);
112     }
113     
114     _TestUtil.rmDir(path);
115   }
116
117   // LUCENE-1464
118   public void testDontCreate() throws Throwable {
119     File path = new File(TEMP_DIR, "doesnotexist");
120     try {
121       assertTrue(!path.exists());
122       Directory dir = new SimpleFSDirectory(path, null);
123       assertTrue(!path.exists());
124       dir.close();
125     } finally {
126       _TestUtil.rmDir(path);
127     }
128   }
129
130   // LUCENE-1468
131   public void testRAMDirectoryFilter() throws IOException {
132     checkDirectoryFilter(new RAMDirectory());
133   }
134
135   // LUCENE-1468
136   public void testFSDirectoryFilter() throws IOException {
137     checkDirectoryFilter(newFSDirectory(_TestUtil.getTempDir("test")));
138   }
139
140   // LUCENE-1468
141   private void checkDirectoryFilter(Directory dir) throws IOException {
142     String name = "file";
143     try {
144       dir.createOutput(name).close();
145       assertTrue(dir.fileExists(name));
146       assertTrue(Arrays.asList(dir.listAll()).contains(name));
147     } finally {
148       dir.close();
149     }
150   }
151
152   // LUCENE-1468
153   public void testCopySubdir() throws Throwable {
154     File path = _TestUtil.getTempDir("testsubdir");
155     try {
156       path.mkdirs();
157       new File(path, "subdir").mkdirs();
158       Directory fsDir = new SimpleFSDirectory(path, null);
159       assertEquals(0, new RAMDirectory(fsDir).listAll().length);
160     } finally {
161       _TestUtil.rmDir(path);
162     }
163   }
164
165   // LUCENE-1468
166   public void testNotDirectory() throws Throwable {
167     File path = _TestUtil.getTempDir("testnotdir");
168     Directory fsDir = new SimpleFSDirectory(path, null);
169     try {
170       IndexOutput out = fsDir.createOutput("afile");
171       out.close();
172       assertTrue(fsDir.fileExists("afile"));
173       try {
174         new SimpleFSDirectory(new File(path, "afile"), null);
175         fail("did not hit expected exception");
176       } catch (NoSuchDirectoryException nsde) {
177         // Expected
178       }
179     } finally {
180       fsDir.close();
181       _TestUtil.rmDir(path);
182     }
183   }
184 }
185