import com.izouma.awesomeadmin.dao.CommonQueryMapper;
import com.izouma.awesomeadmin.datasource.DataSource;
import com.izouma.awesomeadmin.model.CommonQuery;
import com.izouma.awesomeadmin.util.DatabaseUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.GenericXmlContextLoader;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Map;

@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext.xml"}, loader = GenericXmlContextLoader.class)
@Transactional
public class DatabaseServiceTest {

    @Autowired
    private CommonQueryMapper commonQueryMapper;

    @Test
    public void testLoadDatabaseTables() {

        List<String> tables = DatabaseUtil.loadDatabaseTables(null);

        System.out.println(tables);
    }

    @Test
    public void test() throws NoSuchFieldException, IllegalAccessException {
        CommonQuery commonQuery = new CommonQuery();
        commonQuery.setTableName("test_aaa");

        commonQueryMapper.getTableListAll(commonQuery);

        //获取val字段上的Foo注解实例
        DataSource foo = CommonQueryMapper.class.getAnnotation(DataSource.class);
        //获取Foo注解实例的 value 属性值
        String value = foo.value();
        //打印该值
        System.out.println("修改之前的注解值：" + value);

        System.out.println("------------以下是修改注解的值------------");

        //获取 foo 这个代理实例所持有的 InvocationHandler
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(foo);
        // 获取 AnnotationInvocationHandler 的 memberValues 字段
        Field declaredField = invocationHandler.getClass().getDeclaredField("memberValues");
        // 因为这个字段事 private final 修饰，所以要打开权限
        declaredField.setAccessible(true);
        // 获取 memberValues
        Map memberValues = (Map) declaredField.get(invocationHandler);
        // 修改 value 属性值
        memberValues.put("value", "dataSourceSqlserver");
        // 获取 foo 的 value 属性值
        String newValue = foo.value();
        System.out.println("修改之后的注解值：" + newValue);

        commonQueryMapper.getTableListAll(commonQuery);


    }


}
