补充测试,重构代码

This commit is contained in:
DigitalSonic
2018-04-12 23:16:18 +08:00
parent cdefafd232
commit 64be5cc703
8 changed files with 142 additions and 33 deletions
@@ -7,12 +7,20 @@ import org.junit.Test;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
/**
* Test class for HostAndPortHolder
*
* @author DigitalSonic
*/
public class HostAndPortHolderTest {
@Before
public void setUp() {
HostAndPortHolder.loadFromProperties("/com/alibaba/druid/filter/url/datasource.properties");
HostAndPortHolder.loadProperties("/com/alibaba/druid/filter/url/datasource.properties");
}
@After
@@ -20,6 +28,13 @@ public class HostAndPortHolderTest {
HostAndPortHolder.getInstance().clearBlacklist();
}
@Test
public void testLoadFromFileNotExisted() {
int size = HostAndPortHolder.getInstance().getHolder().size();
HostAndPortHolder.loadProperties("FILE_NOT_FOUND");
assertEquals(size, HostAndPortHolder.getInstance().getHolder().size());
}
@Test
public void testLoadFromProperties() {
Map<String, List<String>> holder = HostAndPortHolder.getInstance().getHolder();
@@ -0,0 +1,40 @@
package com.alibaba.druid.filter.url;
import com.alibaba.druid.pool.DruidDataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Test class for HostAndPortValidatorThread
*
* @author DigitalSonic
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/alibaba/druid/filter/url/dataSource.xml")
public class HostAndPortValidatorThreadTest {
@Autowired
private DruidDataSource dataSource;
@Test
public void testValidateFail() throws Exception {
assertEquals(0, HostAndPortHolder.getInstance().getBlacklistSize());
dataSource.setValidationQuery("select xxx from foo");
Thread.sleep(20 * 1000);
assertTrue(HostAndPortHolder.getInstance().getBlacklistSize() > 0);
assertTrue(dataSource.isTestOnBorrow());
assertTrue(dataSource.isTestOnReturn());
dataSource.setValidationQuery(null);
Thread.sleep(20 * 1000);
assertEquals(0, HostAndPortHolder.getInstance().getBlacklistSize());
assertFalse(dataSource.isTestOnBorrow());
assertFalse(dataSource.isTestOnReturn());
}
}
@@ -14,8 +14,14 @@ import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Test class for JdbcUrlHostAndPortReplaceFilter
*
* @author DigitalSonic
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/alibaba/druid/filter/url/dataSource.xml")
public class JdbcUrlHostAndPortReplaceFilterTest {
@@ -46,5 +52,4 @@ public class JdbcUrlHostAndPortReplaceFilterTest {
c.close();
}
}
}