Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
113   295   13   16.14
12   157   0.12   7
7     1.86  
1    
 
  TestMultiSearcher       Line # 40 113 13 99.2% 0.99242425
 
  (4)
 
1    package org.apache.lucene.search;
2   
3    /**
4    * Copyright 2004 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 org.apache.lucene.analysis.standard.StandardAnalyzer;
20    import org.apache.lucene.analysis.KeywordAnalyzer;
21    import org.apache.lucene.document.Document;
22    import org.apache.lucene.document.Field;
23    import org.apache.lucene.index.IndexReader;
24    import org.apache.lucene.index.IndexWriter;
25    import org.apache.lucene.index.Term;
26    import org.apache.lucene.queryParser.QueryParser;
27    import org.apache.lucene.search.Searcher;
28    import org.apache.lucene.store.Directory;
29    import org.apache.lucene.store.RAMDirectory;
30   
31    import junit.framework.TestCase;
32   
33    import java.io.IOException;
34   
35    /**
36    * Tests {@link MultiSearcher} class.
37    *
38    * @version $Id: TestMultiSearcher.java 387550 2006-03-21 15:36:32Z yonik $
39    */
 
40    public class TestMultiSearcher extends TestCase
41    {
 
42  4 toggle public TestMultiSearcher(String name)
43    {
44  4 super(name);
45    }
46   
47    /**
48    * ReturnS a new instance of the concrete MultiSearcher class
49    * used in this test.
50    */
 
51  4 toggle protected MultiSearcher getMultiSearcherInstance(Searcher[] searchers) throws IOException {
52  4 return new MultiSearcher(searchers);
53    }
54   
 
55  2 toggle public void testEmptyIndex()
56    throws Exception
57    {
58    // creating two directories for indices
59  2 Directory indexStoreA = new RAMDirectory();
60  2 Directory indexStoreB = new RAMDirectory();
61   
62    // creating a document to store
63  2 Document lDoc = new Document();
64  2 lDoc.add(new Field("fulltext", "Once upon a time.....", Field.Store.YES, Field.Index.TOKENIZED));
65  2 lDoc.add(new Field("id", "doc1", Field.Store.YES, Field.Index.UN_TOKENIZED));
66  2 lDoc.add(new Field("handle", "1", Field.Store.YES, Field.Index.UN_TOKENIZED));
67   
68    // creating a document to store
69  2 Document lDoc2 = new Document();
70  2 lDoc2.add(new Field("fulltext", "in a galaxy far far away.....",
71    Field.Store.YES, Field.Index.TOKENIZED));
72  2 lDoc2.add(new Field("id", "doc2", Field.Store.YES, Field.Index.UN_TOKENIZED));
73  2 lDoc2.add(new Field("handle", "1", Field.Store.YES, Field.Index.UN_TOKENIZED));
74   
75    // creating a document to store
76  2 Document lDoc3 = new Document();
77  2 lDoc3.add(new Field("fulltext", "a bizarre bug manifested itself....",
78    Field.Store.YES, Field.Index.TOKENIZED));
79  2 lDoc3.add(new Field("id", "doc3", Field.Store.YES, Field.Index.UN_TOKENIZED));
80  2 lDoc3.add(new Field("handle", "1", Field.Store.YES, Field.Index.UN_TOKENIZED));
81   
82    // creating an index writer for the first index
83  2 IndexWriter writerA = new IndexWriter(indexStoreA, new StandardAnalyzer(), true);
84    // creating an index writer for the second index, but writing nothing
85  2 IndexWriter writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), true);
86   
87    //--------------------------------------------------------------------
88    // scenario 1
89    //--------------------------------------------------------------------
90   
91    // writing the documents to the first index
92  2 writerA.addDocument(lDoc);
93  2 writerA.addDocument(lDoc2);
94  2 writerA.addDocument(lDoc3);
95  2 writerA.optimize();
96  2 writerA.close();
97   
98    // closing the second index
99  2 writerB.close();
100   
101    // creating the query
102  2 QueryParser parser = new QueryParser("fulltext", new StandardAnalyzer());
103  2 Query query = parser.parse("handle:1");
104   
105    // building the searchables
106  2 Searcher[] searchers = new Searcher[2];
107    // VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
108  2 searchers[0] = new IndexSearcher(indexStoreB);
109  2 searchers[1] = new IndexSearcher(indexStoreA);
110    // creating the multiSearcher
111  2 Searcher mSearcher = getMultiSearcherInstance(searchers);
112    // performing the search
113  2 Hits hits = mSearcher.search(query);
114   
115  2 assertEquals(3, hits.length());
116   
117    // iterating over the hit documents
118  8 for (int i = 0; i < hits.length(); i++) {
119  6 Document d = hits.doc(i);
120    }
121  2 mSearcher.close();
122   
123   
124    //--------------------------------------------------------------------
125    // scenario 2
126    //--------------------------------------------------------------------
127   
128    // adding one document to the empty index
129  2 writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
130  2 writerB.addDocument(lDoc);
131  2 writerB.optimize();
132  2 writerB.close();
133   
134    // building the searchables
135  2 Searcher[] searchers2 = new Searcher[2];
136    // VITAL STEP:adding the searcher for the empty index first, before the searcher for the populated index
137  2 searchers2[0] = new IndexSearcher(indexStoreB);
138  2 searchers2[1] = new IndexSearcher(indexStoreA);
139    // creating the mulitSearcher
140  2 Searcher mSearcher2 = getMultiSearcherInstance(searchers2);
141    // performing the same search
142  2 Hits hits2 = mSearcher2.search(query);
143   
144  2 assertEquals(4, hits2.length());
145   
146    // iterating over the hit documents
147  10 for (int i = 0; i < hits2.length(); i++) {
148    // no exception should happen at this point
149  8 Document d = hits2.doc(i);
150    }
151  2 mSearcher2.close();
152   
153    //--------------------------------------------------------------------
154    // scenario 3
155    //--------------------------------------------------------------------
156   
157    // deleting the document just added, this will cause a different exception to take place
158  2 Term term = new Term("id", "doc1");
159  2 IndexReader readerB = IndexReader.open(indexStoreB);
160  2 readerB.deleteDocuments(term);
161  2 readerB.close();
162   
163    // optimizing the index with the writer
164  2 writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), false);
165  2 writerB.optimize();
166  2 writerB.close();
167   
168    // building the searchables
169  2 Searcher[] searchers3 = new Searcher[2];
170   
171  2 searchers3[0] = new IndexSearcher(indexStoreB);
172  2 searchers3[1] = new IndexSearcher(indexStoreA);
173    // creating the mulitSearcher
174  2 Searcher mSearcher3 = getMultiSearcherInstance(searchers3);
175    // performing the same search
176  2 Hits hits3 = mSearcher3.search(query);
177   
178  2 assertEquals(3, hits3.length());
179   
180    // iterating over the hit documents
181  8