1 8 package test.annotation; 9 10 import junit.framework.TestCase; 11 import org.codehaus.aspectwerkz.reflect.MethodInfo; 12 import org.codehaus.aspectwerkz.reflect.impl.java.JavaMethodInfo; 13 import org.codehaus.aspectwerkz.annotation.AnnotationInfo; 14 15 import java.lang.reflect.Method ; 16 17 22 public class DefaultValueTest extends TestCase { 23 24 @DefaultedAnnotation 25 void annotatedMethod() { 26 } 27 28 public MethodInfo getAnnotatedMethod() { 29 try { 30 Method m = DefaultValueTest.class.getDeclaredMethod("annotatedMethod", new Class [0]); 31 MethodInfo mi = JavaMethodInfo.getMethodInfo(m); 32 if (mi.getAnnotations().size() <= 0) { 33 throw new Error ("test corrupted"); 34 } 35 return mi; 36 } catch (Throwable t) { 37 throw new Error ("test corrupted"); 38 } 39 } 40 41 public void testDefaultedAnnotation() { 42 AnnotationInfo annI = (AnnotationInfo) getAnnotatedMethod().getAnnotations().get(0); 43 DefaultedAnnotation ann = (DefaultedAnnotation) annI.getAnnotation(); 44 45 assertEquals("default", ann.s()); 47 48 int[] is = ann.is(); 50 int[] defaultIs = {1, 2}; 51 for (int i = 0; i < is.length; i++) { 52 assertEquals(defaultIs[i], is[i]); 53 } 54 55 DefaultedAnnotation.NestedDefaultedAnnotation nested = ann.nested(); 57 assertEquals("default_const", nested.s()); 58 59 DefaultedAnnotation.NestedDefaultedAnnotation nested2 = ann.nested2(); 61 assertEquals("default_nested", nested2.s()); 62 63 System.out.println("----"); 65 assertEquals(ReferencedClass.class, ann.klass()); 67 68 assertEquals(ReferencedClass[].class.getName(), ann.klass2()[0].getName()); 70 assertEquals(ReferencedClass.class, ann.klass2()[1]); 71 } 72 73 public void testToString() throws Throwable { 74 AnnotationInfo annI = (AnnotationInfo) getAnnotatedMethod().getAnnotations().get(0); 75 DefaultedAnnotation ann = (DefaultedAnnotation) annI.getAnnotation(); 76 77 System.out.println(""); 78 System.out.println(ann.toString()); 79 System.out.println(ann.annotationType()); 80 81 82 Method m = DefaultValueTest.class.getDeclaredMethod("annotatedMethod", new Class [0]); 83 System.out.println(m); 84 java.lang.annotation.Annotation [] anns = m.getAnnotations(); 85 for (int i = 0; i < anns.length; i++) { 86 java.lang.annotation.Annotation annotation = anns[i]; 87 System.out.println(annotation); 88 System.out.println(annotation.annotationType()); 89 } 90 91 92 } 93 94 public static void main(String [] args) { 95 junit.textui.TestRunner.run(suite()); 96 } 97 98 public static junit.framework.Test suite() { 99 return new junit.framework.TestSuite(DefaultValueTest.class); 100 } 101 102 103 104 } 105 | Popular Tags |