package me.dslztx.assist.dao;

import javax.sql.DataSource;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.pool.DruidDataSource;

import junit.framework.Assert;
import me.dslztx.assist.client.mysql.DaoFactory;

public class DaoFactoryTest {

    private static final Logger logger = LoggerFactory.getLogger(DaoFactoryTest.class);

    @Test
    public void obtainDao() throws Exception {
        try {
            DataSource dataSource = new DruidDataSource();
            System.out.println(dataSource.hashCode());

            UserDao userDaoFirst = DaoFactory.obtainDao(dataSource, UserDao.class);

            UserDao userDaoSecond = DaoFactory.obtainDao(dataSource, UserDao.class);

            Assert.assertTrue(userDaoFirst == userDaoSecond);

            userDaoFirst.insertUser(new User());
            userDaoSecond.insertUser(new User());
        } catch (Exception e) {
            logger.error("", e);
            Assert.fail();
        }
    }

    public static class UserDao extends Dao {

        public void insertUser(User user) {
            DataSource dataSource = obtainDataSource();
            // 使用dataSource把User插入到数据库

            System.out.println(dataSource.hashCode());
        }
    }

    static class User {

        Long id;
        String name;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}
@@ -50,24 +50,139 @@
 ataSource;%0A%0A
+import org.apache.ibatis.annotations.Select;%0Aimport org.apache.ibatis.session.SqlSession;%0Aimport org.junit.Ignore;%0A
 import org.j
@@ -192,16 +192,16 @@
 t.Test;%0A
-
 import o
@@ -377,16 +377,118 @@
 Factory;
+%0Aimport me.dslztx.assist.client.mysql.DataSourceFactory;%0Aimport me.dslztx.assist.util.CloseableAssist;
 %0A%0Apublic
@@ -1225,24 +1225,529 @@
    %7D%0A    %7D%0A%0A
+    @Test%0A    @Ignore%0A    public void test2() %7B%0A        try %7B%0A            DataSource dataSource = DataSourceFactory.obtainDataSource(%22in%22);%0A%0A            TestDao dao = DaoFactory.obtainDao(dataSource, TestDao.class);%0A%0A            Assert.assertTrue(dao.count() == 50);%0A        %7D catch (Exception e) %7B%0A            logger.error(%22%22, e);%0A            Assert.fail();%0A        %7D%0A    %7D%0A%0A    public static interface TestMapper extends Mapper %7B%0A        @Select(%22select count(*) from User%22)%0A        int count();%0A    %7D%0A%0A
     public s
@@ -2355,9 +2355,502 @@
 %0A    %7D%0A%0A
+    public static class TestDao extends Dao %7B%0A%0A        public int count() %7B%0A            SqlSession session = obtainSqlSessionFactory().openSession();%0A            try %7B%0A                TestMapper testMapper = session.getMapper(TestMapper.class);%0A                return testMapper.count();%0A            %7D catch (Exception e) %7B%0A                e.printStackTrace();%0A            %7D finally %7B%0A                CloseableAssist.closeQuietly(session);%0A            %7D%0A            return -1;%0A        %7D%0A    %7D%0A
 %7D

add test2