Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / jvm / ClassInfoTest.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.jvm;
20
21 import gov.nasa.jpf.util.test.TestJPF;
22 import gov.nasa.jpf.vm.ClassInfo;
23 import gov.nasa.jpf.vm.ClassParseException;
24 import gov.nasa.jpf.vm.FieldInfo;
25 import gov.nasa.jpf.vm.MethodInfo;
26
27
28 import java.io.File;
29
30 import org.junit.Test;
31
32 /**
33  * unit test for ClassInfo initialization
34  */
35 public class ClassInfoTest extends TestJPF {
36
37   @interface X {
38     String value() default "nothing";
39   }
40
41   @interface Y {
42     int[] value();
43   }
44
45   @X
46   public static class MyClass implements Cloneable {
47     public static final int D = 42;
48
49     @X("data") String s;
50
51     public MyClass(String s) {
52       this.s = s;
53       foo();
54     }
55
56     public static int whatIsIt() {
57       int d = D;
58       switch (d) {
59         case 41:
60           d = -1;
61           break;
62         case 42:
63           d = 0;
64           break;
65         case 43:
66           d = 1;
67           break;
68         default:
69           d = 2;
70           break;
71       }
72       return d;
73     }
74
75     public boolean isItTheAnswer (boolean b, @X @Y({1,2,3}) int d, String s){
76       switch (d){
77         case 42: return true;
78         default: return false;
79       }
80     }
81
82     protected void foo() throws IndexOutOfBoundsException {
83       @X int d = D;
84
85       Object[] a = new Object[2];
86       String s = "blah";
87       a[0] = s;
88
89       String x = (String)a[0];
90       Object o = a;
91       if (o instanceof Object[]){
92         o = x;
93       }
94       if (o instanceof String){
95         o = null;
96       }
97
98       Object[][] aa = new Object[2][2];
99
100       try {
101         char c = s.charAt(d);
102       } catch (IndexOutOfBoundsException ioobx) {
103         System.out.println("too big");
104         throw ioobx;
105       }
106     }
107
108     @X
109     String getString() {
110       return s;
111     }
112   }
113
114   @Test
115   public void testClassFileInitialization() {
116     File file = new File("build/tests/gov/nasa/jpf/jvm/ClassInfoTest$MyClass.class");
117
118     try {
119       ClassInfo ci = new NonResolvedClassInfo( "gov.nasa.jpf.jvm.ClassInfoTest$MyClass", file);
120
121       assert ci.getName().equals("gov.nasa.jpf.jvm.ClassInfoTest$MyClass");
122
123       System.out.println("-- declared instance fields");
124       for (FieldInfo fi : ci.getDeclaredInstanceFields()){
125         System.out.print(fi.getType());
126         System.out.print(' ');
127         System.out.println(fi.getName());
128       }
129
130       assert ci.getNumberOfDeclaredInstanceFields() == 1;
131       assert ci.getNumberOfStaticFields() == 1;
132
133       System.out.println();
134       System.out.println("-- methods");
135       for (MethodInfo mi : ci){
136         System.out.println(mi.getUniqueName());
137       }
138
139
140     } catch (ClassParseException cfx){
141       //cfx.printStackTrace();
142       fail("ClassParseException: " + cfx);
143     }
144   }
145
146 }