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 org.apache.hivemind.Location;
018    import org.apache.hivemind.Resource;
019    import org.apache.hivemind.service.MethodSignature;
020    import org.apache.tapestry.BaseComponentTestCase;
021    import org.apache.tapestry.IScript;
022    import org.apache.tapestry.engine.IScriptSource;
023    import org.apache.tapestry.spec.InjectSpecificationImpl;
024    import static org.easymock.EasyMock.*;
025    import org.testng.annotations.Test;
026    
027    import java.lang.reflect.Modifier;
028    
029    /**
030     * Tests for {@link org.apache.tapestry.enhance.InjectScriptWorker}.
031     * 
032     * @author Howard M. Lewis Ship
033     * @since 4.0
034     */
035    @Test
036    public class TestInjectScriptWorker extends BaseComponentTestCase
037    {
038        public void test_Simple_Inject()
039          throws Exception
040        {
041            EnhancementOperation op = newMock(EnhancementOperation.class);
042    
043            final Location injectSpecLocation = newLocation();
044            final IScriptSource source = newMock(IScriptSource.class);
045            Resource scriptResource = newResource();
046            
047            op.claimReadonlyProperty("foo");
048    
049            expect(op.getPropertyType("foo")).andReturn(IScript.class);
050            expect(op.getAccessorMethodName("foo")).andReturn("getFoo");
051            expect(injectSpecLocation.getResource()).andReturn(scriptResource);
052            
053            expect(scriptResource.getRelativeResource("bar.script")).andReturn(scriptResource);
054            expect(scriptResource.getResourceURL()).andReturn(getResource("autowire-single.xml").getResourceURL());
055            expect(op.addInjectedField(eq("_$script"), eq(DeferredScript.class), anyObject()))
056            .andReturn("_script");
057            
058            MethodSignature sig = new MethodSignature(IScript.class, "getFoo", null, null);
059    
060            op.addMethod(Modifier.PUBLIC, sig, "return _script.getScript();", injectSpecLocation);
061    
062            replay();
063    
064            InjectSpecificationImpl is = new InjectSpecificationImpl();
065            is.setProperty("foo");
066            is.setObject("bar.script");
067            is.setLocation(injectSpecLocation);
068    
069            InjectScriptWorker worker = new InjectScriptWorker();
070            worker.setSource(source);
071    
072            worker.performEnhancement(op, is);
073    
074            verify();
075        }
076    }