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 com.google.gwt.core.ext.TreeLogger;
019: import com.google.gwt.core.ext.UnableToCompleteException;
020: import com.google.gwt.core.ext.typeinfo.test.AnnotatedClass;
021: import com.google.gwt.core.ext.typeinfo.test.ClassLiteralReferenceAnnotation;
022: import com.google.gwt.core.ext.typeinfo.test.TestAnnotation;
023: import com.google.gwt.dev.cfg.ModuleDef;
024: import com.google.gwt.dev.cfg.ModuleDefLoader;
025:
026: import junit.framework.TestCase;
027:
028: import java.lang.annotation.Annotation;
029: import java.lang.reflect.Constructor;
030: import java.lang.reflect.Field;
031: import java.lang.reflect.Method;
032:
033: /**
034: * Test cases for the {@link TypeOracle}'s {@link Annotation} support.
035: *
036: * Array annotations Enum annotations String from field annotations
037: */
038: public class TypeOracleAnnotationSupportTest extends TestCase {
039: static {
040: ModuleDefLoader.setEnableCachingModules(true);
041: }
042:
043: private static void validateAnnotation(
044: HasAnnotations annotatedElement,
045: String testAnnotationValue, String nestedAnnotationValue,
046: TestAnnotation realAnnotation) {
047: assertNotNull(annotatedElement);
048:
049: TestAnnotation testAnnotation = annotatedElement
050: .getAnnotation(TestAnnotation.class);
051: assertNotNull(testAnnotation);
052:
053: // Check our proxy objects against the real thing.
054: assertEquals(realAnnotation, testAnnotation);
055: assertEquals(realAnnotation.hashCode(), testAnnotation
056: .hashCode());
057:
058: // tobyr doesn't like this.
059: // assertEquals(realAnnotation.toString(), testAnnotation.toString());
060:
061: // checks default value
062: assertEquals(testAnnotationValue, testAnnotation.value());
063: assertEquals(nestedAnnotationValue, testAnnotation
064: .nestedAnnotation().value());
065:
066: // Tests default values using conditional statements.
067: assertEquals(realAnnotation.longValue(), testAnnotation
068: .longValue());
069:
070: // Tests default value of array type.
071: assertEquals(realAnnotation.intArrayValue().length,
072: testAnnotation.intArrayValue().length);
073:
074: // Tests default value which is a field reference.
075: assertEquals(realAnnotation.stringValue(), testAnnotation
076: .stringValue());
077:
078: // Tests default value that is a class literal.
079: assertEquals(realAnnotation.classLiteral(), testAnnotation
080: .classLiteral());
081: }
082:
083: private final TreeLogger logger = TreeLogger.NULL;
084: private ModuleDef moduleDef;
085:
086: private final TypeOracle typeOracle;
087:
088: public TypeOracleAnnotationSupportTest()
089: throws UnableToCompleteException {
090: moduleDef = ModuleDefLoader.loadFromClassPath(logger,
091: "com.google.gwt.core.ext.typeinfo.TypeOracleTest");
092: typeOracle = moduleDef.getTypeOracle(logger);
093: }
094:
095: /**
096: * Test that a class can be annotated.
097: */
098: public void testAnnotatedClass() throws NotFoundException {
099: JClassType annotatedClass = typeOracle
100: .getType(AnnotatedClass.class.getName());
101:
102: TestAnnotation realAnnotation = AnnotatedClass.class
103: .getAnnotation(TestAnnotation.class);
104: validateAnnotation(annotatedClass, "Class", "Foo",
105: realAnnotation);
106:
107: ClassLiteralReferenceAnnotation classReferenceAnnotation = annotatedClass
108: .getAnnotation(ClassLiteralReferenceAnnotation.class);
109: assertEquals(ClassLiteralReferenceAnnotation.Foo.class,
110: classReferenceAnnotation.value());
111:
112: assertEquals(2, annotatedClass.getAnnotations().length);
113: }
114:
115: /**
116: * Test that a constructor can be annotated.
117: */
118: public void testAnnotatedConstructor() throws NotFoundException,
119: SecurityException, NoSuchMethodException {
120: JClassType annotatedClass = typeOracle
121: .getType(AnnotatedClass.class.getName());
122: JConstructor ctor = annotatedClass.getConstructor(new JType[0]);
123:
124: Constructor<AnnotatedClass> constructor = AnnotatedClass.class
125: .getConstructor();
126: TestAnnotation realAnnotation = constructor
127: .getAnnotation(TestAnnotation.class);
128:
129: validateAnnotation(ctor, "Constructor", "Not assigned",
130: realAnnotation);
131: }
132:
133: /**
134: * Test that a field can be annotated.
135: */
136: public void testAnnotatedField() throws NotFoundException,
137: SecurityException, NoSuchFieldException {
138: JClassType annotatedClass = typeOracle
139: .getType(AnnotatedClass.class.getName());
140: JField annotatedField = annotatedClass
141: .getField("annotatedField");
142:
143: Field field = AnnotatedClass.class
144: .getDeclaredField("annotatedField");
145: TestAnnotation realAnnotation = field
146: .getAnnotation(TestAnnotation.class);
147:
148: validateAnnotation(annotatedField, "Field", "Not assigned",
149: realAnnotation);
150: }
151:
152: /**
153: * Tests that methods can be annotated.
154: */
155: public void testAnnotatedMethod() throws NotFoundException,
156: SecurityException, NoSuchMethodException {
157: JClassType annotatedClass = typeOracle
158: .getType(AnnotatedClass.class.getName());
159: JMethod annotatedMethod = annotatedClass.getMethod(
160: "annotatedMethod", new JType[0]);
161:
162: Method method = AnnotatedClass.class
163: .getDeclaredMethod("annotatedMethod");
164: TestAnnotation realAnnotation = method
165: .getAnnotation(TestAnnotation.class);
166:
167: validateAnnotation(annotatedMethod, "Method", "Not assigned",
168: realAnnotation);
169: }
170:
171: /**
172: * Tests that packages can be annotated. This necessitates the existence of a
173: * package-info.java file in the package that you wish to annotate.
174: */
175: public void testAnnotatedPackage() throws NotFoundException,
176: ClassNotFoundException {
177: JPackage annotatedPackage = typeOracle
178: .getPackage("com.google.gwt.core.ext.typeinfo.test");
179: assertNotNull(annotatedPackage);
180:
181: TestAnnotation realAnnotation = Class.forName(
182: "com.google.gwt.core.ext.typeinfo.test.package-info")
183: .getAnnotation(TestAnnotation.class);
184:
185: validateAnnotation(annotatedPackage, "Package", "Not assigned",
186: realAnnotation);
187: }
188:
189: /**
190: * Tests that parameters can be annotated.
191: */
192: public void testAnnotatedParameter() throws NotFoundException,
193: SecurityException, NoSuchMethodException {
194: JClassType annotatedClass = typeOracle
195: .getType(AnnotatedClass.class.getName());
196: JMethod jmethod = annotatedClass.getMethod(
197: "methodWithAnnotatedParameter",
198: new JType[] { JPrimitiveType.INT });
199: JParameter parameter = jmethod.getParameters()[0];
200:
201: Method method = AnnotatedClass.class.getDeclaredMethod(
202: "methodWithAnnotatedParameter", int.class);
203: Annotation[][] paramAnnotations = method
204: .getParameterAnnotations();
205: TestAnnotation realAnnotation = (TestAnnotation) paramAnnotations[0][0];
206:
207: validateAnnotation(parameter, "Parameter", "Not assigned",
208: realAnnotation);
209: }
210: }
|