1 16 17 package org.springframework.web.context; 18 19 import java.util.Date ; 20 import java.util.Locale ; 21 22 import org.springframework.context.AbstractApplicationContextTests; 23 import org.springframework.context.ConfigurableApplicationContext; 24 import org.springframework.context.MessageSource; 25 import org.springframework.context.NoSuchMessageException; 26 import org.springframework.context.support.AbstractMessageSource; 27 import org.springframework.mock.web.MockServletContext; 28 import org.springframework.ui.context.Theme; 29 import org.springframework.web.context.support.XmlWebApplicationContext; 30 import org.springframework.web.servlet.theme.AbstractThemeResolver; 31 32 51 public class ResourceBundleMessageSourceTests extends AbstractApplicationContextTests { 52 53 57 public static final String WAR_ROOT = "/org/springframework/web/context"; 58 59 private ConfigurableWebApplicationContext root; 60 61 private MessageSource themeMsgSource; 62 63 protected ConfigurableApplicationContext createContext() throws Exception { 64 root = new XmlWebApplicationContext(); 65 MockServletContext sc = new MockServletContext(""); 66 root.setServletContext(sc); 67 root.setConfigLocations(new String [] {"/org/springframework/web/context/WEB-INF/applicationContext.xml"}); 68 root.refresh(); 69 70 ConfigurableWebApplicationContext wac = new XmlWebApplicationContext(); 71 wac.setParent(root); 72 wac.setServletContext(sc); 73 wac.setNamespace("test-servlet"); 74 wac.setConfigLocations(new String [] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); 75 wac.refresh(); 76 77 Theme theme = wac.getTheme(AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME); 78 assertNotNull(theme); 79 assertTrue("Theme name has to be the default theme name", AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME.equals(theme.getName())); 80 themeMsgSource = theme.getMessageSource(); 81 assertNotNull(themeMsgSource); 82 return wac; 83 } 84 85 public void testCount() { 86 assertTrue("should have 14 beans, not " + 87 this.applicationContext.getBeanDefinitionCount(), 88 this.applicationContext.getBeanDefinitionCount() == 14); 89 } 90 91 95 public void testEvents() throws Exception { 96 } 98 99 public void testRootMessageSourceWithUseCodeAsDefaultMessage() throws NoSuchMessageException { 100 AbstractMessageSource messageSource = (AbstractMessageSource) root.getBean("messageSource"); 101 messageSource.setUseCodeAsDefaultMessage(true); 102 103 assertEquals("message1", applicationContext.getMessage("code1", null, Locale.getDefault())); 104 assertEquals("message2", applicationContext.getMessage("code2", null, Locale.getDefault())); 105 106 try { 107 applicationContext.getMessage("code0", null, Locale.getDefault()); 108 fail("looking for code0 should throw a NoSuchMessageException"); 109 } 110 catch (NoSuchMessageException ex) { 111 } 113 } 114 115 119 public void testGetMessageWithDefaultPassedInAndFoundInMsgCatalog() { 120 assertTrue("valid msg from resourcebundle with default msg passed in returned default msg. Expected msg from catalog.", 121 getApplicationContext().getMessage("message.format.example2", null, "This is a default msg if not found in msg.cat.", Locale.US 122 ) 123 .equals("This is a test message in the message catalog with no args.")); 124 } 126 127 131 public void testGetMessageWithDefaultPassedInAndNotFoundInMsgCatalog() { 132 assertTrue("bogus msg from resourcebundle with default msg passed in returned default msg", 133 getApplicationContext().getMessage("bogus.message", null, "This is a default msg if not found in msg.cat.", Locale.UK 134 ) 135 .equals("This is a default msg if not found in msg.cat.")); 136 } 137 138 145 public void testGetMessageWithMessageAlreadyLookedFor() throws Exception { 146 Object [] arguments = { 147 new Integer (7), new Date (System.currentTimeMillis()), 148 "a disturbance in the Force" 149 }; 150 151 getApplicationContext().getMessage("message.format.example1", arguments, Locale.US); 153 154 assertTrue("2nd search within MsgFormat cache returned expected message for Locale.US", 156 getApplicationContext().getMessage("message.format.example1", arguments, Locale.US 157 ) 158 .indexOf("there was \"a disturbance in the Force\" on planet 7.") != -1); 159 160 Object [] newArguments = { 161 new Integer (8), new Date (System.currentTimeMillis()), 162 "a disturbance in the Force" 163 }; 164 165 assertTrue("2nd search within MsgFormat cache with different args returned expected message for Locale.US", 167 getApplicationContext().getMessage("message.format.example1", newArguments, Locale.US 168 ) 169 .indexOf("there was \"a disturbance in the Force\" on planet 8.") != -1); 170 } 171 172 177 public void testGetMessageWithNoDefaultPassedInAndFoundInMsgCatalog() 178 throws Exception { 179 Object [] arguments = { 180 new Integer (7), new Date (System.currentTimeMillis()), 181 "a disturbance in the Force" 182 }; 183 184 185 193 assertTrue("msg from resourcebundle for Locale.US substituting args for placeholders is as expected", 194 getApplicationContext().getMessage("message.format.example1", arguments, Locale.US 195 ) 196 .indexOf("there was \"a disturbance in the Force\" on planet 7.") != -1); 197 198 assertTrue("msg from resourcebundle for Locale.UK substituting args for placeholders is as expected", 200 getApplicationContext().getMessage("message.format.example1", arguments, Locale.UK 201 ) 202 .indexOf("there was \"a disturbance in the Force\" on station number 7.") != -1); 203 204 assertTrue("msg from resourcebundle that requires no args for Locale.US is as expected", 206 getApplicationContext().getMessage("message.format.example2", null, Locale.US) 207 .equals("This is a test message in the message catalog with no args.")); 208 } 209 210 214 public void testGetMessageWithNoDefaultPassedInAndNotFoundInMsgCatalog() { 215 try { 217 getApplicationContext().getMessage("bogus.message", null, Locale.UK); 218 fail("bogus msg from resourcebundle without default msg should have thrown exception"); 219 } 220 catch (NoSuchMessageException tExcept) { 221 assertTrue("bogus msg from resourcebundle without default msg threw expected exception", 222 true); 223 } 224 } 225 226 public void testGetMultipleBasenamesForMessageSource() throws NoSuchMessageException { 227 assertEquals("message1", getApplicationContext().getMessage("code1", null, Locale.UK)); 228 assertEquals("message2", getApplicationContext().getMessage("code2", null, Locale.UK)); 229 assertEquals("message3", getApplicationContext().getMessage("code3", null, Locale.UK)); 230 } 231 232 236 public void testGetMessageWithDefaultPassedInAndFoundInThemeCatalog() { 237 String msg = getThemeMessage("theme.example1", null, "This is a default theme msg if not found in theme cat.", Locale.US); 239 assertTrue("valid msg from theme resourcebundle with default msg passed in returned default msg. Expected msg from catalog. Received: " + msg, 240 msg.equals("This is a test message in the theme message catalog.")); 241 msg = getThemeMessage("theme.example1", null, "This is a default theme msg if not found in theme cat.", Locale.UK); 243 assertTrue("valid msg from theme resourcebundle with default msg passed in returned default msg. Expected msg from catalog.", 244 msg.equals("This is a test message in the theme message catalog with no args.")); 245 } 246 247 251 public void testGetMessageWithDefaultPassedInAndNotFoundInThemeCatalog() { 252 assertTrue("bogus msg from theme resourcebundle with default msg passed in returned default msg", 253 getThemeMessage("bogus.message", null, "This is a default msg if not found in theme cat.", Locale.UK 254 ) 255 .equals("This is a default msg if not found in theme cat.")); 256 } 257 258 public void testThemeSourceNesting() throws NoSuchMessageException { 259 String overriddenMsg = getThemeMessage("theme.example2", null, null, Locale.UK); 260 String originalMsg = root.getTheme(AbstractThemeResolver.ORIGINAL_DEFAULT_THEME_NAME).getMessageSource().getMessage("theme.example2", null, Locale.UK); 261 assertTrue("correct overridden msg", "test-message2".equals(overriddenMsg)); 262 assertTrue("correct original msg", "message2".equals(originalMsg)); 263 } 264 265 private String getThemeMessage(String code, Object args[], String defaultMessage, Locale locale) { 266 return themeMsgSource.getMessage(code, args, defaultMessage, locale); 267 } 268 269 } 270 | Popular Tags |