DataSourceMessageSource extends AbstractMessageSource implements
        InitializingBean { @Override protected synchronized MessageFormat resolveCode( String code, Locale locale) { MessageFormat messageFormat = getMessageFormat(code, locale); if (messageFormat != null) { if (log.isDebugEnabled()) { log.debug("resolved [" + code + "] for locale [" + locale + "] => [" + messageFormat + "]"); } return messageFormat; } List<Locale> locales = getAlternativeLocales(locale); for (int i = 0; i < locales.size(); i++) { messageFormat = getMessageFormat(code, locales.get(i)); if (messageFormat != null) { if (log.isDebugEnabled()) { log.debug("resolved [" + code + "] for locale [" + locale + "] => [" + messageFormat + "]"); } return messageFormat; } } if (messageFormat == null) { if (log.isDebugEnabled()) { log.debug("could not resolve [" + code + "] for locale [" + locale + "]"); } } return null; }  void setDefaultLocale(Locale defaultLocale); synchronized void setDbMessageResourceDAO(
            DBMessageResourceDAO dbMessageResourceDAO); @Override void afterPropertiesSet(); synchronized void reloadDataSourceMessage();  }
@Test public void testResolveCode01() throws Exception { DataSourceMessageSource ds = new DataSourceMessageSource(); String code = "code01"; Locale locale = new Locale("ja", "JP", ""); Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); Locale formatlocale = new Locale("ja", "JP", ""); MessageFormat mFormat = new MessageFormat(""); mFormat.setLocale(formatlocale); mFormat.applyPattern("テストメッセージ０１"); map.put(formatlocale, mFormat); ds.cachedMessageFormats.put(code, map); MessageFormat mfreturn = ds.resolveCode(code, locale); assertSame(mFormat, mfreturn); }
@Test public void testResolveCode02() throws Exception { DataSourceMessageSource ds = new DataSourceMessageSource(); String code = "code01"; Locale locale = new Locale("ja", "JP", ""); Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); Locale formatlocale = new Locale("ja", "", ""); MessageFormat mFormat = new MessageFormat(""); mFormat.setLocale(formatlocale); mFormat.applyPattern("テストメッセージ０１"); map.put(formatlocale, mFormat); ds.cachedMessageFormats.put(code, map); MessageFormat mfreturn = ds.resolveCode(code, locale); assertSame(mFormat, mfreturn); }
@Test public void testResolveCode03() throws Exception { DataSourceMessageSource ds = new DataSourceMessageSource(); String code = "code01"; Locale locale = new Locale("ja", "JP", "kansai"); Locale defaultLocale = new Locale("en", "", ""); ds.defaultLocale = defaultLocale; Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); Locale formatlocale = new Locale("en", "", ""); MessageFormat mFormat = new MessageFormat(""); mFormat.setLocale(formatlocale); mFormat.applyPattern("テストメッセージ０１"); map.put(formatlocale, mFormat); ds.cachedMessageFormats.put(code, map); MessageFormat mfreturn = ds.resolveCode(code, locale); assertSame(mFormat, mfreturn); }
@Test public void testResolveCode04() throws Exception { DataSourceMessageSource ds = new DataSourceMessageSource(); String code = "code01"; Locale locale = new Locale("ja", "JP", "kansai"); Locale defaultLocale = new Locale("en", "", ""); ds.defaultLocale = defaultLocale; Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); Locale formatlocale = new Locale("en", "US", "NY"); MessageFormat mFormat = new MessageFormat(""); mFormat.setLocale(formatlocale); mFormat.applyPattern("テストメッセージ０１"); map.put(formatlocale, mFormat); ds.cachedMessageFormats.put(code, map); MessageFormat mfreturn = ds.resolveCode(code, locale); assertNull(mfreturn); }
@Test public void testResolveCode05() throws Exception { DataSourceMessageSource ds = new DataSourceMessageSource(); String code = "code01"; Locale locale = new Locale("ja", "", ""); ds.defaultLocale = null; Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); Locale formatlocale = new Locale("en", "US", "NY"); MessageFormat mFormat = new MessageFormat(""); mFormat.setLocale(formatlocale); mFormat.applyPattern("テストメッセージ０１"); map.put(formatlocale, mFormat); ds.cachedMessageFormats.put(code, map); MessageFormat mfreturn = ds.resolveCode(code, locale); assertNull(mfreturn); }