1 package org.apache.lucene.util;
4 * Copyright 2009 The Apache Software Foundation
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
31 import java.io.IOException;
33 public class TestFieldCacheSanityChecker extends LuceneTestCase {
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;
42 public void setUp() throws Exception {
44 dirA = newDirectory();
45 dirB = newDirectory();
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)));
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));
72 readerA = IndexReader.open(dirA, true);
73 readerB = IndexReader.open(dirB, true);
74 readerX = new MultiReader(new IndexReader[] { readerA, readerB });
78 public void tearDown() throws Exception {
87 public void testSanity() throws IOException {
88 FieldCache cache = FieldCache.DEFAULT;
89 cache.purgeAllCaches();
91 cache.getDoubles(readerA, "theDouble");
92 cache.getDoubles(readerA, "theDouble", FieldCache.DEFAULT_DOUBLE_PARSER);
93 cache.getDoubles(readerB, "theDouble", FieldCache.DEFAULT_DOUBLE_PARSER);
95 cache.getInts(readerX, "theInt");
96 cache.getInts(readerX, "theInt", FieldCache.DEFAULT_INT_PARSER);
100 Insanity[] insanity =
101 FieldCacheSanityChecker.checkSanity(cache.getCacheEntries());
103 if (0 < insanity.length)
104 dumpArray(getTestLabel() + " INSANITY", insanity, System.err);
106 assertEquals("shouldn't be any cache insanity", 0, insanity.length);
107 cache.purgeAllCaches();
110 public void testInsanity1() throws IOException {
111 FieldCache cache = FieldCache.DEFAULT;
112 cache.purgeAllCaches();
114 cache.getInts(readerX, "theInt", FieldCache.DEFAULT_INT_PARSER);
115 cache.getStrings(readerX, "theInt");
116 cache.getBytes(readerX, "theByte");
120 Insanity[] insanity =
121 FieldCacheSanityChecker.checkSanity(cache.getCacheEntries());
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);
130 // we expect bad things, don't let tearDown complain about them
131 cache.purgeAllCaches();
134 public void testInsanity2() throws IOException {
135 FieldCache cache = FieldCache.DEFAULT;
136 cache.purgeAllCaches();
138 cache.getStrings(readerA, "theString");
139 cache.getStrings(readerB, "theString");
140 cache.getStrings(readerX, "theString");
142 cache.getBytes(readerX, "theByte");
147 Insanity[] insanity =
148 FieldCacheSanityChecker.checkSanity(cache.getCacheEntries());
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);
157 // we expect bad things, don't let tearDown complain about them
158 cache.purgeAllCaches();
161 public void testInsanity3() throws IOException {
163 // :TODO: subreader tree walking is really hairy ... add more crazy tests.