Run script for Benchexec
[jpf-core.git] / src / tests / gov / nasa / jpf / test / java / lang / reflect / FieldTest.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.java.lang.reflect;
20
21 import gov.nasa.jpf.util.test.TestJPF;
22 import java.lang.annotation.Inherited;
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.RetentionPolicy;
25 import java.lang.reflect.Field;
26
27 import org.junit.Test;
28
29 public class FieldTest extends TestJPF {
30
31   public static final int testField1 = 1;
32
33   public String testField2;
34
35   public static void main (String[] args) throws SecurityException, NoSuchFieldException{
36     runTestsOfThisClass(args);
37   }
38
39   @Test
40   public void equalsTest () throws SecurityException, NoSuchFieldException{
41     if (verifyNoPropertyViolation()){
42       Field f1 = FieldTest.class.getField("testField1");
43       Field f2 = FieldTest.class.getField("testField1");
44       Field f3 = FieldTest.class.getField("testField2");
45       Field f4 = FieldTest.class.getField("testField2");
46       assertTrue(f1.equals(f2));
47       assertTrue(f3.equals(f4));
48       assertFalse(f1.equals(f3));
49     }
50   }
51
52   @Test
53   public void toStringTest () throws SecurityException, NoSuchFieldException{
54     if (verifyNoPropertyViolation()){
55       Field f1 = FieldTest.class.getField("testField1");
56       System.out.println(f1);
57       assertEquals(f1.toString(), "public static final int gov.nasa.jpf.test.java.lang.reflect.FieldTest.testField1");
58       Field f2 = FieldTest.class.getField("testField2");
59       assertEquals(f2.toString(), "public java.lang.String gov.nasa.jpf.test.java.lang.reflect.FieldTest.testField2");
60     }
61   }
62
63   enum TestEnum{
64     f1, f2, f3
65   }
66
67   @Test
68   public void isEnumConstantTest (){
69     if (verifyNoPropertyViolation()){
70       for (Field f : TestEnum.class.getFields())
71         assertTrue(f.isEnumConstant());
72     }
73   }
74
75   @Test
76   public void hashCodeTest () throws SecurityException, NoSuchFieldException{
77     if (verifyNoPropertyViolation()){
78       Field f1 = FieldTest.class.getField("testField1");
79       Field f2 = FieldTest.class.getField("testField1");
80       Field f3 = FieldTest.class.getField("testField2");
81       assertTrue(f1.hashCode() == f2.hashCode());
82       assertFalse(f1.hashCode() == f3.hashCode());
83     }
84   }
85
86   @Retention(RetentionPolicy.RUNTIME)
87   @Inherited
88   public @interface A1 {
89   }
90
91   public class Super {
92     @A1
93     public int f;
94   }
95
96   public class Sub {
97     public int f;
98   }
99
100   @Test
101   public void getDeclaredAnnotationsTest () throws SecurityException, NoSuchFieldException{
102     if (verifyNoPropertyViolation()){
103       Field f1 = Sub.class.getField("f");
104       Field f2 = Super.class.getField("f");
105       assertEquals(f1.getDeclaredAnnotations().length, 0);
106       assertEquals(f2.getDeclaredAnnotations().length, 1);
107     }
108   }
109 }