pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.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.Arrays;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.Set;
25
26 import org.apache.lucene.analysis.MockAnalyzer;
27 import org.apache.lucene.index.IndexFileNames;
28 import org.apache.lucene.index.IndexReader;
29 import org.apache.lucene.index.IndexWriter;
30 import org.apache.lucene.index.IndexWriterConfig;
31 import org.apache.lucene.index.TestIndexWriterReader;
32 import org.apache.lucene.util.LuceneTestCase;
33 import org.apache.lucene.util._TestUtil;
34
35 public class TestFileSwitchDirectory extends LuceneTestCase {
36   /**
37    * Test if writing doc stores to disk and everything else to ram works.
38    * @throws IOException
39    */
40   public void testBasic() throws IOException {
41     Set<String> fileExtensions = new HashSet<String>();
42     fileExtensions.add(IndexFileNames.FIELDS_EXTENSION);
43     fileExtensions.add(IndexFileNames.FIELDS_INDEX_EXTENSION);
44     
45     MockDirectoryWrapper primaryDir = new MockDirectoryWrapper(random, new RAMDirectory());
46     primaryDir.setCheckIndexOnClose(false); // only part of an index
47     MockDirectoryWrapper secondaryDir = new MockDirectoryWrapper(random, new RAMDirectory());
48     secondaryDir.setCheckIndexOnClose(false); // only part of an index
49     
50     FileSwitchDirectory fsd = new FileSwitchDirectory(fileExtensions, primaryDir, secondaryDir, true);
51     IndexWriter writer = new IndexWriter(
52         fsd,
53         new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
54             setMergePolicy(newLogMergePolicy(false))
55     );
56     TestIndexWriterReader.createIndexNoClose(true, "ram", writer);
57     IndexReader reader = IndexReader.open(writer, true);
58     assertEquals(100, reader.maxDoc());
59     writer.commit();
60     // we should see only fdx,fdt files here
61     String[] files = primaryDir.listAll();
62     assertTrue(files.length > 0);
63     for (int x=0; x < files.length; x++) {
64       String ext = FileSwitchDirectory.getExtension(files[x]);
65       assertTrue(fileExtensions.contains(ext));
66     }
67     files = secondaryDir.listAll();
68     assertTrue(files.length > 0);
69     // we should not see fdx,fdt files here
70     for (int x=0; x < files.length; x++) {
71       String ext = FileSwitchDirectory.getExtension(files[x]);
72       assertFalse(fileExtensions.contains(ext));
73     }
74     reader.close();
75     writer.close();
76
77     files = fsd.listAll();
78     for(int i=0;i<files.length;i++) {
79       assertNotNull(files[i]);
80     }
81     fsd.close();
82   }
83   
84   private Directory newFSSwitchDirectory(Set<String> primaryExtensions) throws IOException {
85     Directory a = new SimpleFSDirectory(_TestUtil.getTempDir("foo"));
86     Directory b = new SimpleFSDirectory(_TestUtil.getTempDir("bar"));
87     FileSwitchDirectory switchDir = new FileSwitchDirectory(primaryExtensions, a, b, true);
88     return new MockDirectoryWrapper(random, switchDir);
89   }
90   
91   // LUCENE-3380 -- make sure we get exception if the directory really does not exist.
92   public void testNoDir() throws Throwable {
93     Directory dir = newFSSwitchDirectory(Collections.<String>emptySet());
94     try {
95       IndexReader.open(dir, true);
96       fail("did not hit expected exception");
97     } catch (NoSuchDirectoryException nsde) {
98       // expected
99     }
100     dir.close();
101   }
102   
103   // LUCENE-3380 test that we can add a file, and then when we call list() we get it back
104   public void testDirectoryFilter() throws IOException {
105     Directory dir = newFSSwitchDirectory(Collections.<String>emptySet());
106     String name = "file";
107     try {
108       dir.createOutput(name).close();
109       assertTrue(dir.fileExists(name));
110       assertTrue(Arrays.asList(dir.listAll()).contains(name));
111     } finally {
112       dir.close();
113     }
114   }
115 }