/** Example taken from the javadocs for the java.text.MessageFormat class */ public void testGetMessageWithNoDefaultPassedInAndFoundInMsgCatalog() { Object[] arguments = { new Integer(7), new Date(System.currentTimeMillis()), "a disturbance in the Force" }; /* Try with Locale.US Since the msg has a time value in it, we will use String.indexOf(...) to just look for a substring without the time. This is because it is possible that by the time we store a time variable in this method and the time the ResourceBundleMessageSource resolves the msg the minutes of the time might not be the same. */ assertTrue( "msg from staticMsgSource for Locale.US substituting args for placeholders is as expected", sac.getMessage("message.format.example1", arguments, Locale.US) .indexOf("there was \"a disturbance in the Force\" on planet 7.") != -1); // Try with Locale.UK assertTrue( "msg from staticMsgSource for Locale.UK substituting args for placeholders is as expected", sac.getMessage("message.format.example1", arguments, Locale.UK) .indexOf("there was \"a disturbance in the Force\" on station number 7.") != -1); // Try with Locale.US - Use a different test msg that requires no args assertTrue( "msg from staticMsgSource for Locale.US that requires no args is as expected", sac.getMessage("message.format.example2", null, Locale.US) .equals("This is a test message in the message catalog with no args.")); }
/** * We really are testing the AbstractMessageSource class here. The underlying implementation uses * a hashMap to cache messageFormats once a message has been asked for. This test is an attempt to * make sure the cache is being used properly. * * @see org.springframework.context.support.AbstractMessageSource for more details. */ public void testGetMessageWithMessageAlreadyLookedFor() { Object[] arguments = { new Integer(7), new Date(System.currentTimeMillis()), "a disturbance in the Force" }; // The first time searching, we don't care about for this test // Try with Locale.US sac.getMessage("message.format.example1", arguments, Locale.US); // Now msg better be as expected assertTrue( "2nd search within MsgFormat cache returned expected message for Locale.US", sac.getMessage("message.format.example1", arguments, Locale.US) .indexOf("there was \"a disturbance in the Force\" on planet 7.") != -1); Object[] newArguments = { new Integer(8), new Date(System.currentTimeMillis()), "a disturbance in the Force" }; // Now msg better be as expected even with different args assertTrue( "2nd search within MsgFormat cache with different args returned expected message for Locale.US", sac.getMessage("message.format.example1", newArguments, Locale.US) .indexOf("there was \"a disturbance in the Force\" on planet 8.") != -1); }
public void testMessageSourceResolvable() { // first code valid String[] codes1 = new String[] {"message.format.example3", "message.format.example2"}; MessageSourceResolvable resolvable1 = new DefaultMessageSourceResolvable(codes1, null, "default"); try { assertTrue( "correct message retrieved", MSG_TXT3_US.equals(sac.getMessage(resolvable1, Locale.US))); } catch (NoSuchMessageException ex) { fail("Should not throw NoSuchMessageException"); } // only second code valid String[] codes2 = new String[] {"message.format.example99", "message.format.example2"}; MessageSourceResolvable resolvable2 = new DefaultMessageSourceResolvable(codes2, null, "default"); try { assertTrue( "correct message retrieved", MSG_TXT2_US.equals(sac.getMessage(resolvable2, Locale.US))); } catch (NoSuchMessageException ex) { fail("Should not throw NoSuchMessageException"); } // no code valid, but default given String[] codes3 = new String[] {"message.format.example99", "message.format.example98"}; MessageSourceResolvable resolvable3 = new DefaultMessageSourceResolvable(codes3, null, "default"); try { assertTrue( "correct message retrieved", "default".equals(sac.getMessage(resolvable3, Locale.US))); } catch (NoSuchMessageException ex) { fail("Should not throw NoSuchMessageException"); } // no code valid, no default String[] codes4 = new String[] {"message.format.example99", "message.format.example98"}; MessageSourceResolvable resolvable4 = new DefaultMessageSourceResolvable(codes4); try { sac.getMessage(resolvable4, Locale.US); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected } }
public void testGetMessageWithDefaultPassedInAndNotFoundInMsgCatalog() { // Try with Locale.US assertTrue( "bogus msg from staticMsgSource with default msg passed in returned default msg for Locale.US", sac.getMessage( "bogus.message", null, "This is a default msg if not found in MessageSource.", Locale.US) .equals("This is a default msg if not found in MessageSource.")); }
public void testGetMessageWithDefaultPassedInAndFoundInMsgCatalog() { // Try with Locale.US assertTrue( "valid msg from staticMsgSource with default msg passed in returned msg from msg catalog for Locale.US", sac.getMessage( "message.format.example2", null, "This is a default msg if not found in MessageSource.", Locale.US) .equals("This is a test message in the message catalog with no args.")); }
public void testGetMessageWithNoDefaultPassedInAndNotFoundInMsgCatalog() { // Expecting an exception try { // Try with Locale.US sac.getMessage("bogus.message", null, Locale.US); fail( "bogus msg from staticMsgSource for Locale.US without default msg should have thrown exception"); } catch (NoSuchMessageException tExcept) { assertTrue( "bogus msg from staticMsgSource for Locale.US without default msg threw expected exception", true); } }
/** Run for each test */ @Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<String, String>(); m.put("name", "Roderick"); parent.registerPrototype( "rod", org.springframework.beans.TestBean.class, new MutablePropertyValues(m)); m.put("name", "Albert"); parent.registerPrototype( "father", org.springframework.beans.TestBean.class, new MutablePropertyValues(m)); parent.refresh(); parent.addApplicationListener(parentListener); this.sac = new StaticApplicationContext(parent); sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues()); sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues()); sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory()); reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass())); sac.refresh(); sac.addApplicationListener(listener); StaticMessageSource messageSource = sac.getStaticMessageSource(); Map<String, String> usMessages = new HashMap<String, String>(3); usMessages.put("message.format.example1", MSG_TXT1_US); usMessages.put("message.format.example2", MSG_TXT2_US); usMessages.put("message.format.example3", MSG_TXT3_US); messageSource.addMessages(usMessages, Locale.US); messageSource.addMessage("message.format.example1", Locale.UK, MSG_TXT1_UK); return sac; }