Search in sources :

Example 1 with IUserService

use of com.alibaba.druid.spring.IUserService in project druid by alibaba.

the class SpringIbatisFilterTest method test_spring.

public void test_spring() throws Exception {
    Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/alibaba/druid/pool/ibatis/spring-config-ibatis.xml");
    DataSource dataSource = (DataSource) context.getBean("dataSource");
    {
        Connection conn = dataSource.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute("CREATE TABLE sequence_seed (value INTEGER, name VARCHAR(50) PRIMARY KEY)");
        stmt.close();
        conn.close();
    }
    {
        Connection conn = dataSource.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute("CREATE TABLE t_User (id BIGINT PRIMARY KEY, name VARCHAR(50))");
        stmt.close();
        conn.close();
    }
    {
        Connection conn = dataSource.getConnection();
        conn.setAutoCommit(false);
        Statement stmt = conn.createStatement();
        stmt.execute("insert into sequence_seed (value ,name) values (0, 'druid-spring-test')");
        stmt.close();
        conn.commit();
        conn.close();
    }
    // user-service
    IUserService service = (IUserService) context.getBean("user-service");
    User user = new User();
    user.setName("xx");
    service.addUser(user);
    TestFilter filter = (TestFilter) context.getBean("test-filter");
    Assert.assertEquals(2, filter.getConnectCount());
    {
        Connection conn = dataSource.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute("DROP TABLE sequence_seed");
        stmt.close();
        conn.close();
    }
    {
        Connection conn = dataSource.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute("DROP TABLE t_User");
        stmt.close();
        conn.close();
    }
    Assert.assertEquals(1, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
    Map<String, Object> wallStats = DruidStatService.getInstance().getWallStatMap(Collections.<String, String>emptyMap());
    System.out.println("wall-stats : " + JSONUtils.toJSONString(wallStats));
    context.close();
    Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
}
Also used : User(com.alibaba.druid.spring.User) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Statement(java.sql.Statement) IUserService(com.alibaba.druid.spring.IUserService) Connection(java.sql.Connection) DataSource(javax.sql.DataSource)

Aggregations

IUserService (com.alibaba.druid.spring.IUserService)1 User (com.alibaba.druid.spring.User)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 DataSource (javax.sql.DataSource)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1