001: package org.apache.lucene.index;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.util.LuceneTestCase;
021: import org.apache.lucene.analysis.Analyzer;
022: import org.apache.lucene.analysis.SimpleAnalyzer;
023: import org.apache.lucene.document.*;
024: import org.apache.lucene.store.Directory;
025: import org.apache.lucene.store.RAMDirectory;
026:
027: import java.util.*;
028: import java.lang.reflect.Array;
029:
030: /**
031: * Test demonstrating EOF bug on the last field of the last doc
032: * if other docs have allready been accessed.
033: */
034: public class TestLazyBug extends LuceneTestCase {
035:
036: public static int BASE_SEED = 13;
037:
038: public static int NUM_DOCS = 500;
039: public static int NUM_FIELDS = 100;
040:
041: private static String[] data = new String[] {
042: "now",
043: "is the time",
044: "for all good men",
045: "to come to the aid",
046: "of their country!",
047: "this string contains big chars:{\u0111 \u0222 \u0333 \u1111 \u2222 \u3333}",
048: "this string is a bigger string, mary had a little lamb, little lamb, little lamb!" };
049:
050: private static Set dataset = new HashSet(Arrays.asList(data));
051:
052: private static String MAGIC_FIELD = "f" + (NUM_FIELDS / 3);
053:
054: private static FieldSelector SELECTOR = new FieldSelector() {
055: public FieldSelectorResult accept(String f) {
056: if (f.equals(MAGIC_FIELD)) {
057: return FieldSelectorResult.LOAD;
058: }
059: return FieldSelectorResult.LAZY_LOAD;
060: }
061: };
062:
063: private static Directory makeIndex() throws RuntimeException {
064: Directory dir = new RAMDirectory();
065: try {
066: Random r = new Random(BASE_SEED + 42);
067: Analyzer analyzer = new SimpleAnalyzer();
068: IndexWriter writer = new IndexWriter(dir, analyzer, true);
069:
070: writer.setUseCompoundFile(false);
071:
072: for (int d = 1; d <= NUM_DOCS; d++) {
073: Document doc = new Document();
074: for (int f = 1; f <= NUM_FIELDS; f++) {
075: doc.add(new Field("f" + f, data[f % data.length]
076: + '#' + data[r.nextInt(data.length)],
077: Field.Store.YES, Field.Index.TOKENIZED));
078: }
079: writer.addDocument(doc);
080: }
081: writer.close();
082: } catch (Exception e) {
083: throw new RuntimeException(e);
084: }
085: return dir;
086: }
087:
088: public static void doTest(int[] docs) throws Exception {
089: Directory dir = makeIndex();
090: IndexReader reader = IndexReader.open(dir);
091: for (int i = 0; i < docs.length; i++) {
092: Document d = reader.document(docs[i], SELECTOR);
093: String trash = d.get(MAGIC_FIELD);
094:
095: List fields = d.getFields();
096: for (Iterator fi = fields.iterator(); fi.hasNext();) {
097: Fieldable f = null;
098: try {
099: f = (Fieldable) fi.next();
100: String fname = f.name();
101: String fval = f.stringValue();
102: assertNotNull(docs[i] + " FIELD: " + fname, fval);
103: String[] vals = fval.split("#");
104: if (!dataset.contains(vals[0])
105: || !dataset.contains(vals[1])) {
106: fail("FIELD:" + fname + ",VAL:" + fval);
107: }
108: } catch (Exception e) {
109: throw new Exception(docs[i] + " WTF: " + f.name(),
110: e);
111: }
112: }
113: }
114: reader.close();
115: }
116:
117: public void testLazyWorks() throws Exception {
118: doTest(new int[] { 399 });
119: }
120:
121: public void testLazyAlsoWorks() throws Exception {
122: doTest(new int[] { 399, 150 });
123: }
124:
125: public void testLazyBroken() throws Exception {
126: doTest(new int[] { 150, 399 });
127: }
128:
129: }
|