AccessibleObjectTest.java revision 5d709784bbf5001012d7f25172927d46f6c1abe1
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.api.java.lang.reflect;
19
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargetClass;
24
25import java.lang.annotation.Annotation;
26import java.lang.annotation.ElementType;
27import java.lang.annotation.Inherited;
28import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
30import java.lang.annotation.Target;
31import java.lang.reflect.AccessibleObject;
32import java.lang.reflect.Modifier;
33import java.util.HashSet;
34import java.util.Set;
35
36@TestTargetClass(AccessibleObject.class)
37public class AccessibleObjectTest extends junit.framework.TestCase {
38
39    public class TestClass {
40        public Object aField;
41
42        @InheritedRuntime
43        public void annotatedMethod(){}
44    }
45
46    public class SubTestClass extends TestClass{
47        @AnnotationRuntime0
48        @AnnotationRuntime1
49        @AnnotationClass0
50        @AnnotationSource0
51        public void annotatedMethod(){}
52    }
53
54
55    @Retention(RetentionPolicy.RUNTIME)
56    @Target( {ElementType.METHOD})
57    static @interface AnnotationRuntime0 {
58    }
59
60    @Retention(RetentionPolicy.RUNTIME)
61    @Target( { ElementType.METHOD})
62    static @interface AnnotationRuntime1 {
63    }
64
65    @Retention(RetentionPolicy.CLASS)
66    @Target( { ElementType.METHOD})
67    static @interface AnnotationClass0 {
68    }
69
70    @Retention(RetentionPolicy.SOURCE)
71    @Target( {ElementType.METHOD})
72    static @interface AnnotationSource0 {
73    }
74
75    @Inherited
76    @Retention(RetentionPolicy.RUNTIME)
77    @Target( {ElementType.METHOD})
78    static @interface InheritedRuntime {
79    }
80
81    //used for constructor test
82    private static class MyAccessibleObject extends AccessibleObject{
83        public MyAccessibleObject() {
84            super();
85        }
86    }
87
88    /**
89     * @tests java.lang.reflect.AccessibleObject#AccessibleObject()
90     */
91    @TestTargetNew(
92        level = TestLevel.COMPLETE,
93        notes = "The only thing I can do",
94        method = "AccessibleObject",
95        args = {}
96    )
97    public void test_Constructor() {
98        assertNotNull(new MyAccessibleObject());
99    }
100
101    /**
102     * @tests java.lang.reflect.AccessibleObject#isAccessible()
103     */
104    @TestTargetNew(
105        level = TestLevel.COMPLETE,
106        notes = "",
107        method = "isAccessible",
108        args = {}
109    )
110    public void test_isAccessible() {
111        // Test for method boolean
112        // java.lang.reflect.AccessibleObject.isAccessible()
113        try {
114            AccessibleObject ao = TestClass.class.getField("aField");
115            ao.setAccessible(true);
116            assertTrue("Returned false to isAccessible", ao.isAccessible());
117            ao.setAccessible(false);
118            assertTrue("Returned true to isAccessible", !ao.isAccessible());
119        } catch (Exception e) {
120            fail("Exception during test : " + e.getMessage());
121        }
122    }
123
124    /**
125     * @tests java.lang.reflect.AccessibleObject#setAccessible(java.lang.reflect.AccessibleObject[],
126     *        boolean)
127     */
128    @TestTargetNew(
129        level = TestLevel.COMPLETE,
130        notes = "SecurityExeption is tested in tests.security.permissions.JavaLangReflectAccessibleObjectTest",
131        method = "setAccessible",
132        args = {java.lang.reflect.AccessibleObject[].class, boolean.class}
133    )
134    public void test_setAccessible$Ljava_lang_reflect_AccessibleObjectZ() {
135        try {
136            AccessibleObject ao = TestClass.class.getField("aField");
137            AccessibleObject[] aoa = new AccessibleObject[] { ao };
138            AccessibleObject.setAccessible(aoa, true);
139            assertTrue("Returned false to isAccessible", ao.isAccessible());
140            AccessibleObject.setAccessible(aoa, false);
141            assertTrue("Returned true to isAccessible", !ao.isAccessible());
142        } catch (Exception e) {
143            fail("Exception during test : " + e.getMessage());
144        }
145    }
146
147    /**
148     * @tests java.lang.reflect.AccessibleObject#setAccessible(boolean)
149     */
150    @TestTargetNew(
151        level = TestLevel.COMPLETE,
152        notes = "SecurityExeption is tested in tests.security.permissions.JavaLangReflectAccessibleObjectTest",
153        method = "setAccessible",
154        args = {boolean.class}
155    )
156    public void test_setAccessible() throws Exception {
157        AccessibleObject ao = TestClass.class.getField("aField");
158        ao.setAccessible(true);
159        assertTrue("Returned false to isAccessible", ao.isAccessible());
160        ao.setAccessible(false);
161        assertFalse("Returned true to isAccessible", ao.isAccessible());
162    }
163
164    @TestTargetNew(
165        level = TestLevel.COMPLETE,
166        notes = "",
167        method = "getAnnotation",
168        args = {java.lang.Class.class}
169    )
170    public void test_getAnnotation() throws Exception{
171        AccessibleObject ao = SubTestClass.class.getMethod("annotatedMethod");
172        //test error case
173        boolean npeThrown = false;
174        try {
175          ao.getAnnotation(null);
176          fail("NPE expected");
177        } catch (NullPointerException e) {
178            npeThrown = true;
179        }
180        assertTrue("NPE expected", npeThrown);
181
182        //test inherited on method has no effect
183        InheritedRuntime ir = ao.getAnnotation(InheritedRuntime.class);
184        assertNull("Inherited Annotations should have no effect", ir);
185
186        //test ordinary runtime annotation
187        AnnotationRuntime0 rt0 = ao.getAnnotation(AnnotationRuntime0.class);
188        assertNotNull("AnnotationRuntime0 instance expected", rt0);
189    }
190
191    @TestTargetNew(
192        level = TestLevel.COMPLETE,
193        notes = "",
194        method = "getAnnotations",
195        args = {}
196    )
197     public void test_getAnnotations() throws Exception {
198        AccessibleObject ao = SubTestClass.class.getMethod("annotatedMethod");
199        Annotation[] annotations = ao.getAnnotations();
200        assertEquals(2, annotations.length);
201
202        Set<Class<?>> ignoreOrder = new HashSet<Class<?>>();
203        ignoreOrder.add(annotations[0].annotationType());
204        ignoreOrder.add(annotations[1].annotationType());
205
206        assertTrue("Missing @AnnotationRuntime0",
207                ignoreOrder.contains(AnnotationRuntime0.class));
208        assertTrue("Missing @AnnotationRuntime1",
209                ignoreOrder.contains(AnnotationRuntime1.class));
210    }
211
212    @TestTargetNew(
213        level = TestLevel.COMPLETE,
214        notes = "",
215        method = "getDeclaredAnnotations",
216        args = {}
217    )
218     public void test_getDeclaredAnnotations() throws Exception {
219        AccessibleObject ao = SubTestClass.class.getMethod("annotatedMethod");
220        Annotation[] annotations = ao.getDeclaredAnnotations();
221        assertEquals(2, annotations.length);
222
223        Set<Class<?>> ignoreOrder = new HashSet<Class<?>>();
224        ignoreOrder.add(annotations[0].annotationType());
225        ignoreOrder.add(annotations[1].annotationType());
226
227        assertTrue("Missing @AnnotationRuntime0",
228                ignoreOrder.contains(AnnotationRuntime0.class));
229        assertTrue("Missing @AnnotationRuntime1",
230                ignoreOrder.contains(AnnotationRuntime1.class));
231    }
232
233    @TestTargetNew(
234        level = TestLevel.COMPLETE,
235        notes = "",
236        method = "isAnnotationPresent",
237        args = {java.lang.Class.class}
238    )
239    public void test_isAnnotationPresent() throws Exception {
240        AccessibleObject ao = SubTestClass.class.getMethod("annotatedMethod");
241        assertTrue("Missing @AnnotationRuntime0",
242                ao.isAnnotationPresent(AnnotationRuntime0.class));
243        assertFalse("AnnotationSource0 should not be visible at runtime",
244                ao.isAnnotationPresent(AnnotationSource0.class));
245        boolean npeThrown = false;
246        try {
247          ao.isAnnotationPresent(null);
248          fail("NPE expected");
249        } catch (NullPointerException e) {
250            npeThrown = true;
251        }
252        assertTrue("NPE expected", npeThrown);
253    }
254
255
256    /**
257     * Sets up the fixture, for example, open a network connection. This method
258     * is called before a test is executed.
259     */
260    protected void setUp() {
261    }
262
263    /**
264     * Tears down the fixture, for example, close a network connection. This
265     * method is called after a test is executed.
266     */
267    protected void tearDown() {
268    }
269}
270