/*
 * 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 anyframe.common.Page;
import anyframe.core.query.IQueryService;

/**
 * DefaultQueryService가 제공하는 기능을 테스트하기 위한 샘플 테스트 코드
 */
public class QueryServiceTestAbstractDAO extends AbstractTest {
	public CustomerDAO customerDAO = null;

	/**
	 * 테스트 수행을 위한 main
	 */
	public static void main(String[] args) throws Exception {
		QueryServiceTestAbstractDAO queryTest = new QueryServiceTestAbstractDAO();
		// 1. initialize context
		queryTest.setup();
		// 2. testInsertQuery();
		queryTest.testInsertQuery();
		// 3. testSelectQuery();
		queryTest.testSelectQuery();
		queryTest.testSelectPagingQuery();
		// 4. testUpdateQuery();
		queryTest.testUpdateQuery();
		// 5. testDeleteQuery();
		queryTest.testDeleteQuery();

		// 6. 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");
		queryService.updateBySQL("DROP TABLE TBL_CUSTOMER IF EXISTS",
				new String[] {}, new Object[] {});
		queryService.updateBySQL("CREATE TABLE TBL_CUSTOMER ( "
				+ "SSNO varchar(13) NOT NULL, " + "NAME varchar(20), "
				+ "ADDRESS varchar(20), " + "PRIMARY KEY (SSNO))",
				new String[] {}, new Object[] {});

		customerDAO = (CustomerDAO) context.getBean("customerDAO");
	}

	/**
	 * AbstractDAO를 통해 DB에 신규 데이터를 입력하는 테스트 코드
	 */
	public void testInsertQuery() throws Exception {
		CustomerVO vo1 = new CustomerVO("1234567890123", "AAAAA", "seoul");
		// 매핑 XML 파일에 정의되어 있는 query id를 이용하여 INSERT를 실행한다.
		customerDAO.createCustomer(vo1);
		CustomerVO vo2 = new CustomerVO("2345678901234", "BBBBB", "inchun");
		customerDAO.createCustomer(vo2);

	}

	/**
	 * AbstractDAO를 통해 DB에 입력된 데이터를 조회하는 테스트 코드
	 */
	public void testSelectQuery() throws Exception {
		String ssno = "1234567890123";
		CustomerVO vo = null;

		// 매핑 XML 파일에 정의되어 있는 query id를 이용하여 SELECT를 실행한다.
		vo = customerDAO.selectCustomer(ssno);
		if (!vo.getSsno().equals(ssno)) {
			throw new Exception("Select query failed");
		}

	}

	/**
	 * AbstractDAO를 통해 DB에 입력된 데이터를 페이징 처리하기 위한 테스트 코드
	 */
	public void testSelectPagingQuery() throws Exception {
		CustomerVO vo = new CustomerVO("2345678901234", "", "");

		// 매핑 XML 파일에 정의되어 있는 query id를 이용하여 SELECT를 실행하고,
		// 페이징 처리된 실행 결과를 얻는다.
		Page page = customerDAO.selectCustomerList(vo, 1, 2, 10);
		if (page.getSize() != 1) {
			throw new Exception("Select paging query failed");
		}

	}

	/**
	 * AbstractDAO를 통해 DB에 입력된 데이터를 수정하는 테스트 코드
	 */
	public void testUpdateQuery() throws Exception {

		// 매핑 XML 파일에 정의되어 있는 query id를 이용하여 UPDATE를 실행한다.
		CustomerVO vo = new CustomerVO("1234567890123", "AAAAA", "busan");
		int rs = customerDAO.updateCustomer(vo);

		if (rs == -1) {
			throw new Exception("Update query failed");
		}
	}

	/**
	 * AbstractDAO를 통해 DB에 입력된 데이터를 삭제하는 테스트 코드
	 */
	public void testDeleteQuery() throws Exception {
		CustomerVO vo = new CustomerVO("1234567890123", "", "");
		// 매핑 XML 파일에 정의되어 있는 query id를 이용하여 DELETE를 실행한다.
		int rs = customerDAO.deleteCustomer(vo);

		if (rs == -1) {
			throw new Exception("Delete query failed");
		}
	}

	protected String[] getConfigLocations() {
		return new String[] {
				"classpath*:/common/applicationContext-*.xml",
				"classpath*:/applications/customer/applicationContext-*.xml",
				"classpath*:/services/datasource/applicationContext-*.xml",
				"classpath*:/services/query/applicationContext-query-common.xml",
				"classpath*:/services/query/applicationContext-query-sqlloader-abstractdao.xml" };
	}
}

