Search in sources :

Example 6 with ConnectionProxy

use of com.alibaba.druid.proxy.jdbc.ConnectionProxy in project druid by alibaba.

the class EncodingConvertFilterTest method test_stat.

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();
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) MockResultSet(com.alibaba.druid.mock.MockResultSet) ResultSet(java.sql.ResultSet) EncodingConvertFilter(com.alibaba.druid.filter.encoding.EncodingConvertFilter) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) PreparedStatement(java.sql.PreparedStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Example 7 with ConnectionProxy

use of com.alibaba.druid.proxy.jdbc.ConnectionProxy in project druid by alibaba.

the class EncodingConvertFilterTest2 method test_stat.

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 = "中国";
    CallableStatement stmt = conn.prepareCall("select ?");
    stmt.setString(1, PARAM_VALUE);
    MockCallableStatement raw = stmt.unwrap(MockCallableStatement.class);
    stmt.execute();
    String param1 = (String) raw.getParameters().get(0);
    String C_TEXT = new String(param1.getBytes(SERVER_ENCODING), CLIENT_ENCODING);
    Assert.assertEquals(PARAM_VALUE, C_TEXT);
    Assert.assertFalse(param1.equals(PARAM_VALUE));
    MyResultSet rawRs = new MyResultSet(raw);
    rawRs.setValue(filter.encode((ConnectionProxy) conn.getConnection(), text));
    raw.getOutParameters().add(rawRs);
    ResultSet rs = (ResultSet) stmt.getObject(1);
    rs.next();
    Assert.assertEquals(text, rs.getString(1));
    Assert.assertEquals(text, rs.getString("1"));
    Assert.assertEquals(text, rs.getObject(1));
    Assert.assertEquals(text, rs.getObject("1"));
    Assert.assertEquals(text, rs.getObject(1, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getObject("1", Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getString(2));
    Assert.assertEquals(text, rs.getString("2"));
    Assert.assertEquals(text, rs.getObject(2));
    Assert.assertEquals(text, rs.getObject("2"));
    Assert.assertEquals(text, rs.getObject(2, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getObject("2", Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getString(3));
    Assert.assertEquals(text, rs.getString("3"));
    Assert.assertEquals(text, rs.getObject(3));
    Assert.assertEquals(text, rs.getObject("3"));
    Assert.assertEquals(text, rs.getObject(3, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getObject("3", Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getString(4));
    Assert.assertEquals(text, rs.getString("4"));
    Assert.assertEquals(text, rs.getObject(4));
    Assert.assertEquals(text, rs.getObject("4"));
    Assert.assertEquals(text, rs.getObject(4, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getObject("4", Collections.<String, Class<?>>emptyMap()));
    stmt.registerOutParameter(2, Types.VARCHAR);
    stmt.registerOutParameter(3, Types.CLOB);
    raw.getOutParameters().add(param1);
    raw.getOutParameters().add(param1);
    Assert.assertEquals(C_TEXT, stmt.getString(4));
    Assert.assertEquals(C_TEXT, stmt.getString("4"));
    Assert.assertEquals(C_TEXT, stmt.getObject(4));
    Assert.assertEquals(C_TEXT, stmt.getObject("4"));
    Assert.assertEquals(C_TEXT, stmt.getObject(4, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(C_TEXT, stmt.getObject("4", Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(C_TEXT, stmt.getString(5));
    Assert.assertEquals(C_TEXT, stmt.getString("5"));
    Assert.assertEquals(C_TEXT, stmt.getObject(5));
    Assert.assertEquals(C_TEXT, stmt.getObject("5"));
    Assert.assertEquals(C_TEXT, stmt.getObject(5, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(C_TEXT, stmt.getObject("5", Collections.<String, Class<?>>emptyMap()));
    stmt.setObject(1, C_TEXT);
    Assert.assertEquals(param1, raw.getParameters().get(0));
    stmt.setObject(2, new StringReader(C_TEXT));
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(1)));
    stmt.setCharacterStream(3, new StringReader(C_TEXT));
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(2)));
    stmt.setCharacterStream(4, new StringReader(C_TEXT), C_TEXT.length());
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(3)));
    stmt.setCharacterStream(5, new StringReader(C_TEXT), (long) C_TEXT.length());
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(4)));
    stmt.setObject(6, C_TEXT, Types.VARCHAR);
    Assert.assertEquals(param1, raw.getParameters().get(5));
    stmt.setObject(7, new StringReader(C_TEXT), Types.VARCHAR);
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(6)));
    stmt.setObject(8, C_TEXT, Types.VARCHAR, 0);
    Assert.assertEquals(param1, raw.getParameters().get(7));
    stmt.setObject(9, new StringReader(C_TEXT), Types.VARCHAR, 0);
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(8)));
    stmt.setObject(10, 1, Types.INTEGER);
    Assert.assertEquals(1, raw.getParameters().get(9));
    stmt.setObject(11, 2, Types.INTEGER, 0);
    Assert.assertEquals(2, raw.getParameters().get(10));
    stmt.setObject(12, 3);
    Assert.assertEquals(3, raw.getParameters().get(11));
    stmt.setObject("13", C_TEXT, Types.VARCHAR);
    Assert.assertEquals(param1, raw.getParameters().get(12));
    stmt.setObject("14", new StringReader(C_TEXT), Types.VARCHAR);
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(13)));
    stmt.setObject("15", C_TEXT, Types.VARCHAR, 0);
    Assert.assertEquals(param1, raw.getParameters().get(14));
    stmt.setObject("16", new StringReader(C_TEXT), Types.VARCHAR, 0);
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(15)));
    stmt.setObject("17", 1, Types.INTEGER);
    Assert.assertEquals(1, raw.getParameters().get(16));
    stmt.setObject("18", 2, Types.INTEGER, 0);
    Assert.assertEquals(2, raw.getParameters().get(17));
    stmt.setObject("19", 3);
    Assert.assertEquals(3, raw.getParameters().get(18));
    stmt.setCharacterStream("20", new StringReader(C_TEXT));
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(19)));
    stmt.setCharacterStream("21", new StringReader(C_TEXT), C_TEXT.length());
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(20)));
    stmt.setCharacterStream("22", new StringReader(C_TEXT), (long) C_TEXT.length());
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(21)));
    stmt.setObject("23", C_TEXT);
    Assert.assertEquals(param1, raw.getParameters().get(22));
    stmt.setObject("24", new StringReader(C_TEXT));
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(23)));
    stmt.setObject("25", 1, Types.INTEGER);
    Assert.assertEquals(1, raw.getParameters().get(24));
    stmt.setObject("26", 2, Types.INTEGER, 0);
    Assert.assertEquals(2, raw.getParameters().get(25));
    stmt.setObject("27", 3);
    Assert.assertEquals(3, raw.getParameters().get(26));
    rs.close();
    stmt.close();
    conn.close();
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) CallableStatement(java.sql.CallableStatement) MockResultSet(com.alibaba.druid.mock.MockResultSet) ResultSet(java.sql.ResultSet) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) EncodingConvertFilter(com.alibaba.druid.filter.encoding.EncodingConvertFilter) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Example 8 with ConnectionProxy

use of com.alibaba.druid.proxy.jdbc.ConnectionProxy in project druid by alibaba.

the class DruidDataSourceTest3 method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setTestOnBorrow(false);
    dataSource.setInitialSize(1);
    dataSource.getProxyFilters().add(new FilterAdapter() {

        public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
            try {
                Thread.sleep(Long.MAX_VALUE);
            } catch (InterruptedException e) {
            // skip
            }
            return null;
        }
    });
}
Also used : SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) FilterAdapter(com.alibaba.druid.filter.FilterAdapter) Properties(java.util.Properties) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Example 9 with ConnectionProxy

use of com.alibaba.druid.proxy.jdbc.ConnectionProxy in project druid by alibaba.

the class DruidConnectionHolderTest method setUp.

protected void setUp() throws Exception {
    driver = new Driver();
    if (driver.getMajorVersion() == 5) {
        exceptionClass = Class.forName("com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException");
        dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mock:xxx");
        dataSource.setTestOnBorrow(false);
        dataSource.setInitialSize(1);
        dataSource.getProxyFilters().add(new FilterAdapter() {

            public int connection_getTransactionIsolation(FilterChain chain, ConnectionProxy connection) throws SQLException {
                throw createSyntaxException();
            }
        });
    }
}
Also used : SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) Driver(com.mysql.jdbc.Driver) FilterAdapter(com.alibaba.druid.filter.FilterAdapter) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Example 10 with ConnectionProxy

use of com.alibaba.druid.proxy.jdbc.ConnectionProxy in project druid by alibaba.

the class DruidConnectionHolderTest3 method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setTestOnBorrow(false);
    dataSource.setInitialSize(1);
    dataSource.getProxyFilters().add(new FilterAdapter() {

        public int connection_getTransactionIsolation(FilterChain chain, ConnectionProxy connection) throws SQLException {
            throw new MySQLException();
        }
    });
}
Also used : SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) FilterAdapter(com.alibaba.druid.filter.FilterAdapter) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Aggregations

ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)32 SQLException (java.sql.SQLException)19 FilterChain (com.alibaba.druid.filter.FilterChain)18 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)18 FilterAdapter (com.alibaba.druid.filter.FilterAdapter)17 Properties (java.util.Properties)13 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)7 ResultSet (java.sql.ResultSet)5 MockResultSet (com.alibaba.druid.mock.MockResultSet)3 DataSourceProxy (com.alibaba.druid.proxy.jdbc.DataSourceProxy)3 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)3 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)3 Statement (java.sql.Statement)3 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)2 FilterEventAdapter (com.alibaba.druid.filter.FilterEventAdapter)2 EncodingConvertFilter (com.alibaba.druid.filter.encoding.EncodingConvertFilter)2 ConnectionProxyImpl (com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl)2 PreparedStatementProxy (com.alibaba.druid.proxy.jdbc.PreparedStatementProxy)2 ResultSetProxyImpl (com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl)2 StatementProxy (com.alibaba.druid.proxy.jdbc.StatementProxy)2