1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.util.Arrays;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
26 import org.apache.lucene.analysis.MockAnalyzer;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29 import org.apache.lucene.document.FieldSelector;
30 import org.apache.lucene.document.FieldSelectorResult;
31 import org.apache.lucene.document.Fieldable;
32 import org.apache.lucene.store.Directory;
33 import org.apache.lucene.util.LuceneTestCase;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
39 * Test demonstrating EOF bug on the last field of the last doc
40 * if other docs have already been accessed.
42 public class TestLazyBug extends LuceneTestCase {
44 public static int NUM_DOCS = TEST_NIGHTLY ? 500 : 50;
45 public static int NUM_FIELDS = TEST_NIGHTLY ? 100 : 10;
47 private static String[] data = new String[] {
53 "this string contains big chars:{\u0111 \u0222 \u0333 \u1111 \u2222 \u3333}",
54 "this string is a bigger string, mary had a little lamb, little lamb, little lamb!"
57 private static Set<String> dataset = new HashSet<String>(Arrays.asList(data));
59 private static String MAGIC_FIELD = "f"+(NUM_FIELDS/3);
61 private static Directory directory;
64 public static void beforeClass() throws Exception {
65 directory = makeIndex();
69 public static void afterClass() throws Exception {
74 private static FieldSelector SELECTOR = new FieldSelector() {
75 public FieldSelectorResult accept(String f) {
76 if (f.equals(MAGIC_FIELD)) {
77 return FieldSelectorResult.LOAD;
79 return FieldSelectorResult.LAZY_LOAD;
83 private static Directory makeIndex() throws Exception {
84 Directory dir = newDirectory();
86 IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
87 TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
88 LogMergePolicy lmp = (LogMergePolicy) writer.getConfig().getMergePolicy();
89 lmp.setUseCompoundFile(false);
91 for (int d = 1; d <= NUM_DOCS; d++) {
92 Document doc = new Document();
93 for (int f = 1; f <= NUM_FIELDS; f++ ) {
94 doc.add(newField("f"+f,
96 + '#' + data[random.nextInt(data.length)],
98 Field.Index.ANALYZED));
100 writer.addDocument(doc);
103 } catch (Exception e) {
104 throw new RuntimeException(e);
109 public void doTest(int[] docs) throws Exception {
110 IndexReader reader = IndexReader.open(directory, true);
111 for (int i = 0; i < docs.length; i++) {
112 Document d = reader.document(docs[i], SELECTOR);
115 List<Fieldable> fields = d.getFields();
116 for (Iterator<Fieldable> fi = fields.iterator(); fi.hasNext(); ) {
120 String fname = f.name();
121 String fval = f.stringValue();
122 assertNotNull(docs[i]+" FIELD: "+fname, fval);
123 String[] vals = fval.split("#");
124 if (!dataset.contains(vals[0]) || !dataset.contains(vals[1])) {
125 fail("FIELD:"+fname+",VAL:"+fval);
127 } catch (Exception e) {
128 throw new Exception(docs[i]+" WTF: "+f.name(), e);
135 public void testLazyWorks() throws Exception {
136 doTest(new int[] { NUM_DOCS-1 });
139 public void testLazyAlsoWorks() throws Exception {
140 doTest(new int[] { NUM_DOCS-1, NUM_DOCS/2 });
143 public void testLazyBroken() throws Exception {
144 doTest(new int[] { NUM_DOCS/2, NUM_DOCS-1 });