Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
52   126   10   6.5
4   85   0.19   8
8     1.25  
1    
 
  TestDocumentWriter       Line # 31 52 10 100% 1.0
 
  (3)
 
1    package org.apache.lucene.index;
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 junit.framework.TestCase;
20    import org.apache.lucene.analysis.Analyzer;
21    import org.apache.lucene.analysis.WhitespaceAnalyzer;
22    import org.apache.lucene.analysis.TokenStream;
23    import org.apache.lucene.analysis.WhitespaceTokenizer;
24    import org.apache.lucene.document.*;
25    import org.apache.lucene.search.Similarity;
26    import org.apache.lucene.store.RAMDirectory;
27   
28    import java.io.Reader;
29    import java.io.IOException;
30   
 
31    public class TestDocumentWriter extends TestCase {
32    private RAMDirectory dir;
33   
 
34  3 toggle public TestDocumentWriter(String s) {
35  3 super(s);
36    }
37   
 
38  3 toggle protected void setUp() {
39  3 dir = new RAMDirectory();
40    }
41   
 
42  3 toggle protected void tearDown() {
43   
44    }
45   
 
46  1 toggle public void test() {
47  1 assertTrue(dir != null);
48   
49    }
50   
 
51  1 toggle public void testAddDocument() throws Exception {
52  1 Document testDoc = new Document();
53  1 DocHelper.setupDoc(testDoc);
54  1 Analyzer analyzer = new WhitespaceAnalyzer();
55  1 Similarity similarity = Similarity.getDefault();
56  1 DocumentWriter writer = new DocumentWriter(dir, analyzer, similarity, 50);
57  1 String segName = "test";
58  1 writer.addDocument(segName, testDoc);
59    //After adding the document, we should be able to read it back in
60  1 SegmentReader reader = SegmentReader.get(new SegmentInfo(segName, 1, dir));
61  1 assertTrue(reader != null);
62  1 Document doc = reader.document(0);
63  1 assertTrue(doc != null);
64   
65    //System.out.println("Document: " + doc);
66  1 Fieldable [] fields = doc.getFields("textField2");
67  1 assertTrue(fields != null && fields.length == 1);
68  1 assertTrue(fields[0].stringValue().equals(DocHelper.FIELD_2_TEXT));
69  1 assertTrue(fields[0].isTermVectorStored());
70   
71  1 fields = doc.getFields("textField1");
72  1 assertTrue(fields != null && fields.length == 1);
73  1 assertTrue(fields[0].stringValue().equals(DocHelper.FIELD_1_TEXT));
74  1 assertFalse(fields[0].isTermVectorStored());
75   
76  1 fields = doc.getFields("keyField");
77  1 assertTrue(fields != null && fields.length == 1);
78  1 assertTrue(fields[0].stringValue().equals(DocHelper.KEYWORD_TEXT));
79   
80  1 fields = doc.getFields(DocHelper.NO_NORMS_KEY);
81  1 assertTrue(fields != null && fields.length == 1);
82  1 assertTrue(fields[0].stringValue().equals(DocHelper.NO_NORMS_TEXT));
83   
84  1 fields = doc.getFields(DocHelper.TEXT_FIELD_3_KEY);
85  1 assertTrue(fields != null && fields.length == 1);
86  1 assertTrue(fields[0].stringValue().equals(DocHelper.FIELD_3_TEXT));
87   
88    // test that the norm file is not present if omitNorms is true
89  15 for (int i = 0; i < reader.fieldInfos.size(); i++) {
90  14 FieldInfo fi = reader.fieldInfos.fieldInfo(i);
91  14 if (fi.isIndexed) {
92  12 assertTrue(fi.omitNorms == !dir.fileExists(segName + ".f" + i));
93    }
94    }
95   
96    }
97   
 
98  1 toggle public void testPositionIncrementGap() throws IOException {
99  1 Analyzer analyzer = new Analyzer() {
 
100  2 toggle public TokenStream tokenStream(String fieldName, Reader reader) {
101  2 return new WhitespaceTokenizer(reader);
102    }
103   
 
104  1 toggle public int getPositionIncrementGap(String fieldName) {
105  1 return 500;
106    }
107    };
108   
109  1 Similarity similarity = Similarity.getDefault();
110  1 DocumentWriter writer = new DocumentWriter(dir, analyzer, similarity, 50);
111  1 Document doc = new Document();
112  1 doc.add(new Field("repeated", "repeated one", Field.Store.YES, Field.Index.TOKENIZED));
113  1 doc.add(new Field("repeated", "repeated two", Field.Store.YES, Field.Index.TOKENIZED));
114   
115  1 String segName = "test";
116  1 writer.addDocument(segName, doc);
117  1 SegmentReader reader = SegmentReader.get(new SegmentInfo(segName, 1, dir));
118   
119  1 TermPositions termPositions = reader.termPositions(new Term("repeated", "repeated"));
120  1 assertTrue(termPositions.next());
121  1 int freq = termPositions.freq();
122  1 assertEquals(2, freq);
123  1 assertEquals(0, termPositions.nextPosition());
124  1 assertEquals(502, termPositions.nextPosition());
125    }
126    }