Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
36   107   8   12
10   66   0.22   3
3     2.67  
1    
 
  TestIndexWriterMerging       Line # 28 36 8 85.7% 0.85714287
 
  (1)
 
1    package org.apache.lucene.index;
2    /**
3    * Copyright 2006 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10    *
11    * Unless required by applicable law or agreed to in writing, software
12    * distributed under the License is distributed on an "AS IS" BASIS,
13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    * See the License for the specific language governing permissions and
15    * limitations under the License.
16    */
17   
18    import org.apache.lucene.store.Directory;
19    import org.apache.lucene.store.RAMDirectory;
20    import org.apache.lucene.analysis.standard.StandardAnalyzer;
21    import org.apache.lucene.document.Document;
22    import org.apache.lucene.document.Field;
23    import junit.framework.TestCase;
24   
25    import java.io.IOException;
26   
27   
 
28    public class TestIndexWriterMerging extends TestCase
29    {
30   
31    /**
32    * Tests that index merging (specifically addIndexes()) doesn't
33    * change the index order of documents.
34    */
 
35  1 toggle public void testLucene() throws IOException
36    {
37   
38  1 int num=100;
39   
40  1 Directory indexA = new RAMDirectory();
41  1 Directory indexB = new RAMDirectory();
42   
43  1 fillIndex(indexA, 0, num);
44  1 boolean fail = verifyIndex(indexA, 0);
45  1 if (fail)
46    {
47  0 fail("Index a is invalid");
48    }
49   
50  1 fillIndex(indexB, num, num);
51  1 fail = verifyIndex(indexB, num);
52  1 if (fail)
53    {
54  0 fail("Index b is invalid");
55    }
56   
57  1 Directory merged = new RAMDirectory();
58   
59  1 IndexWriter writer = new IndexWriter(merged, new StandardAnalyzer(), true);
60  1 writer.setMergeFactor(2);
61   
62  1 writer.addIndexes(new Directory[]{indexA, indexB});
63  1 writer.close();
64   
65  1 fail = verifyIndex(merged, 0);
66  1 merged.close();
67   
68  1 assertFalse("The merged index is invalid", fail);
69    }
70   
 
71  3 toggle private boolean verifyIndex(Directory directory, int startAt) throws IOException
72    {
73  3 boolean fail = false;
74  3 IndexReader reader = IndexReader.open(directory);
75   
76  3 int max = reader.maxDoc();
77  403 for (int i = 0; i < max; i++)
78    {
79  400 Document temp = reader.document(i);
80    //System.out.println("doc "+i+"="+temp.getField("count").stringValue());
81    //compare the index doc number to the value that it should be
82  400 if (!temp.getField("count").stringValue().equals((i + startAt) + ""))
83    {
84  0 fail = true;
85  0 System.out.println("Document " + (i + startAt) + " is returning document " + temp.getField("count").stringValue());
86    }
87    }
88  3 return fail;
89    }
90   
 
91  2 toggle private void fillIndex(Directory dir, int start, int numDocs) throws IOException
92    {
93   
94  2 IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
95  2 writer.setMergeFactor(2);
96  2 writer.setMaxBufferedDocs(2);
97   
98  202 for (int i = start; i < (start + numDocs); i++)
99    {
100  200 Document temp = new Document();
101  200 temp.add(new Field("count", (""+i), Field.Store.YES, Field.Index.UN_TOKENIZED));
102   
103  200 writer.addDocument(temp);
104    }
105  2 writer.close();
106    }
107    }