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.pageload;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.hivemind.Location;
021    import org.apache.tapestry.BaseComponentTestCase;
022    import org.apache.tapestry.IBinding;
023    import org.apache.tapestry.IComponent;
024    import org.apache.tapestry.spec.ComponentSpecification;
025    import org.apache.tapestry.spec.IComponentSpecification;
026    import org.apache.tapestry.spec.ParameterSpecification;
027    import org.testng.annotations.Test;
028    
029    /**
030     * Tests for {@link org.apache.tapestry.pageload.VerifyRequiredParametersVisitor}.
031     * 
032     * @author Howard M. Lewis Ship
033     * @since 4.0
034     */
035    @Test
036    public class TestVerifyRequiredParametersVisitor extends BaseComponentTestCase
037    {
038        private IComponent newComponent(IComponentSpecification spec)
039        {
040            IComponent component = newComponent();
041    
042            expect(component.getSpecification()).andReturn(spec);
043    
044            return component;
045        }
046    
047        public void testNotRequired()
048        {
049            ParameterSpecification pspec = new ParameterSpecification();
050            pspec.setParameterName("fred");
051    
052            ComponentSpecification cspec = new ComponentSpecification();
053            cspec.addParameter(pspec);
054    
055            IComponent component = newComponent(cspec);
056    
057            replay();
058    
059            VerifyRequiredParametersVisitor visitor = new VerifyRequiredParametersVisitor();
060    
061            visitor.visitComponent(component);
062    
063            verify();
064        }
065    
066        public void testRequiredWithAlias()
067        {
068            ParameterSpecification pspec = new ParameterSpecification();
069            pspec.setParameterName("fred");
070            pspec.setAliases("barney");
071            pspec.setRequired(true);
072    
073            ComponentSpecification cspec = new ComponentSpecification();
074            cspec.addParameter(pspec);
075    
076            IBinding fredBinding = newBinding();
077            IComponent component = newComponent(cspec);
078    
079            // Notice that we don't ever check for "barney", just
080            // "fred"
081    
082            expect(component.getBinding("fred")).andReturn(fredBinding);
083    
084            replay();
085    
086            VerifyRequiredParametersVisitor visitor = new VerifyRequiredParametersVisitor();
087    
088            visitor.visitComponent(component);
089    
090            verify();
091        }
092    
093        public void testRequiredNotBound()
094        {
095            ParameterSpecification pspec = new ParameterSpecification();
096            pspec.setParameterName("fred");
097            pspec.setRequired(true);
098    
099            ComponentSpecification cspec = new ComponentSpecification();
100            cspec.addParameter(pspec);
101    
102            Location l = newLocation();
103            
104            IComponent component = newComponent(cspec);
105    
106            expect(component.getBinding("fred")).andReturn(null);
107    
108            expect(component.getExtendedId()).andReturn("Fred/flintstone");
109    
110            expect(component.getLocation()).andReturn(l);
111    
112            replay();
113    
114            VerifyRequiredParametersVisitor visitor = new VerifyRequiredParametersVisitor();
115    
116            try
117            {
118                visitor.visitComponent(component);
119                unreachable();
120            }
121            catch (ApplicationRuntimeException ex)
122            {
123                assertEquals("Required parameter fred of component Fred/flintstone is not bound.", ex
124                        .getMessage());
125                assertSame(component, ex.getComponent());
126                assertSame(l, ex.getLocation());
127            }
128    
129            verify();
130        }
131    }