001    // Copyright May 2, 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 org.apache.hivemind.Location;
017    import org.apache.hivemind.Registry;
018    import org.apache.hivemind.impl.RegistryBuilder;
019    import org.apache.hivemind.service.MethodSignature;
020    import org.apache.tapestry.spec.IComponentSpecification;
021    import static org.easymock.EasyMock.expect;
022    import org.testng.annotations.Test;
023    
024    import java.lang.reflect.Modifier;
025    import java.util.Collections;
026    
027    
028    /**
029     * 
030     * @author James Carman
031     *
032     */
033    @Test(sequential = true)
034    public class TestAutowireWorker extends BaseEnhancementTestCase
035    {
036        
037        private static final String HELLO_SERVICE_PROPERTY = "helloService";
038        
039        public void test_No_Service() throws Exception
040        {
041            assertNotAutowired( RegistryBuilder.constructDefaultRegistry() );
042        }
043        
044        public void test_Many_Services() throws Exception
045        {        
046            assertNotAutowired( buildFrameworkRegistry("autowire-multiple.xml" ) );   
047        }
048        
049        public void test_One_Service() throws Exception
050        {
051            final Registry registry = buildFrameworkRegistry("autowire-single.xml" );
052            Location l = newLocation();
053            EnhancementOperation op = newMock(EnhancementOperation.class);
054            
055            expect(op.findUnclaimedAbstractProperties()).andReturn(Collections.singletonList( HELLO_SERVICE_PROPERTY ));
056            expect(op.getPropertyType( HELLO_SERVICE_PROPERTY )).andReturn(HelloService.class);
057            expect(op.canClaimAsReadOnlyProperty(HELLO_SERVICE_PROPERTY)).andReturn(true);
058            
059            IComponentSpecification spec = newMock(IComponentSpecification.class);
060            
061            expect(spec.getLocation()).andReturn(l);
062            expect(spec.getDescription()).andReturn("Component1");
063            
064            final String fieldName = "_$" + HELLO_SERVICE_PROPERTY;
065            final HelloService proxy = ( HelloService )registry.getService( HelloService.class );
066            
067            expect(op.addInjectedField( fieldName, HelloService.class, proxy )).andReturn( fieldName );
068            expect(op.getAccessorMethodName( HELLO_SERVICE_PROPERTY )).andReturn("getHelloService");
069            
070            op.addMethod(Modifier.PUBLIC, new MethodSignature(HelloService.class, "getHelloService", null,
071                    null), "return " + fieldName + ";", l);
072            op.claimReadonlyProperty( HELLO_SERVICE_PROPERTY );
073            
074            replay();
075            
076            final EnhancementWorker worker = ( EnhancementWorker )registry.getService( "tapestry.enhance.AutowireWorker", EnhancementWorker.class );
077            worker.performEnhancement( op, spec );
078            
079            verify();
080        }
081    
082        private static final String GENERIC_PROPERTY = "genericService";
083    
084        public void test_Generic_Service()
085        throws Exception
086        {
087            final Registry registry = buildFrameworkRegistry("autowire-single.xml" );
088            Location l = newLocation();
089            EnhancementOperation op = newMock(EnhancementOperation.class);
090    
091            expect(op.findUnclaimedAbstractProperties()).andReturn(Collections.singletonList( GENERIC_PROPERTY ));
092            expect(op.getPropertyType( GENERIC_PROPERTY )).andReturn(SimpleGenericsInterface.class);
093            expect(op.canClaimAsReadOnlyProperty(GENERIC_PROPERTY)).andReturn(true);
094    
095            IComponentSpecification spec = newMock(IComponentSpecification.class);
096    
097            expect(spec.getLocation()).andReturn(l);
098            expect(spec.getDescription()).andReturn("Component1");
099    
100            final String fieldName = "_$" + GENERIC_PROPERTY;
101            final SimpleGenericsInterface proxy = ( SimpleGenericsInterface )registry.getService( SimpleGenericsInterface.class );
102    
103            expect(op.addInjectedField( fieldName, SimpleGenericsInterface.class, proxy )).andReturn( fieldName );
104            expect(op.getAccessorMethodName( GENERIC_PROPERTY )).andReturn("getGenericService");
105    
106            op.addMethod(Modifier.PUBLIC, new MethodSignature(SimpleGenericsInterface.class, "getGenericService", null,
107                    null), "return " + fieldName + ";", l);
108            op.claimReadonlyProperty( GENERIC_PROPERTY );
109    
110            replay();
111    
112            final EnhancementWorker worker = ( EnhancementWorker )registry.getService( "tapestry.enhance.AutowireWorker", EnhancementWorker.class );
113            worker.performEnhancement( op, spec );
114    
115            verify();
116        }
117    
118        private void assertNotAutowired( Registry registry )
119        {
120            EnhancementOperation op = newMock(EnhancementOperation.class);
121            
122            expect(op.findUnclaimedAbstractProperties())
123            .andReturn(Collections.singletonList( HELLO_SERVICE_PROPERTY ));
124            
125            expect(op.getPropertyType( HELLO_SERVICE_PROPERTY )).andReturn(HelloService.class);
126            
127            expect(op.canClaimAsReadOnlyProperty(HELLO_SERVICE_PROPERTY)).andReturn(true);
128            
129            replay();
130            
131            final EnhancementWorker worker = ( EnhancementWorker )registry.getService( "tapestry.enhance.AutowireWorker", EnhancementWorker.class );
132            
133            worker.performEnhancement( op, null);
134            
135            verify();
136        }
137        
138    }