Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / vm / basic / RecursiveClinitTest.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.vm.basic;
20
21 import gov.nasa.jpf.util.test.TestJPF;
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.Method;
24 import org.junit.Test;
25
26 /**
27  * test automatic and recursive clinit invocation
28  */
29 public class RecursiveClinitTest extends TestJPF {
30
31   static class Base {
32     static int d = 1;
33     static {
34       System.out.println("Base clinit");
35     }
36   }
37
38   static class Derived extends Base {
39     static int d = Base.d * 42;
40     static {
41       System.out.println("Derived clinit");
42     }
43     
44     public Derived (int i){
45       System.out.println("Derived(" + i + ')');
46     }
47     
48     public static void foo(){
49       System.out.println("Derived.foo()");
50     }
51   }
52
53   @Test 
54   public void testStaticField (){
55     if (verifyNoPropertyViolation()) {
56       System.out.println("main now referencing Derived.d");
57       int d = Derived.d;
58       System.out.println("back in main");
59       
60       assertTrue(d == 42);
61     }
62   }
63   
64   @Test
65   public void testNewInstance (){
66     if (verifyNoPropertyViolation()) {
67       System.out.println("main now calling Derived.class.newInstance()");
68       try {
69         Derived.class.newInstance();
70       } catch (Throwable t) {
71         fail("instantiation failed with " + t);
72       }
73       System.out.println("back in main");
74       
75       assertTrue(Derived.d == 42);
76     }
77   }
78   
79   @Test
80   public void testMethodReflection (){
81     if (verifyNoPropertyViolation()) {
82       try {
83         Class<?> clazz = Class.forName("gov.nasa.jpf.test.vm.basic.RecursiveClinitTest$Derived");
84         System.out.println("main now calling Derived.foo()");
85         Method m = clazz.getDeclaredMethod("foo", new Class[0]);
86         m.invoke(null);
87         
88         System.out.println("back in main");
89         assertTrue(Derived.d == 42);
90         
91       } catch (Throwable t){
92         fail("test failed with: " + t);
93       }
94     }    
95   }
96   
97   @Test
98   public void testCtorReflection (){
99     if (verifyNoPropertyViolation()) {
100       try {
101         Class<?> clazz = Class.forName("gov.nasa.jpf.test.vm.basic.RecursiveClinitTest$Derived");
102         System.out.println("main now creating Derived(-42)");
103         Constructor ctor = clazz.getConstructor(new Class[] {int.class});
104         Object o = ctor.newInstance( new Object[] {Integer.valueOf(-42)});
105         
106         System.out.println("back in main");
107         assertTrue( o instanceof Derived);
108         assertTrue(Derived.d == 42);
109         
110       } catch (Throwable t){
111         fail("test failed with: " + t);
112       }
113     }    
114   }
115   
116   // <2do> we also need the SerializatinConstructor
117 }