2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
19 package gov.nasa.jpf.test.java.lang.reflect;
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;
28 import org.junit.Test;
30 public class FieldTest extends TestJPF {
32 public static final int testField1 = 1;
34 public String testField2;
36 public static void main (String[] args) throws SecurityException, NoSuchFieldException{
37 runTestsOfThisClass(args);
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));
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");
69 public void isEnumConstantTest (){
70 if (verifyNoPropertyViolation()){
71 for (Field f : TestEnum.class.getFields())
72 assertTrue(f.isEnumConstant());
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());
87 @Retention(RetentionPolicy.RUNTIME)
89 public @interface A1 {
101 public static class ShortField {
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);
116 public void getSetShortFieldTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
117 if(verifyNoPropertyViolation()) {
118 ShortField sf = new ShortField();
119 Field f = sf.getClass().getField("f");
121 assertEquals((short)f.get(sf), (short)3);
123 short[] x = new short[] {1,2,3};
124 Array.setShort(x, 0, (short) 3);
125 assertEquals(Array.getShort(x, 0), (short)3);