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.service.MethodSignature;
026    import org.apache.tapestry.BaseComponent;
027    import org.apache.tapestry.spec.IComponentSpecification;
028    import org.testng.annotations.Test;
029    
030    /**
031     * Tests for {@link org.apache.tapestry.enhance.InjectSpecificationWorker}.
032     * 
033     * @author Howard M. Lewis Ship
034     * @since 4.0
035     */
036    @Test
037    public class TestInjectSpecificationWorker extends BaseEnhancementTestCase
038    {
039    
040        public void testSuccess() throws Exception
041        {
042            Location l = newLocation();
043            
044            EnhancementOperation op = newOp();
045    
046            IComponentSpecification spec = newSpec(l);
047    
048            op.claimReadonlyProperty("specification");
049    
050            expect(op.addInjectedField("_$specification", IComponentSpecification.class, spec))
051            .andReturn("_$specification");
052    
053            expect(op.getAccessorMethodName("specification")).andReturn("getSpecification");
054    
055            op.addMethod(Modifier.PUBLIC, new MethodSignature(IComponentSpecification.class,
056                    "getSpecification", null, null), "return _$specification;", l);
057    
058            replay();
059    
060            new InjectSpecificationWorker().performEnhancement(op, spec);
061    
062            verify();
063        }
064    
065        public void testFailure()
066        {
067            Location l = newLocation();
068            EnhancementOperation op = newOp();
069    
070            ErrorLog log = newMock(ErrorLog.class);
071    
072            Throwable ex = new ApplicationRuntimeException(EnhanceMessages
073                    .claimedProperty("specification"));
074            
075            IComponentSpecification spec = newSpec();
076    
077            op.claimReadonlyProperty("specification");
078            expectLastCall().andThrow(ex);
079    
080            expect(op.getBaseClass()).andReturn(BaseComponent.class);
081    
082            expect(spec.getLocation()).andReturn(l);
083    
084            log.error(
085                    EnhanceMessages.errorAddingProperty("specification", BaseComponent.class, ex),
086                    l,
087                    ex);
088    
089            replay();
090    
091            InjectSpecificationWorker w = new InjectSpecificationWorker();
092            w.setErrorLog(log);
093    
094            w.performEnhancement(op, spec);
095    
096            verify();
097        }
098    }