pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / index / TestNoDeletionPolicy.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.lang.reflect.Constructor;
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Modifier;
23 import java.util.Arrays;
24
25 import org.apache.lucene.analysis.MockAnalyzer;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.document.Field.Index;
28 import org.apache.lucene.document.Field.Store;
29 import org.apache.lucene.store.Directory;
30 import org.apache.lucene.util.LuceneTestCase;
31 import org.junit.Test;
32
33 public class TestNoDeletionPolicy extends LuceneTestCase {
34
35   @Test
36   public void testNoDeletionPolicy() throws Exception {
37     IndexDeletionPolicy idp = NoDeletionPolicy.INSTANCE;
38     idp.onInit(null);
39     idp.onCommit(null);
40   }
41
42   @Test
43   public void testFinalSingleton() throws Exception {
44     assertTrue(Modifier.isFinal(NoDeletionPolicy.class.getModifiers()));
45     Constructor<?>[] ctors = NoDeletionPolicy.class.getDeclaredConstructors();
46     assertEquals("expected 1 private ctor only: " + Arrays.toString(ctors), 1, ctors.length);
47     assertTrue("that 1 should be private: " + ctors[0], Modifier.isPrivate(ctors[0].getModifiers()));
48   }
49
50   @Test
51   public void testMethodsOverridden() throws Exception {
52     // Ensures that all methods of IndexDeletionPolicy are
53     // overridden/implemented. That's important to ensure that NoDeletionPolicy 
54     // overrides everything, so that no unexpected behavior/error occurs.
55     // NOTE: even though IndexDeletionPolicy is an interface today, and so all
56     // methods must be implemented by NoDeletionPolicy, this test is important
57     // in case one day IDP becomes an abstract class.
58     for (Method m : NoDeletionPolicy.class.getMethods()) {
59       // getDeclaredMethods() returns just those methods that are declared on
60       // NoDeletionPolicy. getMethods() returns those that are visible in that
61       // context, including ones from Object. So just filter out Object. If in
62       // the future IndexDeletionPolicy will become a class that extends a
63       // different class than Object, this will need to change.
64       if (m.getDeclaringClass() != Object.class) {
65         assertTrue(m + " is not overridden !", m.getDeclaringClass() == NoDeletionPolicy.class);
66       }
67     }
68   }
69
70   @Test
71   public void testAllCommitsRemain() throws Exception {
72     Directory dir = newDirectory();
73     IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
74         TEST_VERSION_CURRENT, new MockAnalyzer(random))
75         .setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE));
76     for (int i = 0; i < 10; i++) {
77       Document doc = new Document();
78       doc.add(newField("c", "a" + i, Store.YES, Index.ANALYZED));
79       writer.addDocument(doc);
80       writer.commit();
81       assertEquals("wrong number of commits !", i + 1, IndexReader.listCommits(dir).size());
82     }
83     writer.close();
84     dir.close();
85   }
86   
87 }