Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / vm / reflection / ReflectionTest.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 package gov.nasa.jpf.test.vm.reflection;
19
20 import gov.nasa.jpf.util.test.TestJPF;
21
22 import org.junit.Test;
23
24 public class ReflectionTest extends TestJPF {
25
26   static class MyClass {
27     void bar(){
28       foo();
29     }
30
31     // compilation will cause a warning about internal proprietary API that cannot be suppressed, but we have to test this
32     // since it is still used by standard libs
33     void foo (){
34       Class<?> callerCls = sun.reflect.Reflection.getCallerClass(0); // that would be getCallerClass()
35       System.out.println("-- getCallerClass(0) = " + callerCls);
36       assertTrue(callerCls.getName().equals("sun.reflect.Reflection"));
37       
38       callerCls = sun.reflect.Reflection.getCallerClass(1); // foo()
39       System.out.println("-- getCallerClass(1) = " + callerCls);
40       assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest$MyClass"));
41       
42       callerCls = sun.reflect.Reflection.getCallerClass(2); // bar()
43       System.out.println("-- getCallerClass(2) = " + callerCls);
44       assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest$MyClass"));
45
46       callerCls = sun.reflect.Reflection.getCallerClass(3); // callIt()
47       System.out.println("-- getCallerClass(3) = " + callerCls);
48       assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest"));
49       
50       // <2do> should also test Method.invoke skipping
51     }
52   }
53   
54   void callIt(){
55     MyClass o = new MyClass();
56     o.bar();
57   }
58   
59   @Test
60   public void testCallerClass() {
61     if (verifyNoPropertyViolation()){
62       callIt();
63     }
64   }
65 }