### Question:
DataSourceStatusChecker implements StatusChecker { @Override @SuppressWarnings("unchecked") public Status check() { ApplicationContext context = null; for (ApplicationContext c : SpringExtensionFactory.getContexts()) { if (c != null) { context = c; break; } } if (context == null) { return new Status(Status.Level.UNKNOWN); } Map<String, DataSource> dataSources = context.getBeansOfType(DataSource.class, false, false); if (CollectionUtils.isEmptyMap(dataSources)) { return new Status(Status.Level.UNKNOWN); } Status.Level level = Status.Level.OK; StringBuilder buf = new StringBuilder(); for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) { DataSource dataSource = entry.getValue(); if (buf.length() > 0) { buf.append(", "); } buf.append(entry.getKey()); try { Connection connection = dataSource.getConnection(); try { DatabaseMetaData metaData = connection.getMetaData(); ResultSet resultSet = metaData.getTypeInfo(); try { if (!resultSet.next()) { level = Status.Level.ERROR; } } finally { resultSet.close(); } buf.append(metaData.getURL()); buf.append("("); buf.append(metaData.getDatabaseProductName()); buf.append("-"); buf.append(metaData.getDatabaseProductVersion()); buf.append(")"); } finally { connection.close(); } } catch (Throwable e) { logger.warn(e.getMessage(), e); return new Status(level, e.getMessage()); } } return new Status(level, buf.toString()); }  @Override @SuppressWarnings("unchecked") Status check();  }### Answer:
@Test public void testWithoutApplicationContext() { Status status = dataSourceStatusChecker.check(); assertThat(status.getLevel(), is(Status.Level.UNKNOWN)); }
@Test public void testWithoutDatasource() { Map<String, DataSource> map = new HashMap<String, DataSource>(); given(applicationContext.getBeansOfType(eq(DataSource.class), anyBoolean(), anyBoolean())).willReturn(map); Status status = dataSourceStatusChecker.check(); assertThat(status.getLevel(), is(Status.Level.UNKNOWN)); }
@Test public void testWithDatasourceHasNextResult() throws SQLException { Map<String, DataSource> map = new HashMap<String, DataSource>(); DataSource dataSource = mock(DataSource.class); Connection connection = mock(Connection.class, Answers.RETURNS_DEEP_STUBS); given(dataSource.getConnection()).willReturn(connection); given(connection.getMetaData().getTypeInfo().next()).willReturn(true); map.put("mockDatabase", dataSource); given(applicationContext.getBeansOfType(eq(DataSource.class), anyBoolean(), anyBoolean())).willReturn(map); Status status = dataSourceStatusChecker.check(); assertThat(status.getLevel(), is(Status.Level.OK)); }
@Test public void testWithDatasourceNotHasNextResult() throws SQLException { Map<String, DataSource> map = new HashMap<String, DataSource>(); DataSource dataSource = mock(DataSource.class); Connection connection = mock(Connection.class, Answers.RETURNS_DEEP_STUBS); given(dataSource.getConnection()).willReturn(connection); given(connection.getMetaData().getTypeInfo().next()).willReturn(false); map.put("mockDatabase", dataSource); given(applicationContext.getBeansOfType(eq(DataSource.class), anyBoolean(), anyBoolean())).willReturn(map); Status status = dataSourceStatusChecker.check(); assertThat(status.getLevel(), is(Status.Level.ERROR)); }