add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / index / TestIndexWriterLockRelease.java
1 package org.apache.lucene.index;
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 java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import org.apache.lucene.util.LuceneTestCase;
24 import org.apache.lucene.analysis.standard.StandardAnalyzer;
25 import org.apache.lucene.util._TestUtil;
26 import org.apache.lucene.index.IndexWriter;
27 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
28 import org.apache.lucene.store.Directory;
29
30 /**
31  * This tests the patch for issue #LUCENE-715 (IndexWriter does not
32  * release its write lock when trying to open an index which does not yet
33  * exist).
34  */
35
36 public class TestIndexWriterLockRelease extends LuceneTestCase {
37     private java.io.File __test_dir;
38
39     @Override
40     public void setUp() throws Exception {
41         super.setUp();
42         if (this.__test_dir == null) {
43             this.__test_dir = _TestUtil.getTempDir("testIndexWriter");
44
45             if (this.__test_dir.exists()) {
46                 throw new IOException("test directory \"" + this.__test_dir.getPath() + "\" already exists (please remove by hand)");
47             }
48
49             if (!this.__test_dir.mkdirs()
50                 && !this.__test_dir.isDirectory()) {
51                 throw new IOException("unable to create test directory \"" + this.__test_dir.getPath() + "\"");
52             }
53         }
54     }
55
56     @Override
57     public void tearDown() throws Exception {
58         if (this.__test_dir != null) {
59             File[] files = this.__test_dir.listFiles();
60
61             for (int i = 0;
62                 i < files.length;
63                 ++i) {
64                 if (!files[i].delete()) {
65                     throw new IOException("unable to remove file in test directory \"" + this.__test_dir.getPath() + "\" (please remove by hand)");
66                 }
67             }
68
69             if (!this.__test_dir.delete()) {
70                 throw new IOException("unable to remove test directory \"" + this.__test_dir.getPath() + "\" (please remove by hand)");
71             }
72         }
73         super.tearDown();
74     }
75
76     public void testIndexWriterLockRelease() throws IOException {
77         Directory dir = newFSDirectory(this.__test_dir);
78         try {
79           new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT,
80               new StandardAnalyzer(TEST_VERSION_CURRENT))
81           .setOpenMode(OpenMode.APPEND));
82         } catch (FileNotFoundException e) {
83             try {
84               new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT,
85                   new StandardAnalyzer(TEST_VERSION_CURRENT))
86               .setOpenMode(OpenMode.APPEND));
87             } catch (FileNotFoundException e1) {
88             }
89         } finally {
90           dir.close();
91         }
92     }
93 }