1 package org.objectweb.celtix.common.annotation; 2 3 4 5 import java.lang.annotation.Annotation ; 6 import java.lang.reflect.Field ; 7 import java.lang.reflect.Method ; 8 import java.util.ArrayList ; 9 import java.util.List ; 10 import javax.annotation.Resource; 11 import javax.jws.WebMethod; 12 import javax.jws.WebService; 13 import javax.xml.ws.WebServiceContext; 14 import junit.framework.TestCase; 15 import org.easymock.EasyMock; 16 17 18 public class AnnotationProcessorTest extends TestCase { 19 20 AnnotatedGreeterImpl greeterImpl = new AnnotatedGreeterImpl(); 21 AnnotationProcessor processor = new AnnotationProcessor(greeterImpl); 22 List <Class <? extends Annotation >> expectedAnnotations = new ArrayList <Class <? extends Annotation >>(); 23 24 AnnotationVisitor visitor = EasyMock.createMock(AnnotationVisitor.class); 25 26 public void setUp() { 27 EasyMock.checkOrder(visitor, false); 28 } 29 30 public void testVisitClass() { 31 32 expectedAnnotations.add(WebService.class); 33 34 prepareCommonExpectations(visitor); 35 visitor.visitClass((Class <?>)EasyMock.eq(AnnotatedGreeterImpl.class), 36 (Annotation )EasyMock.isA(WebService.class)); 37 38 runProcessor(visitor); 39 } 40 41 public void testVisitField() throws Exception { 42 43 Field expectedField = AnnotatedGreeterImpl.class.getDeclaredField("foo"); 44 45 expectedAnnotations.add(Resource.class); 46 prepareCommonExpectations(visitor); 47 visitor.visitField(EasyMock.eq(expectedField), 48 (Annotation )EasyMock.isA(Resource.class)); 49 visitor.visitMethod((Method )EasyMock.anyObject(), (Annotation )EasyMock.anyObject()); 50 51 runProcessor(visitor); 52 53 } 54 55 public void testVisitMethod() throws Exception { 56 57 Field expectedField = AnnotatedGreeterImpl.class.getDeclaredField("foo"); 58 Method expectedMethod1 = AnnotatedGreeterImpl.class.getDeclaredMethod("sayHi"); 59 Method expectedMethod2 = AnnotatedGreeterImpl.class.getDeclaredMethod("sayHi", String .class); 60 Method expectedMethod3 = AnnotatedGreeterImpl.class.getDeclaredMethod("greetMe", String .class); 61 Method expectedMethod4 = 62 AnnotatedGreeterImpl.class.getDeclaredMethod("setContext", WebServiceContext.class); 63 64 expectedAnnotations.add(WebMethod.class); 65 expectedAnnotations.add(Resource.class); 66 67 prepareCommonExpectations(visitor); 68 visitor.visitField(EasyMock.eq(expectedField), 69 (Annotation )EasyMock.isA(Resource.class)); 70 visitor.visitMethod(EasyMock.eq(expectedMethod1), 71 (Annotation )EasyMock.isA(WebMethod.class)); 72 visitor.visitMethod(EasyMock.eq(expectedMethod2), 73 (Annotation )EasyMock.isA(WebMethod.class)); 74 visitor.visitMethod(EasyMock.eq(expectedMethod3), 75 (Annotation )EasyMock.isA(WebMethod.class)); 76 visitor.visitMethod(EasyMock.eq(expectedMethod4), 77 (Annotation )EasyMock.isA(Resource.class)); 78 runProcessor(visitor); 79 } 80 81 public void testVisitMemberOverrideIgnoresClass() { 82 } 83 84 public void testVisitSuperClassAnnotations() { 85 } 86 87 public void testVisitDerivedClassMemberNoAnnotation() { 88 } 89 90 public void testProcessorInvalidConstructorArgs() { 91 92 try { 93 new AnnotationProcessor(null); 94 fail("did not get expected argument"); 95 } catch (IllegalArgumentException e) { 96 } 98 99 } 100 101 public void testProcessorInvalidAcceptArg() { 102 103 try { 104 processor.accept(null); 105 fail("did not get expected exception"); 106 } catch (IllegalArgumentException e) { 107 } 109 110 } 111 112 113 private void prepareCommonExpectations(AnnotationVisitor v) { 114 v.getTargetAnnotations(); 115 EasyMock.expectLastCall().andReturn(expectedAnnotations); 116 v.setTarget(greeterImpl); 117 } 118 119 private void runProcessor(AnnotationVisitor v) { 120 EasyMock.replay(v); 121 processor.accept(v); 122 EasyMock.verify(v); 123 } 124 } 125 126 | Popular Tags |