Search in sources :

Example 11 with MockPreparedStatement

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

the class PreparedStatementKeyTest method test_contains.

public void test_contains() throws Exception {
    DruidDataSource dataSource = new DruidDataSource();
    MockConnection conn = new MockConnection();
    PreparedStatementKey k1 = new PreparedStatementKey("x1", "c1", MethodType.M1);
    PreparedStatementPool pool = new PreparedStatementPool(new DruidConnectionHolder(dataSource, conn, 0));
    MockPreparedStatement raw = new MockPreparedStatement(null, null);
    pool.put(new PreparedStatementHolder(k1, raw));
    Assert.assertTrue(pool.get(k1) != null);
    Assert.assertTrue(pool.get(k1) != null);
}
Also used : PreparedStatementHolder(com.alibaba.druid.pool.PreparedStatementHolder) PreparedStatementPool(com.alibaba.druid.pool.PreparedStatementPool) PreparedStatementKey(com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey) DruidConnectionHolder(com.alibaba.druid.pool.DruidConnectionHolder) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 12 with MockPreparedStatement

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

the class ConnectionTest4 method test_prepCall_1.

public void test_prepCall_1() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
    MockPreparedStatement raw = null;
    {
        PreparedStatement stmt = conn.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        raw = stmt.unwrap(MockPreparedStatement.class);
        stmt.close();
    }
    {
        PreparedStatement stmt = conn.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        Assert.assertEquals(raw, stmt.unwrap(MockPreparedStatement.class));
        stmt.close();
    }
    conn.getConnectionHolder().toString();
    conn.getConnectionHolder().setLastActiveTimeMillis(0);
    conn.getConnectionHolder().toString();
    conn.getConnectionHolder().getUseCount();
    conn.getConnectionHolder().getTimeMillis();
    conn.close();
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) PreparedStatement(java.sql.PreparedStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement)

Example 13 with MockPreparedStatement

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

the class ConnectionTest4 method test_prepareStatement.

public void test_prepareStatement() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
    MockPreparedStatement raw = null;
    {
        PreparedStatement stmt = conn.prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        raw = stmt.unwrap(MockPreparedStatement.class);
        stmt.close();
    }
    {
        PreparedStatement stmt = conn.prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        Assert.assertSame(raw, stmt.unwrap(MockPreparedStatement.class));
        stmt.close();
    }
    conn.close();
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) PreparedStatement(java.sql.PreparedStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement)

Example 14 with MockPreparedStatement

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

the class DruidJdbcExtractorTest method test_spring.

public void test_spring() throws Exception {
    DruidDataSource dataSource = new DruidDataSource();
    try {
        DruidNativeJdbcExtractor extractor = new DruidNativeJdbcExtractor();
        dataSource.setUrl("jdbc:mock:xx1");
        Connection conn = dataSource.getConnection();
        Assert.assertEquals(true, extractor.getNativeConnection(conn) instanceof MockConnection);
        Statement stmt = conn.createStatement();
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(stmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(stmt) instanceof MockStatement);
        stmt.close();
        PreparedStatement preStmt = conn.prepareStatement("select 1");
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(preStmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(preStmt) instanceof MockPreparedStatement);
        Assert.assertEquals(true, extractor.getNativePreparedStatement(preStmt) instanceof MockPreparedStatement);
        preStmt.close();
        PreparedStatement callStmt = conn.prepareCall("select 1");
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(callStmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(callStmt) instanceof MockCallableStatement);
        Assert.assertEquals(true, extractor.getNativePreparedStatement(callStmt) instanceof MockCallableStatement);
        callStmt.close();
        conn.close();
    } finally {
        dataSource.close();
    }
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) MockStatement(com.alibaba.druid.mock.MockStatement) MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) MockStatement(com.alibaba.druid.mock.MockStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) PreparedStatement(java.sql.PreparedStatement) DruidNativeJdbcExtractor(com.alibaba.druid.support.spring.DruidNativeJdbcExtractor) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 15 with MockPreparedStatement

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

the class DruidJdbcExtractorTest method test_spring_filter.

public void test_spring_filter() throws Exception {
    DruidDataSource dataSource = new DruidDataSource();
    try {
        DruidNativeJdbcExtractor extractor = new DruidNativeJdbcExtractor();
        dataSource.setUrl("jdbc:mock:xx1");
        dataSource.setFilters("stat");
        Connection conn = dataSource.getConnection();
        Assert.assertEquals(true, extractor.getNativeConnection(conn) instanceof MockConnection);
        Statement stmt = conn.createStatement();
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(stmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(stmt) instanceof MockStatement);
        stmt.close();
        PreparedStatement preStmt = conn.prepareStatement("select 1");
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(preStmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(preStmt) instanceof MockPreparedStatement);
        Assert.assertEquals(true, extractor.getNativePreparedStatement(preStmt) instanceof MockPreparedStatement);
        preStmt.close();
        PreparedStatement callStmt = conn.prepareCall("select 1");
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(callStmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(callStmt) instanceof MockCallableStatement);
        Assert.assertEquals(true, extractor.getNativePreparedStatement(callStmt) instanceof MockCallableStatement);
        callStmt.close();
        conn.close();
    } finally {
        dataSource.close();
    }
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) MockStatement(com.alibaba.druid.mock.MockStatement) MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) MockStatement(com.alibaba.druid.mock.MockStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) PreparedStatement(java.sql.PreparedStatement) DruidNativeJdbcExtractor(com.alibaba.druid.support.spring.DruidNativeJdbcExtractor) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection)

Aggregations

MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)24 PreparedStatement (java.sql.PreparedStatement)14 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)10 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)9 Connection (java.sql.Connection)8 MockConnection (com.alibaba.druid.mock.MockConnection)6 MockDriver (com.alibaba.druid.mock.MockDriver)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 MockResultSet (com.alibaba.druid.mock.MockResultSet)4 MockStatement (com.alibaba.druid.mock.MockStatement)4 Statement (java.sql.Statement)4 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)2 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)2 DruidConnectionHolder (com.alibaba.druid.pool.DruidConnectionHolder)2 PreparedStatementKey (com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)2 PreparedStatementHolder (com.alibaba.druid.pool.PreparedStatementHolder)2 DruidNativeJdbcExtractor (com.alibaba.druid.support.spring.DruidNativeJdbcExtractor)2 EncodingConvertFilter (com.alibaba.druid.filter.encoding.EncodingConvertFilter)1 MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)1