01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.lucene.index;
18:
19: import java.io.File;
20: import java.io.IOException;
21:
22: import junit.framework.TestCase;
23: import org.apache.lucene.analysis.SimpleAnalyzer;
24: import org.apache.lucene.document.Document;
25: import org.apache.lucene.document.Field;
26: import org.apache.lucene.store.Directory;
27: import org.apache.lucene.store.FSDirectory;
28:
29: /**
30: * @author kimchy
31: */
32: public class LuceneUtilsTests extends TestCase {
33:
34: private String indexFilePath = "target/test-index";
35:
36: private Directory directory;
37:
38: protected void setUp() throws Exception {
39: File indexFile = new File(indexFilePath);
40: org.compass.core.lucene.util.LuceneUtils.deleteDir(indexFile);
41: directory = FSDirectory.getDirectory(indexFile, true);
42: }
43:
44: protected void tearDown() throws Exception {
45: File indexFile = new File(indexFilePath);
46: org.compass.core.lucene.util.LuceneUtils.deleteDir(indexFile);
47: }
48:
49: public void testIsCompound() throws IOException {
50: assertTrue(LuceneUtils.isCompound(directory));
51: createIndex();
52: assertTrue(LuceneUtils.isCompound(directory));
53: addDocument(true);
54: assertTrue(LuceneUtils.isCompound(directory));
55: }
56:
57: public void testIsCompound2() throws IOException {
58: assertTrue(LuceneUtils.isCompound(directory));
59: createIndex();
60: assertTrue(LuceneUtils.isCompound(directory));
61: addDocument(false);
62: assertFalse(LuceneUtils.isCompound(directory));
63: }
64:
65: public void testIsUnCompound() throws IOException {
66: assertTrue(LuceneUtils.isUnCompound(directory));
67: createIndex();
68: assertTrue(LuceneUtils.isUnCompound(directory));
69: addDocument(false);
70: assertTrue(LuceneUtils.isUnCompound(directory));
71: }
72:
73: public void testIsUnCompound2() throws IOException {
74: assertTrue(LuceneUtils.isUnCompound(directory));
75: createIndex();
76: assertTrue(LuceneUtils.isUnCompound(directory));
77: addDocument(true);
78: assertFalse(LuceneUtils.isUnCompound(directory));
79: }
80:
81: private void createIndex() throws IOException {
82: IndexWriter indexWriter = new IndexWriter(directory,
83: new SimpleAnalyzer(), true);
84: indexWriter.close();
85: }
86:
87: private void addDocument(boolean useCompound) throws IOException {
88: IndexWriter indexWriter = new IndexWriter(directory,
89: new SimpleAnalyzer(), false);
90: indexWriter.setUseCompoundFile(useCompound);
91: Document doc = new Document();
92: doc.add(new Field("test", "test", Field.Store.YES,
93: Field.Index.TOKENIZED));
94: indexWriter.addDocument(doc);
95: indexWriter.close();
96: }
97: }
|