mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
补充测试,重构代码
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user