KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > spec > TestComponentSpecification


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.spec;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.Location;
19 import org.apache.hivemind.test.HiveMindTestCase;
20
21 /**
22  * Tests a number of <em>failure</em> cases in
23  * {@link org.apache.tapestry.spec.ComponentSpecification}. Success cases are covered by
24  * {@link org.apache.tapestry.junit.parse.TestSpecificationParser} and the (ugly and slow) mock
25  * integration tests.
26  *
27  * @author Howard M. Lewis Ship
28  * @since 4.0
29  */

30 public class TestComponentSpecification extends HiveMindTestCase
31 {
32     public void testClaimPropertyOK()
33     {
34         InjectSpecificationImpl inject1 = new InjectSpecificationImpl();
35         inject1.setProperty("fred");
36
37         InjectSpecificationImpl inject2 = new InjectSpecificationImpl();
38         inject2.setProperty("barney");
39
40         ComponentSpecification cs = new ComponentSpecification();
41
42         cs.addInjectSpecification(inject1);
43         cs.addInjectSpecification(inject2);
44
45         assertEquals(2, cs.getInjectSpecifications().size());
46     }
47
48     public void testClaimPropertyConflict()
49     {
50         Location l1 = fabricateLocation(13);
51         Location l2 = fabricateLocation(97);
52
53         InjectSpecificationImpl inject1 = new InjectSpecificationImpl();
54         inject1.setProperty("fred");
55         inject1.setLocation(l1);
56
57         InjectSpecificationImpl inject2 = new InjectSpecificationImpl();
58         inject2.setProperty("fred");
59         inject2.setLocation(l2);
60
61         ComponentSpecification cs = new ComponentSpecification();
62
63         cs.addInjectSpecification(inject1);
64
65         try
66         {
67             cs.addInjectSpecification(inject2);
68             unreachable();
69         }
70         catch (ApplicationRuntimeException ex)
71         {
72             assertEquals(SpecMessages.claimedProperty("fred", inject1), ex.getMessage());
73             assertSame(l2, ex.getLocation());
74         }
75
76         assertEquals(1, cs.getInjectSpecifications().size());
77     }
78
79     public void testAddAssetConflict()
80     {
81         Location l1 = fabricateLocation(13);
82         Location l2 = fabricateLocation(97);
83
84         AssetSpecification asset1 = new AssetSpecification();
85         asset1.setLocation(l1);
86
87         AssetSpecification asset2 = new AssetSpecification();
88         asset2.setLocation(l2);
89
90         ComponentSpecification cs = new ComponentSpecification();
91
92         cs.addAsset("wilma", asset1);
93         try
94         {
95             cs.addAsset("wilma", asset2);
96             unreachable();
97         }
98         catch (ApplicationRuntimeException ex)
99         {
100             assertEquals(SpecMessages.duplicateAsset("wilma", asset1), ex.getMessage());
101             assertSame(l2, ex.getLocation());
102         }
103     }
104
105     public void testAddComponentConflict()
106     {
107         Location l1 = fabricateLocation(13);
108         Location l2 = fabricateLocation(97);
109
110         ContainedComponent cc1 = new ContainedComponent();
111         cc1.setLocation(l1);
112
113         ContainedComponent cc2 = new ContainedComponent();
114         cc2.setLocation(l2);
115
116         ComponentSpecification cs = new ComponentSpecification();
117
118         cs.addComponent("fred", cc1);
119         try
120         {
121             cs.addComponent("fred", cc2);
122             unreachable();
123         }
124         catch (ApplicationRuntimeException ex)
125         {
126             assertEquals(SpecMessages.duplicateComponent("fred", cc1), ex.getMessage());
127             assertSame(l2, ex.getLocation());
128         }
129     }
130
131     public void testAddParameterConflict()
132     {
133         Location l1 = fabricateLocation(13);
134         Location l2 = fabricateLocation(97);
135
136         ParameterSpecification p1 = new ParameterSpecification();
137         p1.setParameterName("dino");
138         p1.setLocation(l1);
139
140         ParameterSpecification p2 = new ParameterSpecification();
141         p2.setParameterName("dino");
142         p2.setLocation(l2);
143
144         ComponentSpecification cs = new ComponentSpecification();
145
146         cs.addParameter(p1);
147
148         try
149         {
150             cs.addParameter(p2);
151             unreachable();
152         }
153         catch (ApplicationRuntimeException ex)
154         {
155             assertEquals(SpecMessages.duplicateParameter("dino", p1), ex.getMessage());
156             assertSame(l2, ex.getLocation());
157         }
158     }
159
160     public void testAddBeanSpecificationConflict()
161     {
162         Location l1 = fabricateLocation(13);
163         Location l2 = fabricateLocation(97);
164
165         BeanSpecification b1 = new BeanSpecification();
166         b1.setLocation(l1);
167
168         BeanSpecification b2 = new BeanSpecification();
169         b2.setLocation(l2);
170
171         ComponentSpecification cs = new ComponentSpecification();
172
173         cs.addBeanSpecification("wilma", b1);
174
175         try
176         {
177             cs.addBeanSpecification("wilma", b2);
178             unreachable();
179         }
180         catch (ApplicationRuntimeException ex)
181         {
182             assertEquals(SpecMessages.duplicateBean("wilma", b1), ex.getMessage());
183             assertSame(l2, ex.getLocation());
184         }
185     }
186
187     public void testGetRequiredParametersNone()
188     {
189         ComponentSpecification cs = new ComponentSpecification();
190
191         assertTrue(cs.getRequiredParameters().isEmpty());
192     }
193
194     public void testGetRequiredParametersOptional()
195     {
196         ComponentSpecification cs = new ComponentSpecification();
197
198         ParameterSpecification ps = new ParameterSpecification();
199         ps.setParameterName("fred");
200         ps.setAliases("barney");
201
202         cs.addParameter(ps);
203
204         assertTrue(cs.getRequiredParameters().isEmpty());
205     }
206
207     public void testGetRequiredParametersFiltersAliases()
208     {
209         ComponentSpecification cs = new ComponentSpecification();
210
211         ParameterSpecification ps = new ParameterSpecification();
212         ps.setParameterName("fred");
213         ps.setAliases("barney");
214         ps.setRequired(true);
215
216         cs.addParameter(ps);
217
218         assertListsEqual(new Object JavaDoc[]
219         { ps }, cs.getRequiredParameters().toArray());
220     }
221 }
222
Popular Tags