package me.chyxion.tigon.mybatis.test;

import lombok.extern.slf4j.Slf4j;
import me.chyxion.tigon.mybatis.test.model.Activity;
import org.junit.Test;
import org.springframework.data.repository.query.parser.Part;
import org.springframework.data.repository.query.parser.PartTree;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import org.xml.sax.helpers.DefaultHandler;
import me.chyxion.tigon.mybatis.BaseMapper;
import org.apache.ibatis.parsing.XPathParser;
import java.io.UnsupportedEncodingException;
import javax.xml.transform.TransformerException;
import me.chyxion.tigon.mybatis.test.model.App;
import org.springframework.core.GenericTypeResolver;
import me.chyxion.tigon.mybatis.test.mapper.AppMapper;
import org.apache.ibatis.builder.xml.XMLMapperEntityResolver;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * May 13, 2016 10:45:07 AM
 */
@Slf4j
public class TestDriver {

    @Test
    public void testMapperParse() throws TransformerException, UnsupportedEncodingException {
        Map<String, Object> stringObjectMap = new App().toMap();
        System.err.println();
        System.err.println(new App().cols());
    }

    @Test
    public void testGetModel() {
        Class<?>[] classes = GenericTypeResolver.resolveTypeArguments(AppMapper.class, BaseMapper.class);
        System.err.println(classes);
    }

    @Test
    public void testParseXml() throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        long timeStart = System.currentTimeMillis();
        Document doc = builder.parse(TestDriver.class
            .getResourceAsStream("/mybatis/mappers/app-mapper.xml"));
        long timeElapse = System.currentTimeMillis() - timeStart;
        System.err.println("Time Elapse: " + timeElapse);
    }

    @Test
    public void testParseXml2() {
        long timeStart = System.currentTimeMillis();
        XPathParser xPathParser = new XPathParser(TestDriver.class
                .getResourceAsStream("/mybatis/mappers/app-mapper.xml"),
                false, null, new XMLMapperEntityResolver());
        long timeElapse = System.currentTimeMillis() - timeStart;
        System.err.println("Time Elapse: " + timeElapse);
    }

    @Test
    public void testParseXml3() throws ParserConfigurationException, SAXException, IOException {
        long timeStart = System.currentTimeMillis();
        //创建一个解析XML的工厂对象
        SAXParserFactory parserFactory=SAXParserFactory.newInstance();
        //创建一个解析XML的对象
        SAXParser parser = parserFactory.newSAXParser();
        parser.parse(TestDriver.class
            .getResourceAsStream("/mybatis/mappers/app-mapper.xml"),
                new DefaultHandler());
        long timeElapse = System.currentTimeMillis() - timeStart;
        System.err.println("Time Elapse: " + timeElapse);
    }

    @Test
    public void testParse() {
        PartTree tree = new PartTree("findByIdAndHoldingDateOrderById", Activity.class);
        Iterator<PartTree.OrPart> iterator = tree.iterator();
        while (iterator.hasNext()) {
            log.info("OrPart [{}].", iterator.next());
        }
        for (Part part : tree.getParts()) {
            System.err.println(part);
        }
        System.err.println(tree);
        // tree = new PartTree("listByIdOrLastName", Customer.class);
        // System.err.println(tree);
    }
}
