1 15 package org.apache.tapestry.enhance; 16 17 import java.util.Collections ; 18 import java.util.Map ; 19 20 import org.apache.hivemind.ErrorLog; 21 import org.apache.hivemind.Location; 22 import org.apache.hivemind.test.HiveMindTestCase; 23 import org.apache.tapestry.html.BasePage; 24 import org.apache.tapestry.spec.IComponentSpecification; 25 import org.apache.tapestry.spec.InjectSpecification; 26 import org.apache.tapestry.spec.InjectSpecificationImpl; 27 import org.easymock.MockControl; 28 29 35 public class TestDispatchToInjectWorker extends HiveMindTestCase 36 { 37 private InjectSpecification newInjectSpecification(String propertyName, String type, 38 String object) 39 { 40 return newInjectSpecification(propertyName, type, object, null); 41 } 42 43 private InjectSpecification newInjectSpecification(String propertyName, String type, 44 String object, Location location) 45 { 46 InjectSpecification result = new InjectSpecificationImpl(); 47 result.setProperty(propertyName); 48 result.setType(type); 49 result.setObject(object); 50 result.setLocation(location); 51 52 return result; 53 } 54 55 private IComponentSpecification newSpec(InjectSpecification injectSpec) 56 { 57 MockControl control = newControl(IComponentSpecification.class); 58 IComponentSpecification spec = (IComponentSpecification) control.getMock(); 59 60 spec.getInjectSpecifications(); 61 control.setReturnValue(Collections.singletonList(injectSpec)); 62 63 return spec; 64 } 65 66 private Map newMap(String key, Object value) 67 { 68 return Collections.singletonMap(key, value); 69 } 70 71 public void testSuccess() 72 { 73 EnhancementOperation op = newOp(); 74 InjectSpecification is = newInjectSpecification("property", "object", "service:Foo"); 75 InjectEnhancementWorker worker = newWorker(); 76 Map map = newMap("object", worker); 77 IComponentSpecification spec = newSpec(is); 78 79 worker.performEnhancement(op, is); 80 81 replayControls(); 82 83 DispatchToInjectWorker d = new DispatchToInjectWorker(); 84 d.setInjectWorkers(map); 85 86 d.performEnhancement(op, spec); 87 88 verifyControls(); 89 } 90 91 public void testUnknownType() 92 { 93 Location l = newLocation(); 94 EnhancementOperation op = newOp(); 95 InjectSpecification is = newInjectSpecification( 96 "injectedProperty", 97 "object", 98 "service:Foo", 99 l); 100 IComponentSpecification spec = newSpec(is); 101 ErrorLog log = newLog(); 102 103 log.error(EnhanceMessages.unknownInjectType("injectedProperty", "object"), l, null); 104 105 replayControls(); 106 107 DispatchToInjectWorker d = new DispatchToInjectWorker(); 108 d.setInjectWorkers(Collections.EMPTY_MAP); 109 d.setErrorLog(log); 110 111 d.performEnhancement(op, spec); 112 113 verifyControls(); 114 } 115 116 public void testFailure() 117 { 118 Location l = newLocation(); 119 MockControl opc = newControl(EnhancementOperation.class); 120 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 121 InjectSpecification is = newInjectSpecification("myProperty", "object", "service:Foo", l); 122 MockControl workerc = newControl(InjectEnhancementWorker.class); 123 InjectEnhancementWorker worker = (InjectEnhancementWorker) workerc.getMock(); 124 Map map = newMap("object", worker); 125 IComponentSpecification spec = newSpec(is); 126 Throwable t = new RuntimeException ("Simulated failure."); 127 ErrorLog log = newLog(); 128 129 worker.performEnhancement(op, is); 130 workerc.setThrowable(t); 131 132 op.getBaseClass(); 133 opc.setReturnValue(BasePage.class); 134 135 log 136 .error( 137 "Error adding property myProperty to class org.apache.tapestry.html.BasePage: Simulated failure.", 138 l, 139 t); 140 141 replayControls(); 142 143 DispatchToInjectWorker d = new DispatchToInjectWorker(); 144 d.setInjectWorkers(map); 145 d.setErrorLog(log); 146 147 d.performEnhancement(op, spec); 148 149 verifyControls(); 150 } 151 152 private InjectEnhancementWorker newWorker() 153 { 154 return (InjectEnhancementWorker) newMock(InjectEnhancementWorker.class); 155 } 156 157 private ErrorLog newLog() 158 { 159 return (ErrorLog) newMock(ErrorLog.class); 160 } 161 162 private EnhancementOperation newOp() 163 { 164 return (EnhancementOperation) newMock(EnhancementOperation.class); 165 } 166 } 167 | Popular Tags |