| 1 |
|
package org.apache.lucene.index; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 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 |
|
|
| 32 |
|
|
| 33 |
|
|
|
|
|
| 92.9% |
Uncovered Elements: 4 (56) |
Complexity: 15 |
Complexity Density: 0.39 |
|
| 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() { |
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 56 |
500
|
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 |
|
|
|
|
|
| 94.4% |
Uncovered Elements: 1 (18) |
Complexity: 4 |
Complexity Density: 0.29 |
|
| 64 |
3
|
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 |
|
|
|
|
|
| 87.5% |
Uncovered Elements: 3 (24) |
Complexity: 6 |
Complexity Density: 0.33 |
|
| 91 |
3
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
| 118 |
1
|
public void testLazyWorks() throws Exception {... |
| 119 |
1
|
doTest(new int[] { 399 }); |
| 120 |
|
} |
| 121 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
| 122 |
1
|
public void testLazyAlsoWorks() throws Exception {... |
| 123 |
1
|
doTest(new int[] { 399, 150 }); |
| 124 |
|
} |
| 125 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
| 126 |
1
|
public void testLazyBroken() throws Exception {... |
| 127 |
1
|
doTest(new int[] { 150, 399 }); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
} |