Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
141   283   26   12.82
16   222   0.18   3.67
11     2.36  
3    
 
  TestIndexModifier       Line # 43 99 13 95.5% 0.954955
  TestIndexModifier.PowerIndex       Line # 186 5 2 100% 1.0
  IndexThread       Line # 201 37 11 92% 0.92
 
  (3)
 
1    package org.apache.lucene.index;
2   
3    /**
4    * Copyright 2005 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19    import junit.framework.TestCase;
20    import org.apache.lucene.analysis.Analyzer;
21    import org.apache.lucene.analysis.SimpleAnalyzer;
22    import org.apache.lucene.analysis.standard.StandardAnalyzer;
23    import org.apache.lucene.document.Document;
24    import org.apache.lucene.document.Field;
25    import org.apache.lucene.document.Field.Index;
26    import org.apache.lucene.document.Field.Store;
27    import org.apache.lucene.store.Directory;
28    import org.apache.lucene.store.FSDirectory;
29    import org.apache.lucene.store.RAMDirectory;
30   
31    import java.io.File;
32    import java.io.IOException;
33    import java.util.EmptyStackException;
34    import java.util.Random;
35    import java.util.Stack;
36   
37    /**
38    * Tests for the "IndexModifier" class, including accesses from two threads at the
39    * same time.
40    *
41    * @author Daniel Naber
42    */
 
43    public class TestIndexModifier extends TestCase {
44   
45    private int docCount = 0;
46   
47    private final Term allDocTerm = new Term("all", "x");
48   
 
49  1 toggle public void testIndex() throws IOException {
50  1 Directory ramDir = new RAMDirectory();
51  1 IndexModifier i = new IndexModifier(ramDir, new StandardAnalyzer(), true);
52  1 i.addDocument(getDoc());
53  1 assertEquals(1, i.docCount());
54  1 i.flush();
55  1 i.addDocument(getDoc(), new SimpleAnalyzer());
56  1 assertEquals(2, i.docCount());
57  1 i.optimize();
58  1 assertEquals(2, i.docCount());
59  1 i.flush();
60  1 i.deleteDocument(0);
61  1 assertEquals(1, i.docCount());
62  1 i.flush();
63  1 assertEquals(1, i.docCount());
64  1 i.addDocument(getDoc());
65  1 i.addDocument(getDoc());
66  1 i.flush();
67    // depend on merge policy - assertEquals(3, i.docCount());
68  1 i.deleteDocuments(allDocTerm);
69  1 assertEquals(0, i.docCount());
70  1 i.optimize();
71  1 assertEquals(0, i.docCount());
72   
73    // Lucene defaults:
74  1 assertNull(i.getInfoStream());
75  1 assertTrue(i.getUseCompoundFile());
76  1 assertEquals(10, i.getMaxBufferedDocs());
77  1 assertEquals(10000, i.getMaxFieldLength());
78  1 assertEquals(10, i.getMergeFactor());
79    // test setting properties:
80  1 i.setMaxBufferedDocs(100);
81  1 i.setMergeFactor(25);
82  1 i.setMaxFieldLength(250000);
83  1 i.addDocument(getDoc());
84  1 i.setUseCompoundFile(false);
85  1 i.flush();
86  1 assertEquals(100, i.getMaxBufferedDocs());
87  1 assertEquals(25, i.getMergeFactor());
88  1 assertEquals(250000, i.getMaxFieldLength());
89  1 assertFalse(i.getUseCompoundFile());
90   
91    // test setting properties when internally the reader is opened:
92  1 i.deleteDocuments(allDocTerm);
93  1 i.setMaxBufferedDocs(100);
94  1 i.setMergeFactor(25);
95  1 i.setMaxFieldLength(250000);
96  1 i.addDocument(getDoc());
97  1 i.setUseCompoundFile(false);
98  1 i.optimize();
99  1 assertEquals(100, i.getMaxBufferedDocs());
100  1 assertEquals(25, i.getMergeFactor());
101  1 assertEquals(250000, i.getMaxFieldLength());
102  1 assertFalse(i.getUseCompoundFile());
103   
104  1 i.close();
105  1 try {
106  1 i.docCount();
107  0 fail();
108    } catch (IllegalStateException e) {
109    // expected exception
110    }
111    }
112   
 
113  1 toggle public void testExtendedIndex() throws IOException {
114  1 Directory ramDir = new RAMDirectory();
115  1 PowerIndex powerIndex = new PowerIndex(ramDir, new StandardAnalyzer(), true);
116  1 powerIndex.addDocument(getDoc());
117  1 powerIndex.addDocument(getDoc());
118  1 powerIndex.addDocument(getDoc());
119  1 powerIndex.addDocument(getDoc());
120  1 powerIndex.addDocument(getDoc());
121  1 powerIndex.flush();
122  1 assertEquals(5, powerIndex.docFreq(allDocTerm));
123  1 powerIndex.close();
124    }
125   
 
126  11 toggle private Document getDoc() {
127  11 Document doc = new Document();
128  11 doc.add(new Field("body", new Integer(docCount).toString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
129  11 doc.add(new Field("all", "x", Field.Store.YES, Field.Index.UN_TOKENIZED));
130  11 docCount++;
131  11 return doc;
132    }
133   
 
134  1 toggle public void testIndexWithThreads() throws IOException {
135  1 testIndexInternal(0);
136  1 testIndexInternal(10);
137  1 testIndexInternal(50);
138    }
139   
 
140  3 toggle private void testIndexInternal(int maxWait) throws IOException {
141  3 final boolean create = true;
142    //Directory rd = new RAMDirectory();
143    // work on disk to make sure potential lock problems are tested:
144  3 String tempDir = System.getProperty("java.io.tmpdir");
145  3 if (tempDir == null)
146  0 throw new IOException("java.io.tmpdir undefined, cannot run test");
147  3 File indexDir = new File(tempDir, "lucenetestindex");
148  3 Directory rd = FSDirectory.getDirectory(indexDir, create);
149  3 IndexThread.id = 0;
150  3 IndexThread.idStack.clear();
151  3 IndexModifier index = new IndexModifier(rd, new StandardAnalyzer(), create);
152  3 IndexThread thread1 = new IndexThread(index, maxWait, 1);