001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.core.ext.typeinfo;
017:
018: import junit.framework.TestCase;
019:
020: import java.lang.annotation.Annotation;
021: import java.lang.annotation.ElementType;
022: import java.lang.annotation.Inherited;
023: import java.lang.annotation.Retention;
024: import java.lang.annotation.RetentionPolicy;
025: import java.lang.annotation.Target;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: /**
030: * Test cases for the {@link Annotations} class.
031: */
032: public class AnnotationsTest extends TestCase {
033:
034: @TestAnnotation1("1")
035: private static class AnnotatedClass1 {
036: }
037:
038: @TestAnnotation2("2")
039: private static class AnnotatedClass2 extends AnnotatedClass1 {
040: }
041:
042: @Inherited
043: @Retention(RetentionPolicy.RUNTIME)
044: @Target(ElementType.TYPE)
045: private @interface TestAnnotation1 {
046: String value();
047: }
048:
049: @Retention(RetentionPolicy.RUNTIME)
050: @Target(ElementType.TYPE)
051: private @interface TestAnnotation2 {
052: String value();
053: }
054:
055: @Inherited
056: @Retention(RetentionPolicy.RUNTIME)
057: @Target(ElementType.TYPE)
058: private @interface UnusedAnnotation {
059: String value();
060: }
061:
062: /**
063: * Initializes an {@link Annotations} instance from a given {@link Class}.
064: */
065: private static Annotations initializeAnnotationsFromClass(
066: Class<?> annotatedClass, Annotations parent) {
067: Annotation[] jAnnotations = annotatedClass
068: .getDeclaredAnnotations();
069: Annotations annotations = new Annotations();
070: for (Annotation annotation : jAnnotations) {
071: annotations.addAnnotation(annotation.annotationType(),
072: annotation);
073: }
074:
075: if (parent != null) {
076: annotations.setParent(parent);
077: }
078:
079: return annotations;
080: }
081:
082: /**
083: * Test method for
084: * {@link com.google.gwt.core.ext.typeinfo.Annotations#addAnnotations(java.util.Map)}.
085: */
086: public void testAddAnnotations() {
087: Annotations annotations = new Annotations();
088: Map<Class<? extends Annotation>, Annotation> entries = new HashMap<Class<? extends Annotation>, Annotation>();
089: entries.put(TestAnnotation1.class, AnnotatedClass1.class
090: .getAnnotation(TestAnnotation1.class));
091: annotations.addAnnotations(entries);
092: assertNotNull(annotations.getAnnotation(TestAnnotation1.class));
093: }
094:
095: /**
096: * Test method for
097: * {@link com.google.gwt.core.ext.typeinfo.Annotations#getAnnotation(java.lang.Class)}.
098: *
099: * case 1: annotation is a declared case 2: annotation is inherited case 3:
100: * annotation is not found
101: */
102: public void testGetAnnotationDeclared() {
103: Annotations annotations = initializeAnnotationsFromClass(
104: AnnotatedClass1.class, null);
105: assertNotNull(annotations.getAnnotation(TestAnnotation1.class));
106: assertNull(annotations.getAnnotation(UnusedAnnotation.class));
107: }
108:
109: /**
110: * Test method for
111: * {@link com.google.gwt.core.ext.typeinfo.Annotations#getAnnotation(java.lang.Class)}.
112: *
113: * case 1: annotation is a declared case 2: annotation is inherited case 3:
114: * annotation is not found
115: */
116: public void testGetAnnotationInherited() {
117: Annotations annotations1 = initializeAnnotationsFromClass(
118: AnnotatedClass1.class, null);
119: Annotations annotations2 = initializeAnnotationsFromClass(
120: AnnotatedClass2.class, annotations1);
121:
122: assertNotNull(annotations2.getAnnotation(TestAnnotation1.class));
123: assertNull(annotations2.getAnnotation(UnusedAnnotation.class));
124: }
125:
126: /**
127: * Test method for
128: * {@link com.google.gwt.core.ext.typeinfo.Annotations#getAnnotations()}.
129: */
130: public void testGetAnnotations() {
131: Annotations annotations1 = initializeAnnotationsFromClass(
132: AnnotatedClass1.class, null);
133: Annotations annotations2 = initializeAnnotationsFromClass(
134: AnnotatedClass2.class, annotations1);
135:
136: assertEquals(2, annotations2.getAnnotations().length);
137: }
138:
139: /**
140: * Test method for
141: * {@link com.google.gwt.core.ext.typeinfo.Annotations#getDeclaredAnnotations()}.
142: */
143: public void testGetDeclaredAnnotations() {
144: Annotations annotations1 = initializeAnnotationsFromClass(
145: AnnotatedClass1.class, null);
146: Annotations annotations2 = initializeAnnotationsFromClass(
147: AnnotatedClass2.class, annotations1);
148:
149: assertEquals(1, annotations2.getDeclaredAnnotations().length);
150: assertEquals(1, annotations1.getDeclaredAnnotations().length);
151: }
152:
153: /**
154: * Test method for
155: * {@link com.google.gwt.core.ext.typeinfo.Annotations#isAnnotationPresent(java.lang.Class)}.
156: */
157: public void testIsAnnotationPresent() {
158: Annotations annotations1 = initializeAnnotationsFromClass(
159: AnnotatedClass1.class, null);
160: Annotations annotations2 = initializeAnnotationsFromClass(
161: AnnotatedClass2.class, annotations1);
162:
163: assertTrue(annotations2
164: .isAnnotationPresent(TestAnnotation1.class));
165: assertTrue(annotations2
166: .isAnnotationPresent(TestAnnotation2.class));
167: assertFalse(annotations2
168: .isAnnotationPresent(UnusedAnnotation.class));
169: }
170: }
|