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 java.lang;
018:
019: import java.lang.annotation.Annotation;
020: import java.lang.annotation.Inherited;
021: import java.lang.annotation.Retention;
022: import java.lang.annotation.RetentionPolicy;
023: import java.lang.reflect.AnnotatedElement;
024: import java.util.Arrays;
025: import java.util.List;
026:
027: import notfound.MissingAntn;
028:
029: import org.apache.harmony.lang.AnnotatedElementTestFrame;
030: import org.apache.harmony.lang.AnnotatedElementTestFrame.TagAntn;
031: import org.apache.harmony.lang.AnnotatedElementTestFrame.ValAntn;
032:
033: /**
034: * Test of the {@link java.lang.reflect.AnnotatedElement AnnotatedElement}
035: * functionality in {@link java.lang.Class java.lang.Class} class.
036: *
037: * @author Alexey V. Varlamov
038: * @version $Revision$
039: */
040: public class Class5Test extends AnnotatedElementTestFrame {
041:
042: public static void main(String[] args) {
043: junit.textui.TestRunner.run(Class5Test.class);
044: }
045:
046: @TagAntn
047: interface First {
048: }
049:
050: protected @Override
051: AnnotatedElement getElement1() throws Throwable {
052: return First.class;
053: }
054:
055: protected @Override
056: AnnotatedElement getElement2() throws Throwable {
057: @TagAntn
058: @ValAntn
059: class Second {
060: }
061: return Second.class;
062: }
063:
064: protected @Override
065: AnnotatedElement getElement3() throws Throwable {
066: return Third.class;
067: }
068:
069: protected @Override
070: AnnotatedElement getElement4() throws Throwable {
071: @MissingAntn
072: class Fourth {
073: }
074: return Fourth.class;
075: }
076:
077: protected @Override
078: AnnotatedElement getElement5() throws Throwable {
079: @MissingClassValAntn
080: class X {
081: }
082: return X.class;
083: }
084:
085: protected @Override
086: AnnotatedElement getElement6() throws Throwable {
087: @MissingTypeAntn
088: class X {
089: }
090: return X.class;
091: }
092:
093: /**
094: * For Class instances, getAnnotations() must return
095: * annotations declared + inherited from superclasses.
096: */
097: public void testGetAnnotationsInherited() throws Throwable {
098: Annotation[] an = C4.class.getDeclaredAnnotations();
099: assertNotNull("declared in C4", an);
100: assertEquals("number of Declared Annotations in C4", 1,
101: an.length);
102: assertSame("declared in C4", TagAntn.class, an[0]
103: .annotationType());
104:
105: Annotation[] an2 = C4.class.getAnnotations();
106: assertNotNull("all in C4", an2);
107: assertEquals("number of all Annotations in C4", 3, an2.length);
108:
109: List<Class> la = Arrays.asList(new Class[] {
110: an2[0].annotationType(), an2[1].annotationType(),
111: an2[2].annotationType() });
112: assertTrue("1st annotation", la.contains(SuperTagAntn.class));
113: assertTrue("2nd annotation", la.contains(TagAntn.class));
114: assertTrue("3rd annotation", la.contains(SuperValAntn.class));
115: }
116:
117: /**
118: * For Class instances, getAnnotations()
119: * must not return annotations inherited from superinterfaces.
120: */
121: public void testGetAnnotationsInherited2() throws Throwable {
122: Annotation[] an = I3.class.getAnnotations();
123: assertNotNull("all in I3", an);
124: assertEquals("number of Annotations in I3", 1, an.length);
125: assertSame("annotation of I3", TagAntn.class, an[0]
126: .annotationType());
127:
128: Annotation[] an2 = CI1.class.getAnnotations();
129: assertNotNull("all in CI1", an2);
130: assertEquals("number of all Annotations in CI1", 1, an2.length);
131: assertSame("annotation of CI1", ValAntn.class, an2[0]
132: .annotationType());
133:
134: Annotation[] an3 = CI2.class.getAnnotations();
135: assertNotNull("all in CI2", an3);
136: assertEquals("number of all Annotations in CI2", 1, an3.length);
137: assertSame("annotation of CI2", SuperValAntn.class, an3[0]
138: .annotationType());
139: assertSame("annotation value of CI2", CI2.class,
140: ((SuperValAntn) an3[0]).value());
141: }
142:
143: /**
144: * For Class instances, annotations inherited from a superclass
145: * may be overriden by descendants.
146: * In this case getAnnotations() must return
147: * annotations declared + non-overriden inherited.
148: */
149: public void testGetAnnotationsInheritedOverriden() throws Throwable {
150: Annotation[] an = C6.class.getDeclaredAnnotations();
151: assertNotNull("declared in C6", an);
152: assertEquals("number of Declared Annotations in C6", 1,
153: an.length);
154: assertSame("declared in C6", SuperValAntn.class, an[0]
155: .annotationType());
156:
157: Annotation[] an2 = C6.class.getAnnotations();
158: assertNotNull("all in C6", an2);
159: assertEquals("number of all Annotations in C6", 2, an2.length);
160:
161: List<Class> la = Arrays.asList(new Class[] {
162: an2[0].annotationType(), an2[1].annotationType(), });
163: assertTrue("C6 1st annotation", la.contains(SuperTagAntn.class));
164: assertTrue("C6 2rd annotation", la.contains(SuperValAntn.class));
165:
166: Annotation[] an3 = C7.class.getAnnotations();
167: assertNotNull("declared in C7", an3);
168: assertEquals("number of all Annotations in C7", 2, an3.length);
169:
170: List<Class> la3 = Arrays.asList(new Class[] {
171: an3[0].annotationType(), an3[1].annotationType(), });
172: assertTrue("C7 1st annotation", la3
173: .contains(SuperTagAntn.class));
174: assertTrue("C7 2rd annotation", la3
175: .contains(SuperValAntn.class));
176: }
177:
178: /**
179: * For Class instances, isAnnotationPresent() must account for
180: * annotations declared + inherited from superclasses.
181: */
182: public void testIsAnnotationPresentInherited() throws Throwable {
183: assertTrue("case 1", C4.class
184: .isAnnotationPresent(SuperTagAntn.class));
185: assertTrue("case 2", C4.class
186: .isAnnotationPresent(SuperValAntn.class));
187: assertTrue("case 3", C4.class
188: .isAnnotationPresent(TagAntn.class));
189: assertTrue("case 4", C6.class
190: .isAnnotationPresent(SuperTagAntn.class));
191: assertTrue("case 5", C6.class
192: .isAnnotationPresent(SuperValAntn.class));
193: assertTrue("case 6", C5.class
194: .isAnnotationPresent(SuperTagAntn.class));
195: }
196:
197: /**
198: * For Class instances, isAnnotationPresent() must not account for
199: * non-inheritable annotations from superclasses.
200: */
201: public void testIsAnnotationPresentInherited_Negative()
202: throws Throwable {
203: assertFalse("case 1", C4.class
204: .isAnnotationPresent(ValAntn.class));
205: assertFalse("case 2", C4.class.isAnnotationPresent(None.class));
206: assertFalse("case 3", C5.class
207: .isAnnotationPresent(TagAntn.class));
208: assertFalse("case 4", C6.class
209: .isAnnotationPresent(TagAntn.class));
210: assertFalse("case 5", C6.class
211: .isAnnotationPresent(ValAntn.class));
212: assertFalse("case 6", C5.class.isAnnotationPresent(None.class));
213: }
214:
215: /**
216: * For Class instances, getAnnotation() must account for
217: * annotations declared + inherited from superclasses.
218: */
219: public void testGetAnnotationInherited() throws Throwable {
220: SuperValAntn an = C5.class.getAnnotation(SuperValAntn.class);
221: assertNotNull(an);
222: assertSame("value of inherited annotation", C3.class, an
223: .value());
224: }
225:
226: /**
227: * For Class instances, annotations inherited from a superclass
228: * may be overriden by descendants.
229: * In this case getAnnotation() must return latest overriden annotation.
230: */
231: public void testGetAnnotationInheritedOverriden() throws Throwable {
232: SuperValAntn an = C6.class.getAnnotation(SuperValAntn.class);
233: assertNotNull("overriden in C6", an);
234: assertSame("value of overriden annotation in C6", C6.class, an
235: .value());
236:
237: SuperValAntn an2 = C7.class.getAnnotation(SuperValAntn.class);
238: assertNotNull("overriden in C7", an2);
239: assertSame("value of overriden annotation in C7", C6.class, an2
240: .value());
241: }
242: }
243:
244: @interface Third {
245: }
246:
247: @Inherited
248: @Retention(RetentionPolicy.RUNTIME)
249: @interface SuperTagAntn {
250: }
251:
252: @Inherited
253: @Retention(RetentionPolicy.RUNTIME)
254: @interface SuperValAntn {
255: Class value();
256: }
257:
258: @SuperTagAntn
259: class C1 {
260: }
261:
262: @ValAntn
263: class C2 extends C1 {
264: }
265:
266: @SuperValAntn(C3.class)
267: class C3 extends C2 {
268: }
269:
270: @TagAntn
271: class C4 extends C3 {
272: }
273:
274: class C5 extends C4 {
275: }
276:
277: @SuperValAntn(C6.class)
278: class C6 extends C5 {
279: }
280:
281: class C7 extends C6 {
282: }
283:
284: @SuperTagAntn
285: interface I1 {
286: }
287:
288: @SuperValAntn(I2.class)
289: interface I2 {
290: }
291:
292: @TagAntn
293: interface I3 extends I1, I2 {
294: }
295:
296: @ValAntn
297: class CI1 implements I3 {
298: }
299:
300: @SuperValAntn(CI2.class)
301: class CI2 extends CI1 {
302: }
|