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 org.apache.commons.logging.Log;
018    import org.apache.hivemind.ApplicationRuntimeException;
019    import org.apache.hivemind.Location;
020    import org.apache.tapestry.BaseComponentTestCase;
021    import org.apache.tapestry.IBinding;
022    import org.apache.tapestry.IComponent;
023    import org.apache.tapestry.binding.BindingSource;
024    import org.apache.tapestry.services.ComponentPropertySource;
025    import org.apache.tapestry.spec.*;
026    import static org.easymock.EasyMock.*;
027    import org.testng.annotations.Test;
028    
029    /**
030     * Additional tests for {@link org.apache.tapestry.pageload.PageLoader}. Ultimately, testing this
031     * beast without the mock unit test suites is going to take a lot of work and refactoring.
032     * 
033     * @author Howard M. Lewis Ship
034     * @since 4.0
035     */
036    @Test
037    public class PageLoaderTest extends BaseComponentTestCase
038    {
039    
040        public void test_Add_Duplicate_Binding_Fails()
041        {
042            IComponent component = newComponent();
043            Location l1 = newLocation();
044            Location l2 = newLocation();
045            IBinding oldBinding = newBinding(l1);
046            IBinding newBinding = newBinding(l2);
047    
048            trainGetBinding(component, "dupe", oldBinding);
049    
050            replay();
051    
052            try
053            {
054                PageLoader.addBindingToComponent(component, "dupe", newBinding);
055                unreachable();
056            }
057            catch (ApplicationRuntimeException ex)
058            {
059                assert ex.getMessage()
060                .indexOf("A binding for parameter dupe conflicts with a previous binding") > -1;
061                assertSame(component, ex.getComponent());
062                assertSame(l2, ex.getLocation());
063            }
064        }
065    
066        public void test_Bind_Alias()
067        {
068            IComponent container = newComponent();
069            IComponent component = newComponent();
070            Log log = newLog();
071            IBinding binding = newBinding();
072            BindingSource source = newBindingSource();
073    
074            ParameterSpecification pspec = new ParameterSpecification();
075            pspec.setParameterName("fred");
076            pspec.setAliases("barney");
077    
078            Location l = newLocation();
079    
080            BindingSpecification bspec = new BindingSpecification();
081            bspec.setType(BindingType.PREFIXED);
082            bspec.setValue("an-expression");
083            bspec.setLocation(l);
084    
085            ContainedComponent contained = new ContainedComponent();
086            contained.setBinding("barney", bspec);
087            contained.setType("FredComponent");
088    
089            IComponentSpecification spec = new ComponentSpecification();
090            spec.addParameter(pspec);
091    
092            trainGetSpecification(component, spec);
093    
094            log.warn(startsWith("Parameter barney (for component FredComponent, at "));
095    
096            trainCreateBinding(
097                    source,
098                    container,
099                    pspec,
100                    "parameter barney",
101                    "an-expression",
102                    "ognl",
103                    l,
104                    binding);
105    
106            trainGetBinding(component, "fred", null);
107    
108            component.setBinding("fred", binding);
109    
110            replay();
111    
112            PageLoader loader = new PageLoader();
113            loader.setLog(log);
114            loader.setBindingSource(source);
115    
116            loader.bind(container, component, contained, "ognl");
117    
118            verify();
119        }
120    
121        private void trainCreateBinding(BindingSource source, IComponent container, IParameterSpecification ps, String description,
122                String expression, String defaultBindingPrefix, Location l, IBinding binding)
123        {
124            expect(source.createBinding(container, ps, description, expression, defaultBindingPrefix, l)).andReturn(binding);
125        }
126    
127        protected BindingSource newBindingSource()
128        {
129            return newMock(BindingSource.class);
130        }
131    
132        public void test_Bind_Deprecated()
133        {
134            IComponent container = newComponent();
135            IComponent component = newComponent();
136            IBinding binding = newBinding();
137            BindingSource source = newBindingSource();
138            Log log = newLog();
139    
140            ParameterSpecification pspec = new ParameterSpecification();
141            pspec.setParameterName("fred");
142            pspec.setDeprecated(true);
143    
144            Location l = newLocation();
145    
146            BindingSpecification bspec = new BindingSpecification();
147            bspec.setType(BindingType.PREFIXED);
148            bspec.setValue("an-expression");
149            bspec.setLocation(l);
150    
151            ContainedComponent contained = new ContainedComponent();
152            contained.setBinding("fred", bspec);
153            contained.setType("FredComponent");
154    
155            IComponentSpecification spec = new ComponentSpecification();
156            spec.addParameter(pspec);
157    
158            trainGetSpecification(component, spec);
159    
160            log.warn(endsWith("has been deprecated, "
161                            + "and may be removed in a future release. Consult the documentation for component FredComponent to "
162                            + "determine an appropriate replacement."));
163    
164            trainCreateBinding(source, container, pspec, "parameter fred", "an-expression", "ognl", l, binding);
165    
166            trainGetBinding(component, "fred", null);
167    
168            component.setBinding("fred", binding);
169    
170            replay();
171    
172            PageLoader loader = new PageLoader();
173            loader.setLog(log);
174            loader.setBindingSource(source);
175    
176            loader.bind(container, component, contained, "ognl");
177    
178            verify();
179        }
180    
181        protected ComponentPropertySource newPropertySource()
182        {
183            return newMock(ComponentPropertySource.class);
184        }
185    }