KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > pageload > TestPageLoader


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.pageload;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.hivemind.ApplicationRuntimeException;
19 import org.apache.hivemind.Location;
20 import org.apache.hivemind.test.HiveMindTestCase;
21 import org.apache.tapestry.IBinding;
22 import org.apache.tapestry.IComponent;
23 import org.apache.tapestry.binding.BindingSource;
24 import org.apache.tapestry.spec.BindingSpecification;
25 import org.apache.tapestry.spec.BindingType;
26 import org.apache.tapestry.spec.ComponentSpecification;
27 import org.apache.tapestry.spec.ContainedComponent;
28 import org.apache.tapestry.spec.IComponentSpecification;
29 import org.apache.tapestry.spec.ParameterSpecification;
30 import org.easymock.MockControl;
31
32 /**
33  * Additional tests for {@link org.apache.tapestry.pageload.PageLoader}. Ultimately, testing this
34  * beast without the mock unit test suites is going to take a lot of work and refactoring.
35  *
36  * @author Howard M. Lewis Ship
37  * @since 4.0
38  */

39 public class TestPageLoader extends HiveMindTestCase
40 {
41     public IComponent newComponent(IComponentSpecification spec)
42     {
43         MockControl control = newControl(IComponent.class);
44         IComponent component = (IComponent) control.getMock();
45
46         component.getSpecification();
47         control.setReturnValue(spec);
48
49         return component;
50     }
51
52     private IBinding newBinding()
53     {
54         return (IBinding) newMock(IBinding.class);
55     }
56
57     private IBinding newBinding(Location l)
58     {
59         MockControl control = newControl(IBinding.class);
60         IBinding binding = (IBinding) control.getMock();
61
62         binding.getLocation();
63         control.setReturnValue(l);
64
65         return binding;
66     }
67
68     public void testAddDuplicateBindingFails()
69     {
70         MockControl componentc = newControl(IComponent.class);
71         IComponent component = (IComponent) componentc.getMock();
72
73         Location l1 = newLocation();
74         Location l2 = newLocation();
75
76         IBinding oldBinding = newBinding(l1);
77         IBinding newBinding = newBinding(l2);
78
79         component.getBinding("dupe");
80         componentc.setReturnValue(oldBinding);
81
82         replayControls();
83
84         try
85         {
86             PageLoader.addBindingToComponent(component, "dupe", newBinding);
87             unreachable();
88         }
89         catch (ApplicationRuntimeException ex)
90         {
91             assertEquals(
92                     "A binding for parameter dupe conflicts with a previous binding (at classpath:/org/apache/tapestry/pageload/TestPageLoader, line 1).",
93                     ex.getMessage());
94             assertSame(component, ex.getComponent());
95             assertSame(l2, ex.getLocation());
96         }
97     }
98
99     public void testBindAlias()
100     {
101         MockControl containerc = newControl(IComponent.class);
102         IComponent container = (IComponent) containerc.getMock();
103
104         MockControl componentc = newControl(IComponent.class);
105         IComponent component = (IComponent) componentc.getMock();
106
107         ParameterSpecification pspec = new ParameterSpecification();
108         pspec.setParameterName("fred");
109         pspec.setAliases("barney");
110
111         Location l = newLocation();
112
113         BindingSpecification bspec = new BindingSpecification();
114         bspec.setType(BindingType.PREFIXED);
115         bspec.setValue("an-expression");
116         bspec.setLocation(l);
117
118         ContainedComponent contained = new ContainedComponent();
119         contained.setBinding("barney", bspec);
120         contained.setType("FredComponent");
121
122         IComponentSpecification spec = new ComponentSpecification();
123         spec.addParameter(pspec);
124
125         component.getSpecification();
126         componentc.setReturnValue(spec);
127
128         Log log = (Log) newMock(Log.class);
129
130         log
131                 .error("Parameter barney (for component FredComponent, at classpath:/org/apache/tapestry/pageload/TestPageLoader, line 1) was bound; this parameter has been deprecated, bind parameter fred instead.");
132
133         IBinding binding = newBinding();
134         MockControl sourcec = newControl(BindingSource.class);
135         BindingSource source = (BindingSource) sourcec.getMock();
136
137         source.createBinding(container, "parameter barney", "an-expression", "ognl", l);
138         sourcec.setReturnValue(binding);
139
140         component.getBinding("fred");
141         componentc.setReturnValue(null);
142
143         component.setBinding("fred", binding);
144
145         replayControls();
146
147         PageLoader loader = new PageLoader();
148         loader.setLog(log);
149         loader.setBindingSource(source);
150
151         loader.bind(container, component, contained);
152
153         verifyControls();
154     }
155
156     public void testBindDeprecated()
157     {
158         MockControl containerc = newControl(IComponent.class);
159         IComponent container = (IComponent) containerc.getMock();
160
161         MockControl componentc = newControl(IComponent.class);
162         IComponent component = (IComponent) componentc.getMock();
163
164         ParameterSpecification pspec = new ParameterSpecification();
165         pspec.setParameterName("fred");
166         pspec.setDeprecated(true);
167
168         Location l = newLocation();
169
170         BindingSpecification bspec = new BindingSpecification();
171         bspec.setType(BindingType.PREFIXED);
172         bspec.setValue("an-expression");
173         bspec.setLocation(l);
174
175         ContainedComponent contained = new ContainedComponent();
176         contained.setBinding("fred", bspec);
177         contained.setType("FredComponent");
178
179         IComponentSpecification spec = new ComponentSpecification();
180         spec.addParameter(pspec);
181
182         component.getSpecification();
183         componentc.setReturnValue(spec);
184
185         Log log = (Log) newMock(Log.class);
186
187         log
188                 .error("Parameter fred (at classpath:/org/apache/tapestry/pageload/TestPageLoader, line 1) has been deprecated, "
189                         + "and may be removed in a future release. Consult the documentation for component FredComponent to "
190                         + "determine an appropriate replacement.");
191
192         IBinding binding = newBinding();
193         MockControl sourcec = newControl(BindingSource.class);
194         BindingSource source = (BindingSource) sourcec.getMock();
195
196         source.createBinding(container, "parameter fred", "an-expression", "ognl", l);
197         sourcec.setReturnValue(binding);
198
199         component.getBinding("fred");
200         componentc.setReturnValue(null);
201
202         component.setBinding("fred", binding);
203
204         replayControls();
205
206         PageLoader loader = new PageLoader();
207         loader.setLog(log);
208         loader.setBindingSource(source);
209
210         loader.bind(container, component, contained);
211
212         verifyControls();
213     }
214 }
215
Popular Tags