001: package org.objectweb.celtix.common.annotation;
002:
003: import java.lang.annotation.Annotation;
004: import java.lang.reflect.Field;
005: import java.lang.reflect.Method;
006: import java.util.ArrayList;
007: import java.util.List;
008: import javax.annotation.Resource;
009: import javax.jws.WebMethod;
010: import javax.jws.WebService;
011: import javax.xml.ws.WebServiceContext;
012: import junit.framework.TestCase;
013: import org.easymock.EasyMock;
014:
015: public class AnnotationProcessorTest extends TestCase {
016:
017: AnnotatedGreeterImpl greeterImpl = new AnnotatedGreeterImpl();
018: AnnotationProcessor processor = new AnnotationProcessor(greeterImpl);
019: List<Class<? extends Annotation>> expectedAnnotations = new ArrayList<Class<? extends Annotation>>();
020:
021: AnnotationVisitor visitor = EasyMock
022: .createMock(AnnotationVisitor.class);
023:
024: public void setUp() {
025: EasyMock.checkOrder(visitor, false);
026: }
027:
028: public void testVisitClass() {
029:
030: expectedAnnotations.add(WebService.class);
031:
032: prepareCommonExpectations(visitor);
033: visitor.visitClass((Class<?>) EasyMock
034: .eq(AnnotatedGreeterImpl.class), (Annotation) EasyMock
035: .isA(WebService.class));
036:
037: runProcessor(visitor);
038: }
039:
040: public void testVisitField() throws Exception {
041:
042: Field expectedField = AnnotatedGreeterImpl.class
043: .getDeclaredField("foo");
044:
045: expectedAnnotations.add(Resource.class);
046: prepareCommonExpectations(visitor);
047: visitor.visitField(EasyMock.eq(expectedField),
048: (Annotation) EasyMock.isA(Resource.class));
049: visitor.visitMethod((Method) EasyMock.anyObject(),
050: (Annotation) EasyMock.anyObject());
051:
052: runProcessor(visitor);
053:
054: }
055:
056: public void testVisitMethod() throws Exception {
057:
058: Field expectedField = AnnotatedGreeterImpl.class
059: .getDeclaredField("foo");
060: Method expectedMethod1 = AnnotatedGreeterImpl.class
061: .getDeclaredMethod("sayHi");
062: Method expectedMethod2 = AnnotatedGreeterImpl.class
063: .getDeclaredMethod("sayHi", String.class);
064: Method expectedMethod3 = AnnotatedGreeterImpl.class
065: .getDeclaredMethod("greetMe", String.class);
066: Method expectedMethod4 = AnnotatedGreeterImpl.class
067: .getDeclaredMethod("setContext",
068: WebServiceContext.class);
069:
070: expectedAnnotations.add(WebMethod.class);
071: expectedAnnotations.add(Resource.class);
072:
073: prepareCommonExpectations(visitor);
074: visitor.visitField(EasyMock.eq(expectedField),
075: (Annotation) EasyMock.isA(Resource.class));
076: visitor.visitMethod(EasyMock.eq(expectedMethod1),
077: (Annotation) EasyMock.isA(WebMethod.class));
078: visitor.visitMethod(EasyMock.eq(expectedMethod2),
079: (Annotation) EasyMock.isA(WebMethod.class));
080: visitor.visitMethod(EasyMock.eq(expectedMethod3),
081: (Annotation) EasyMock.isA(WebMethod.class));
082: visitor.visitMethod(EasyMock.eq(expectedMethod4),
083: (Annotation) EasyMock.isA(Resource.class));
084: runProcessor(visitor);
085: }
086:
087: public void testVisitMemberOverrideIgnoresClass() {
088: }
089:
090: public void testVisitSuperClassAnnotations() {
091: }
092:
093: public void testVisitDerivedClassMemberNoAnnotation() {
094: }
095:
096: public void testProcessorInvalidConstructorArgs() {
097:
098: try {
099: new AnnotationProcessor(null);
100: fail("did not get expected argument");
101: } catch (IllegalArgumentException e) {
102: // happy
103: }
104:
105: }
106:
107: public void testProcessorInvalidAcceptArg() {
108:
109: try {
110: processor.accept(null);
111: fail("did not get expected exception");
112: } catch (IllegalArgumentException e) {
113: // happy
114: }
115:
116: }
117:
118: private void prepareCommonExpectations(AnnotationVisitor v) {
119: v.getTargetAnnotations();
120: EasyMock.expectLastCall().andReturn(expectedAnnotations);
121: v.setTarget(greeterImpl);
122: }
123:
124: private void runProcessor(AnnotationVisitor v) {
125: EasyMock.replay(v);
126: processor.accept(v);
127: EasyMock.verify(v);
128: }
129: }
|