Search in sources :

Example 1 with DruidDriver

use of com.alibaba.druid.proxy.DruidDriver in project druid by alibaba.

the class WrapImplTest method test_clone.

public void test_clone() throws Exception {
    Class.forName("com.alibaba.druid.proxy.DruidDriver");
    DruidDriver driver = (DruidDriver) DriverManager.getDriver(url);
    ConnectionProxyImpl connection = (ConnectionProxyImpl) driver.connect(url, new Properties());
    connection.getRawObject();
    FilterChain filterChain = (FilterChain) connection.createChain();
    filterChain.cloneChain();
    DataSourceProxyImpl dataSource = (DataSourceProxyImpl) connection.getDirectDataSource();
    dataSource.getId();
    Assert.assertEquals(4, dataSource.getProxyFilters().size());
    Assert.assertEquals(4, dataSource.getFilterClasses().length);
    Assert.assertNotNull(dataSource.getCreatedTime());
    Assert.assertTrue(dataSource.getCreatedTime().getTime() != 0);
    Assert.assertEquals("org.apache.derby.jdbc.EmbeddedDriver", dataSource.getRawDriverClassName());
    Assert.assertEquals(url, dataSource.getUrl());
    Assert.assertEquals("jdbc:derby:classpath:petstore-db", dataSource.getRawUrl());
    Assert.assertEquals(10, dataSource.getRawDriverMajorVersion());
    Assert.assertEquals(12, dataSource.getRawDriverMinorVersion());
    Class<?> mysql5ConnectionClass = Utils.loadClass("com.mysql.jdbc.Connection");
    if (mysql5ConnectionClass != null) {
        Assert.assertFalse(connection.isWrapperFor(mysql5ConnectionClass));
    }
    Assert.assertTrue(connection.isWrapperFor(ConnectionProxyImpl.class));
    Assert.assertTrue(connection.isWrapperFor(org.apache.derby.impl.jdbc.EmbedConnection.class));
    Assert.assertNotNull(connection.unwrap(ConnectionProxyImpl.class));
    Assert.assertNull(connection.unwrap(null));
    org.apache.derby.impl.jdbc.EmbedConnection derbyConnection = connection.unwrap(org.apache.derby.impl.jdbc.EmbedConnection.class);
    Assert.assertNotNull(derbyConnection);
    Statement statement = connection.createStatement();
    if (mysql5ConnectionClass != null) {
        Assert.assertFalse(statement.isWrapperFor(Class.forName("com.mysql.jdbc.Statement")));
    }
    Assert.assertFalse(statement.isWrapperFor(null));
    Assert.assertTrue(statement.isWrapperFor(org.apache.derby.impl.jdbc.EmbedStatement.class));
    org.apache.derby.impl.jdbc.EmbedStatement rayStatement = statement.unwrap(org.apache.derby.impl.jdbc.EmbedStatement.class);
    Assert.assertNotNull(rayStatement);
    statement.close();
}
Also used : DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) Statement(java.sql.Statement) FilterChain(com.alibaba.druid.filter.FilterChain) ConnectionProxyImpl(com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl) DruidDriver(com.alibaba.druid.proxy.DruidDriver) Properties(java.util.Properties)

Example 2 with DruidDriver

use of com.alibaba.druid.proxy.DruidDriver in project druid by alibaba.

the class DruidDriverTest method test_getRawDriver.

public void test_getRawDriver() throws Exception {
    DruidDriver driver = new DruidDriver();
    Assert.assertNotNull(driver.createDriver(MockDriver.class.getName()));
    {
        Exception error = null;
        try {
            driver.createDriver(null);
        } catch (Exception ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        Exception error = null;
        try {
            driver.createDriver(PrivateDriver.class.getName());
        } catch (Exception ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        Exception error = null;
        try {
            driver.createDriver(InitErrorDriver.class.getName());
        } catch (Exception ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
}
Also used : DruidDriver(com.alibaba.druid.proxy.DruidDriver)

Example 3 with DruidDriver

use of com.alibaba.druid.proxy.DruidDriver in project druid by alibaba.

the class DruidDriverTest method test_0.

public void test_0() throws Exception {
    String url = "jdbc:wrap-jdbc:filters=default,commonLogging,log4j:name=preCallTest:jdbc:fake:c1";
    Properties info = new Properties();
    DruidDriver driver = new DruidDriver();
    Connection conn = driver.connect(url, info);
    Assert.assertNotNull(conn);
    Assert.assertEquals("c1", conn.getCatalog());
    conn.setCatalog("c2");
    Assert.assertEquals("c2", conn.getCatalog());
    conn.setTransactionIsolation(100);
    Assert.assertEquals(100, conn.getTransactionIsolation());
    conn.close();
}
Also used : Connection(java.sql.Connection) Properties(java.util.Properties) DruidDriver(com.alibaba.druid.proxy.DruidDriver)

Example 4 with DruidDriver

use of com.alibaba.druid.proxy.DruidDriver in project druid by alibaba.

the class DriverTest method test_driver.

public void test_driver() throws Exception {
    String url_0 = "jdbc:wrap-jdbc:filters=default:name=driverTest:jdbc:derby:memory:driverTestDB;create=true";
    String url_1 = "jdbc:wrap-jdbc:filters=counter:name=driverTest:jdbc:derby:memory:driverTestDB;create=true";
    String url_2 = "jdbc:wrap-jdbc:filters=log4j:name=driverTest:jdbc:derby:memory:driverTestDB;create=true";
    String url_3 = "jdbc:wrap-jdbc:filters=commonLogging:name=driverTest:jdbc:derby:memory:driverTestDB;create=true";
    String url_4 = "jdbc:wrap-jdbc:driver=org.apache.derby.jdbc.EmbeddedDriver:filters=commonLogging:name=driverTest:jdbc:derby:memory:driverTestDB;create=true";
    Class.forName("com.alibaba.druid.proxy.DruidDriver");
    Connection connection = DriverManager.getConnection(url_0);
    connection.close();
    Driver driver = DriverManager.getDriver(url_0);
    DruidDriver driverWrapper = (DruidDriver) driver;
    Assert.assertEquals(4, driverWrapper.getMajorVersion());
    Assert.assertEquals(0, driverWrapper.getMinorVersion());
    Assert.assertEquals(true, driverWrapper.jdbcCompliant());
    Assert.assertTrue(driverWrapper.getConnectCount() > 0);
    Assert.assertNotNull(DruidDriver.getInstance());
    Assert.assertEquals("jdbc:wrap-jdbc:", driverWrapper.getAcceptPrefix());
    Assert.assertTrue(driverWrapper.getDataSourceUrls().length > 0);
    driverWrapper.getPropertyInfo(url_0, new Properties());
    Assert.assertFalse(driverWrapper.acceptsURL(null));
    Assert.assertFalse(driverWrapper.acceptsURL("xxx"));
    Assert.assertTrue(driverWrapper.acceptsURL(url_1));
    Connection connection_1 = DriverManager.getConnection(url_1);
    ConnectionProxy connection_wrapper_1 = connection_1.unwrap(ConnectionProxy.class);
    DataSourceProxy dataSource_1 = connection_wrapper_1.getDirectDataSource();
    Assert.assertEquals(1, dataSource_1.getProxyFilters().size());
    {
        Connection connection_2 = DriverManager.getConnection(url_2);
        ConnectionProxy connection_wrapper_2 = connection_2.unwrap(ConnectionProxy.class);
        DataSourceProxy dataSource_2 = connection_wrapper_2.getDirectDataSource();
        Assert.assertEquals(1, dataSource_2.getProxyFilters().size());
    }
    {
        Connection connection_3 = DriverManager.getConnection(url_3);
        ConnectionProxy connection_wrapper_3 = connection_3.unwrap(ConnectionProxy.class);
        DataSourceProxy dataSource_3 = connection_wrapper_3.getDirectDataSource();
        Assert.assertEquals(1, dataSource_3.getProxyFilters().size());
    }
    {
        Connection connection_4 = DriverManager.getConnection(url_4);
        ConnectionProxy connection_wrapper_4 = connection_4.unwrap(ConnectionProxy.class);
        DataSourceProxy dataSource_4 = connection_wrapper_4.getDirectDataSource();
        dataSource_4.getProxyFilters().toString();
        Assert.assertEquals(1, dataSource_4.getProxyFilters().size());
    }
}
Also used : DataSourceProxy(com.alibaba.druid.proxy.jdbc.DataSourceProxy) Connection(java.sql.Connection) DruidDriver(com.alibaba.druid.proxy.DruidDriver) Driver(java.sql.Driver) DruidDriver(com.alibaba.druid.proxy.DruidDriver) Properties(java.util.Properties) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Aggregations

DruidDriver (com.alibaba.druid.proxy.DruidDriver)4 Properties (java.util.Properties)3 Connection (java.sql.Connection)2 FilterChain (com.alibaba.druid.filter.FilterChain)1 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)1 ConnectionProxyImpl (com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl)1 DataSourceProxy (com.alibaba.druid.proxy.jdbc.DataSourceProxy)1 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)1 Driver (java.sql.Driver)1 Statement (java.sql.Statement)1