Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / vm / AnnotationInfoTest.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.vm;
20
21 import gov.nasa.jpf.jvm.ClassFile;
22 import gov.nasa.jpf.jvm.DirClassFileContainer;
23 import gov.nasa.jpf.jvm.JVMAnnotationParser;
24 import gov.nasa.jpf.jvm.JVMClassFileContainer;
25 import gov.nasa.jpf.util.test.TestJPF;
26 import java.io.File;
27
28 import org.junit.Test;
29
30 /**
31  * unit test for AnnotationInfo creation
32  */
33 public class AnnotationInfoTest extends TestJPF {
34
35   @interface X {
36     String value() default "nothing";
37   }
38
39   @interface Y {
40     String name();
41     int[] someArray() default { 1, 2, 3 };
42   }
43
44   protected AnnotationInfo createAnnotationInfo (String annotationName) throws ClassParseException {
45     DirClassFileContainer dfc = new DirClassFileContainer( new File("build/tests"));
46     ClassPath cp = new ClassPath();
47     cp.addClassFileContainer(dfc);
48     
49     JVMClassFileContainer.JVMClassFileMatch match = (JVMClassFileContainer.JVMClassFileMatch)cp.findMatch( annotationName);
50     byte[] data = match.getData();
51     
52     ClassFile cf = new ClassFile( data);
53     JVMAnnotationParser parser = new JVMAnnotationParser(cf);
54     
55     return new AnnotationInfo( annotationName, null, parser);
56   }
57   
58   @Test
59   public void testStringDefaultValue() {
60     try {
61       String annotationName = "gov.nasa.jpf.vm.AnnotationInfoTest$X";
62       
63       AnnotationInfo ai = createAnnotationInfo(annotationName);
64       AnnotationInfo.Entry[] entries = ai.getEntries();
65       
66       assertTrue(entries.length == 1);
67       assertTrue(entries[0].getKey().equals("value"));
68       assertTrue(entries[0].getValue().equals("nothing"));
69     
70     } catch (Throwable t){
71       t.printStackTrace();
72       fail("unexpected exception: " + t);
73     }
74   }
75
76   @Test
77   public void testIntArrayDefaultValue() {
78     try {
79       String annotationName = "gov.nasa.jpf.vm.AnnotationInfoTest$Y";
80       
81       AnnotationInfo ai = createAnnotationInfo(annotationName);
82       AnnotationInfo.Entry[] entries = ai.getEntries();
83
84       assertTrue(entries.length == 2);
85       assertTrue(entries[1].getKey().equals("someArray"));
86
87       Object[] a = (Object[]) entries[1].getValue();
88       assertTrue(a.length == 3);
89       assertTrue((Integer)a[0] == 1 && (Integer)a[1] == 2 && (Integer)a[2] == 3);
90       
91     } catch (Throwable t){
92       t.printStackTrace();
93       fail("unexpected exception: " + t);
94     }
95   }
96 }