parent
6587029851
commit
526227c77b
@ -29,6 +29,7 @@ import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
@ -281,7 +282,11 @@ public class ExternalDataSourceServiceImpl implements DataSourceService {
|
||||
for (int i = 0; i < testJtList.size(); i++) {
|
||||
JdbcTemplate jdbcTemplate = testJtList.get(i);
|
||||
try {
|
||||
jdbcTemplate.queryForMap(sql);
|
||||
try {
|
||||
jdbcTemplate.queryForMap(sql);
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
// do nothing.
|
||||
}
|
||||
isHealthList.set(i, Boolean.TRUE);
|
||||
} catch (DataAccessException e) {
|
||||
if (i == masterIndex) {
|
||||
|
@ -24,14 +24,18 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.UncategorizedSQLException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ -64,6 +68,9 @@ public class ExternalDataSourceServiceImplTest {
|
||||
ReflectionTestUtils.setField(service, "tjt", tjt);
|
||||
ReflectionTestUtils.setField(service, "testMasterJT", testMasterJT);
|
||||
ReflectionTestUtils.setField(service, "testMasterWritableJT", testMasterWritableJT);
|
||||
List<HikariDataSource> dataSourceList = new ArrayList<>();
|
||||
dataSourceList.add(new HikariDataSource());
|
||||
ReflectionTestUtils.setField(service, "dataSourceList", dataSourceList);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -107,4 +114,37 @@ public class ExternalDataSourceServiceImplTest {
|
||||
Assert.assertTrue(isHealthList.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDbHealthTaskRunWhenEmptyResult() {
|
||||
List<JdbcTemplate> testJtList = new ArrayList<>();
|
||||
testJtList.add(jt);
|
||||
ReflectionTestUtils.setField(service, "testJtList", testJtList);
|
||||
|
||||
List<Boolean> isHealthList = new ArrayList<>();
|
||||
isHealthList.add(Boolean.FALSE);
|
||||
ReflectionTestUtils.setField(service, "isHealthList", isHealthList);
|
||||
|
||||
when(jt.queryForMap(anyString())).thenThrow(new EmptyResultDataAccessException("Expected exception", 1));
|
||||
service.new CheckDbHealthTask().run();
|
||||
Assert.assertEquals(1, isHealthList.size());
|
||||
Assert.assertTrue(isHealthList.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDbHealthTaskRunWhenSqlException() {
|
||||
List<JdbcTemplate> testJtList = new ArrayList<>();
|
||||
testJtList.add(jt);
|
||||
ReflectionTestUtils.setField(service, "testJtList", testJtList);
|
||||
|
||||
List<Boolean> isHealthList = new ArrayList<>();
|
||||
isHealthList.add(Boolean.FALSE);
|
||||
ReflectionTestUtils.setField(service, "isHealthList", isHealthList);
|
||||
|
||||
when(jt.queryForMap(anyString())).thenThrow(
|
||||
new UncategorizedSQLException("Expected exception", "", new SQLException()));
|
||||
service.new CheckDbHealthTask().run();
|
||||
Assert.assertEquals(1, isHealthList.size());
|
||||
Assert.assertFalse(isHealthList.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user