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.IAsset; 27 import org.apache.tapestry.IComponent; 28 import org.apache.tapestry.spec.AssetSpecification; 29 import org.apache.tapestry.spec.IAssetSpecification; 30 import org.apache.tapestry.spec.IComponentSpecification; 31 import org.easymock.MockControl; 32 33 39 public class TestInjectAssetWorker extends HiveMindTestCase 40 { 41 private IComponentSpecification newSpec(String assetName, String propertyName, Location location) 42 { 43 IAssetSpecification as = new AssetSpecification(); 44 as.setPropertyName(propertyName); 45 as.setLocation(location); 46 47 MockControl control = newControl(IComponentSpecification.class); 48 IComponentSpecification spec = (IComponentSpecification) control.getMock(); 49 50 spec.getAssetNames(); 51 control.setReturnValue(Collections.singletonList(assetName)); 52 53 spec.getAsset(assetName); 54 control.setReturnValue(as); 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 InjectAssetWorker().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(IAsset.class); 79 80 op.claimProperty("barney"); 81 82 op.addField("_$barney", IAsset.class); 83 84 op.getAccessorMethodName("barney"); 85 control.setReturnValue("getBarney"); 86 87 op.addMethod( 88 Modifier.PUBLIC, 89 new MethodSignature(IAsset.class, "getBarney", null, null), 90 "return _$barney;"); 91 92 op.extendMethodImplementation( 93 IComponent.class, 94 EnhanceUtils.FINISH_LOAD_SIGNATURE, 95 "_$barney = getAsset(\"fred\");"); 96 97 replayControls(); 98 99 new InjectAssetWorker().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 InjectAssetWorker w = new InjectAssetWorker(); 130 131 w.setErrorLog(log); 132 133 w.performEnhancement(op, spec); 134 135 verifyControls(); 136 } 137 138 public void testWrongPropertyType() 139 { 140 MockControl control = newControl(EnhancementOperation.class); 141 EnhancementOperation op = (EnhancementOperation) control.getMock(); 142 143 op.getPropertyType("barney"); 144 control.setReturnValue(IComponent.class); 145 146 op.claimProperty("barney"); 147 148 replayControls(); 149 150 InjectAssetWorker w = new InjectAssetWorker(); 151 try 152 { 153 w.injectAsset(op, "fred", "barney"); 154 unreachable(); 155 } 156 catch (ApplicationRuntimeException ex) 157 { 158 assertEquals("Property barney is type org.apache.tapestry.IComponent, which is not compatible with the expected type, org.apache.tapestry.IAsset.", ex.getMessage()); 159 } 160 161 verifyControls(); 162 163 } 164 } | Popular Tags |