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.annotations;
016    
017    import java.lang.reflect.Method;
018    import java.util.HashMap;
019    import java.util.List;
020    
021    import org.apache.hivemind.Location;
022    import org.apache.tapestry.bean.LightweightBeanInitializer;
023    import org.apache.tapestry.enhance.EnhancementOperation;
024    import org.apache.tapestry.spec.BeanLifecycle;
025    import org.apache.tapestry.spec.ComponentSpecification;
026    import org.apache.tapestry.spec.IBeanSpecification;
027    import org.apache.tapestry.spec.IComponentSpecification;
028    import static org.easymock.EasyMock.expect;
029    import org.testng.annotations.Test;
030    
031    /**
032     * Tests for {@link org.apache.tapestry.annotations.BeanAnnotationWorker}.
033     * 
034     * @author Howard M. Lewis Ship
035     * @since 4.0
036     */
037    @Test
038    public class TestBeanAnnotationWorker extends BaseAnnotationTestCase
039    {
040        public void testBeanClassSpecified()
041        {
042            Location l = newLocation();
043            EnhancementOperation op = newOp();
044            IComponentSpecification spec = new ComponentSpecification();
045    
046            Method m = findMethod(AnnotatedPage.class, "getMapBean");
047    
048            replay();
049    
050            new BeanAnnotationWorker().performEnhancement(op, spec, m, l);
051    
052            verify();
053    
054            IBeanSpecification bs = spec.getBeanSpecification("mapBean");
055    
056            assertEquals("mapBean", bs.getPropertyName());
057            assertEquals(HashMap.class.getName(), bs.getClassName());
058            assertEquals(BeanLifecycle.REQUEST, bs.getLifecycle());
059            assertSame(l, bs.getLocation());
060            assertNull(bs.getInitializers());
061        }
062    
063        private EnhancementOperation newOp(String propertyName, Class propertyType)
064        {
065            EnhancementOperation op = newMock(EnhancementOperation.class);
066            
067            expect(op.getPropertyType(propertyName)).andReturn(propertyType);
068    
069            return op;
070        }
071    
072        public void testBeanClassNotSpecified()
073        {
074            Location l = newLocation();
075            EnhancementOperation op = newOp("hashMapBean", HashMap.class);
076            IComponentSpecification spec = new ComponentSpecification();
077    
078            Method m = findMethod(AnnotatedPage.class, "getHashMapBean");
079    
080            replay();
081    
082            new BeanAnnotationWorker().performEnhancement(op, spec, m, l);
083    
084            verify();
085    
086            IBeanSpecification bs = spec.getBeanSpecification("hashMapBean");
087    
088            assertEquals("hashMapBean", bs.getPropertyName());
089            assertEquals(HashMap.class.getName(), bs.getClassName());
090            assertEquals(BeanLifecycle.REQUEST, bs.getLifecycle());
091            assertSame(l, bs.getLocation());
092            assertNull(bs.getInitializers());
093        }
094    
095        public void testInitializer()
096        {
097            EnhancementOperation op = newOp("beanWithInitializer", TargetValues.class);
098            IComponentSpecification spec = new ComponentSpecification();
099    
100            Method m = findMethod(AnnotatedPage.class, "getBeanWithInitializer");
101    
102            replay();
103    
104            new BeanAnnotationWorker().performEnhancement(op, spec, m, null);
105    
106            verify();
107    
108            IBeanSpecification bs = spec.getBeanSpecification("beanWithInitializer");
109    
110            List l = bs.getInitializers();
111            LightweightBeanInitializer lbi = (LightweightBeanInitializer) l.get(0);
112    
113            assertEquals("intValue=10", lbi.getPropertyName());
114        }
115    
116        public void testLifecycle()
117        {
118            EnhancementOperation op = newOp();
119            IComponentSpecification spec = new ComponentSpecification();
120    
121            Method m = findMethod(AnnotatedPage.class, "getRenderLifecycleBean");
122    
123            replay();
124    
125            new BeanAnnotationWorker().performEnhancement(op, spec, m, null);
126    
127            verify();
128    
129            IBeanSpecification bs = spec.getBeanSpecification("renderLifecycleBean");
130    
131            assertEquals(BeanLifecycle.RENDER, bs.getLifecycle());
132        }
133    }