1 15 package org.apache.tapestry.junit; 16 17 import java.util.Calendar ; 18 import java.util.Date ; 19 import java.util.GregorianCalendar ; 20 import java.util.Locale ; 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 53 54 public class TestComponentMessages extends TapestryTestCase 55 { 56 private static class NullComponentPropertySource implements ComponentPropertySource 57 { 58 59 public String getComponentProperty(IComponent component, String propertyName) 60 { 61 return null; 62 } 63 64 public String getLocalizedComponentProperty(IComponent component, Locale locale, 65 String propertyName) 66 { 67 return null; 68 } 69 70 public String getNamespaceProperty(INamespace namespace, String propertyName) 71 { 72 return null; 73 } 74 75 public String getLocalizedNamespaceProperty(INamespace namespace, Locale locale, 76 String propertyName) 77 { 78 return null; 79 } 80 } 81 82 private void check(Messages messages, String key, String expected) 83 { 84 String actual = messages.getMessage(key); 85 86 assertEquals("Key " + key, expected, actual); 87 88 } 89 90 private static final String MOCK1 = "/org/apache/tapestry/junit/MockPage1.page"; 91 92 private IComponentSpecification newSpec(String 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 118 119 private IPage newPage(IComponentSpecification specification, ComponentMessagesSource source, Locale 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 location, Locale 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 ("en", "US")); 160 161 check(messages, "only-in-base", "BASE1"); 162 } 163 164 165 public void testOnlyInNamespace() 166 { 167 Messages messages = createMessages(MOCK1, new Locale ("en", "US")); 168 169 check(messages, "only-in-namespace", "LIBRARY_BASE.only-in-namespace"); 170 } 171 172 173 public void testLocalizedInNamespace() 174 { 175 Messages messages = createMessages(MOCK1, new Locale ("fr")); 176 177 check(messages, "localized-in-namespace", "LIBRARY_FR.localized-in-namespace"); 178 } 179 180 181 public void testComponentOverridesNamespace() 182 { 183 Messages messages = createMessages(MOCK1, new Locale ("en", "US")); 184 185 check( 186 messages, 187 "component-overrides-namespace", 188 "MOCKPAGE1_BASE.component-overrides-namespace"); 189 } 190 191 192 public void testLocalizedComponentOverridesLocalizedNamespace() 193 { 194 Messages messages = createMessages(MOCK1, new Locale ("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 ("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 ("en", "US")); 212 213 check(messages, "overwritten-in-language", "LANGUAGE1_en"); 214 215 messages = createMessages(MOCK1, new Locale ("fr", "")); 216 217 check(messages, "overwritten-in-language", "LANGUAGE1_fr"); 218 } 219 220 public void testOverwrittenInCountry() 221 { 222 Messages messages = createMessages(MOCK1, new Locale ("en", "US")); 223 224 check(messages, "overwritten-in-country", "COUNTRY1_en_US"); 225 226 messages = createMessages(MOCK1, new Locale ("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 ("en", "US", "Tapestry")); 234 235 check(messages, "overwritten-in-variant", "VARIANT1_en_US_Tapestry"); 236 237 messages = createMessages(MOCK1, new Locale ("fr", "CD", "Foo")); 238 239 check(messages, "overwritten-in-variant", "VARIANT1_fr_CD_Foo"); 240 } 241 242 private static final String MOCK2 = "/org/apache/tapestry/junit/MockPage2.page"; 243 244 248 249 public void testMissingBase() 250 { 251 Messages messages = createMessages(MOCK2, new Locale ("en", "US")); 252 253 check(messages, "language-key", "LANGUAGE1"); 254 } 255 256 260 261 public void testMissingCountry() 262 { 263 Messages messages = createMessages(MOCK2, new Locale ("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 c = new GregorianCalendar (1966, Calendar.DECEMBER, 24); 273 274 Date 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 c = new GregorianCalendar (1966, Calendar.DECEMBER, 24); 284 285 Date d = c.getTime(); 286 287 289 assertEquals("A formatted date: 24/12/66", messages.format("using-date-format", d)); 290 291 } 292 } | Popular Tags |