1 15 package org.apache.tapestry.enhance; 16 17 import java.lang.reflect.Modifier ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.ErrorLog; 21 import org.apache.hivemind.Messages; 22 import org.apache.hivemind.service.BodyBuilder; 23 import org.apache.hivemind.test.HiveMindTestCase; 24 import org.apache.tapestry.BaseComponent; 25 import org.apache.tapestry.services.ComponentMessagesSource; 26 import org.apache.tapestry.spec.IComponentSpecification; 27 import org.easymock.MockControl; 28 29 35 public class TestInjectMessagesWorker extends HiveMindTestCase 36 { 37 private IComponentSpecification newSpec() 38 { 39 return (IComponentSpecification) newMock(IComponentSpecification.class); 40 } 41 42 private ComponentMessagesSource newSource() 43 { 44 return (ComponentMessagesSource) newMock(ComponentMessagesSource.class); 45 } 46 47 public void testSuccess() 48 { 49 50 InjectMessagesWorker w = new InjectMessagesWorker(); 51 52 IComponentSpecification spec = newSpec(); 53 ComponentMessagesSource source = newSource(); 54 55 MockControl control = newControl(EnhancementOperation.class); 56 EnhancementOperation op = (EnhancementOperation) control.getMock(); 57 58 op.claimProperty(w.MESSAGES_PROPERTY); 59 op.addInjectedField("_$componentMessagesSource", ComponentMessagesSource.class, source); 60 control.setReturnValue("fred"); 61 62 BodyBuilder builder = new BodyBuilder(); 63 builder.begin(); 64 builder.addln("if (_$messages == null)"); 65 builder.addln(" _$messages = fred.getMessages(this);"); 66 builder.addln("return _$messages;"); 67 builder.end(); 68 69 op.addField("_$messages", Messages.class); 70 op.addMethod(Modifier.PUBLIC, w.METHOD_SIGNATURE, builder.toString()); 71 72 replayControls(); 73 74 w.setComponentMessagesSource(source); 75 76 w.performEnhancement(op, spec); 77 78 verifyControls(); 79 } 80 81 public void testFailure() 82 { 83 84 InjectMessagesWorker w = new InjectMessagesWorker(); 85 86 Throwable ex = new ApplicationRuntimeException(EnhanceMessages 87 .claimedProperty(w.MESSAGES_PROPERTY)); 88 89 IComponentSpecification spec = newSpec(); 90 91 MockControl control = newControl(EnhancementOperation.class); 92 EnhancementOperation op = (EnhancementOperation) control.getMock(); 93 94 op.claimProperty(w.MESSAGES_PROPERTY); 95 control.setThrowable(ex); 96 97 op.getBaseClass(); 98 control.setReturnValue(BaseComponent.class); 99 100 ErrorLog log = (ErrorLog) newMock(ErrorLog.class); 101 102 log.error( 103 EnhanceMessages.errorAddingProperty(w.MESSAGES_PROPERTY, BaseComponent.class, ex), 104 null, 105 ex); 106 107 replayControls(); 108 109 w.setErrorLog(log); 110 111 w.performEnhancement(op, spec); 112 113 verifyControls(); 114 } 115 } | Popular Tags |