package com.qa.framework.core;

import com.library.common.IOHelper;
import com.qa.framework.bean.DataConfig;
import com.qa.framework.bean.TestData;
import com.qa.framework.config.PropConfig;
import com.qa.framework.library.multithread.WorkerThread;
import org.apache.log4j.Logger;
import org.testng.annotations.DataProvider;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2016/6/23.
 */
public class TestXmlData {
    private static final Logger logger = Logger.getLogger(TestXmlData.class);

    /**
     * Xml factory data iterator.
     *
     * @return the iterator
     */
    @DataProvider(name = "xmlFactoryData")
    public static Iterator<Object[]> xmlFactoryData() throws InterruptedException {
        List<Object[]> xmldata = new ArrayList<Object[]>();
        List<String> files = getTestCaseFiles();
        if (PropConfig.getIsMultithread()){
            WorkerThread workerThread=new WorkerThread();
//            HandleThread.buildThreadPool(workerThread, files.size());
            xmldata=workerThread.getXmlDate();
            for (String filePath : files) {

            }
        }else {
            for (String filePath : files) {
                DataConvertor dataConvertor = new DataConvertor(filePath);
                DataConfig dataConfig = dataConvertor.getDataConfig();
                ParamValueProcessor paramValueProcessor = new ParamValueProcessor(dataConfig);
                paramValueProcessor.process();
                List<TestData> testDataList = dataConfig.getTestDataList();
                for (TestData data : testDataList) {
                    Object[] d = {data, dataConfig.getUrl(), dataConfig.getHttpMethod()};
                    xmldata.add(d);
                }
            }
        }

        return xmldata.iterator();
    }


    /**
     * Gets test case files.
     *
     * @return the test case files
     */
    public static List<String> getTestCaseFiles() {
        String useDir = System.getProperty("user.dir");
        List<String> files = IOHelper.listFilesInDirectoryRecursive(useDir, "*.xml");
        List<String> testCaseXml = new ArrayList<String>();
        for (String filePath : files) {
            String contents = IOHelper.readFileToString(filePath);
            if (contents != null && contents.lastIndexOf("<DataConfig") > 0) {
                testCaseXml.add(filePath);
            }
        }
        return testCaseXml;
    }
}

@@ -228,28 +228,33 @@
 ithread.
-WorkerThread
+multiThreadHandle
 ;%0Aimport
@@ -940,217 +940,1033 @@
     
-WorkerThread workerThread=new WorkerThread();%0A//            HandleThread.buildThreadPool(workerThread, files.size());%0A            xmldata=workerThread.getXmlDate();%0A            for (String filePath : files) %7B%0A
+final List%3CObject%5B%5D%3E xmldataMulti = new ArrayList%3CObject%5B%5D%3E();%0A            for (final String filePath : files) %7B%0A                Runnable e= new Runnable()%7B%0A               String fileName;%0A                @Override%0A                public void run() %7B%0A                    DataConvertor dataConvertor = new DataConvertor(filePath);%0A                    DataConfig dataConfig = dataConvertor.getDataConfig();%0A                    ParamValueProcessor paramValueProcessor = new ParamValueProcessor(dataConfig);%0A                    paramValueProcessor.process();%0A                    for (TestData data : dataConfig.getTestDataList()) %7B%0A                        Object%5B%5D d = %7Bdata, dataConfig.getUrl(), dataConfig.getHttpMethod()%7D;%0A                        xmldataMulti.add(d);%0A                    %7D%0A                %7D%0A            %7D;%0A            multiThreadHandle.buildThreadPool(e);%0A            %7D%0A            if (multiThreadHandle.isEnd()) %7B%0A                xmldata=xmldataMulti;%0A                logger.info(%22processed xmldata is end%22);
 %0A   

update TestXmlData