package org.bench4q.web.init;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.bench4q.web.model.ResultInfoModel;
import org.bench4q.web.model.TestResult;

public class TestResultHandle {
	private static final String SCRIPT_RESULT_FILE_NAME = "script_result.xml";
	private static final String SUT_RESULT_FILE_NAME = "SUT_result.xml";
	public static List<ResultInfoModel> scriptResultInfoModel;
	public static List<ResultInfoModel> sutInfoModel;
	static {
		String filePath;
		try {
			filePath = TestResultHandle.class.getClassLoader().getResource("")
					.toURI().getPath();

			scriptResultInfoModel = readTestResultFromFile(filePath
					+ SCRIPT_RESULT_FILE_NAME);
			sutInfoModel = readTestResultFromFile(filePath
					+ SUT_RESULT_FILE_NAME);
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static List<ResultInfoModel> readTestResultFromFile(String filePath)
			throws JAXBException, IOException {
		InputStreamReader is = new InputStreamReader(new FileInputStream(
				new File(filePath)), "UTF-8");
		BufferedReader reader = new BufferedReader(is);
		JAXBContext context = JAXBContext.newInstance(TestResult.class);
		Unmarshaller unmarshaller = context.createUnmarshaller();
		TestResult testResult = (TestResult) unmarshaller.unmarshal(reader);
		return testResult.getResultInfoModels();
	}

}
