001    // Copyright May 21, 2006 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    package org.apache.tapestry.enhance;
015    
016    import static org.easymock.EasyMock.expect;
017    import static org.easymock.EasyMock.expectLastCall;
018    
019    import java.lang.reflect.Modifier;
020    
021    import org.apache.hivemind.ApplicationRuntimeException;
022    import org.apache.hivemind.ErrorLog;
023    import org.apache.hivemind.Location;
024    import org.apache.hivemind.service.MethodSignature;
025    import org.apache.tapestry.BaseComponent;
026    import org.apache.tapestry.internal.event.IComponentEventInvoker;
027    import org.apache.tapestry.internal.event.impl.ComponentEventInvoker;
028    import org.apache.tapestry.spec.IComponentSpecification;
029    import org.testng.annotations.Test;
030    
031    
032    /**
033     * Test functionality of {@link InjectEventInvokerWorker} enhancer.
034     * 
035     * @author jkuhnert
036     */
037    @Test
038    public class TestInjectEventInvokerWorker extends BaseEnhancementTestCase
039    {
040    
041        public void testSuccess() throws Exception
042        {
043            EnhancementOperation op = newOp();
044            IComponentEventInvoker invoker = new ComponentEventInvoker();
045            
046            Location l = newLocation();
047            IComponentSpecification spec = newSpec(l);
048            
049            op.claimReadonlyProperty("eventInvoker");
050            
051            expect(op.addInjectedField("_$eventInvoker", IComponentEventInvoker.class, invoker))
052            .andReturn("_$eventInvoker");
053            
054            expect(op.getAccessorMethodName("eventInvoker")).andReturn("getEventInvoker");
055            
056            op.addMethod(Modifier.PUBLIC, new MethodSignature(IComponentEventInvoker.class,
057                    "getEventInvoker", null, null), "return _$eventInvoker;", l);
058            
059            replay();
060            
061            InjectEventInvokerWorker worker = new InjectEventInvokerWorker();
062            worker.setComponentEventInvoker(invoker);
063            worker.performEnhancement(op, spec);
064            
065            verify();
066        }
067        
068        public void testFailure()
069        {
070            Location l = newLocation();
071            
072            EnhancementOperation op = newOp();
073            IComponentEventInvoker invoker = new ComponentEventInvoker();
074            
075            ErrorLog log = newMock(ErrorLog.class);
076            
077            Throwable ex = new ApplicationRuntimeException(EnhanceMessages
078                    .claimedProperty("eventInvoker"));
079            
080            IComponentSpecification spec = newSpec();
081    
082            op.claimReadonlyProperty("eventInvoker");
083            expectLastCall().andThrow(ex);
084    
085            expect(op.getBaseClass()).andReturn(BaseComponent.class);
086            
087            expect(spec.getLocation()).andReturn(l);
088    
089            log.error(
090                    EnhanceMessages.errorAddingProperty("eventInvoker", BaseComponent.class, ex),
091                    l,
092                    ex);
093    
094            replay();
095    
096            InjectEventInvokerWorker worker = new InjectEventInvokerWorker();
097            worker.setComponentEventInvoker(invoker);
098            worker.setErrorLog(log);
099            
100            worker.performEnhancement(op, spec);
101    
102            verify();
103        }
104        
105    }