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.lang.reflect.Constructor;
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Modifier;
23 import java.util.Arrays;
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;
33 public class TestNoDeletionPolicy extends LuceneTestCase {
36 public void testNoDeletionPolicy() throws Exception {
37 IndexDeletionPolicy idp = NoDeletionPolicy.INSTANCE;
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()));
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);
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);
81 assertEquals("wrong number of commits !", i + 1, IndexReader.listCommits(dir).size());