1 package org.apache.lucene.util;
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 public class TestVirtualMethod extends LuceneTestCase {
22 private static final VirtualMethod<TestVirtualMethod> publicTestMethod =
23 new VirtualMethod<TestVirtualMethod>(TestVirtualMethod.class, "publicTest", String.class);
24 private static final VirtualMethod<TestVirtualMethod> protectedTestMethod =
25 new VirtualMethod<TestVirtualMethod>(TestVirtualMethod.class, "protectedTest", int.class);
27 public void publicTest(String test) {}
28 protected void protectedTest(int test) {}
30 static class TestClass1 extends TestVirtualMethod {
32 public void publicTest(String test) {}
34 protected void protectedTest(int test) {}
37 static class TestClass2 extends TestClass1 {
38 @Override // make it public here
39 public void protectedTest(int test) {}
42 static class TestClass3 extends TestClass2 {
44 public void publicTest(String test) {}
47 static class TestClass4 extends TestVirtualMethod {
50 static class TestClass5 extends TestClass4 {
53 public void testGeneral() {
54 assertEquals(0, publicTestMethod.getImplementationDistance(this.getClass()));
55 assertEquals(1, publicTestMethod.getImplementationDistance(TestClass1.class));
56 assertEquals(1, publicTestMethod.getImplementationDistance(TestClass2.class));
57 assertEquals(3, publicTestMethod.getImplementationDistance(TestClass3.class));
58 assertFalse(publicTestMethod.isOverriddenAsOf(TestClass4.class));
59 assertFalse(publicTestMethod.isOverriddenAsOf(TestClass5.class));
61 assertEquals(0, protectedTestMethod.getImplementationDistance(this.getClass()));
62 assertEquals(1, protectedTestMethod.getImplementationDistance(TestClass1.class));
63 assertEquals(2, protectedTestMethod.getImplementationDistance(TestClass2.class));
64 assertEquals(2, protectedTestMethod.getImplementationDistance(TestClass3.class));
65 assertFalse(protectedTestMethod.isOverriddenAsOf(TestClass4.class));
66 assertFalse(protectedTestMethod.isOverriddenAsOf(TestClass5.class));
68 assertTrue(VirtualMethod.compareImplementationDistance(TestClass3.class, publicTestMethod, protectedTestMethod) > 0);
69 assertEquals(0, VirtualMethod.compareImplementationDistance(TestClass5.class, publicTestMethod, protectedTestMethod));
72 @SuppressWarnings("unchecked")
73 public void testExceptions() {
75 // cast to Class to remove generics:
76 publicTestMethod.getImplementationDistance((Class) LuceneTestCase.class);
77 fail("LuceneTestCase is not a subclass and can never override publicTest(String)");
78 } catch (IllegalArgumentException arg) {
83 new VirtualMethod<TestVirtualMethod>(TestVirtualMethod.class, "bogus");
84 fail("Method bogus() does not exist, so IAE should be thrown");
85 } catch (IllegalArgumentException arg) {
90 new VirtualMethod<TestClass2>(TestClass2.class, "publicTest", String.class);
91 fail("Method publicTest(String) is not declared in TestClass2, so IAE should be thrown");
92 } catch (IllegalArgumentException arg) {
97 // try to create a second instance of the same baseClass / method combination
98 new VirtualMethod<TestVirtualMethod>(TestVirtualMethod.class, "publicTest", String.class);
99 fail("Violating singleton status succeeded");
100 } catch (UnsupportedOperationException arg) {