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