/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package integration.anyframe.services.query;

import integration.anyframe.services.AbstractTest;
import integration.anyframe.services.query.vo.CustomerVO;

import java.util.Collection;
import java.util.Iterator;

import anyframe.core.query.IQueryService;

/**
 * DefaultQueryService가 제공하는 기능을 테스트하기 위한 샘플 테스트 코드
 */
public class QueryServiceResultSetMapperTest extends AbstractTest {
	/**
	 * 테스트 수행을 위한 main
	 */
	public static void main(String[] args) throws Exception {
		QueryServiceResultSetMapperTest queryTest = new QueryServiceResultSetMapperTest();
		// 1. initialize context
		queryTest.setup();
		// 2. testFindWithCustomResultSetMapper();
		queryTest.testFindWithCustomResultSetMapper();
		// 3. close context
		queryTest.teardown();

		System.out.println("Successful!!!!!");
	}

	protected void setup() {
		super.setup();
		try {
			testInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 테스트 코드를 샐행하기 위한 data table 생성
	 */
	public void testInit() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");

		try {
			queryService.updateBySQL("DROP TABLE TBL_CUSTOMER",
					new String[] {}, new Object[] {});
		} catch (Exception e) {
			System.out.println("Fail to DROP Table.");
		}

		queryService.updateBySQL("CREATE TABLE TBL_CUSTOMER ( "
				+ "SSNO varchar(13) NOT NULL, " + "NAME varchar(30), "
				+ "ADDRESS varchar(20), " + "PRIMARY KEY (SSNO))",
				new String[] {}, new Object[] {});

		queryService
				.updateBySQL(
						"INSERT INTO TBL_CUSTOMER VALUES('1234567890123','test1','Seoul')",
						new String[] {}, new Object[] {});
		queryService
				.updateBySQL(
						"INSERT INTO TBL_CUSTOMER VALUES('1234567890124','test2','Seoul')",
						new String[] {}, new Object[] {});
		queryService
				.updateBySQL(
						"INSERT INTO TBL_CUSTOMER VALUES('1234567890125','test3','Seoul')",
						new String[] {}, new Object[] {});
	}

	/**
	 * QueryService의 find() 메소드를 호출하여 매핑 XML에 정의된 쿼리문을 실행시키고, 매핑 XML에 정의된
	 * IResultSestMapper 유형의 Mapper를 이용하여, 결과값이 매핑되는지 체크하기 위한 테스트 코드
	 */
	public void testFindWithCustomResultSetMapper() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");

		// execute query
		Collection rtList = queryService.find(
				"findCustomerWithResultSetMapper", new Object[] { "%123456%" });
		// assert a size of result
		if (rtList.size() != 3)
			throw new Exception("Fail to select with custom ResultSetMapper.");

		// assert in detail
		Iterator resultItr = rtList.iterator();
		while (resultItr.hasNext()) {
			CustomerVO customer = (CustomerVO) resultItr.next();
			if (!customer.getAddr().equals("Seoul"))
				throw new Exception("Fail to compare result in defail.");
		}
	}

	protected String[] getConfigLocations() {
		return new String[] {
				"classpath*:/common/applicationContext-*.xml",
				"classpath*:/services/datasource/applicationContext-datasource-common.xml",
				"classpath*:/services/query/applicationContext-query-common.xml",
				"classpath*:/services/query/applicationContext-query-sqlloader.xml" };
	}
}

