package net.w_horse.excelpojo.converter;

import static org.junit.Assert.assertEquals;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import net.w_horse.excelpojo.AbstractTest;
import net.w_horse.excelpojo.ExcelPOJOBridge;
import net.w_horse.excelpojo.ExcelPOJOException;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.converter.AnnotationConverter;
import test.converter.AnnotationConverterPriority;


public class AnnotationConverterTest extends AbstractTest {
	/**
	 * アノテーションによる日付型のフォーマットの差異テスト
	 * @throws ParseException
	 *
	 */
	@Test
	public void testConverterDateFormat() throws ExcelPOJOException, IOException, ParseException {
		ExcelPOJOBridge bridge = new ExcelPOJOBridge(AnnotationConverter.class);
		AnnotationConverter targetBean = (AnnotationConverter) bridge.load("./files/upload/ParserTest.xls", "変換");

		assertEquals("変換 Data01", (new SimpleDateFormat("yyyy-MM-dd").parse("2010-08-05")),targetBean.getDate1());
		assertEquals("変換 DateStr01", "2010年08月06日",targetBean.getDateStr1());
		assertEquals("変換 DateStr02", "2010年08月07日",targetBean.getDateStr2());

		assertEquals("変換 boolean01", true, targetBean.isBoolean1());
		assertEquals("変換 boolean02", false,targetBean.isBoolean2());

		bridge.save(
				"./files/template/ParserTest_Template.xls",
				"変換",
				"./files/export/AnnotationParserTest_変換.xls",
				targetBean);

		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream("./files/export/AnnotationParserTest_変換.xls");
			HSSFWorkbook workBook = new HSSFWorkbook(new POIFSFileSystem(fileInputStream));
			HSSFSheet sheet = workBook.getSheet("変換");

			assertEquals("変換 Data01", "2010-08-05", getCellValue(sheet, 2, 4, CELL_TYPE_STRING));
			assertEquals("変換 DateStr01", "2010-08-06", getCellValue(sheet, 3, 4, CELL_TYPE_STRING));
			assertEquals("変換 DateStr02", (new SimpleDateFormat("yyyy-MM-dd").parse("2010-08-07")), getCellValue(sheet, 4, 4, CELL_TYPE_DATE));

			assertEquals("変換 boolean01", "true", getCellValue(sheet, 5, 4, CELL_TYPE_STRING));
			assertEquals("変換 boolean02", "false", getCellValue(sheet, 6, 4, CELL_TYPE_STRING));
		} finally {
			try { fileInputStream.close(); } catch (Exception e) { }
		}
	}

	/**
	 * XMLの定義がアノテーションを優先することを確認
	 * @throws ExcelPOJOException
	 * @throws IOException
	 * @throws ParseException
	 */
	@Test
	public void testConverterXMLPriorityThanAnnotation() throws ExcelPOJOException, IOException, ParseException {
		ApplicationContext context = new ClassPathXmlApplicationContext("./config/testConverterXMLPriorityThanAnnotation.xml");
		ExcelPOJOBridge bridge = (ExcelPOJOBridge)context.getBean("testConverterXMLPriorityThanAnnotation");
		AnnotationConverterPriority targetBean = (AnnotationConverterPriority) bridge.load("./files/upload/ParserTest.xls", "変換");

		assertEquals("変換 Data01", (new SimpleDateFormat("yyyy-MM-dd").parse("2010-08-05")),targetBean.getDate1());
		assertEquals("変換 DateStr01", "20100806",targetBean.getDateStr1());
		assertEquals("変換 DateStr02", "2010年08月06日",targetBean.getDateStr2());

		bridge.save(
				"./files/template/ParserTest_Template.xls",
				"変換",
				"./files/export/AnnotationParserTest_変換XMLPriority.xls",
				targetBean);

		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream("./files/export/AnnotationParserTest_変換XMLPriority.xls");
			HSSFWorkbook workBook = new HSSFWorkbook(new POIFSFileSystem(fileInputStream));
			HSSFSheet sheet = workBook.getSheet("変換");

			assertEquals("変換 Data01", "2010-08-05", getCellValue(sheet, 2, 4, CELL_TYPE_STRING));
			assertEquals("変換 DateStr01", "2010/8/6", getCellValue(sheet, 17, 4, CELL_TYPE_STRING));
			assertEquals("変換 DateStr02", "2010-08-06", getCellValue(sheet, 3, 4, CELL_TYPE_STRING));
		} finally {
			try { fileInputStream.close(); } catch (Exception e) { }
		}
	}

}
