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