1 15 package org.apache.tapestry.annotations; 16 17 import java.lang.reflect.Method ; 18 import java.util.HashMap ; 19 import java.util.List ; 20 21 import org.apache.hivemind.Location; 22 import org.apache.tapestry.bean.LightweightBeanInitializer; 23 import org.apache.tapestry.enhance.EnhancementOperation; 24 import org.apache.tapestry.spec.BeanLifecycle; 25 import org.apache.tapestry.spec.ComponentSpecification; 26 import org.apache.tapestry.spec.IBeanSpecification; 27 import org.apache.tapestry.spec.IComponentSpecification; 28 import org.easymock.MockControl; 29 30 36 37 public class TestBeanAnnotationWorker extends BaseAnnotationTestCase 38 { 39 public void testBeanClassSpecified() 40 { 41 Location l = newLocation(); 42 EnhancementOperation op = newOp(); 43 IComponentSpecification spec = new ComponentSpecification(); 44 45 Method m = findMethod(AnnotatedPage.class, "getMapBean"); 46 47 replayControls(); 48 49 new BeanAnnotationWorker().performEnhancement(op, spec, m, l); 50 51 verifyControls(); 52 53 IBeanSpecification bs = spec.getBeanSpecification("mapBean"); 54 55 assertEquals("mapBean", bs.getPropertyName()); 56 assertEquals(HashMap .class.getName(), bs.getClassName()); 57 assertEquals(BeanLifecycle.REQUEST, bs.getLifecycle()); 58 assertSame(l, bs.getLocation()); 59 assertNull(bs.getInitializers()); 60 } 61 62 private EnhancementOperation newOp(String propertyName, Class propertyType) 63 { 64 MockControl opc = newControl(EnhancementOperation.class); 65 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 66 67 op.getPropertyType(propertyName); 68 opc.setReturnValue(propertyType); 69 70 return op; 71 } 72 73 public void testBeanClassNotSpecified() 74 { 75 Location l = newLocation(); 76 EnhancementOperation op = newOp("hashMapBean", HashMap .class); 77 IComponentSpecification spec = new ComponentSpecification(); 78 79 Method m = findMethod(AnnotatedPage.class, "getHashMapBean"); 80 81 replayControls(); 82 83 new BeanAnnotationWorker().performEnhancement(op, spec, m, l); 84 85 verifyControls(); 86 87 IBeanSpecification bs = spec.getBeanSpecification("hashMapBean"); 88 89 assertEquals("hashMapBean", bs.getPropertyName()); 90 assertEquals(HashMap .class.getName(), bs.getClassName()); 91 assertEquals(BeanLifecycle.REQUEST, bs.getLifecycle()); 92 assertSame(l, bs.getLocation()); 93 assertNull(bs.getInitializers()); 94 } 95 96 public void testInitializer() 97 { 98 EnhancementOperation op = newOp("beanWithInitializer", Target.class); 99 IComponentSpecification spec = new ComponentSpecification(); 100 101 Method m = findMethod(AnnotatedPage.class, "getBeanWithInitializer"); 102 103 replayControls(); 104 105 new BeanAnnotationWorker().performEnhancement(op, spec, m, null); 106 107 verifyControls(); 108 109 IBeanSpecification bs = spec.getBeanSpecification("beanWithInitializer"); 110 111 List l = bs.getInitializers(); 112 LightweightBeanInitializer lbi = (LightweightBeanInitializer) l.get(0); 113 114 assertEquals("intValue=10", lbi.getPropertyName()); 115 } 116 117 public void testLifecycle() 118 { 119 EnhancementOperation op = newOp(); 120 IComponentSpecification spec = new ComponentSpecification(); 121 122 Method m = findMethod(AnnotatedPage.class, "getRenderLifecycleBean"); 123 124 replayControls(); 125 126 new BeanAnnotationWorker().performEnhancement(op, spec, m, null); 127 128 verifyControls(); 129 130 IBeanSpecification bs = spec.getBeanSpecification("renderLifecycleBean"); 131 132 assertEquals(BeanLifecycle.RENDER, bs.getLifecycle()); 133 } 134 } 135 | Popular Tags |