Search in sources :

Example 1 with MockClob

use of com.alibaba.druid.mock.MockClob in project druid by alibaba.

the class DruidLobHandlerTest method test_0.

public void test_0() throws Exception {
    DruidLobHandler handler = new DruidLobHandler();
    List<Object[]> rows = new ArrayList<Object[]>();
    rows.add(new Object[] { null, new MockBlob(), new MockClob() });
    MockResultSet rs = new MockResultSet(null, rows);
    rs.next();
    handler.getBlobAsBinaryStream(rs, 1);
    handler.getBlobAsBinaryStream(rs, "1");
    handler.getBlobAsBytes(rs, 1);
    handler.getBlobAsBytes(rs, "1");
    handler.getBlobAsBinaryStream(rs, 2);
    handler.getBlobAsBinaryStream(rs, "2");
    handler.getBlobAsBytes(rs, 2);
    handler.getBlobAsBytes(rs, "2");
    handler.getClobAsAsciiStream(rs, 1);
    handler.getClobAsAsciiStream(rs, "1");
    handler.getClobAsCharacterStream(rs, 1);
    handler.getClobAsCharacterStream(rs, "1");
    handler.getClobAsString(rs, 1);
    handler.getClobAsString(rs, "1");
    handler.getClobAsAsciiStream(rs, 3);
    handler.getClobAsAsciiStream(rs, "3");
    handler.getClobAsCharacterStream(rs, 3);
    handler.getClobAsCharacterStream(rs, "3");
    handler.getClobAsString(rs, 3);
    handler.getClobAsString(rs, "3");
    handler.getLobCreator();
}
Also used : MockBlob(com.alibaba.druid.mock.MockBlob) MockClob(com.alibaba.druid.mock.MockClob) ArrayList(java.util.ArrayList) MockResultSet(com.alibaba.druid.mock.MockResultSet) DruidLobHandler(com.alibaba.druid.support.spring.DruidLobHandler)

Example 2 with MockClob

use of com.alibaba.druid.mock.MockClob in project druid by alibaba.

the class FilterChainTest_Clob method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    ConnectionProxyImpl conn = new ConnectionProxyImpl(dataSource, null, new Properties(), 0);
    statement = new StatementProxyImpl(conn, null, 1);
    mockResultSet = new MockResultSet(null) {

        public Object getObject(int columnIndex) throws SQLException {
            invokeCount++;
            return new MockClob();
        }
    };
}
Also used : SQLException(java.sql.SQLException) MockClob(com.alibaba.druid.mock.MockClob) ConnectionProxyImpl(com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl) MockResultSet(com.alibaba.druid.mock.MockResultSet) Properties(java.util.Properties) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) StatementProxyImpl(com.alibaba.druid.proxy.jdbc.StatementProxyImpl)

Example 3 with MockClob

use of com.alibaba.druid.mock.MockClob in project druid by alibaba.

the class StatFilterBuildSlowParameterTest method test_buildSlowSql.

public void test_buildSlowSql() throws Exception {
    long currentMillis = System.currentTimeMillis();
    String sql = "select ?, ?, ?, ?, ?";
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date(System.currentTimeMillis());
    String dateText = dateFormat.format(date);
    {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setBoolean(1, true);
        stmt.setInt(2, 123);
        stmt.setLong(3, 10001);
        stmt.setTimestamp(4, new java.sql.Timestamp(currentMillis));
        stmt.setDate(5, new java.sql.Date(currentMillis));
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals(true, parameters.get(0));
        Assert.assertEquals(123, parameters.get(1));
        Assert.assertEquals(10001, parameters.get(2));
        Assert.assertEquals(dateText, parameters.get(3));
        Assert.assertEquals(dateText, parameters.get(4));
    }
    currentMillis = System.currentTimeMillis();
    date = new Date(System.currentTimeMillis());
    dateText = dateFormat.format(date);
    {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setBoolean(1, false);
        stmt.setInt(2, 234);
        stmt.setLong(3, 10002);
        stmt.setTimestamp(4, new java.sql.Timestamp(currentMillis));
        stmt.setDate(5, new java.sql.Date(currentMillis));
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals(false, parameters.get(0));
        Assert.assertEquals(234, parameters.get(1));
        Assert.assertEquals(10002, parameters.get(2));
        Assert.assertEquals(dateText, parameters.get(3));
        Assert.assertEquals(dateText, parameters.get(4));
    }
    {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < 10; ++i) {
            buf.append("abcdefghijklmnABCDEFGHIJKLMN1234567890!@#$%^&*(");
        }
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setNull(1, Types.VARCHAR);
        stmt.setString(2, buf.toString());
        stmt.setClob(3, new MockClob());
        stmt.setNClob(4, new MockNClob());
        stmt.setBlob(5, new MockBlob());
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals(null, parameters.get(0));
        Assert.assertEquals(buf.substring(0, 97) + "...", parameters.get(1));
        Assert.assertEquals("<Clob>", parameters.get(2));
        Assert.assertEquals("<NClob>", parameters.get(3));
        Assert.assertEquals("<Blob>", parameters.get(4));
    }
    {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < 10; ++i) {
            buf.append("中国abcdefghijklmnABCDEFGHIJKLMN1234567890!@#$%^&*(");
        }
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setBinaryStream(1, new ByteInputStream(new byte[0]));
        stmt.setString(2, buf.toString());
        stmt.setTime(3, new Time(currentMillis));
        stmt.setBigDecimal(4, new BigDecimal("56789.123"));
        stmt.setRowId(5, new MockRowId());
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals("<InputStream>", parameters.get(0));
        Assert.assertEquals(buf.substring(0, 97) + "...", parameters.get(1));
        Assert.assertEquals(dateText, parameters.get(2));
        Assert.assertEquals(56789.123, parameters.get(3));
        Assert.assertEquals("<com.alibaba.druid.mock.MockRowId>", parameters.get(4));
    }
}
Also used : MockNClob(com.alibaba.druid.mock.MockNClob) JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) MockRowId(com.alibaba.druid.mock.MockRowId) ByteInputStream(org.nutz.lang.util.ByteInputStream) MockBlob(com.alibaba.druid.mock.MockBlob) MockClob(com.alibaba.druid.mock.MockClob) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Time(java.sql.Time) Date(java.util.Date) BigDecimal(java.math.BigDecimal) ResultSet(java.sql.ResultSet) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with MockClob

use of com.alibaba.druid.mock.MockClob in project druid by alibaba.

the class StatFilterOpenClobCountTest2 method setUp.

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

        @Override
        public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, int columnIndex) throws SQLException {
            return new ClobProxyImpl(result.getStatementProxy().getConnectionProxy().getDirectDataSource(), result.getStatementProxy().getConnectionProxy(), new MockClob());
        }

        @Override
        public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, String columnLabel) throws SQLException {
            return new ClobProxyImpl(result.getStatementProxy().getConnectionProxy().getDirectDataSource(), result.getStatementProxy().getConnectionProxy(), new MockClob());
        }

        @Override
        public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, int columnIndex, java.util.Map<String, Class<?>> map) throws SQLException {
            return new ClobProxyImpl(result.getStatementProxy().getConnectionProxy().getDirectDataSource(), result.getStatementProxy().getConnectionProxy(), new MockClob());
        }

        @Override
        public Object resultSet_getObject(FilterChain chain, ResultSetProxy result, String columnIndex, java.util.Map<String, Class<?>> map) throws SQLException {
            return new ClobProxyImpl(result.getStatementProxy().getConnectionProxy().getDirectDataSource(), result.getStatementProxy().getConnectionProxy(), new MockClob());
        }
    });
    dataSource.init();
}
Also used : ClobProxyImpl(com.alibaba.druid.proxy.jdbc.ClobProxyImpl) SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) MockClob(com.alibaba.druid.mock.MockClob) FilterAdapter(com.alibaba.druid.filter.FilterAdapter) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy)

Example 5 with MockClob

use of com.alibaba.druid.mock.MockClob in project druid by alibaba.

the class FilterChainTest_Clob_2 method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    MockCallableStatement mockStmt = new MockCallableStatement(null, "") {

        @Override
        public Object getObject(int parameterIndex) throws SQLException {
            invokeCount++;
            return new MockClob();
        }
    };
    statement = new CallableStatementProxyImpl(new ConnectionProxyImpl(null, null, null, 0), mockStmt, "", 1);
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) MockClob(com.alibaba.druid.mock.MockClob) ConnectionProxyImpl(com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl) CallableStatementProxyImpl(com.alibaba.druid.proxy.jdbc.CallableStatementProxyImpl) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Aggregations

MockClob (com.alibaba.druid.mock.MockClob)7 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)4 SQLException (java.sql.SQLException)3 FilterAdapter (com.alibaba.druid.filter.FilterAdapter)2 FilterChain (com.alibaba.druid.filter.FilterChain)2 MockBlob (com.alibaba.druid.mock.MockBlob)2 MockResultSet (com.alibaba.druid.mock.MockResultSet)2 ClobProxyImpl (com.alibaba.druid.proxy.jdbc.ClobProxyImpl)2 ConnectionProxyImpl (com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl)2 ResultSetProxy (com.alibaba.druid.proxy.jdbc.ResultSetProxy)2 JdbcSqlStat (com.alibaba.druid.stat.JdbcSqlStat)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)1 MockNClob (com.alibaba.druid.mock.MockNClob)1 MockRowId (com.alibaba.druid.mock.MockRowId)1 CallableStatementProxyImpl (com.alibaba.druid.proxy.jdbc.CallableStatementProxyImpl)1 ClobProxy (com.alibaba.druid.proxy.jdbc.ClobProxy)1 StatementProxyImpl (com.alibaba.druid.proxy.jdbc.StatementProxyImpl)1