Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / java / lang / reflect / MethodTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.test.java.lang.reflect;
20
21 import gov.nasa.jpf.util.test.TestJPF;
22 import java.lang.reflect.Method;
23 import org.junit.Test;
24
25 public class MethodTest extends TestJPF {
26
27   public static void main (String[] args){
28     runTestsOfThisClass(args);
29   }
30
31   @Test
32   public void equalsTest () throws SecurityException, NoSuchMethodException{
33     if (verifyNoPropertyViolation()){
34       Method m1 = MethodTest.class.getMethod("equalsTest", new Class[0]);
35       Method m2 = MethodTest.class.getMethod("equalsTest", new Class[0]);
36       assertTrue(m1.equals(m2));
37       assertFalse(m1 == m2);
38     }
39   }
40
41   public void testIsVarArg1s (Class<?>... argTypes){
42   }
43
44   public void testIsVarArgs2 (Class<?>[] argTypes){
45   }
46
47   @Test
48   public void isVarArgsTest () throws SecurityException, NoSuchMethodException{
49     if (verifyNoPropertyViolation()){
50       for (Method m : MethodTest.class.getDeclaredMethods()){
51         if (m.getName().equals("testIsVarArg1s"))
52           assertTrue(m.isVarArgs());
53         else if (m.getName().equals("testIsVarArg1s")) {
54           assertFalse(m.isVarArgs());
55         }
56       }
57     }
58   }
59
60   @Test
61   public void hashCodeTest () throws SecurityException, NoSuchMethodException{
62     if (verifyNoPropertyViolation()){
63       Method m1 = MethodTest.class.getMethod("hashCodeTest", new Class[0]);
64       Method m2 = MethodTest.class.getMethod("hashCodeTest", new Class[0]);
65       Method m3 = MethodTest.class.getMethod("equalsTest", new Class[0]);
66       assertTrue(m1.equals(m2));
67       assertTrue(m1.hashCode() == m2.hashCode());
68       assertFalse(m1.hashCode() == m3.hashCode());
69     }
70   }
71
72   public static class A {
73     public A foo (int a){
74       return new A();
75     }
76   }
77
78   public static class B extends A {
79     @Override
80         public B foo (int x){
81       return new B();
82     }
83   }
84
85   @Test
86   public void isBridgeTest (){
87     if (verifyNoPropertyViolation()){
88       assertFalse(B.class.getDeclaredMethods()[0].isBridge());
89       assertTrue(B.class.getDeclaredMethods()[1].isBridge());
90     }
91   }
92   
93   //--- aux
94   
95   void recordSeen (boolean[] seen, String[] expected, Method m){
96     String mname = m.toString();
97     for (int i=0; i<expected.length; i++){
98       if (expected[i].equals(mname)){
99         seen[i] = true;
100       }
101     }
102   }
103   
104   boolean checkSeen(boolean[] seen, String[] expected){
105     for (int i=0; i<expected.length; i++){
106       if (!seen[i]){
107         System.out.println("NOT seen: " + expected[i]);
108         return false;
109       }
110     }
111     return true;
112   }
113   
114   //------------ getMethods() on interfaces
115   interface I1 {
116     void i1();
117   }
118   
119   interface I2 extends I1 {
120     void i2();
121   }
122   
123   @Test
124   public void testGetMethodsOnIfc(){
125     if (verifyNoPropertyViolation()){
126       String[] expected = {
127         "public abstract void gov.nasa.jpf.test.java.lang.reflect.MethodTest$I2.i2()",
128         "public abstract void gov.nasa.jpf.test.java.lang.reflect.MethodTest$I1.i1()"
129       };
130       boolean[] seen = new boolean[expected.length];
131       
132       Method[] methods = I2.class.getMethods();
133       
134       for (Method m : methods){
135         System.out.println(m);
136         recordSeen(seen, expected, m);
137       }
138       assertTrue(methods.length == expected.length);
139       //assertTrue(checkSeen(seen, expected));
140     }
141   }
142   
143   
144   //------------ getMethods() on classes
145   public static class C extends B {
146     static {
147       System.out.println("C.<clinit>");
148     }
149     
150     // non-public method
151     void nope(){
152     }
153     
154     // ctor
155     C (){
156       System.out.println("C.<init>");      
157     }
158   } 
159   
160   @Test
161   public void testGetMethodsOnClass(){
162     if (verifyNoPropertyViolation()){
163       String[] expected = {
164         "public native int java.lang.Object.hashCode()",
165         "public final native void java.lang.Object.notify()",
166         "public final native void java.lang.Object.notifyAll()",
167         "public java.lang.String java.lang.Object.toString()",
168         "public final native java.lang.Class java.lang.Object.getClass()",
169         "public final native void java.lang.Object.wait(long)",
170         "public final void java.lang.Object.wait(long,int)",
171         "public gov.nasa.jpf.test.java.lang.reflect.MethodTest$B gov.nasa.jpf.test.java.lang.reflect.MethodTest$B.foo(int)",
172         "public boolean java.lang.Object.equals(java.lang.Object)",
173         "public volatile gov.nasa.jpf.test.java.lang.reflect.MethodTest$A gov.nasa.jpf.test.java.lang.reflect.MethodTest$B.foo(int)",
174         "public final void java.lang.Object.wait()"
175       };
176       boolean[] seen = new boolean[expected.length];
177       
178       Method[] methods = C.class.getMethods();
179       for (Method m : methods){
180         System.out.println(m);
181       }
182     }
183   }
184 }