001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.lang.annotation;
018:
019: import java.lang.annotation.Annotation;
020: import java.lang.reflect.Constructor;
021: import java.lang.reflect.Field;
022: import java.lang.reflect.Method;
023:
024: import org.apache.harmony.test.TestResources;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * Test verifies that correct classloader is used to reflect
030: * annotations.
031: *
032: * @author Alexey V. Varlamov
033: * @version $Revision$
034: */
035: public class AnnotationLoaderTest extends TestCase {
036:
037: public static void main(String[] args) {
038: junit.textui.TestRunner.run(AnnotationLoaderTest.class);
039: }
040:
041: protected ClassLoader ld;
042: protected Class<?> test;
043: protected Class<?> anotherAntnClass;
044:
045: @Override
046: protected void setUp() throws Exception {
047: ld = TestResources.getLoader();
048: test = ld
049: .loadClass("org.apache.harmony.lang.test.resource.AnnotatedMembers");
050: anotherAntnClass = ld
051: .loadClass("org.apache.harmony.lang.test.resource.AnotherAntn");
052: }
053:
054: /**
055: * Tests that the defining classloader is used to lookup class annotations.
056: */
057: public void testClass() throws Throwable {
058: Annotation[] an = test.getAnnotations();
059: assertNotNull(an);
060: assertEquals("annotations num", 1, an.length);
061: assertEquals("the class annotation", anotherAntnClass, an[0]
062: .annotationType());
063: }
064:
065: /**
066: * Tests that the defining classloader is used to lookup package annotations.
067: */
068: public void testPackage() throws Throwable {
069: Package p = test.getPackage();
070: assertNotNull("package", p);
071: Annotation[] an = p.getAnnotations();
072: assertNotNull(an);
073: assertEquals("annotations num", 1, an.length);
074: assertEquals("the package annotation", "AnotherAntn", an[0]
075: .annotationType().getSimpleName());
076: }
077:
078: /**
079: * Tests that the defining classloader is used to lookup annotations
080: * of fields of a class.
081: */
082: public void testField() throws Throwable {
083: Field f = test.getField("foo");
084: assertNotNull("field", f);
085: Annotation[] an = f.getAnnotations();
086: assertNotNull("annotations", an);
087: assertEquals("annotations num", 1, an.length);
088: assertEquals("the class annotation", "AnotherAntn", an[0]
089: .annotationType().getSimpleName());
090: }
091:
092: /**
093: * Tests that the defining classloader is used to lookup annotations
094: * of methods of a class.
095: */
096: public void testMethod() throws Throwable {
097: Method m = test.getMethod("bar");
098: assertNotNull("method", m);
099: Annotation[] an = m.getAnnotations();
100: assertNotNull("annotations", an);
101: assertEquals("annotations num", 1, an.length);
102: assertEquals("the class annotation", "AnotherAntn", an[0]
103: .annotationType().getSimpleName());
104: }
105:
106: /**
107: * Tests that the defining classloader is used to lookup annotations
108: * of constructors of a class.
109: */
110: public void testCtor() throws Throwable {
111: Constructor ctor = test.getConstructor();
112: assertNotNull("ctor", ctor);
113: Annotation[] an = ctor.getAnnotations();
114: assertNotNull("annotations", an);
115: assertEquals("annotations num", 1, an.length);
116: assertEquals("the class annotation", "AnotherAntn", an[0]
117: .annotationType().getSimpleName());
118: }
119:
120: /**
121: * Tests that the defining classloader is used to lookup parameter annotations
122: * of class's methods.
123: */
124: public void testParam() throws Throwable {
125: Method m = test.getMethod("buz", String.class);
126: assertNotNull("method", m);
127: Annotation[][] an = m.getParameterAnnotations();
128: assertNotNull("annotations", an);
129: assertEquals("param num", 1, an.length);
130: assertEquals("annotations num", 1, an[0].length);
131: assertEquals("the class annotation", "AnotherAntn", an[0][0]
132: .annotationType().getSimpleName());
133: }
134:
135: /**
136: * HARMONY-5180 regression test.
137: */
138: public void testMemberValue() throws Throwable {
139: Field f = test.getField("acme");
140: assertNotNull("field", f);
141: Annotation[] an = f.getAnnotations();
142: assertNotNull("annotations", an);
143: assertEquals("annotations num", 1, an.length);
144: assertEquals("the class annotation", AllTypesAntn.class, an[0]
145: .annotationType());
146: assertEquals("the class-member", anotherAntnClass,
147: ((AllTypesAntn) an[0]).classValue());
148: }
149:
150: }
|