1 15 package org.apache.tapestry.enhance; 16 17 import java.lang.reflect.Modifier ; 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.ErrorLog; 23 import org.apache.hivemind.Location; 24 import org.apache.hivemind.service.MethodSignature; 25 import org.apache.hivemind.test.HiveMindTestCase; 26 import org.apache.tapestry.BaseComponent; 27 import org.apache.tapestry.spec.BeanSpecification; 28 import org.apache.tapestry.spec.IBeanSpecification; 29 import org.apache.tapestry.spec.IComponentSpecification; 30 import org.easymock.MockControl; 31 32 38 public class TestInjectBeanWorker extends HiveMindTestCase 39 { 40 private IComponentSpecification newSpec(String beanName, String propertyName, Location location) 41 { 42 IBeanSpecification bs = new BeanSpecification(); 43 bs.setPropertyName(propertyName); 44 bs.setLocation(location); 45 46 MockControl control = newControl(IComponentSpecification.class); 47 IComponentSpecification spec = (IComponentSpecification) control.getMock(); 48 49 spec.getBeanNames(); 50 control.setReturnValue(Collections.singletonList(beanName)); 51 52 spec.getBeanSpecification(beanName); 53 control.setReturnValue(bs); 54 55 return spec; 56 } 57 58 public void testNoWork() 59 { 60 IComponentSpecification spec = newSpec("fred", null, null); 61 EnhancementOperation op = (EnhancementOperation) newMock(EnhancementOperation.class); 62 63 replayControls(); 64 65 new InjectBeanWorker().performEnhancement(op, spec); 66 67 verifyControls(); 68 } 69 70 public void testSuccess() 71 { 72 IComponentSpecification spec = newSpec("fred", "barney", null); 73 MockControl control = newControl(EnhancementOperation.class); 74 EnhancementOperation op = (EnhancementOperation) control.getMock(); 75 76 op.claimProperty("barney"); 77 78 op.getPropertyType("barney"); 79 control.setReturnValue(ArrayList .class); 80 81 op.getAccessorMethodName("barney"); 82 control.setReturnValue("getBarney"); 83 84 op.addMethod( 85 Modifier.PUBLIC, 86 new MethodSignature(ArrayList .class, "getBarney", null, null), 87 "return (java.util.ArrayList) getBeans().getBean(\"fred\");"); 88 89 replayControls(); 90 91 new InjectBeanWorker().performEnhancement(op, spec); 92 93 verifyControls(); 94 } 95 96 public void testFailure() 97 { 98 Location l = fabricateLocation(99); 99 Throwable ex = new ApplicationRuntimeException(EnhanceMessages.claimedProperty("barney")); 100 101 MockControl control = newControl(EnhancementOperation.class); 102 EnhancementOperation op = (EnhancementOperation) control.getMock(); 103 104 IComponentSpecification spec = newSpec("fred", "barney", l); 105 106 ErrorLog log = (ErrorLog) newMock(ErrorLog.class); 107 108 op.claimProperty("barney"); 109 control.setThrowable(ex); 110 111 op.getBaseClass(); 112 control.setReturnValue(BaseComponent.class); 113 114 log.error(EnhanceMessages.errorAddingProperty("barney", BaseComponent.class, ex), l, ex); 115 116 replayControls(); 117 118 InjectBeanWorker w = new InjectBeanWorker(); 119 120 w.setErrorLog(log); 121 122 w.performEnhancement(op, spec); 123 124 verifyControls(); 125 } 126 127 }
| Popular Tags
|