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.spec;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.Location;
019    import org.apache.tapestry.BaseComponentTestCase;
020    import org.apache.tapestry.internal.event.ComponentEventProperty;
021    import org.apache.tapestry.internal.event.EventBoundListener;
022    import org.testng.annotations.Test;
023    
024    /**
025     * Tests a number of <em>failure</em> cases in
026     * {@link org.apache.tapestry.spec.ComponentSpecification}. Success cases are covered by
027     * {@link org.apache.tapestry.junit.parse.TestSpecificationParser} and the (ugly and slow) mock
028     * integration tests.
029     * 
030     * @author Howard M. Lewis Ship
031     * @since 4.0
032     */
033    @Test
034    public class TestComponentSpecification extends BaseComponentTestCase
035    {
036        public void test_Claim_Property_OK()
037        {
038            InjectSpecificationImpl inject1 = new InjectSpecificationImpl();
039            inject1.setProperty("fred");
040    
041            InjectSpecificationImpl inject2 = new InjectSpecificationImpl();
042            inject2.setProperty("barney");
043    
044            ComponentSpecification cs = new ComponentSpecification();
045    
046            cs.addInjectSpecification(inject1);
047            cs.addInjectSpecification(inject2);
048    
049            assertEquals(2, cs.getInjectSpecifications().size());
050        }
051    
052        public void test_Claim_Property_Conflict()
053        {
054            Location l1 = fabricateLocation(13);
055            Location l2 = fabricateLocation(97);
056    
057            InjectSpecificationImpl inject1 = new InjectSpecificationImpl();
058            inject1.setProperty("fred");
059            inject1.setLocation(l1);
060    
061            InjectSpecificationImpl inject2 = new InjectSpecificationImpl();
062            inject2.setProperty("fred");
063            inject2.setLocation(l2);
064    
065            ComponentSpecification cs = new ComponentSpecification();
066    
067            cs.addInjectSpecification(inject1);
068    
069            try
070            {
071                cs.addInjectSpecification(inject2);
072                unreachable();
073            }
074            catch (ApplicationRuntimeException ex)
075            {
076                assertEquals(SpecMessages.claimedProperty("fred", inject1), ex.getMessage());
077                assertSame(l2, ex.getLocation());
078            }
079    
080            assertEquals(1, cs.getInjectSpecifications().size());
081        }
082    
083        public void test_Add_Asset_Conflict()
084        {
085            Location l1 = fabricateLocation(13);
086            Location l2 = fabricateLocation(97);
087    
088            AssetSpecification asset1 = new AssetSpecification();
089            asset1.setLocation(l1);
090    
091            AssetSpecification asset2 = new AssetSpecification();
092            asset2.setLocation(l2);
093    
094            ComponentSpecification cs = new ComponentSpecification();
095    
096            cs.addAsset("wilma", asset1);
097            try
098            {
099                cs.addAsset("wilma", asset2);
100                unreachable();
101            }
102            catch (ApplicationRuntimeException ex)
103            {
104                assertEquals(SpecMessages.duplicateAsset("wilma", asset1), ex.getMessage());
105                assertSame(l2, ex.getLocation());
106            }
107        }
108    
109        public void test_Add_Component_Conflict()
110        {
111            Location l1 = fabricateLocation(13);
112            Location l2 = fabricateLocation(97);
113    
114            ContainedComponent cc1 = new ContainedComponent();
115            cc1.setLocation(l1);
116    
117            ContainedComponent cc2 = new ContainedComponent();
118            cc2.setLocation(l2);
119    
120            ComponentSpecification cs = new ComponentSpecification();
121    
122            cs.addComponent("fred", cc1);
123            try
124            {
125                cs.addComponent("fred", cc2);
126                unreachable();
127            }
128            catch (ApplicationRuntimeException ex)
129            {
130                assertEquals(SpecMessages.duplicateComponent("fred", cc1), ex.getMessage());
131                assertSame(l2, ex.getLocation());
132            }
133        }
134    
135        public void test_Add_Parameter_Conflict()
136        {
137            Location l1 = fabricateLocation(13);
138            Location l2 = fabricateLocation(97);
139    
140            ParameterSpecification p1 = new ParameterSpecification();
141            p1.setParameterName("dino");
142            p1.setLocation(l1);
143    
144            ParameterSpecification p2 = new ParameterSpecification();
145            p2.setParameterName("dino");
146            p2.setLocation(l2);
147    
148            ComponentSpecification cs = new ComponentSpecification();
149    
150            cs.addParameter(p1);
151    
152            try
153            {
154                cs.addParameter(p2);
155                unreachable();
156            }
157            catch (ApplicationRuntimeException ex)
158            {
159                assertEquals(SpecMessages.duplicateParameter("dino", p1), ex.getMessage());
160                assertSame(l2, ex.getLocation());
161            }
162        }
163    
164        public void test_Add_Bean_Specification_Conflict()
165        {
166            Location l1 = fabricateLocation(13);
167            Location l2 = fabricateLocation(97);
168    
169            BeanSpecification b1 = new BeanSpecification();
170            b1.setLocation(l1);
171    
172            BeanSpecification b2 = new BeanSpecification();
173            b2.setLocation(l2);
174    
175            ComponentSpecification cs = new ComponentSpecification();
176    
177            cs.addBeanSpecification("wilma", b1);
178    
179            try
180            {
181                cs.addBeanSpecification("wilma", b2);
182                unreachable();
183            }
184            catch (ApplicationRuntimeException ex)
185            {
186                assertEquals(SpecMessages.duplicateBean("wilma", b1), ex.getMessage());
187                assertSame(l2, ex.getLocation());
188            }
189        }
190    
191        public void test_Get_Required_Parameters_None()
192        {
193            ComponentSpecification cs = new ComponentSpecification();
194    
195            assertTrue(cs.getRequiredParameters().isEmpty());
196        }
197    
198        public void test_Get_Required_Parameters_Optional()
199        {
200            ComponentSpecification cs = new ComponentSpecification();
201    
202            ParameterSpecification ps = new ParameterSpecification();
203            ps.setParameterName("fred");
204            ps.setAliases("barney");
205    
206            cs.addParameter(ps);
207    
208            assertTrue(cs.getRequiredParameters().isEmpty());
209        }
210    
211        public void test_Get_Required_Parameters_Filters_Aliases()
212        {
213            ComponentSpecification cs = new ComponentSpecification();
214    
215            ParameterSpecification ps = new ParameterSpecification();
216            ps.setParameterName("fred");
217            ps.setAliases("barney");
218            ps.setRequired(true);
219    
220            cs.addParameter(ps);
221    
222            assertListEquals(new Object[] { ps }, cs.getRequiredParameters().toArray());
223        }
224    
225        public void test_Rewire_Component_Id()
226        {
227            ComponentSpecification cs = new ComponentSpecification();
228            cs.addEventListener("comp", new String[] {"foo"}, "doFoo", null, false, true, false, false);
229    
230            assertEquals(cs.getComponentEvents().size(), 1);
231            assert cs.getComponentEvents("comp") != null;
232    
233            cs.rewireComponentId("unknown", "new/path", "id/path");
234    
235            assertEquals(cs.getComponentEvents().size(), 1);
236    
237            cs.rewireComponentId("comp", "page/comp", "id/path");
238            cs.rewireComponentId("comp", "page/comp", "id/path");
239    
240            assertEquals(cs.getComponentEvents().size(), 2);
241            assert cs.getComponentEvents("comp") != null;
242            assert cs.getComponentEvents("page/comp") != null;
243    
244            ComponentEventProperty prop = cs.getComponentEvents("comp");
245            assertEquals(prop.getComponentId(), "comp");
246            assertEquals(prop.getEvents().size(), 1);
247            EventBoundListener listener = (EventBoundListener)prop.getEventListeners("foo").get(0);
248            assertEquals(listener.getComponentId(), "comp");
249    
250            prop = cs.getComponentEvents("page/comp");
251            assertEquals(prop.getComponentId(), "page/comp");
252            assertEquals(prop.getEvents().size(), 1);
253            listener = (EventBoundListener)prop.getEventListeners("foo").get(0);
254            assertEquals(listener.getComponentId(), "page/comp");
255        }
256    }