Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / vm / basic / JPFAttrAnnotationTest.java
1 /*
2  * Copyright (C) 2015, 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.basic;
19
20 import gov.nasa.jpf.ListenerAdapter;
21 import gov.nasa.jpf.annotation.JPFAttribute;
22 import gov.nasa.jpf.util.test.TestJPF;
23 import gov.nasa.jpf.vm.ClassInfo;
24 import gov.nasa.jpf.vm.FieldInfo;
25 import gov.nasa.jpf.vm.MethodInfo;
26 import gov.nasa.jpf.vm.VM;
27 import org.junit.Test;
28
29 /**
30  * regression test for JPFAttribute annotations
31  */
32 public class JPFAttrAnnotationTest extends TestJPF {
33
34   static final String LISTENER = "gov.nasa.jpf.test.vm.basic.JPFAttrAnnotationTest$LoadListener";
35   static final String ATTR_CLS = "gov.nasa.jpf.test.vm.basic.JPFAttrAnnotationTest$MyAttr"; // needs to const
36   static final String TGT_CLS = "gov.nasa.jpf.test.vm.basic.JPFAttrAnnotationTest$SomeClass";
37   
38   public static class MyAttr {}
39   
40   @JPFAttribute(ATTR_CLS)
41   public static class SomeClass {
42
43     Object data1 = 42;
44     
45     @JPFAttribute(ATTR_CLS)
46     Object data2 = "whatever";
47     
48     public void foo(){}
49     
50     @JPFAttribute(ATTR_CLS)
51     public void bar(){
52       System.out.println("SomeClass.bar() executed");
53     }
54   }
55   
56   public static class LoadListener extends ListenerAdapter {
57     
58     @Override
59     public void classLoaded (VM vm, ClassInfo ci){
60       if (ci.getName().equals(TGT_CLS)){
61         System.out.println("#--- checking attribute annotations of " + ci.getName());
62         
63         assertTrue( ci.hasAttr(MyAttr.class));
64         System.out.println("# class attr Ok");
65         
66         MethodInfo mi = ci.getMethod("bar()V", false);
67         assertTrue( mi.hasAttr(MyAttr.class));
68         System.out.println("# method bar() attr Ok");
69        
70         mi = ci.getMethod("foo()V", false);
71         assertFalse( mi.hasAttr(MyAttr.class));
72         
73         FieldInfo fi = ci.getDeclaredInstanceField("data2");
74         assertTrue( fi.hasAttr(MyAttr.class));
75         System.out.println("# field data2 attr Ok");
76
77         fi = ci.getDeclaredInstanceField("data1");
78         assertFalse( fi.hasAttr(MyAttr.class));
79       }
80     }
81   }
82   
83   @Test
84   public void testAttrs(){
85     if (verifyNoPropertyViolation("+listener=" + LISTENER)){
86       SomeClass o = new SomeClass();
87       o.bar();
88     }
89   }
90   
91 }