add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / store / TestFileSwitchDirectory.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 java.io.IOException;
21 import java.util.HashSet;
22 import java.util.Set;
23
24 import org.apache.lucene.analysis.MockAnalyzer;
25 import org.apache.lucene.index.IndexFileNames;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.lucene.index.IndexWriter;
28 import org.apache.lucene.index.IndexWriterConfig;
29 import org.apache.lucene.index.TestIndexWriterReader;
30 import org.apache.lucene.util.LuceneTestCase;
31
32 public class TestFileSwitchDirectory extends LuceneTestCase {
33   /**
34    * Test if writing doc stores to disk and everything else to ram works.
35    * @throws IOException
36    */
37   public void testBasic() throws IOException {
38     Set<String> fileExtensions = new HashSet<String>();
39     fileExtensions.add(IndexFileNames.FIELDS_EXTENSION);
40     fileExtensions.add(IndexFileNames.FIELDS_INDEX_EXTENSION);
41     
42     MockDirectoryWrapper primaryDir = new MockDirectoryWrapper(random, new RAMDirectory());
43     primaryDir.setCheckIndexOnClose(false); // only part of an index
44     MockDirectoryWrapper secondaryDir = new MockDirectoryWrapper(random, new RAMDirectory());
45     secondaryDir.setCheckIndexOnClose(false); // only part of an index
46     
47     FileSwitchDirectory fsd = new FileSwitchDirectory(fileExtensions, primaryDir, secondaryDir, true);
48     IndexWriter writer = new IndexWriter(
49         fsd,
50         new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
51             setMergePolicy(newLogMergePolicy(false))
52     );
53     TestIndexWriterReader.createIndexNoClose(true, "ram", writer);
54     IndexReader reader = IndexReader.open(writer, true);
55     assertEquals(100, reader.maxDoc());
56     writer.commit();
57     // we should see only fdx,fdt files here
58     String[] files = primaryDir.listAll();
59     assertTrue(files.length > 0);
60     for (int x=0; x < files.length; x++) {
61       String ext = FileSwitchDirectory.getExtension(files[x]);
62       assertTrue(fileExtensions.contains(ext));
63     }
64     files = secondaryDir.listAll();
65     assertTrue(files.length > 0);
66     // we should not see fdx,fdt files here
67     for (int x=0; x < files.length; x++) {
68       String ext = FileSwitchDirectory.getExtension(files[x]);
69       assertFalse(fileExtensions.contains(ext));
70     }
71     reader.close();
72     writer.close();
73
74     files = fsd.listAll();
75     for(int i=0;i<files.length;i++) {
76       assertNotNull(files[i]);
77     }
78     fsd.close();
79   }
80 }