Merge branch 'sv-comp-run-script' of git://github.com/peterschrammel/jpf-core into...
[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.Array;
26 import java.lang.reflect.Field;
27
28 import org.junit.Test;
29
30 public class FieldTest extends TestJPF {
31
32   public static final int testField1 = 1;
33
34   public String testField2;
35
36   public static void main (String[] args) throws SecurityException, NoSuchFieldException{
37     runTestsOfThisClass(args);
38   }
39
40   @Test
41   public void equalsTest () throws SecurityException, NoSuchFieldException{
42     if (verifyNoPropertyViolation()){
43       Field f1 = FieldTest.class.getField("testField1");
44       Field f2 = FieldTest.class.getField("testField1");
45       Field f3 = FieldTest.class.getField("testField2");
46       Field f4 = FieldTest.class.getField("testField2");
47       assertTrue(f1.equals(f2));
48       assertTrue(f3.equals(f4));
49       assertFalse(f1.equals(f3));
50     }
51   }
52
53   @Test
54   public void toStringTest () throws SecurityException, NoSuchFieldException{
55     if (verifyNoPropertyViolation()){
56       Field f1 = FieldTest.class.getField("testField1");
57       System.out.println(f1);
58       assertEquals(f1.toString(), "public static final int gov.nasa.jpf.test.java.lang.reflect.FieldTest.testField1");
59       Field f2 = FieldTest.class.getField("testField2");
60       assertEquals(f2.toString(), "public java.lang.String gov.nasa.jpf.test.java.lang.reflect.FieldTest.testField2");
61     }
62   }
63
64   enum TestEnum{
65     f1, f2, f3
66   }
67
68   @Test
69   public void isEnumConstantTest (){
70     if (verifyNoPropertyViolation()){
71       for (Field f : TestEnum.class.getFields())
72         assertTrue(f.isEnumConstant());
73     }
74   }
75
76   @Test
77   public void hashCodeTest () throws SecurityException, NoSuchFieldException{
78     if (verifyNoPropertyViolation()){
79       Field f1 = FieldTest.class.getField("testField1");
80       Field f2 = FieldTest.class.getField("testField1");
81       Field f3 = FieldTest.class.getField("testField2");
82       assertTrue(f1.hashCode() == f2.hashCode());
83       assertFalse(f1.hashCode() == f3.hashCode());
84     }
85   }
86
87   @Retention(RetentionPolicy.RUNTIME)
88   @Inherited
89   public @interface A1 {
90   }
91
92   public class Super {
93     @A1
94     public int f;
95   }
96
97   public class Sub {
98     public int f;
99   }
100   
101   public static class ShortField {
102     short f;
103   }
104
105   @Test
106   public void getDeclaredAnnotationsTest () throws SecurityException, NoSuchFieldException{
107     if (verifyNoPropertyViolation()){
108       Field f1 = Sub.class.getField("f");
109       Field f2 = Super.class.getField("f");
110       assertEquals(f1.getDeclaredAnnotations().length, 0);
111       assertEquals(f2.getDeclaredAnnotations().length, 1);
112     }
113   }
114   
115   @Test
116   public void getSetShortFieldTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
117     if(verifyNoPropertyViolation()) {
118       ShortField sf = new ShortField();
119       Field f = sf.getClass().getField("f");
120       f.set(sf, (short)3);
121       assertEquals((short)f.get(sf), (short)3);
122       
123       short[] x = new short[] {1,2,3};
124       Array.setShort(x, 0, (short) 3);
125       assertEquals(Array.getShort(x, 0), (short)3);
126     }
127   }
128 }