KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > junit > TestComponentMessages


1 // Copyright 2004, 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.junit;
16
17 import java.util.Calendar JavaDoc;
18 import java.util.Date JavaDoc;
19 import java.util.GregorianCalendar JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import org.apache.hivemind.Messages;
23 import org.apache.hivemind.Resource;
24 import org.apache.hivemind.impl.DefaultClassResolver;
25 import org.apache.hivemind.service.ClassFactory;
26 import org.apache.hivemind.service.impl.ClassFactoryImpl;
27 import org.apache.hivemind.util.ClasspathResource;
28 import org.apache.tapestry.IComponent;
29 import org.apache.tapestry.INamespace;
30 import org.apache.tapestry.IPage;
31 import org.apache.tapestry.engine.Namespace;
32 import org.apache.tapestry.enhance.EnhancementOperationImpl;
33 import org.apache.tapestry.enhance.InjectMessagesWorker;
34 import org.apache.tapestry.enhance.InjectSpecificationWorker;
35 import org.apache.tapestry.html.BasePage;
36 import org.apache.tapestry.services.ComponentMessagesSource;
37 import org.apache.tapestry.services.ComponentPropertySource;
38 import org.apache.tapestry.services.impl.ComponentMessagesSourceImpl;
39 import org.apache.tapestry.spec.ComponentSpecification;
40 import org.apache.tapestry.spec.IComponentSpecification;
41 import org.apache.tapestry.spec.ILibrarySpecification;
42 import org.apache.tapestry.spec.LibrarySpecification;
43
44 /**
45  * Tests the class {@link org.apache.tapestry.services.impl.ComponentMessagesSourceImpl}.
46  * <p>
47  * TODO: Add tests realted to messages encoding (which can be specified as meta-data on the
48  * component specification or, ultimately, in the namespace (library specification).
49  *
50  * @author Howard Lewis Ship
51  * @since 2.0.4
52  */

53
54 public class TestComponentMessages extends TapestryTestCase
55 {
56     private static class NullComponentPropertySource implements ComponentPropertySource
57     {
58
59         public String JavaDoc getComponentProperty(IComponent component, String JavaDoc propertyName)
60         {
61             return null;
62         }
63
64         public String JavaDoc getLocalizedComponentProperty(IComponent component, Locale JavaDoc locale,
65                 String JavaDoc propertyName)
66         {
67             return null;
68         }
69
70         public String JavaDoc getNamespaceProperty(INamespace namespace, String JavaDoc propertyName)
71         {
72             return null;
73         }
74
75         public String JavaDoc getLocalizedNamespaceProperty(INamespace namespace, Locale JavaDoc locale,
76                 String JavaDoc propertyName)
77         {
78             return null;
79         }
80     }
81
82     private void check(Messages messages, String JavaDoc key, String JavaDoc expected)
83     {
84         String JavaDoc actual = messages.getMessage(key);
85
86         assertEquals("Key " + key, expected, actual);
87
88     }
89
90     private static final String JavaDoc MOCK1 = "/org/apache/tapestry/junit/MockPage1.page";
91
92     private IComponentSpecification newSpec(String JavaDoc path)
93     {
94         Resource resource = new ClasspathResource(new DefaultClassResolver(), path);
95
96         IComponentSpecification spec = new ComponentSpecification();
97         spec.setSpecificationLocation(resource);
98
99         return spec;
100     }
101
102     private ILibrarySpecification newLibrarySpec()
103     {
104         Resource resource = new ClasspathResource(new DefaultClassResolver(),
105                 "/org/apache/tapestry/junit/Library.library");
106
107         ILibrarySpecification spec = new LibrarySpecification();
108         spec.setSpecificationLocation(resource);
109
110         return spec;
111     }
112
113     /**
114      * Mocking up the page is too hard ... the relationship between the component messagess source
115      * and the page is too varied and complex. Instead, we use the tools to create the page itself,
116      * much as the full framework would do at runtime.
117      */

118
119     private IPage newPage(IComponentSpecification specification, ComponentMessagesSource source, Locale JavaDoc locale)
120     {
121         ClassFactory classFactory = new ClassFactoryImpl();
122
123         EnhancementOperationImpl op = new EnhancementOperationImpl(new DefaultClassResolver(),
124                 specification, BasePage.class, classFactory);
125
126         InjectMessagesWorker injectMessages = new InjectMessagesWorker();
127         injectMessages.setComponentMessagesSource(source);
128         
129         injectMessages.performEnhancement(op, specification);
130         
131         new InjectSpecificationWorker().performEnhancement(op, specification);
132             
133         IPage result = (IPage) op.getConstructor().newInstance();
134
135         result.setLocale(locale);
136         result.setPage(result);
137
138         return result;
139     }
140
141     private Messages createMessages(String JavaDoc location, Locale JavaDoc locale)
142     {
143         ComponentMessagesSourceImpl source = new ComponentMessagesSourceImpl();
144         source.setComponentPropertySource(new NullComponentPropertySource());
145
146         IComponentSpecification spec = newSpec(location);
147
148         IPage page = newPage(spec, source, locale);
149
150         INamespace namespace = new Namespace(null, null, newLibrarySpec(), null, null);
151
152         page.setNamespace(namespace);
153
154         return source.getMessages(page);
155     }
156
157     public void testOnlyInBase()
158     {
159         Messages messages = createMessages(MOCK1, new Locale JavaDoc("en", "US"));
160
161         check(messages, "only-in-base", "BASE1");
162     }
163
164     /** @since 4.0 */
165     public void testOnlyInNamespace()
166     {
167         Messages messages = createMessages(MOCK1, new Locale JavaDoc("en", "US"));
168
169         check(messages, "only-in-namespace", "LIBRARY_BASE.only-in-namespace");
170     }
171
172     /** @since 4.0 */
173     public void testLocalizedInNamespace()
174     {
175         Messages messages = createMessages(MOCK1, new Locale JavaDoc("fr"));
176
177         check(messages, "localized-in-namespace", "LIBRARY_FR.localized-in-namespace");
178     }
179
180     /** @since 4.0 */
181     public void testComponentOverridesNamespace()
182     {
183         Messages messages = createMessages(MOCK1, new Locale JavaDoc("en", "US"));
184
185         check(
186                 messages,
187                 "component-overrides-namespace",
188                 "MOCKPAGE1_BASE.component-overrides-namespace");
189     }
190
191     /** @since 4.0 */
192     public void testLocalizedComponentOverridesLocalizedNamespace()
193     {
194         Messages messages = createMessages(MOCK1, new Locale JavaDoc("fr"));
195
196         check(
197                 messages,
198                 "localized-component-overrides-namespace",
199                 "MOCKPAGE1_FR.localized-component-overrides-namespace");
200     }
201
202     public void testMissingKey()
203     {
204         Messages messages = createMessages(MOCK1, new Locale JavaDoc("en", "GB"));
205
206         check(messages, "non-existant-key", "[NON-EXISTANT-KEY]");
207     }
208
209     public void testOverwrittenInLanguage()
210     {
211         Messages messages = createMessages(MOCK1, new Locale JavaDoc("en", "US"));
212
213         check(messages, "overwritten-in-language", "LANGUAGE1_en");
214
215         messages = createMessages(MOCK1, new Locale JavaDoc("fr", ""));
216
217         check(messages, "overwritten-in-language", "LANGUAGE1_fr");
218     }
219
220     public void testOverwrittenInCountry()
221     {
222         Messages messages = createMessages(MOCK1, new Locale JavaDoc("en", "US"));
223
224         check(messages, "overwritten-in-country", "COUNTRY1_en_US");
225
226         messages = createMessages(MOCK1, new Locale JavaDoc("fr", "CD"));
227
228         check(messages, "overwritten-in-country", "COUNTRY1_fr_CD");
229     }
230
231     public void testOverwrittenInVariant()
232     {
233         Messages messages = createMessages(MOCK1, new Locale JavaDoc("en", "US", "Tapestry"));
234
235         check(messages, "overwritten-in-variant", "VARIANT1_en_US_Tapestry");
236
237         messages = createMessages(MOCK1, new Locale JavaDoc("fr", "CD", "Foo"));
238
239         check(messages, "overwritten-in-variant", "VARIANT1_fr_CD_Foo");
240     }
241
242     private static final String JavaDoc MOCK2 = "/org/apache/tapestry/junit/MockPage2.page";
243
244     /**
245      * Tests that the code that locates properties files can deal with the base path (i.e.,
246      * Foo.properties) doesn't exist.
247      */

248
249     public void testMissingBase()
250     {
251         Messages messages = createMessages(MOCK2, new Locale JavaDoc("en", "US"));
252
253         check(messages, "language-key", "LANGUAGE1");
254     }
255
256     /**
257      * Tests that naming and search works correctly for locales that specify language and variant,
258      * but no country.
259      */

260
261     public void testMissingCountry()
262     {
263         Messages messages = createMessages(MOCK2, new Locale JavaDoc("en", "", "Tapestry"));
264
265         check(messages, "overwritten-in-variant", "VARIANT1_en__Tapestry");
266     }
267
268     public void testDateFormatting()
269     {
270         Messages messages = createMessages(MOCK1, Locale.ENGLISH);
271
272         Calendar JavaDoc c = new GregorianCalendar JavaDoc(1966, Calendar.DECEMBER, 24);
273
274         Date JavaDoc d = c.getTime();
275
276         assertEquals("A formatted date: 12/24/66", messages.format("using-date-format", d));
277     }
278
279     public void testDateFormatLocalization()
280     {
281         Messages messages = createMessages(MOCK1, Locale.FRENCH);
282
283         Calendar JavaDoc c = new GregorianCalendar JavaDoc(1966, Calendar.DECEMBER, 24);
284
285         Date JavaDoc d = c.getTime();
286
287         // French formatting puts the day before the month.
288

289         assertEquals("A formatted date: 24/12/66", messages.format("using-date-format", d));
290
291     }
292 }
Popular Tags