1 /*****************************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one                *
3  * or more contributor license agreements.  See the NOTICE file              *
4  * distributed with this work for additional information                     *
5  * regarding copyright ownership.  The ASF licenses this file                *
6  * to you under the Apache License, Version 2.0 (the                         *
7  * "License"); you may not use this file except in compliance                *
8  * with the License.  You may obtain a copy of the License at                *
9  *                                                                           *
10  *     http://www.apache.org/licenses/LICENSE-2.0                            *
11  *                                                                           *
12  * Unless required by applicable law or agreed to in writing,                *
13  * software distributed under the License is distributed on an               *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY                    *
15  * KIND, either express or implied.  See the License for the                 *
16  * specific language governing permissions and limitations                   *
17  * under the License.                                                        *
18  *                                                                           *
19  * This file is part of the BeanShell Java Scripting distribution.           *
20  * Documentation and updates may be found at http://www.beanshell.org/       *
21  * Patrick Niemeyer (pat@pat.net)                                            *
22  * Author of Learning Java, O'Reilly & Associates                            *
23  *                                                                           *
24  *****************************************************************************/
25 
26 package bsh;
27 
28 import junit.framework.Assert;
29 
30 import org.junit.Test;
31 
32 public class BshMethodTest {
33 
34    /**
35     * Verifies that subclasses are not considered equal to superclass interfaces
36     * with a (potential) subset of the subclasses fields.
37     */
38    @SuppressWarnings("serial")
39    @Test
testEqualsObject_subclassEquality()40    public void testEqualsObject_subclassEquality() {
41       // define a simple subclass of BshMethod:
42       class SubMethod extends BshMethod {
43          public SubMethod(String name, Class returnType, String[] paramNames,
44                Class[] paramTypes, BSHBlock methodBody,
45                NameSpace declaringNameSpace, Modifiers modifiers) {
46             super(name, returnType, paramNames, paramTypes, methodBody,
47                   declaringNameSpace, modifiers);
48          }
49       }
50 
51       final String name = "testMethod";
52         final BshMethod subInst = new SubMethod(name, Integer.class, new String[]{}, new Class[]{}, null, null, null);
53         final BshMethod supInst = new BshMethod(name, Integer.class, new String[]{}, new Class[]{}, null, null, null);
54 
55         Assert.assertFalse("Subclasses should not be equal to super classes", supInst.equals(subInst));
56    }
57 
58    /**
59     * Very simple test to verify hashcode contract.
60     */
61    @Test
testHashCode_contract()62    public void testHashCode_contract() {
63       final String name = "testMethod";
64         final BshMethod method1 = new BshMethod(name, Integer.class, new String[]{}, new Class[]{}, null, null, null);
65         final BshMethod method2 = new BshMethod(name, Integer.class, new String[]{}, new Class[]{}, null, null, null);
66 
67         Assert.assertTrue("precondition check for test failed.", method2.equals(method1));
68         Assert.assertEquals("Equal classes should have equal hashcodes", method2.hashCode(), method1.hashCode());
69    }
70 }
71