mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add testcase for EncodingConvertFilter
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
package com.alibaba.druid.bvt.filter;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.filter.encoding.EncodingConvertFilter;
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.mock.MockPreparedStatement;
|
||||
import com.alibaba.druid.mock.MockResultSet;
|
||||
import com.alibaba.druid.mock.MockStatementBase;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class EncodingConvertFilterTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
private static String CLIENT_ENCODING = "UTF-8";
|
||||
private static String SERVER_ENCODING = "ISO-8859-1";
|
||||
|
||||
private static String text = "中华人民共和国";
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
|
||||
dataSource = new DruidDataSource();
|
||||
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setFilters("encoding");
|
||||
|
||||
dataSource.setDriver(new MockDriver() {
|
||||
|
||||
public ResultSet createResultSet(MockPreparedStatement stmt) {
|
||||
return new MyResultSet(stmt);
|
||||
}
|
||||
|
||||
public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
|
||||
return new MyResultSet(stmt);
|
||||
}
|
||||
});
|
||||
|
||||
dataSource.getConnectProperties().put("clientEncoding", CLIENT_ENCODING);
|
||||
dataSource.getConnectProperties().put("serverEncoding", SERVER_ENCODING);
|
||||
|
||||
dataSource.init();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
public void test_stat() throws Exception {
|
||||
|
||||
Assert.assertTrue(dataSource.isInited());
|
||||
|
||||
EncodingConvertFilter filter = (EncodingConvertFilter) dataSource.getProxyFilters().get(0);
|
||||
|
||||
DruidPooledConnection conn = dataSource.getConnection();
|
||||
|
||||
final String PARAM_VALUE = "中国";
|
||||
PreparedStatement stmt = conn.prepareStatement("select ?");
|
||||
stmt.setString(1, PARAM_VALUE);
|
||||
|
||||
MockPreparedStatement raw = stmt.unwrap(MockPreparedStatement.class);
|
||||
|
||||
String param1 = (String) raw.getParameters().get(0);
|
||||
|
||||
Assert.assertEquals(PARAM_VALUE, new String(param1.getBytes(SERVER_ENCODING), CLIENT_ENCODING));
|
||||
Assert.assertFalse(param1.equals(PARAM_VALUE));
|
||||
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
MyResultSet rawRs = rs.unwrap(MyResultSet.class);
|
||||
rawRs.setValue(filter.encode((ConnectionProxy) conn.getConnection(), text));
|
||||
|
||||
rs.next();
|
||||
|
||||
Assert.assertEquals(text, rs.getString(1));
|
||||
|
||||
rs.close();
|
||||
stmt.close();
|
||||
|
||||
conn.close();
|
||||
|
||||
}
|
||||
|
||||
public static class MyResultSet extends MockResultSet {
|
||||
|
||||
private String value;
|
||||
|
||||
public MyResultSet(Statement statement){
|
||||
super(statement);
|
||||
}
|
||||
|
||||
public String getObject(int index) throws SQLException {
|
||||
return getString(index);
|
||||
}
|
||||
|
||||
public String getString(int columnIndex) throws SQLException {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user