Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
135   226   9   22.5
6   175   0.07   6
6     1.5  
1    
 
  TestFieldsReader       Line # 39 135 9 100% 1.0
 
  (4)
 
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 java.io.File;
20    import java.io.IOException;
21    import java.util.Collections;
22    import java.util.HashSet;
23    import java.util.Iterator;
24    import java.util.List;
25    import java.util.Set;
26   
27    import junit.framework.TestCase;
28   
29    import org.apache.lucene.analysis.WhitespaceAnalyzer;
30    import org.apache.lucene.document.Document;
31    import org.apache.lucene.document.Field;
32    import org.apache.lucene.document.Fieldable;
33    import org.apache.lucene.document.LoadFirstFieldSelector;
34    import org.apache.lucene.document.SetBasedFieldSelector;
35    import org.apache.lucene.search.Similarity;
36    import org.apache.lucene.store.FSDirectory;
37    import org.apache.lucene.store.RAMDirectory;
38   
 
39    public class TestFieldsReader extends TestCase {
40    private RAMDirectory dir = new RAMDirectory();
41    private Document testDoc = new Document();
42    private FieldInfos fieldInfos = null;
43   
 
44  4 toggle public TestFieldsReader(String s) {
45  4 super(s);
46    }
47   
 
48  4 toggle protected void setUp() throws IOException {
49  4 fieldInfos = new FieldInfos();
50  4 DocHelper.setupDoc(testDoc);
51  4 fieldInfos.add(testDoc);
52  4 DocumentWriter writer = new DocumentWriter(dir, new WhitespaceAnalyzer(),
53    Similarity.getDefault(), 50);
54  4 assertTrue(writer != null);
55  4 writer.addDocument("test", testDoc);
56    }
57   
 
58  1 toggle public void test() throws IOException {
59  1 assertTrue(dir != null);
60  1 assertTrue(fieldInfos != null);
61  1 FieldsReader reader = new FieldsReader(dir, "test", fieldInfos);
62  1 assertTrue(reader != null);
63  1 assertTrue(reader.size() == 1);
64  1 Document doc = reader.doc(0, null);
65  1 assertTrue(doc != null);
66  1 assertTrue(doc.getField(DocHelper.TEXT_FIELD_1_KEY) != null);
67   
68  1 Fieldable field = doc.getField(DocHelper.TEXT_FIELD_2_KEY);
69  1 assertTrue(field != null);
70  1 assertTrue(field.isTermVectorStored() == true);
71   
72  1 assertTrue(field.isStoreOffsetWithTermVector() == true);
73  1 assertTrue(field.isStorePositionWithTermVector() == true);
74  1 assertTrue(field.getOmitNorms() == false);
75   
76  1 field = doc.getField(DocHelper.TEXT_FIELD_3_KEY);
77  1 assertTrue(field != null);
78  1 assertTrue(field.isTermVectorStored() == false);
79  1 assertTrue(field.isStoreOffsetWithTermVector() == false);
80  1 assertTrue(field.isStorePositionWithTermVector() == false);
81  1 assertTrue(field.getOmitNorms() == true);
82   
83   
84  1 reader.close();
85    }
86   
87   
 
88  1 toggle public void testLazyFields() throws Exception {
89  1 assertTrue(dir != null);
90  1 assertTrue(fieldInfos != null);
91  1 FieldsReader reader = new FieldsReader(dir, "test", fieldInfos);
92  1 assertTrue(reader != null);
93  1 assertTrue(reader.size() == 1);
94  1 Set loadFieldNames = new HashSet();
95  1 loadFieldNames.add(DocHelper.TEXT_FIELD_1_KEY);
96  1 loadFieldNames.add(DocHelper.TEXT_FIELD_UTF1_KEY);
97  1 Set lazyFieldNames = new HashSet();
98    //new String[]{DocHelper.LARGE_LAZY_FIELD_KEY, DocHelper.LAZY_FIELD_KEY, DocHelper.LAZY_FIELD_BINARY_KEY};
99  1 lazyFieldNames.add(DocHelper.LARGE_LAZY_FIELD_KEY);
100  1 lazyFieldNames.add(DocHelper.LAZY_FIELD_KEY);
101  1 lazyFieldNames.add(DocHelper.LAZY_FIELD_BINARY_KEY);
102  1 lazyFieldNames.add(DocHelper.TEXT_FIELD_UTF2_KEY);
103  1 SetBasedFieldSelector fieldSelector = new SetBasedFieldSelector(loadFieldNames, lazyFieldNames);
104  1 Document doc = reader.doc(0, fieldSelector);
105  1 assertTrue("doc is null and it shouldn't be", doc != null);
106  1 Fieldable field = doc.getFieldable(DocHelper.LAZY_FIELD_KEY);
107  1 assertTrue("field is null and it shouldn't be", field != null);
108  1 assertTrue("field is not lazy and it should be", field.isLazy());
109  1 String value = field.stringValue();
110  1 assertTrue("value is null and it shouldn't be", value != null);
111  1 assertTrue(value + " is not equal to " + DocHelper.LAZY_FIELD_TEXT, value.equals(DocHelper.LAZY_FIELD_TEXT) == true);
112  1 field = doc.getFieldable(DocHelper.TEXT_FIELD_1_KEY);
113  1 assertTrue("field is null and it shouldn't be", field != null);
114  1 assertTrue("Field is lazy and it should not be", field.isLazy() == false);
115  1 field = doc.getFieldable(DocHelper.TEXT_FIELD_UTF1_KEY);
116  1 assertTrue("field is null and it shouldn't be", field != null);
117  1 assertTrue("Field is lazy and it should not be", field.isLazy() == false);
118  1 assertTrue(field.stringValue() + " is not equal to " + DocHelper.FIELD_UTF1_TEXT, field.stringValue().equals(DocHelper.FIELD_UTF1_TEXT) == true);
119   
120  1 field = doc.getFieldable(DocHelper.TEXT_FIELD_UTF2_KEY);
121  1 assertTrue("field is null and it shouldn't be", field != null);
122  1 assertTrue("Field is lazy and it should not be", field.isLazy() == true);
123  1 assertTrue(field.stringValue() + " is not equal to " + DocHelper.FIELD_UTF2_TEXT, field.stringValue().equals(DocHelper.FIELD_UTF2_TEXT) == true);
124   
125  1 field = doc.getFieldable(DocHelper.LAZY_FIELD_BINARY_KEY);
126  1 assertTrue("field is null and it shouldn't be", field != null);
127  1 byte [] bytes = field.binaryValue();
128  1 assertTrue("bytes is null and it shouldn't be", bytes != null);
129  1 assertTrue("", DocHelper.LAZY_FIELD_BINARY_BYTES.length == bytes.length);
130  34 for (int i = 0; i < bytes.length; i++) {
131  33 assertTrue("byte[" + i + "] is mismatched", bytes[i] == DocHelper.LAZY_FIELD_BINARY_BYTES[i]);
132   
133    }
134    }
135   
 
136  1 toggle public void testLoadFirst() throws Exception {
137  1 assertTrue(dir != null);
138  1 assertTrue(fieldInfos != null);
139  1 FieldsReader reader = new FieldsReader(dir, "test", fieldInfos);
140  1 assertTrue(reader != null);
141  1 assertTrue(reader.size() == 1);
142  1 LoadFirstFieldSelector fieldSelector = new LoadFirstFieldSelector();
143  1 Document doc = reader.doc(0, fieldSelector);
144  1 assertTrue("doc is null and it shouldn't be", doc != null);
145  1 int count = 0;
146  1 List l = doc.getFields();
147  2 for (Iterator iter = l.iterator(); iter.hasNext();) {
148  1 Field field = (Field) iter.next();
149  1 assertTrue("field is null and it shouldn't be", field != null);
150  1 String sv = field.stringValue();
151  1 assertTrue("sv is null and it shouldn't be", sv != null);
152  1 count++;
153    }
154  1 assertTrue(count + " does not equal: " + 1, count == 1);
155    }
156   
157    /**
158    * Not really a test per se, but we should have some way of assessing whether this is worthwhile.
159    * <p/>
160    * Must test using a File based directory
161    *
162    * @throws Exception
163    */
 
164  1 toggle