1 16 17 package org.springframework.context.support; 18 19 import java.util.Locale ; 20 import java.util.Properties ; 21 22 import junit.framework.TestCase; 23 24 import org.springframework.beans.MutablePropertyValues; 25 import org.springframework.context.MessageSourceResolvable; 26 import org.springframework.context.NoSuchMessageException; 27 import org.springframework.context.i18n.LocaleContextHolder; 28 import org.springframework.core.JdkVersion; 29 30 34 public class ResourceBundleMessageSourceTests extends TestCase { 35 36 protected void doTestMessageAccess( 37 boolean reloadable, boolean fallbackToSystemLocale, 38 boolean expectGermanFallback, boolean useCodeAsDefaultMessage, boolean alwaysUseMessageFormat) { 39 40 StaticApplicationContext ac = new StaticApplicationContext(); 41 if (reloadable) { 42 StaticApplicationContext parent = new StaticApplicationContext(); 43 parent.refresh(); 44 ac.setParent(parent); 45 } 46 47 MutablePropertyValues pvs = new MutablePropertyValues(); 48 String basepath = "org/springframework/context/support/"; 49 String [] basenames = null; 50 if (reloadable) { 51 basenames = new String [] { 52 "classpath:" + basepath + "messages", 53 "classpath:" + basepath + "more-messages"}; 54 } 55 else { 56 basenames = new String [] { 57 basepath + "messages", 58 basepath + "more-messages"}; 59 } 60 pvs.addPropertyValue("basenames", basenames); 61 if (!fallbackToSystemLocale) { 62 pvs.addPropertyValue("fallbackToSystemLocale", Boolean.FALSE); 63 } 64 if (useCodeAsDefaultMessage) { 65 pvs.addPropertyValue("useCodeAsDefaultMessage", Boolean.TRUE); 66 } 67 if (alwaysUseMessageFormat) { 68 pvs.addPropertyValue("alwaysUseMessageFormat", Boolean.TRUE); 69 } 70 Class clazz = reloadable ? 71 (Class ) ReloadableResourceBundleMessageSource.class : ResourceBundleMessageSource.class; 72 ac.registerSingleton("messageSource", clazz, pvs); 73 ac.refresh(); 74 75 expectGermanFallback = expectGermanFallback && (JdkVersion.getMajorJavaVersion() > JdkVersion.JAVA_13); 76 77 Locale.setDefault(expectGermanFallback ? Locale.GERMAN : Locale.CANADA); 78 assertEquals("message1", ac.getMessage("code1", null, Locale.ENGLISH)); 79 assertEquals(fallbackToSystemLocale && expectGermanFallback ? "nachricht2" : "message2", 80 ac.getMessage("code2", null, Locale.ENGLISH)); 81 82 assertEquals("nachricht2", ac.getMessage("code2", null, Locale.GERMAN)); 83 assertEquals("nochricht2", ac.getMessage("code2", null, new Locale ("DE", "at"))); 84 assertEquals("noochricht2", ac.getMessage("code2", null, new Locale ("DE", "at", "oo"))); 85 86 if (reloadable && JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_15) { 87 assertEquals("nachricht2xml", ac.getMessage("code2", null, Locale.GERMANY)); 88 } 89 90 MessageSourceAccessor accessor = new MessageSourceAccessor(ac); 91 LocaleContextHolder.setLocale(new Locale ("DE", "at")); 92 try { 93 assertEquals("nochricht2", accessor.getMessage("code2")); 94 } 95 finally { 96 LocaleContextHolder.setLocale(null); 97 } 98 99 assertEquals("message3", ac.getMessage("code3", null, Locale.ENGLISH)); 100 MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String [] {"code4", "code3"}); 101 assertEquals("message3", ac.getMessage(resolvable, Locale.ENGLISH)); 102 103 assertEquals("message3", ac.getMessage("code3", null, Locale.ENGLISH)); 104 resolvable = new DefaultMessageSourceResolvable(new String [] {"code4", "code3"}); 105 assertEquals("message3", ac.getMessage(resolvable, Locale.ENGLISH)); 106 107 Object [] args = new Object [] {"Hello", new DefaultMessageSourceResolvable(new String [] {"code1"})}; 108 assertEquals("Hello, message1", ac.getMessage("hello", args, Locale.ENGLISH)); 109 110 assertEquals("default", ac.getMessage(null, null, "default", Locale.ENGLISH)); 112 assertEquals("default", ac.getMessage(null, args, "default", Locale.ENGLISH)); 113 assertEquals("{0}, default", ac.getMessage(null, null, "{0}, default", Locale.ENGLISH)); 114 assertEquals("Hello, default", ac.getMessage(null, args, "{0}, default", Locale.ENGLISH)); 115 116 resolvable = new DefaultMessageSourceResolvable(null, null, "default"); 118 assertEquals("default", ac.getMessage(resolvable, Locale.ENGLISH)); 119 resolvable = new DefaultMessageSourceResolvable(null, args, "default"); 120 assertEquals("default", ac.getMessage(resolvable, Locale.ENGLISH)); 121 resolvable = new DefaultMessageSourceResolvable(null, null, "{0}, default"); 122 assertEquals("{0}, default", ac.getMessage(resolvable, Locale.ENGLISH)); 123 resolvable = new DefaultMessageSourceResolvable(null, args, "{0}, default"); 124 assertEquals("Hello, default", ac.getMessage(resolvable, Locale.ENGLISH)); 125 126 assertEquals("Arg1, Arg2", ac.getMessage("hello", new Object [] {"Arg1", "Arg2"}, Locale.ENGLISH)); 128 assertEquals("{0}, {1}", ac.getMessage("hello", null, Locale.ENGLISH)); 129 130 if (alwaysUseMessageFormat) { 131 assertEquals("I'm", ac.getMessage("escaped", null, Locale.ENGLISH)); 132 } 133 else { 134 assertEquals("I''m", ac.getMessage("escaped", null, Locale.ENGLISH)); 135 } 136 assertEquals("I'm", ac.getMessage("escaped", new Object [] {"some arg"}, Locale.ENGLISH)); 137 138 try { 139 assertEquals("code4", ac.getMessage("code4", null, Locale.GERMAN)); 140 if (!useCodeAsDefaultMessage) { 141 fail("Should have thrown NoSuchMessageException"); 142 } 143 } 144 catch (NoSuchMessageException ex) { 145 if (useCodeAsDefaultMessage) { 146 fail("Should have returned code as default message"); 147 } 148 } 149 } 150 151 public void testMessageAccessWithDefaultMessageSource() { 152 doTestMessageAccess(false, true, false, false, false); 153 } 154 155 public void testMessageAccessWithDefaultMessageSourceAndMessageFormat() { 156 doTestMessageAccess(false, true, false, false, true); 157 } 158 159 public void testMessageAccessWithDefaultMessageSourceAndFallbackToGerman() { 160 doTestMessageAccess(false, true, true, true, false); 161 } 162 163 public void testMessageAccessWithReloadableMessageSource() { 164 doTestMessageAccess(true, true, false, false, false); 165 } 166 167 public void testMessageAccessWithReloadableMessageSourceAndMessageFormat() { 168 doTestMessageAccess(true, true, false, false, true); 169 } 170 171 public void testMessageAccessWithReloadableMessageSourceAndFallbackToGerman() { 172 doTestMessageAccess(true, true, true, true, false); 173 } 174 175 public void testMessageAccessWithReloadableMessageSourceAndFallbackTurnedOff() { 176 doTestMessageAccess(true, false, false, false, false); 177 } 178 179 public void testMessageAccessWithReloadableMessageSourceAndFallbackTurnedOffAndFallbackToGerman() { 180 doTestMessageAccess(true, false, true, true, false); 181 } 182 183 public void testResourceBundleMessageSourceStandalone() { 184 ResourceBundleMessageSource ms = new ResourceBundleMessageSource(); 185 ms.setBasename("org/springframework/context/support/messages"); 186 assertEquals("message1", ms.getMessage("code1", null, Locale.ENGLISH)); 187 assertEquals("nachricht2", ms.getMessage("code2", null, Locale.GERMAN)); 188 } 189 190 public void testReloadableResourceBundleMessageSourceStandalone() { 191 ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource(); 192 ms.setBasename("org/springframework/context/support/messages"); 193 assertEquals("message1", ms.getMessage("code1", null, Locale.ENGLISH)); 194 assertEquals("nachricht2", ms.getMessage("code2", null, Locale.GERMAN)); 195 } 196 197 public void testReloadableResourceBundleMessageSourceWithDefaultCharset() { 198 ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource(); 199 ms.setBasename("org/springframework/context/support/messages"); 200 ms.setDefaultEncoding("ISO-8859-1"); 201 assertEquals("message1", ms.getMessage("code1", null, Locale.ENGLISH)); 202 assertEquals("nachricht2", ms.getMessage("code2", null, Locale.GERMAN)); 203 } 204 205 public void testReloadableResourceBundleMessageSourceWithInappropriateDefaultCharset() { 206 ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource(); 207 ms.setBasename("org/springframework/context/support/messages"); 208 ms.setDefaultEncoding("unicode"); 209 Properties fileCharsets = new Properties (); 210 fileCharsets.setProperty("org/springframework/context/support/messages_de", "unicode"); 211 ms.setFileEncodings(fileCharsets); 212 ms.setFallbackToSystemLocale(false); 213 try { 214 ms.getMessage("code1", null, Locale.ENGLISH); 215 fail("Should have thrown NoSuchMessageException"); 216 } 217 catch (NoSuchMessageException ex) { 218 } 220 } 221 222 public void testReloadableResourceBundleMessageSourceWithInappropriateEnglishCharset() { 223 ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource(); 224 ms.setBasename("org/springframework/context/support/messages"); 225 ms.setFallbackToSystemLocale(false); 226 Properties fileCharsets = new Properties (); 227 fileCharsets.setProperty("org/springframework/context/support/messages", "unicode"); 228 ms.setFileEncodings(fileCharsets); 229 try { 230 ms.getMessage("code1", null, Locale.ENGLISH); 231 fail("Should have thrown NoSuchMessageException"); 232 } 233 catch (NoSuchMessageException ex) { 234 } 236 } 237 238 public void testReloadableResourceBundleMessageSourceWithInappropriateGermanCharset() { 239 ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource(); 240 ms.setBasename("org/springframework/context/support/messages"); 241 ms.setFallbackToSystemLocale(false); 242 Properties fileCharsets = new Properties (); 243 fileCharsets.setProperty("org/springframework/context/support/messages_de", "unicode"); 244 ms.setFileEncodings(fileCharsets); 245 assertEquals("message1", ms.getMessage("code1", null, Locale.ENGLISH)); 246 assertEquals("message2", ms.getMessage("code2", null, Locale.GERMAN)); 247 } 248 249 } 250 | Popular Tags |