pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / util / TestFieldCacheSanityChecker.java
1 package org.apache.lucene.util;
2
3 /**
4  * Copyright 2009 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 org.apache.lucene.analysis.MockAnalyzer;
20 import org.apache.lucene.document.Document;
21 import org.apache.lucene.document.Field;
22 import org.apache.lucene.search.FieldCache;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.MultiReader;
25 import org.apache.lucene.index.IndexWriter;
26 import org.apache.lucene.store.Directory;
27 import org.apache.lucene.util.LuceneTestCase;
28 import org.apache.lucene.util.FieldCacheSanityChecker.Insanity;
29 import org.apache.lucene.util.FieldCacheSanityChecker.InsanityType;
30
31 import java.io.IOException;
32
33 public class TestFieldCacheSanityChecker extends LuceneTestCase {
34
35   protected IndexReader readerA;
36   protected IndexReader readerB;
37   protected IndexReader readerX;
38   protected Directory dirA, dirB;
39   private static final int NUM_DOCS = 1000;
40
41   @Override
42   public void setUp() throws Exception {
43     super.setUp();
44     dirA = newDirectory();
45     dirB = newDirectory();
46
47     IndexWriter wA = new IndexWriter(dirA, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
48     IndexWriter wB = new IndexWriter(dirB, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
49
50     long theLong = Long.MAX_VALUE;
51     double theDouble = Double.MAX_VALUE;
52     byte theByte = Byte.MAX_VALUE;
53     short theShort = Short.MAX_VALUE;
54     int theInt = Integer.MAX_VALUE;
55     float theFloat = Float.MAX_VALUE;
56     for (int i = 0; i < NUM_DOCS; i++){
57       Document doc = new Document();
58       doc.add(newField("theLong", String.valueOf(theLong--), Field.Store.NO, Field.Index.NOT_ANALYZED));
59       doc.add(newField("theDouble", String.valueOf(theDouble--), Field.Store.NO, Field.Index.NOT_ANALYZED));
60       doc.add(newField("theByte", String.valueOf(theByte--), Field.Store.NO, Field.Index.NOT_ANALYZED));
61       doc.add(newField("theShort", String.valueOf(theShort--), Field.Store.NO, Field.Index.NOT_ANALYZED));
62       doc.add(newField("theInt", String.valueOf(theInt--), Field.Store.NO, Field.Index.NOT_ANALYZED));
63       doc.add(newField("theFloat", String.valueOf(theFloat--), Field.Store.NO, Field.Index.NOT_ANALYZED));
64       if (0 == i % 3) {
65         wA.addDocument(doc);
66       } else {
67         wB.addDocument(doc);
68       }
69     }
70     wA.close();
71     wB.close();
72     readerA = IndexReader.open(dirA, true);
73     readerB = IndexReader.open(dirB, true);
74     readerX = new MultiReader(new IndexReader[] { readerA, readerB });
75   }
76
77   @Override
78   public void tearDown() throws Exception {
79     readerA.close();
80     readerB.close();
81     readerX.close();
82     dirA.close();
83     dirB.close();
84     super.tearDown();
85   }
86
87   public void testSanity() throws IOException {
88     FieldCache cache = FieldCache.DEFAULT;
89     cache.purgeAllCaches();
90
91     cache.getDoubles(readerA, "theDouble");
92     cache.getDoubles(readerA, "theDouble", FieldCache.DEFAULT_DOUBLE_PARSER);
93     cache.getDoubles(readerB, "theDouble", FieldCache.DEFAULT_DOUBLE_PARSER);
94
95     cache.getInts(readerX, "theInt");
96     cache.getInts(readerX, "theInt", FieldCache.DEFAULT_INT_PARSER);
97
98     // // // 
99
100     Insanity[] insanity = 
101       FieldCacheSanityChecker.checkSanity(cache.getCacheEntries());
102     
103     if (0 < insanity.length)
104       dumpArray(getTestLabel() + " INSANITY", insanity, System.err);
105
106     assertEquals("shouldn't be any cache insanity", 0, insanity.length);
107     cache.purgeAllCaches();
108   }
109
110   public void testInsanity1() throws IOException {
111     FieldCache cache = FieldCache.DEFAULT;
112     cache.purgeAllCaches();
113
114     cache.getInts(readerX, "theInt", FieldCache.DEFAULT_INT_PARSER);
115     cache.getStrings(readerX, "theInt");
116     cache.getBytes(readerX, "theByte");
117
118     // // // 
119
120     Insanity[] insanity = 
121       FieldCacheSanityChecker.checkSanity(cache.getCacheEntries());
122
123     assertEquals("wrong number of cache errors", 1, insanity.length);
124     assertEquals("wrong type of cache error", 
125                  InsanityType.VALUEMISMATCH,
126                  insanity[0].getType());
127     assertEquals("wrong number of entries in cache error", 2,
128                  insanity[0].getCacheEntries().length);
129
130     // we expect bad things, don't let tearDown complain about them
131     cache.purgeAllCaches();
132   }
133
134   public void testInsanity2() throws IOException {
135     FieldCache cache = FieldCache.DEFAULT;
136     cache.purgeAllCaches();
137
138     cache.getStrings(readerA, "theString");
139     cache.getStrings(readerB, "theString");
140     cache.getStrings(readerX, "theString");
141
142     cache.getBytes(readerX, "theByte");
143
144
145     // // // 
146
147     Insanity[] insanity = 
148       FieldCacheSanityChecker.checkSanity(cache.getCacheEntries());
149     
150     assertEquals("wrong number of cache errors", 1, insanity.length);
151     assertEquals("wrong type of cache error", 
152                  InsanityType.SUBREADER,
153                  insanity[0].getType());
154     assertEquals("wrong number of entries in cache error", 3,
155                  insanity[0].getCacheEntries().length);
156
157     // we expect bad things, don't let tearDown complain about them
158     cache.purgeAllCaches();
159   }
160   
161   public void testInsanity3() throws IOException {
162
163     // :TODO: subreader tree walking is really hairy ... add more crazy tests.
164   }
165
166 }