pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / util / TestVirtualMethod.java
1 package org.apache.lucene.util;
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 public class TestVirtualMethod extends LuceneTestCase {
21
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);
26
27   public void publicTest(String test) {}
28   protected void protectedTest(int test) {}
29   
30   static class TestClass1 extends TestVirtualMethod {
31     @Override
32     public void publicTest(String test) {}
33     @Override
34     protected void protectedTest(int test) {}
35   }
36
37   static class TestClass2 extends TestClass1 {
38     @Override // make it public here
39     public void protectedTest(int test) {}
40   }
41
42   static class TestClass3 extends TestClass2 {
43     @Override
44     public void publicTest(String test) {}
45   }
46
47   static class TestClass4 extends TestVirtualMethod {
48   }
49
50   static class TestClass5 extends TestClass4 {
51   }
52
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));
60     
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));
67     
68     assertTrue(VirtualMethod.compareImplementationDistance(TestClass3.class, publicTestMethod, protectedTestMethod) > 0);
69     assertEquals(0, VirtualMethod.compareImplementationDistance(TestClass5.class, publicTestMethod, protectedTestMethod));
70   }
71
72   @SuppressWarnings("unchecked")
73   public void testExceptions() {
74     try {
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) {
79       // pass
80     }
81     
82     try {
83       new VirtualMethod<TestVirtualMethod>(TestVirtualMethod.class, "bogus");
84       fail("Method bogus() does not exist, so IAE should be thrown");
85     } catch (IllegalArgumentException arg) {
86       // pass
87     }
88     
89     try {
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) {
93       // pass
94     }
95
96     try {
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) {
101       // pass
102     }
103   }
104   
105 }