add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / index / TestLazyBug.java
1 package org.apache.lucene.index;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.util.Arrays;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Set;
25
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;
36
37
38 /**
39  * Test demonstrating EOF bug on the last field of the last doc 
40  * if other docs have allready been accessed.
41  */
42 public class TestLazyBug extends LuceneTestCase {
43
44   public static int NUM_DOCS = TEST_NIGHTLY ? 500 : 50;
45   public static int NUM_FIELDS = TEST_NIGHTLY ? 100 : 10;
46
47   private static String[] data = new String[] {
48     "now",
49     "is the time",
50     "for all good men",
51     "to come to the aid",
52     "of their country!",
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!"
55   };
56
57   private static Set<String> dataset = new HashSet<String>(Arrays.asList(data));
58   
59   private static String MAGIC_FIELD = "f"+(NUM_FIELDS/3);
60   
61   private static Directory directory;
62   
63   @BeforeClass
64   public static void beforeClass() throws Exception {
65     directory = makeIndex();
66   }
67   
68   @AfterClass
69   public static void afterClass() throws Exception {
70     directory.close();
71     directory = null;
72   }
73
74   private static FieldSelector SELECTOR = new FieldSelector() {
75       public FieldSelectorResult accept(String f) {
76         if (f.equals(MAGIC_FIELD)) {
77           return FieldSelectorResult.LOAD;
78         }
79         return FieldSelectorResult.LAZY_LOAD;
80       }
81     };
82
83   private static Directory makeIndex() throws Exception {
84     Directory dir = newDirectory();
85     try {
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);
90
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,
95                             data[f % data.length]
96                             + '#' + data[random.nextInt(data.length)],
97                             Field.Store.NO,
98                             Field.Index.ANALYZED));
99         }
100         writer.addDocument(doc);
101       }
102       writer.close();
103     } catch (Exception e) {
104       throw new RuntimeException(e);
105     }
106     return dir;
107   }
108   
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);
113       d.get(MAGIC_FIELD);
114       
115       List<Fieldable> fields = d.getFields();
116       for (Iterator<Fieldable> fi = fields.iterator(); fi.hasNext(); ) {
117         Fieldable f=null;
118         try {
119           f =  fi.next();
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);
126           }
127         } catch (Exception e) {
128           throw new Exception(docs[i]+" WTF: "+f.name(), e);
129         }
130       }
131     }
132     reader.close();
133   }
134
135   public void testLazyWorks() throws Exception {
136     doTest(new int[] { NUM_DOCS-1 });
137   }
138   
139   public void testLazyAlsoWorks() throws Exception {
140     doTest(new int[] { NUM_DOCS-1, NUM_DOCS/2 });
141   }
142
143   public void testLazyBroken() throws Exception {
144     doTest(new int[] { NUM_DOCS/2, NUM_DOCS-1 });
145   }
146
147 }