1 15 package org.apache.tapestry.enhance; 16 17 import java.lang.reflect.Modifier ; 18 import java.util.Map ; 19 20 import org.apache.hivemind.Location; 21 import org.apache.hivemind.service.BodyBuilder; 22 import org.apache.hivemind.service.MethodSignature; 23 import org.apache.hivemind.test.HiveMindTestCase; 24 import org.apache.tapestry.engine.state.ApplicationStateManager; 25 import org.apache.tapestry.event.PageDetachListener; 26 import org.apache.tapestry.spec.InjectSpecification; 27 import org.apache.tapestry.spec.InjectSpecificationImpl; 28 import org.easymock.MockControl; 29 30 36 public class TestInjectStateWorker extends HiveMindTestCase 37 { 38 private ApplicationStateManager newASM() 39 { 40 return (ApplicationStateManager) newMock(ApplicationStateManager.class); 41 } 42 43 private InjectSpecification newSpec(String propertyName, String objectName, Location l) 44 { 45 InjectSpecification spec = new InjectSpecificationImpl(); 46 47 spec.setProperty(propertyName); 48 spec.setObject(objectName); 49 spec.setLocation(l); 50 51 return spec; 52 } 53 54 public void testSuccess() 55 { 56 InjectSpecification spec = newSpec("fred", "barney", null); 57 MockControl opc = newControl(EnhancementOperation.class); 58 EnhancementOperation op = (EnhancementOperation) opc.getMock(); 59 60 ApplicationStateManager asm = newASM(); 61 62 op.getPropertyType("fred"); 63 opc.setReturnValue(Map .class); 64 65 op.claimProperty("fred"); 66 op.addField("_$fred", Map .class); 67 68 op.addInjectedField("_$applicationStateManager", ApplicationStateManager.class, asm); 69 opc.setReturnValue("_$applicationStateManager"); 70 71 op.getAccessorMethodName("fred"); 72 opc.setReturnValue("getFred"); 73 74 BodyBuilder builder = new BodyBuilder(); 75 76 78 builder.begin(); 79 builder.addln("if (_$fred == null)"); 80 builder.addln(" _$fred = (java.util.Map) _$applicationStateManager.get(\"barney\");"); 81 builder.addln("return _$fred;"); 82 builder.end(); 83 84 MethodSignature sig = new MethodSignature(Map .class, "getFred", null, null); 85 86 op.addMethod(Modifier.PUBLIC, sig, builder.toString()); 87 88 builder.clear(); 89 builder.begin(); 90 builder.addln("_$applicationStateManager.store(\"barney\", $1);"); 91 builder.addln("_$fred = $1;"); 92 builder.end(); 93 94 sig = new MethodSignature(void.class, "setFred", new Class [] 95 { Map .class }, null); 96 97 op.addMethod(Modifier.PUBLIC, sig, builder.toString()); 98 op.extendMethodImplementation( 99 PageDetachListener.class, 100 EnhanceUtils.PAGE_DETACHED_SIGNATURE, 101 "_$fred = null;"); 102 103 replayControls(); 104 105 InjectStateWorker w = new InjectStateWorker(); 106 w.setApplicationStateManager(asm); 107 108 w.performEnhancement(op, spec); 109 110 verifyControls(); 111 } 112 } | Popular Tags |