Search in sources :

Example 21 with MockDriver

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

the class LogFilterTest method test_logFilter_1.

public void test_logFilter_1() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setRawUrl("jdbc:mock:");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(new MockDriver(), config);
    Log4jFilter log4jFilter = new Log4jFilter();
    log4jFilter.init(dataSource);
    config.getFilters().add(log4jFilter);
    setLogDisableAll(log4jFilter, true);
    CommonsLogFilter commonLogFilter = new CommonsLogFilter() {

        @Override
        public boolean isDataSourceLogEnabled() {
            return false;
        }

        @Override
        public boolean isConnectionLogEnabled() {
            return false;
        }

        @Override
        public boolean isStatementLogEnabled() {
            return false;
        }

        @Override
        public boolean isResultSetLogEnabled() {
            return false;
        }

        @Override
        public boolean isResultSetLogErrorEnabled() {
            return false;
        }

        @Override
        public boolean isResultSetNextAfterLogEnabled() {
            return false;
        }
    };
    commonLogFilter.init(dataSource);
    config.getFilters().add(commonLogFilter);
    setLogDisableAll(commonLogFilter, true);
    executeSQL(dataSource);
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) CommonsLogFilter(com.alibaba.druid.filter.logging.CommonsLogFilter) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) Log4jFilter(com.alibaba.druid.filter.logging.Log4jFilter)

Example 22 with MockDriver

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

the class ResultSetProxyImplTest method test_resultset.

public void test_resultset() throws Exception {
    MockDriver driver = new MockDriver();
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setUrl("");
    config.setRawUrl("jdbc:mock:");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(driver, config);
    {
        StatFilter filter = new StatFilter();
        filter.init(dataSource);
        config.getFilters().add(filter);
    }
    {
        Log4jFilter filter = new Log4jFilter();
        filter.init(dataSource);
        config.getFilters().add(filter);
    }
    Connection conn = dataSource.connect(null);
    conn.setClientInfo("name", null);
    Statement stmt = conn.createStatement();
    ResultSetProxy rs = (ResultSetProxy) stmt.executeQuery(sql);
    rs.insertRow();
    rs.refreshRow();
    rs.moveToInsertRow();
    rs.moveToCurrentRow();
    rs.next();
    rs.updateRef(1, null);
    rs.updateArray(1, null);
    rs.updateRowId(1, null);
    rs.updateNString(1, null);
    rs.updateNClob(1, (NClob) null);
    rs.updateNClob(1, (Reader) null);
    rs.updateNClob(1, (Reader) null, 0);
    rs.updateSQLXML(1, null);
    rs.updateNCharacterStream(1, null);
    rs.updateNCharacterStream(1, null, 0);
    rs.getArray("1");
    rs.updateRef("1", null);
    rs.updateArray("1", null);
    rs.updateRowId("1", null);
    rs.updateNString("1", null);
    rs.updateNClob("1", (NClob) null);
    rs.updateNClob("1", (Reader) null);
    rs.updateNClob("1", (Reader) null, 0);
    rs.updateSQLXML("1", null);
    rs.updateNCharacterStream("1", null);
    rs.updateNCharacterStream("1", null, 0);
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) Statement(java.sql.Statement) Log4jFilter(com.alibaba.druid.filter.logging.Log4jFilter) StatFilter(com.alibaba.druid.filter.stat.StatFilter) Connection(java.sql.Connection) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy)

Example 23 with MockDriver

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

the class FakeDriverTest method test_0.

public void test_0() throws Exception {
    String url = "jdbc:fake:x1";
    Properties info = new Properties();
    String sql = "SELECT 1";
    MockDriver driver = new MockDriver();
    Connection conn = driver.connect(url, info);
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    Assert.assertEquals(true, rs.next());
    Assert.assertEquals(1, rs.getInt(1));
    conn.close();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Properties(java.util.Properties)

Example 24 with MockDriver

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

the class CounterFilterTest method test_count_filter.

public void test_count_filter() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setUrl("");
    config.setRawUrl("jdbc:mock:");
    StatFilter filter = new StatFilter();
    MockDriver driver = new MockDriver();
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(driver, config);
    filter.init(dataSource);
    config.getFilters().add(filter);
    Connection conn = dataSource.connect(null);
    Statement stmt = conn.createStatement();
    ResultSetProxy rs = (ResultSetProxy) stmt.executeQuery(sql);
    rs.close();
    stmt.close();
    conn.close();
    conn.close();
    dataSource.getCompositeData();
    dataSource.getProperties();
    dataSource.getDataSourceMBeanDomain();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) Statement(java.sql.Statement) StatFilter(com.alibaba.druid.filter.stat.StatFilter) Connection(java.sql.Connection) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy)

Example 25 with MockDriver

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

the class DiscardTest method setUp.

protected void setUp() throws Exception {
    driver = new MockDriver() {

        public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
            if (failed) {
                throw new SQLException("", "", 1040);
            }
            return super.executeQuery(stmt, sql);
        }

        public Connection connect(String url, Properties info) throws SQLException {
            while (failed) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new SQLException(e.getMessage(), e);
                }
            }
            return super.connect(url, info);
        }
    };
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setTestOnBorrow(false);
    dataSource.setDriver(driver);
    dataSource.setDbType("mysql");
    dataSource.setMinIdle(1);
    dataSource.setMaxActive(1);
    dataSource.setFilters("log4j");
    dataSource.setExceptionSorter(new MySqlExceptionSorter());
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) SQLException(java.sql.SQLException) MySqlExceptionSorter(com.alibaba.druid.pool.vendor.MySqlExceptionSorter) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) Properties(java.util.Properties) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockStatementBase(com.alibaba.druid.mock.MockStatementBase)

Aggregations

MockDriver (com.alibaba.druid.mock.MockDriver)60 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)49 Connection (java.sql.Connection)17 SQLException (java.sql.SQLException)13 ResultSet (java.sql.ResultSet)10 Properties (java.util.Properties)9 MockConnection (com.alibaba.druid.mock.MockConnection)8 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)7 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)5 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)5 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)5 JdbcStatContext (com.alibaba.druid.stat.JdbcStatContext)5 Log4jFilter (com.alibaba.druid.filter.logging.Log4jFilter)4 CommonsLogFilter (com.alibaba.druid.filter.logging.CommonsLogFilter)3 MockResultSet (com.alibaba.druid.mock.MockResultSet)3 MockStatement (com.alibaba.druid.mock.MockStatement)3 Statement (java.sql.Statement)3 ObjectName (javax.management.ObjectName)3 StatFilter (com.alibaba.druid.filter.stat.StatFilter)2 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)2