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