package com.alibaba.druid.bvt.pool.dynamic;

import com.alibaba.druid.PoolTestCase;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceStatLogger;
import com.alibaba.druid.pool.DruidDataSourceStatLoggerImpl;
import com.alibaba.druid.support.logging.Log;

import java.lang.reflect.Field;

public class DruidDataSourceStatLoggerImplTest extends PoolTestCase {
    private DruidDataSource dataSource;

    private Log statLog;

    protected void setUp() throws Exception {
        super.setUp();

        Field logField = DruidDataSourceStatLoggerImpl.class.getDeclaredField("LOG");
        logField.setAccessible(true);
        statLog = (Log) logField.get(null);

        statLog.resetStat();

        dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mock:xxx");
        dataSource.setTestOnBorrow(false);
        dataSource.setConnectionProperties("a=3;b=4");
        dataSource.setFilters("stat");
        dataSource.init();
    }

    protected void tearDown() throws Exception {
        dataSource.close();

        super.tearDown();
    }

    public void test_connectPropertiesChange() throws Exception {
        assertTrue(dataSource.isWrapperFor(DruidDataSourceStatLogger.class));
        assertTrue(dataSource.isWrapperFor(DruidDataSourceStatLoggerImpl.class));
        DruidDataSourceStatLoggerImpl loggerImpl = dataSource.unwrap(DruidDataSourceStatLoggerImpl.class);
        assertSame(statLog, loggerImpl.getLogger());

        dataSource.setConnectionProperties("druid.stat.loggerName=xxx");
        assertNotSame(statLog, loggerImpl.getLogger());
    }
}
