Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / vm / reflection / ConstructorTest.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.reflection;
20
21 import gov.nasa.jpf.util.test.TestJPF;
22
23 import java.lang.annotation.Annotation;
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.lang.reflect.Constructor;
27
28 import org.junit.Test;
29
30 /**
31  * regression test for constructor reflection
32  */
33 public class ConstructorTest extends TestJPF {
34
35   @Retention(RetentionPolicy.RUNTIME)
36   @interface A {
37     String value();
38   }
39   
40   static class Y {
41     @A("this is a superclass ctor annotation")
42     protected Y(){}
43   }
44   
45   static class X extends Y {
46     private String a;
47
48     @A("this is a ctor annotation")
49     public X (@A("this is a parameter annotation") String x) {
50       this.a = x;
51       System.out.println(x);
52     }
53   }
54
55   @Test
56   public void testConstructorCall() {
57     if (verifyNoPropertyViolation()){
58       try {
59         Class<X> cls = X.class;
60         Constructor<X> ctor = cls.getDeclaredConstructor(new Class<?>[] { String.class });
61
62         X x = ctor.newInstance("I'm an X");
63         
64         assertNotNull(x); 
65       } catch (Throwable t){
66         fail("ctor invocation failed: " + t);
67       }
68     }
69   }
70
71   static class I {
72     private Integer i;
73
74     public I(Integer i) {
75       this.i = i;
76     }
77   }
78
79   @Test
80   public void testConstructorCallInteger() {
81     if (verifyNoPropertyViolation()) {
82       try {
83         Class<I> cls = I.class;
84         Constructor<I> ctor = cls.getDeclaredConstructor(new Class<?>[] {Integer.class });
85
86         I obj = ctor.newInstance(42);
87         assertNotNull(obj);
88         assertEquals(new Integer(42), obj.i);
89       } catch (Throwable t) {
90         fail("ctor invocation with Integer failed: " + t);
91       }
92     }
93   }
94
95   
96   
97   @Test
98   public void testAnnotations(){
99     if (verifyNoPropertyViolation()) {
100       try {
101         Class<X> cls = X.class;
102         Constructor<X> ctor = cls.getDeclaredConstructor(new Class<?>[] { String.class });
103
104         Annotation[] ai = ctor.getDeclaredAnnotations();
105         assertTrue("no declared ctor annotations found", ai.length == 1);
106         
107         assertTrue("wrong ctor annotation type", ai[0] instanceof A);
108         System.out.printf("ctor annotation: " + ai[0]);
109         
110       } catch (Throwable t) {
111         fail("ctor.getDeclaredAnnotations() failed: " + t);
112       }
113     }    
114   }
115   
116   @Test
117   public void testParameterAnnotations(){
118     if (verifyNoPropertyViolation()) {
119       try {
120         Class<X> cls = X.class;
121         Constructor<X> ctor = cls.getDeclaredConstructor(new Class<?>[] { String.class });
122
123         Annotation[][] pai = ctor.getParameterAnnotations();
124         assertTrue("no ctor parameter annotations found", pai.length == 1);
125         
126         Annotation[] ai = pai[0];
127         assertTrue("wrong number of annotations for first ctor argument", ai.length == 1);
128         
129         assertTrue("wrong parameter annotation type", ai[0] instanceof A);
130         System.out.printf("ctor parameter annotation: " + ai[0]);
131         
132       } catch (Throwable t) {
133         fail("ctor.getParameterAnnotations() failed: " + t);
134       }
135     }        
136   }
137 }