/*
 * 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.datasource;

import integration.anyframe.services.AbstractTest;

import java.sql.Connection;

import javax.sql.DataSource;

//주의 : HSQL DB를 띄운후 실행시킨다.

/**
 * DatasourceService가 제공하는 기능을 테스트하기 위한 샘플 테스트 코드
 */
public class DatasourceServiceTest extends AbstractTest {
	/**
	 * 테스트 수행을 위한 main
	 */
	public static void main(String[] args) throws Exception {
		DatasourceServiceTest datasourceTest = new DatasourceServiceTest();

		// 1. initialize context
		datasourceTest.setup();

		// 2. test (bean id로 dataSource-dbcp, dataSource-c3p0, dataSource-jdbc를 받아와서 testGetConnection 메소드의 파라미터로 넘겨준다.)
		datasourceTest.testGetConnection("dataSource-dbcp");
		datasourceTest.testGetConnection("dataSource-c3p0");
		datasourceTest.testGetConnection("dataSource-jdbc");
		// 3. close context
		datasourceTest.teardown();
		System.out.println("Successful!!!");
	}
	/**
	 * 각각의 DataSourceService를 통해 Connection 객체를 구하는 테스트 
	 */
	public void testGetConnection(String beanId)throws Exception {
		// 1. 각각의 beanId(dataSource-dbcp, dataSource-c3p0, dataSource-jdbc)에 대한 
		// DataSource 객체를 생성한다.
		DataSource datasource = (DataSource) context.getBean(beanId);
		// 2. 생성된 DataSource로 부터 connection을 얻는다.
		Connection conn = datasource.getConnection();
		if (conn==null)
			throw new Exception("fail to get Connection.");
	}
	protected String[] getConfigLocations() {
		// TODO Auto-generated method stub
		return new String[]{"classpath*:/common/applicationContext-*.xml","classpath*:/services/datasource/applicationContext-*.xml"};
	}
}

