001: /**************************************************************************************
002: * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test.annotation;
008:
009: import junit.framework.TestCase;
010: import org.codehaus.aspectwerkz.reflect.MethodInfo;
011: import org.codehaus.aspectwerkz.reflect.impl.java.JavaMethodInfo;
012: import org.codehaus.aspectwerkz.annotation.AnnotationInfo;
013:
014: import java.lang.reflect.Method;
015:
016: /**
017: * TODO change sout in asserts
018: *
019: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
020: */
021: public class DefaultValueTest extends TestCase {
022:
023: @DefaultedAnnotation
024: void annotatedMethod() {
025: }
026:
027: public MethodInfo getAnnotatedMethod() {
028: try {
029: Method m = DefaultValueTest.class.getDeclaredMethod(
030: "annotatedMethod", new Class[0]);
031: MethodInfo mi = JavaMethodInfo.getMethodInfo(m);
032: if (mi.getAnnotations().size() <= 0) {
033: throw new Error("test corrupted");
034: }
035: return mi;
036: } catch (Throwable t) {
037: throw new Error("test corrupted");
038: }
039: }
040:
041: public void testDefaultedAnnotation() {
042: AnnotationInfo annI = (AnnotationInfo) getAnnotatedMethod()
043: .getAnnotations().get(0);
044: DefaultedAnnotation ann = (DefaultedAnnotation) annI
045: .getAnnotation();
046:
047: // string
048: assertEquals("default", ann.s());
049:
050: // primitive array
051: int[] is = ann.is();
052: int[] defaultIs = { 1, 2 };
053: for (int i = 0; i < is.length; i++) {
054: assertEquals(defaultIs[i], is[i]);
055: }
056:
057: // nested annotation which has a default itself but whose assigned default differs
058: DefaultedAnnotation.NestedDefaultedAnnotation nested = ann
059: .nested();
060: assertEquals("default_const", nested.s());
061:
062: // nested annotation which is using the default itself
063: DefaultedAnnotation.NestedDefaultedAnnotation nested2 = ann
064: .nested2();
065: assertEquals("default_nested", nested2.s());
066:
067: //-- so far we should not have triggered any ReferencedClass loading
068: System.out.println("----");
069: // class
070: assertEquals(ReferencedClass.class, ann.klass());
071:
072: // class
073: assertEquals(ReferencedClass[].class.getName(), ann.klass2()[0]
074: .getName());
075: assertEquals(ReferencedClass.class, ann.klass2()[1]);
076: }
077:
078: public void testToString() throws Throwable {
079: AnnotationInfo annI = (AnnotationInfo) getAnnotatedMethod()
080: .getAnnotations().get(0);
081: DefaultedAnnotation ann = (DefaultedAnnotation) annI
082: .getAnnotation();
083:
084: System.out.println("");
085: System.out.println(ann.toString());
086: System.out.println(ann.annotationType());
087:
088: Method m = DefaultValueTest.class.getDeclaredMethod(
089: "annotatedMethod", new Class[0]);
090: System.out.println(m);
091: java.lang.annotation.Annotation[] anns = m.getAnnotations();
092: for (int i = 0; i < anns.length; i++) {
093: java.lang.annotation.Annotation annotation = anns[i];
094: System.out.println(annotation);
095: System.out.println(annotation.annotationType());
096: }
097:
098: }
099:
100: public static void main(String[] args) {
101: junit.textui.TestRunner.run(suite());
102: }
103:
104: public static junit.framework.Test suite() {
105: return new junit.framework.TestSuite(DefaultValueTest.class);
106: }
107:
108: }
|