package jp.agentec.sinaburocast.service;

import java.util.List;

import javax.annotation.Resource;

import jp.agentec.sinaburocast.dto.TestDto;
import jp.agentec.sinaburocast.entity.Test;
import junit.framework.TestSuite;

import org.apache.commons.collections.CollectionUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.seasar.dao.unit.S2DaoTestCase;

public class TestServiceTest extends S2DaoTestCase {

	@Resource
	private TestService testService;

	public TestServiceTest(String string) {
		super(string);
	}

	public static TestSuite suite() {
		TestSuite suite = new TestSuite("ContentServiceTest");
		suite.addTest(new TestServiceTest("insert"));
		suite.addTest(new TestServiceTest("update"));
		suite.addTest(new TestServiceTest("findById"));
		suite.addTest(new TestServiceTest("findDtoById"));
		suite.addTest(new TestServiceTest("findTestListByName"));
		suite.addTest(new TestServiceTest("findTestDtoListByName"));
		suite.addTest(new TestServiceTest("findTestDtoListByNameSql"));
		suite.addTest(new TestServiceTest("findByTestTypeId"));
		suite.addTest(new TestServiceTest("findTestWithChild"));
		return suite;
	}


	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
        include("app.dicon");
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	public void insert() {
		Test test = new Test();
		test.testName = "instest1";
		test.testTypeId = 2;
		test.parentTestId = 1;
		testService.insert(test, "abc");
	}

	public void update() {
		Test test = testService.findById(1);
		test.testName = test.testName + "u";
		testService.update(test, "xyz");
	}

	public void findById() {
		System.out.println(testService.findById(1));
	}

	public void findDtoById() {
		System.out.println(testService.findDtoById(1));
	}

	public void findTestListByName() {
		List<Test> list =testService.findTestListByName("test");
		for (Test test : list) {
			System.out.println(test.testType.testTypeName);
			System.out.println(test);
		}
	}

	public void findTestDtoListByName() {
		List<TestDto> list =testService.findTestDtoListByName("test");
		for (TestDto testDto : list) {
			System.out.println(testDto);
		}
	}

	public void findTestDtoListByNameSql() {
		List<TestDto> list =testService.findTestDtoListByNameSql("test");
		for (TestDto testDto : list) {
			System.out.println(testDto);
		}
	}

	public void findByTestTypeId() {
		List<Test> list =testService.findByTestTypeId(2);
		for (Test test : list) {
			System.out.println(test);
		}
	}
	
	public void findTestWithChild() {
		List<Test> list =testService.findTestWithChild(1);
		for (Test test : list) {
			printTree(test, "");
		}
	}
	
	
	private void printTree(Test test, String tab) {
		System.out.println(tab + test);
		if (CollectionUtils.isEmpty(test.childList)) {
			return;
		}
		for (Test child : test.childList) {
			printTree(child, tab + " ");
		}
	}

}