1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.beanutils2.bugs;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20
21 import java.lang.reflect.Method;
22
23 import org.apache.commons.beanutils2.MethodUtils;
24 import org.junit.jupiter.api.Test;
25
26 /**
27 * MethodUtils's getMatchingAccessibleMethod() does not correctly handle inheritance and method overloading.
28 *
29 * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-381">https://issues.apache.org/jira/browse/BEANUTILS-381</a>
30 */
31 public class Jira381Test {
32
33 /**
34 * Test object.
35 */
36 public class TestObject {
37 }
38
39 /**
40 * Used to match performop with test object
41 */
42 public class TestObjectSubclass extends TestObject {
43 }
44
45 /**
46 * Test bean.
47 */
48 public class TestServiceBean {
49
50 /**
51 * Generic object method
52 */
53 public void performOp(final Object o) {
54 }
55
56 /**
57 * Object method
58 */
59 public void performOp(final TestObject o) {
60 }
61 }
62
63 /**
64 * Test with an private class that overrides a public method of a "grand parent" public class.
65 * <p />
66 * See Jira issue# BEANUTILS-381.
67 */
68 @Test
69 public void testIssue_BEANUTILS_381_getMatchingAccessibleMethod() {
70
71 final Class<?> target = TestServiceBean.class;
72 final String methodName = "performOp";
73 final Class<?>[] runtimeClasses = { TestObjectSubclass.class };
74
75 final Method returned = MethodUtils.getMatchingAccessibleMethod(target, methodName, runtimeClasses);
76
77 assertEquals(target, returned.getDeclaringClass());
78 assertEquals(methodName, returned.getName());
79 assertEquals(TestObject.class, returned.getParameterTypes()[0]);
80 }
81 }