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.Field;
27 import org.junit.Test;
29 public class FieldTest extends TestJPF {
31 public static final int testField1 = 1;
33 public String testField2;
35 public static void main (String[] args) throws SecurityException, NoSuchFieldException{
36 runTestsOfThisClass(args);
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));
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");
68 public void isEnumConstantTest (){
69 if (verifyNoPropertyViolation()){
70 for (Field f : TestEnum.class.getFields())
71 assertTrue(f.isEnumConstant());
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());
86 @Retention(RetentionPolicy.RUNTIME)
88 public @interface A1 {
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);