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.io.IOException;
21 import java.io.ByteArrayOutputStream;
22 import java.io.PrintStream;
23 import java.util.List;
24 import java.util.ArrayList;
26 import org.apache.lucene.util.LuceneTestCase;
27 import org.apache.lucene.store.Directory;
28 import org.apache.lucene.analysis.MockAnalyzer;
29 import org.apache.lucene.analysis.WhitespaceAnalyzer;
30 import org.apache.lucene.document.Document;
31 import org.apache.lucene.document.Field;
32 import org.apache.lucene.util.Constants;
34 public class TestCheckIndex extends LuceneTestCase {
36 public void testDeletedDocs() throws IOException {
37 Directory dir = newDirectory();
38 IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(2));
39 Document doc = new Document();
40 doc.add(newField("field", "aaa", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
41 for(int i=0;i<19;i++) {
42 writer.addDocument(doc);
46 IndexReader reader = IndexReader.open(dir, false);
47 reader.deleteDocument(5);
50 ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
51 CheckIndex checker = new CheckIndex(dir);
52 checker.setInfoStream(new PrintStream(bos));
53 if (VERBOSE) checker.setInfoStream(System.out);
54 CheckIndex.Status indexStatus = checker.checkIndex();
55 if (indexStatus.clean == false) {
56 System.out.println("CheckIndex failed");
57 System.out.println(bos.toString());
61 final CheckIndex.Status.SegmentInfoStatus seg = indexStatus.segmentInfos.get(0);
62 assertTrue(seg.openReaderPassed);
64 assertNotNull(seg.diagnostics);
66 assertNotNull(seg.fieldNormStatus);
67 assertNull(seg.fieldNormStatus.error);
68 assertEquals(1, seg.fieldNormStatus.totFields);
70 assertNotNull(seg.termIndexStatus);
71 assertNull(seg.termIndexStatus.error);
72 assertEquals(1, seg.termIndexStatus.termCount);
73 assertEquals(19, seg.termIndexStatus.totFreq);
74 assertEquals(18, seg.termIndexStatus.totPos);
76 assertNotNull(seg.storedFieldStatus);
77 assertNull(seg.storedFieldStatus.error);
78 assertEquals(18, seg.storedFieldStatus.docCount);
79 assertEquals(18, seg.storedFieldStatus.totFields);
81 assertNotNull(seg.termVectorStatus);
82 assertNull(seg.termVectorStatus.error);
83 assertEquals(18, seg.termVectorStatus.docCount);
84 assertEquals(18, seg.termVectorStatus.totVectors);
86 assertTrue(seg.diagnostics.size() > 0);
87 final List<String> onlySegments = new ArrayList<String>();
88 onlySegments.add("_0");
90 assertTrue(checker.checkIndex(onlySegments).clean == true);
94 public void testLuceneConstantVersion() throws IOException {
95 // common-build.xml sets lucene.version
96 final String version = System.getProperty("lucene.version");
97 assertNotNull( "null version", version);
98 assertTrue("Invalid version: "+version,
99 version.equals(Constants.LUCENE_MAIN_VERSION+"-SNAPSHOT") ||
100 version.equals(Constants.LUCENE_MAIN_VERSION));
101 assertTrue(version + " should start with: "+Constants.LUCENE_VERSION,
102 Constants.LUCENE_VERSION.startsWith(version));