001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.enhance; 016 017 import static org.easymock.EasyMock.expect; 018 import static org.easymock.EasyMock.expectLastCall; 019 020 import java.lang.reflect.Modifier; 021 022 import org.apache.hivemind.ApplicationRuntimeException; 023 import org.apache.hivemind.ErrorLog; 024 import org.apache.hivemind.Location; 025 import org.apache.hivemind.Messages; 026 import org.apache.hivemind.service.BodyBuilder; 027 import org.apache.tapestry.BaseComponent; 028 import org.apache.tapestry.services.ComponentMessagesSource; 029 import org.apache.tapestry.spec.IComponentSpecification; 030 import org.testng.annotations.Test; 031 032 /** 033 * Tests for {@link org.apache.tapestry.enhance.InjectMessagesWorker}. 034 * 035 * @author Howard M. Lewis Ship 036 * @since 4.0 037 */ 038 @Test 039 public class TestInjectMessagesWorker extends BaseEnhancementTestCase 040 { 041 private ComponentMessagesSource newSource() 042 { 043 return newMock(ComponentMessagesSource.class); 044 } 045 046 public void testSuccess() 047 { 048 Location l = newLocation(); 049 InjectMessagesWorker w = new InjectMessagesWorker(); 050 051 IComponentSpecification spec = newSpec(l); 052 ComponentMessagesSource source = newSource(); 053 EnhancementOperation op = newOp(); 054 055 op.claimReadonlyProperty(w._messagesProperty); 056 expect(op.addInjectedField("_$componentMessagesSource", ComponentMessagesSource.class, source)) 057 .andReturn("fred"); 058 059 BodyBuilder builder = new BodyBuilder(); 060 builder.begin(); 061 builder.addln("if (_$messages == null)"); 062 builder.addln(" _$messages = fred.getMessages(this);"); 063 builder.addln("return _$messages;"); 064 builder.end(); 065 066 op.addField("_$messages", Messages.class); 067 op.addMethod(Modifier.PUBLIC, w._methodSignature, builder.toString(), l); 068 069 replay(); 070 071 w.setComponentMessagesSource(source); 072 073 w.performEnhancement(op, spec); 074 075 verify(); 076 } 077 078 public void testFailure() 079 { 080 Location l = newLocation(); 081 InjectMessagesWorker w = new InjectMessagesWorker(); 082 083 Throwable ex = new ApplicationRuntimeException(EnhanceMessages 084 .claimedProperty(w._messagesProperty)); 085 086 IComponentSpecification spec = newSpec(l); 087 EnhancementOperation op = newOp(); 088 089 op.claimReadonlyProperty(w._messagesProperty); 090 expectLastCall().andThrow(ex); 091 092 expect(op.getBaseClass()).andReturn(BaseComponent.class); 093 094 ErrorLog log = newMock(ErrorLog.class); 095 096 log.error( 097 EnhanceMessages.errorAddingProperty(w._messagesProperty, BaseComponent.class, ex), 098 l, 099 ex); 100 101 replay(); 102 103 w.setErrorLog(log); 104 105 w.performEnhancement(op, spec); 106 107 verify(); 108 } 109 }