001    // Copyright 2004, 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.services.impl;
016    
017    import org.apache.hivemind.Location;
018    import org.apache.tapestry.BaseComponentTestCase;
019    import org.apache.tapestry.IBinding;
020    import org.apache.tapestry.IComponent;
021    import org.apache.tapestry.binding.BindingConstants;
022    import org.apache.tapestry.binding.BindingFactory;
023    import org.apache.tapestry.spec.IParameterSpecification;
024    import static org.easymock.EasyMock.expect;
025    import org.testng.annotations.Test;
026    
027    import java.util.Collections;
028    import java.util.HashMap;
029    import java.util.Map;
030    
031    /**
032     * Tests for {@link org.apache.tapestry.services.impl.BindingSourceImpl}.
033     * 
034     * @author Howard Lewis Ship
035     * @since 4.0
036     */
037    @Test
038    public class TestBindingSource extends BaseComponentTestCase
039    {
040        public void test_No_Prefix()
041        {
042            IComponent component = newComponent();
043            IBinding binding = newBinding();
044            BindingFactory factory = newFactory();
045            Location l = newLocation();
046    
047            BindingPrefixContribution c = new BindingPrefixContribution();
048            c.setPrefix(BindingConstants.LITERAL_PREFIX);
049            c.setFactory(factory);
050    
051            // Training
052    
053            expect(factory.createBinding(component, "foo", "a literal value without a prefix", l)).andReturn(binding);
054    
055            replay();
056    
057            BindingSourceImpl bs = new BindingSourceImpl();
058            bs.setContributions(Collections.singletonList(c));
059    
060            bs.initializeService();
061    
062            IBinding actual = bs.createBinding(
063                    component,
064                    "foo",
065                    "a literal value without a prefix",
066                    BindingConstants.LITERAL_PREFIX,
067                    l);
068    
069            assertSame(binding, actual);
070    
071            verify();
072        }
073    
074        public void test_No_Prefix_With_Default()
075        {
076            IComponent component = newComponent();
077            IBinding binding = newBinding();
078            BindingFactory factory = newFactory();
079            Location l = newLocation();
080    
081            // Training
082    
083            expect(factory.createBinding(component, "foo", "an-expression", l)).andReturn(binding);
084    
085            BindingPrefixContribution c = new BindingPrefixContribution();
086            c.setPrefix(BindingConstants.OGNL_PREFIX);
087            c.setFactory(factory);
088    
089            replay();
090    
091            BindingSourceImpl bs = new BindingSourceImpl();
092            bs.setContributions(Collections.singletonList(c));
093            bs.initializeService();
094    
095            IBinding actual = bs.createBinding(
096                    component,
097                    "foo",
098                    "an-expression",
099                    BindingConstants.OGNL_PREFIX,
100                    l);
101    
102            assertSame(binding, actual);
103    
104            verify();
105        }
106    
107        public void test_No_Prefix_ClientIdList_Binding()
108        {
109            IComponent component = newComponent();
110            IBinding binding = newBinding();
111            BindingFactory factory = newFactory();
112            Location l = newLocation();
113    
114            BindingPrefixContribution c = new BindingPrefixContribution();
115            c.setPrefix(BindingConstants.LITERAL_PREFIX);
116            c.setFactory(factory);
117    
118            BindingFactory idListFactory = newFactory();
119            Map propertyMap = new HashMap();
120            propertyMap.put("updateComponents", idListFactory);
121    
122            IParameterSpecification ps = newMock(IParameterSpecification.class);
123    
124            expect(ps.getParameterName()).andReturn("updateComponents").anyTimes();
125    
126            // Training
127    
128            expect(idListFactory.createBinding(component, "foo", "a literal value without a prefix", l)).andReturn(binding);
129    
130            replay();
131    
132            BindingSourceImpl bs = new BindingSourceImpl();
133            bs.setContributions(Collections.singletonList(c));
134            bs.setPropertyContributions(propertyMap);
135    
136            bs.initializeService();
137    
138            IBinding actual = bs.createBinding(
139                    component,
140                    ps,
141                    "foo",
142                    "a literal value without a prefix",
143                    BindingConstants.LITERAL_PREFIX,
144                    l);
145    
146            assertSame(binding, actual);
147    
148            verify();
149        }
150    
151        public void test_Known_Prefix()
152        {
153            IComponent component = newComponent();
154            IBinding binding = newBinding();
155            BindingFactory factory = newFactory();
156            Location l = newLocation();
157    
158            // Training
159    
160            expect(factory.createBinding(component, "bar", "path part of locator", l)).andReturn(binding);
161    
162            BindingPrefixContribution c = new BindingPrefixContribution();
163            c.setPrefix("prefix");
164            c.setFactory(factory);
165    
166            replay();
167    
168            BindingSourceImpl bs = new BindingSourceImpl();
169            bs.setContributions(Collections.singletonList(c));
170    
171            bs.initializeService();
172    
173            IBinding actual = bs.createBinding(
174                    component,
175                    "bar",
176                    "prefix:path part of locator",
177                    BindingConstants.LITERAL_PREFIX,
178                    l);
179    
180            assertSame(binding, actual);
181    
182            verify();
183        }
184    
185        public void test_Prefix_No_Match()
186        {
187            IComponent component = newComponent();
188            IBinding binding = newBinding();
189            BindingFactory factory = newFactory();
190            Location l = newLocation();
191    
192            // Training
193    
194            expect(factory.createBinding(component, "zip", "unknown:path part of locator", l)).andReturn(binding);
195    
196            BindingPrefixContribution c = new BindingPrefixContribution();
197            c.setPrefix(BindingConstants.LITERAL_PREFIX);
198            c.setFactory(factory);
199    
200            replay();
201    
202            BindingSourceImpl bs = new BindingSourceImpl();
203            bs.setContributions(Collections.singletonList(c));
204    
205            bs.initializeService();
206    
207            IBinding actual = bs.createBinding(
208                    component,
209                    "zip",
210                    "unknown:path part of locator",
211                    BindingConstants.LITERAL_PREFIX,
212                    l);
213    
214            assertSame(binding, actual);
215    
216            verify();
217        }
218    
219        public void test_Single_Character_Prefix()
220        {
221            IComponent component = newComponent();
222            IBinding binding = newBinding();
223            BindingFactory factory = newFactory();
224            Location l = newLocation();
225    
226            // Training
227    
228            expect(factory.createBinding(component, "bar", "path part of locator", l)).andReturn(binding);
229    
230            BindingPrefixContribution c = new BindingPrefixContribution();
231            c.setPrefix("c");
232            c.setFactory(factory);
233    
234            replay();
235    
236            BindingSourceImpl bs = new BindingSourceImpl();
237            bs.setContributions(Collections.singletonList(c));
238    
239            bs.initializeService();
240    
241            IBinding actual = bs.createBinding(
242                    component,
243                    "bar",
244                    "c:path part of locator",
245                    "c",
246                    l);
247    
248            assertSame(binding, actual);
249    
250            verify();
251        }
252    
253        public void test_Empty_Character_Prefix()
254        {
255            IComponent component = newComponent();
256            IBinding binding = newBinding();
257            BindingFactory factory = newFactory();
258            Location l = newLocation();
259    
260            // Training
261    
262            expect(factory.createBinding(component, "bar", "path part of locator", l)).andReturn(binding);
263    
264            BindingPrefixContribution c = new BindingPrefixContribution();
265            c.setPrefix("");
266            c.setFactory(factory);
267    
268            replay();
269    
270            BindingSourceImpl bs = new BindingSourceImpl();
271            bs.setContributions(Collections.singletonList(c));
272    
273            bs.initializeService();
274    
275            IBinding actual = bs.createBinding(
276                    component,
277                    "bar",
278                    ":path part of locator",
279                    "",
280                    l);
281    
282            assertSame(binding, actual);
283    
284            verify();
285        }
286    
287        protected BindingFactory newFactory()
288        {
289            return newMock(BindingFactory.class);
290        }
291    }