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.ApplicationRuntimeException;
018    import org.apache.hivemind.Location;
019    import org.apache.hivemind.service.MethodSignature;
020    import org.apache.tapestry.BaseComponentTestCase;
021    import org.apache.tapestry.html.BasePage;
022    import org.apache.tapestry.spec.InjectSpecification;
023    import org.apache.tapestry.spec.InjectSpecificationImpl;
024    import static org.easymock.EasyMock.expect;
025    import org.testng.annotations.Test;
026    
027    import java.lang.reflect.Modifier;
028    
029    /**
030     * Tests for {@link org.apache.tapestry.enhance.InjectPageWorker}.
031     * 
032     * @author Howard Lewis Ship
033     * @since 4.0
034     */
035    @Test(sequential=true)
036    public class TestInjectPageWorker extends BaseComponentTestCase
037    {
038        public void testPrimitivePropertyType()
039        {
040            Location l = newLocation();
041            
042            EnhancementOperation op = newMock(EnhancementOperation.class);
043    
044            expect(op.getPropertyType("somePage")).andReturn(int.class);
045    
046            replay();
047    
048            InjectSpecification is = newSpec(l);
049    
050            try
051            {
052                new InjectPageWorker().performEnhancement(op, is);
053                unreachable();
054            }
055            catch (ApplicationRuntimeException ex)
056            {
057                assertEquals(
058                        "Property somePage is type int, which is not compatible with injection. The property type should be Object, IPage, or a specific page class.",
059                        ex.getMessage());
060                assertSame(l, ex.getLocation());
061            }
062    
063            verify();
064        }
065    
066        private InjectSpecification newSpec(Location l)
067        {
068            InjectSpecification is = new InjectSpecificationImpl();
069            is.setProperty("somePage");
070            is.setObject("SomePage");
071            is.setLocation(l);
072    
073            return is;
074        }
075    
076        /**
077         * Test for when there's no existing property.
078         */
079    
080        public void testNoPropertyType()
081        {
082            Location l = newLocation();
083    
084            EnhancementOperation op = newMock(EnhancementOperation.class);
085    
086            expect(op.getPropertyType("somePage")).andReturn(null);
087    
088            op.claimReadonlyProperty("somePage");
089    
090            expect(op.getAccessorMethodName("somePage")).andReturn("getSomePage");
091    
092            MethodSignature sig = new MethodSignature(Object.class, "getSomePage", null, null);
093    
094            op.addMethod(
095                    Modifier.PUBLIC,
096                    sig,
097                    "return getPage().getRequestCycle().getPage(\"SomePage\");",
098                    l);
099    
100            replay();
101    
102            InjectSpecification is = newSpec(l);
103    
104            new InjectPageWorker().performEnhancement(op, is);
105    
106            verify();
107        }
108    
109        public void testExistingPropertyType()
110        {
111            Location l = newLocation();
112    
113            EnhancementOperation op = newMock(EnhancementOperation.class);
114    
115            expect(op.getPropertyType("somePage")).andReturn(BasePage.class);
116    
117            op.claimReadonlyProperty("somePage");
118    
119            expect(op.getAccessorMethodName("somePage")).andReturn("getSomePage");
120    
121            MethodSignature sig = new MethodSignature(BasePage.class, "getSomePage", null, null);
122    
123            // BasePage is too specific (the getPage() method returns IPage),
124            // so we see a cast.
125    
126            op
127                    .addMethod(
128                            Modifier.PUBLIC,
129                            sig,
130                            "return (org.apache.tapestry.html.BasePage)getPage().getRequestCycle().getPage(\"SomePage\");",
131                            l);
132    
133            replay();
134    
135            InjectSpecification is = newSpec(l);
136    
137            new InjectPageWorker().performEnhancement(op, is);
138    
139            verify();
140        }
141    }