1 8 package test.annotation; 9 10 import junit.framework.TestCase; 11 import org.codehaus.aspectwerkz.annotation.Annotations; 12 import org.codehaus.aspectwerkz.annotation.UntypedAnnotation; 13 14 import java.util.List ; 15 import java.lang.reflect.Method ; 16 17 40 public class AnnotationCTest extends TestCase { 41 42 public void testClassAnnotation() { 43 Class me = AnnotationCTest.class; 44 45 List voids = Annotations.getAnnotations("Void", me); 46 assertEquals(2, voids.size()); 47 48 List simples = Annotations.getAnnotations("Simple", me); 49 assertEquals(2, simples.size()); 50 51 StringBuffer all = new StringBuffer (); 52 for (int i = 0; i < simples.size(); i++) { 53 all.append("[").append(((AnnotationParserTest.Simple) simples.get(i)).s()).append("]"); 54 } 55 String [] lookFor = new String []{ 56 "[null]", 57 "[foo]" 58 }; 59 for (int i = 0; i < lookFor.length; i++) { 60 String s = lookFor[i]; 61 if (all.toString().indexOf(s) < 0) { 62 fail("could not find " + lookFor[i] + " in " + all.toString()); 63 } 64 } 65 66 List beforeActions = Annotations.getAnnotations("BeforeAction", me); 67 assertEquals(3, beforeActions.size()); 68 all = new StringBuffer (); 69 for (int i = 0; i < beforeActions.size(); i++) { 70 all.append("[").append(((UntypedAnnotation)beforeActions.get(i)).value()).append("]"); 71 } 72 lookFor = new String []{ 73 "[some untype that starts with Before]", 74 "[other untyped]", 75 "[yet another untyped]", 76 77 }; 78 for (int i = 0; i < lookFor.length; i++) { 79 String s = lookFor[i]; 80 if (all.toString().indexOf(s) < 0) { 81 fail("could not find " + lookFor[i] + " in " + all.toString()); 82 } 83 } 84 85 assertEquals( 86 "hello", 87 ((AnnotationParserTest.DefaultString) Annotations.getAnnotation("DefaultString", me)).value() 88 ); 89 90 assertEquals( 91 String .class, ((AnnotationParserTest.Complex) Annotations.getAnnotation("Complex", me)).klass() 92 ); 93 94 List untypeds = Annotations.getAnnotations("Untyped", me); 95 assertEquals(5, untypeds.size()); 96 all = new StringBuffer (); 97 for (int i = 0; i < untypeds.size(); i++) { 98 all.append("[").append(((AnnotationParserTest.Untyped) untypeds.get(i)).value()).append("]"); 99 } 100 lookFor = new String []{ 101 "[]", 102 "[hello]", 103 "[(hello) - see the space here !]", 104 "[hello2]", 105 "[preserved hello]" 106 }; 107 for (int i = 0; i < lookFor.length; i++) { 108 String s = lookFor[i]; 109 if (all.toString().indexOf(s) < 0) { 110 fail("could not find " + lookFor[i] + " in " + all.toString()); 111 } 112 } 113 } 114 115 127 public void testMethodAnnotation() throws Throwable { 128 Class me = test.annotation.AnnotationCTest.class; 129 Method m = me.getDeclaredMethod("testMethodAnnotation", new Class [0]); 130 131 140 141 List voids = Annotations.getAnnotations("Void", me); 142 assertEquals(2, voids.size()); 143 144 List simples = Annotations.getAnnotations("Simple", me); 145 assertEquals(2, simples.size()); 146 StringBuffer all = new StringBuffer (); 147 for (int i = 0; i < simples.size(); i++) { 148 all.append("[").append(((AnnotationParserTest.Simple) simples.get(i)).s()).append("]"); 149 } 150 String [] lookFor = new String []{ 151 "[null]", 152 "[foo]" 153 }; 154 for (int i = 0; i < lookFor.length; i++) { 155 String s = lookFor[i]; 156 if (all.toString().indexOf(s) < 0) { 157 fail("could not find " + lookFor[i] + " in " + all.toString()); 158 } 159 } 160 161 assertEquals( 162 "hello", 163 ((AnnotationParserTest.DefaultString) Annotations.getAnnotation("DefaultString", me)).value() 164 ); 165 166 assertEquals( 167 String .class, ((AnnotationParserTest.Complex) Annotations.getAnnotation("Complex", me)).klass() 168 ); 169 170 List untypeds = Annotations.getAnnotations("Untyped", m); 171 assertEquals(4, untypeds.size()); 172 all = new StringBuffer (); 173 for (int i = 0; i < untypeds.size(); i++) { 174 all.append("[").append(((AnnotationParserTest.Untyped) untypeds.get(i)).value()).append("]"); 175 } 176 lookFor = new String []{ 177 "[]", 178 "[hello]", 179 "[(hello) - see the space here !]" 180 }; 181 for (int i = 0; i < lookFor.length; i++) { 182 String s = lookFor[i]; 183 if (all.toString().indexOf(s) < 0) { 184 fail("could not find " + lookFor[i] + " in " + all.toString()); 185 } 186 } 187 } 188 189 public void testNestedAnnotation() throws Throwable { 190 Class me = AnnotationCTest.class; 191 AnnotationParserTest.ComplexNested ann = (AnnotationParserTest.ComplexNested) Annotations.getAnnotation("ComplexNested", me); 192 AnnotationParserTest.Simple ann1 = ann.nesteds()[0]; 193 AnnotationParserTest.Simple ann2 = ann.nesteds()[1]; 194 String ann12 = ann1.val()+"."+ann2.val(); 195 if (ann12.equals("foo.bar") || ann12.equals("bar.foo")) { 196 ; } else { 198 fail("Annotation is not correct " + ann.toString()); 199 } 200 } 201 202 public static void main(String [] args) { 203 junit.textui.TestRunner.run(suite()); 204 } 205 206 public static junit.framework.Test suite() { 207 return new junit.framework.TestSuite(AnnotationCTest.class); 208 } 209 } 210 | Popular Tags |