DataSourceMessageSource extends AbstractMessageSource implements
        InitializingBean { protected synchronized void readMessagesFromDataSource() { if (log.isDebugEnabled()) { log.debug("readMessageFromDataSource"); } cachedMergedProperties.clear(); cachedMessageFormats.clear(); List<DBMessage> messages = dbMessageResourceDAO.findDBMessages(); for (DBMessage message : messages) { if (message.code != null && message.message != null) { mapMessage(message); } } if (log.isDebugEnabled()) { log.debug("get MessageResource from DAO."); } }  void setDefaultLocale(Locale defaultLocale); synchronized void setDbMessageResourceDAO(
            DBMessageResourceDAO dbMessageResourceDAO); @Override void afterPropertiesSet(); synchronized void reloadDataSourceMessage();  }
@Test public void testReadMessageFromDataSource01() throws Exception { DataSourceMessageSource_DataSourceMessageSourceStub02 ds = new DataSourceMessageSource_DataSourceMessageSourceStub02(); DataSourceMessageSource_DBMessageResoueceDAOStub01 dbmr = new DataSourceMessageSource_DBMessageResoueceDAOStub01(); Properties prop = new Properties(); Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); ds.cachedMergedProperties.put(Locale.JAPAN, prop); ds.cachedMessageFormats.put("foo", map); ds.dbMessageResourceDAO = dbmr; dbmr.list = new ArrayList<DBMessage>(); ds.readMessagesFromDataSource(); assertTrue(ds.cachedMergedProperties.isEmpty()); assertTrue(ds.cachedMessageFormats.isEmpty()); assertTrue(dbmr.isRead); assertEquals(0, ds.list.size()); }
@Test public void testReadMessageFromDataSource02() throws Exception { DataSourceMessageSource_DataSourceMessageSourceStub02 ds = new DataSourceMessageSource_DataSourceMessageSourceStub02(); DataSourceMessageSource_DBMessageResoueceDAOStub01 dbmr = new DataSourceMessageSource_DBMessageResoueceDAOStub01(); Properties prop = new Properties(); Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); ds.cachedMergedProperties.put(Locale.JAPAN, prop); ds.cachedMessageFormats.put("foo", map); ds.dbMessageResourceDAO = dbmr; List<DBMessage> list = new ArrayList<DBMessage>(); DBMessage db1 = new DBMessage("test01", "JP", "ja", "kaisai", "テストメッセージ０１"); list.add(db1); dbmr.list = list; ds.readMessagesFromDataSource(); assertTrue(ds.cachedMergedProperties.isEmpty()); assertTrue(ds.cachedMessageFormats.isEmpty()); assertTrue(dbmr.isRead); assertEquals(1, ds.list.size()); assertSame(db1, ds.list.get(0)); }
@Test public void testReadMessageFromDataSource03() throws Exception { DataSourceMessageSource_DataSourceMessageSourceStub02 ds = new DataSourceMessageSource_DataSourceMessageSourceStub02(); DataSourceMessageSource_DBMessageResoueceDAOStub01 dbmr = new DataSourceMessageSource_DBMessageResoueceDAOStub01(); Properties prop = new Properties(); Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); ds.cachedMergedProperties.put(Locale.JAPAN, prop); ds.cachedMessageFormats.put("foo", map); ds.dbMessageResourceDAO = dbmr; List<DBMessage> list = new ArrayList<DBMessage>(); DBMessage db1 = new DBMessage("test01", "JP", "ja", "kaisai", "テストメッセージ０１"); DBMessage db2 = new DBMessage("test02", "JP", "ja", "kaisai", "テストメッセージ０２"); DBMessage db3 = new DBMessage("test03", "JP", "ja", "kaisai", "テストメッセージ０３"); list.add(db1); list.add(db2); list.add(db3); dbmr.list = list; ds.readMessagesFromDataSource(); assertTrue(ds.cachedMergedProperties.isEmpty()); assertTrue(ds.cachedMessageFormats.isEmpty()); assertTrue(dbmr.isRead); assertEquals(3, ds.list.size()); assertSame(db1, ds.list.get(0)); assertSame(db2, ds.list.get(1)); assertSame(db3, ds.list.get(2)); }
@Test public void testReadMessageFromDataSource04() throws Exception { DataSourceMessageSource_DataSourceMessageSourceStub02 ds = new DataSourceMessageSource_DataSourceMessageSourceStub02(); DataSourceMessageSource_DBMessageResoueceDAOStub01 dbmr = new DataSourceMessageSource_DBMessageResoueceDAOStub01(); Properties prop = new Properties(); Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); ds.cachedMergedProperties.put(Locale.JAPAN, prop); ds.cachedMessageFormats.put("foo", map); ds.dbMessageResourceDAO = dbmr; List<DBMessage> list = new ArrayList<DBMessage>(); DBMessage db1 = new DBMessage("", "", "", "", ""); list.add(db1); dbmr.list = list; ds.readMessagesFromDataSource(); assertTrue(ds.cachedMergedProperties.isEmpty()); assertTrue(ds.cachedMessageFormats.isEmpty()); assertTrue(dbmr.isRead); assertEquals(1, ds.list.size()); assertSame(db1, ds.list.get(0)); }
@Test public void testReadMessageFromDataSource05() throws Exception { DataSourceMessageSource_DataSourceMessageSourceStub02 ds = new DataSourceMessageSource_DataSourceMessageSourceStub02(); DataSourceMessageSource_DBMessageResoueceDAOStub01 dbmr = new DataSourceMessageSource_DBMessageResoueceDAOStub01(); Properties prop = new Properties(); Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); ds.cachedMergedProperties.put(Locale.JAPAN, prop); ds.cachedMessageFormats.put("foo", map); ds.dbMessageResourceDAO = dbmr; List<DBMessage> list = new ArrayList<DBMessage>(); DBMessage db1 = new DBMessage(null, null, null, null, null); list.add(db1); dbmr.list = list; ds.readMessagesFromDataSource(); assertTrue(ds.cachedMergedProperties.isEmpty()); assertTrue(ds.cachedMessageFormats.isEmpty()); assertTrue(dbmr.isRead); assertEquals(0, ds.list.size()); }
@Test public void testReadMessageFromDataSource06() throws Exception { DataSourceMessageSource_DataSourceMessageSourceStub02 ds = new DataSourceMessageSource_DataSourceMessageSourceStub02(); DataSourceMessageSource_DBMessageResoueceDAOStub01 dbmr = new DataSourceMessageSource_DBMessageResoueceDAOStub01(); Properties prop = new Properties(); Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); ds.cachedMergedProperties.put(Locale.JAPAN, prop); ds.cachedMessageFormats.put("foo", map); ds.dbMessageResourceDAO = dbmr; List<DBMessage> list = new ArrayList<DBMessage>(); DBMessage db1 = new DBMessage(null, "JP", "ja", "kaisai", "テストメッセージ０１"); list.add(db1); dbmr.list = list; ds.readMessagesFromDataSource(); assertTrue(ds.cachedMergedProperties.isEmpty()); assertTrue(ds.cachedMessageFormats.isEmpty()); assertTrue(dbmr.isRead); assertEquals(0, ds.list.size()); }
@Test public void testReadMessageFromDataSource07() throws Exception { DataSourceMessageSource_DataSourceMessageSourceStub02 ds = new DataSourceMessageSource_DataSourceMessageSourceStub02(); DataSourceMessageSource_DBMessageResoueceDAOStub01 dbmr = new DataSourceMessageSource_DBMessageResoueceDAOStub01(); Properties prop = new Properties(); Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); ds.cachedMergedProperties.put(Locale.JAPAN, prop); ds.cachedMessageFormats.put("foo", map); ds.dbMessageResourceDAO = dbmr; List<DBMessage> list = new ArrayList<DBMessage>(); DBMessage db1 = new DBMessage("test01", "JP", "ja", "kaisai", null); list.add(db1); dbmr.list = list; ds.readMessagesFromDataSource(); assertTrue(ds.cachedMergedProperties.isEmpty()); assertTrue(ds.cachedMessageFormats.isEmpty()); assertTrue(dbmr.isRead); assertEquals(0, ds.list.size()); }
@Test @SuppressWarnings("unchecked") public void testReadMessageFromDataSource08() throws Exception { DataSourceMessageSource_DataSourceMessageSourceStub02 ds = new DataSourceMessageSource_DataSourceMessageSourceStub02(); DataSourceMessageSource_DBMessageResoueceDAOStub01 dbmr = new DataSourceMessageSource_DBMessageResoueceDAOStub01(); Properties prop = new Properties(); Map<Locale, MessageFormat> map = new HashMap<Locale, MessageFormat>(); ds.cachedMergedProperties.put(Locale.JAPAN, prop); ds.cachedMessageFormats.put("foo", map); ds.dbMessageResourceDAO = dbmr; @SuppressWarnings("rawtypes") List list = new ArrayList(); Object db1 = new Object(); list.add(db1); dbmr.list = list; try { ds.readMessagesFromDataSource(); fail(); } catch (ClassCastException e) { assertTrue(ds.cachedMergedProperties.isEmpty()); assertTrue(ds.cachedMessageFormats.isEmpty()); assertTrue(dbmr.isRead); assertEquals(0, ds.list.size()); } }