Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
38   130   15   6.33
12   91   0.39   6
6     2.5  
1    
 
  TestLazyBug       Line # 34 38 15 92.9% 0.9285714
 
  (3)
 
1    package org.apache.lucene.index;
2   
3    /**
4    * Copyright 2006 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.document.*;
23    import org.apache.lucene.store.Directory;
24    import org.apache.lucene.store.RAMDirectory;
25   
26    import java.util.*;
27    import java.lang.reflect.Array;
28   
29   
30    /**
31    * Test demonstrating EOF bug on the last field of the last doc
32    * if other docs have allready been accessed.
33    */
 
34    public class TestLazyBug extends TestCase {
35   
36    public static int BASE_SEED = 13;
37   
38    public static int NUM_DOCS = 500;
39    public static int NUM_FIELDS = 100;
40   
41    private static String[] data = new String[] {
42    "now",
43    "is the time",
44    "for all good men",
45    "to come to the aid",
46    "of their country!",
47    "this string contains big chars:{\u0111 \u0222 \u0333 \u1111 \u2222 \u3333}",
48    "this string is a bigger string, mary had a little lamb, little lamb, little lamb!"
49    };
50   
51    private static Set dataset = new HashSet(Arrays.asList(data));
52   
53    private static String MAGIC_FIELD = "f"+(NUM_FIELDS/3);
54   
55    private static FieldSelector SELECTOR = new FieldSelector() {
 
56  500 toggle public FieldSelectorResult accept(String f) {
57  500 if (f.equals(MAGIC_FIELD)) {
58  5 return FieldSelectorResult.LOAD;
59    }
60  495 return FieldSelectorResult.LAZY_LOAD;
61    }
62    };
63   
 
64  3 toggle private static Directory makeIndex() throws RuntimeException {
65  3 Directory dir = new RAMDirectory();
66  3 try {
67  3 Random r = new Random(BASE_SEED + 42) ;
68  3 Analyzer analyzer = new SimpleAnalyzer();
69  3 IndexWriter writer = new IndexWriter(dir, analyzer, true);
70   
71  3 writer.setUseCompoundFile(false);
72   
73  1503 for (int d = 1; d <= NUM_DOCS; d++) {
74  1500 Document doc = new Document();
75  151500 for (int f = 1; f <= NUM_FIELDS; f++ ) {
76  150000 doc.add(new Field("f"+f,
77    data[f % data.length]
78    + '#' + data[r.nextInt(data.length)],
79    Field.Store.YES,
80    Field.Index.TOKENIZED));
81    }
82  1500 writer.addDocument(doc);
83    }
84  3 writer.close();
85    } catch (Exception e) {
86  0 throw new RuntimeException(e);
87    }
88  3 return dir;
89    }
90   
 
91  3 toggle public static void doTest(int[] docs) throws Exception {
92  3 Directory dir = makeIndex();
93  3 IndexReader reader = IndexReader.open(dir);
94  8 for (int i = 0; i < docs.length; i++) {
95  5 Document d = reader.document(docs[i], SELECTOR);
96  5 String trash = d.get(MAGIC_FIELD);
97   
98  5 List fields = d.getFields();
99  505 for (Iterator fi = fields.iterator(); fi.hasNext(); ) {
100  500 Fieldable f=null;
101  500 try {
102  500 f = (Fieldable) fi.next();
103  500 String fname = f.name();
104  500 String fval = f.stringValue();
105  500 assertNotNull(docs[i]+" FIELD: "+fname, fval);
106  500 String[] vals = fval.split("#");
107  500 if (!dataset.contains(vals[0]) || !dataset.contains(vals[1])) {
108  0 fail("FIELD:"+fname+",VAL:"+fval);
109    }
110    } catch (Exception e) {
111  0 throw new Exception(docs[i]+" WTF: "+f.name(), e);
112    }
113    }
114    }
115  3 reader.close();
116    }
117   
 
118  1 toggle public void testLazyWorks() throws Exception {
119  1 doTest(new int[] { 399 });
120    }
121   
 
122  1 toggle public void testLazyAlsoWorks() throws Exception {
123  1 doTest(new int[] { 399, 150 });
124    }
125   
 
126  1 toggle public void testLazyBroken() throws Exception {
127  1 doTest(new int[] { 150, 399 });
128    }
129   
130    }