/*
 * 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.servicelocator;

import integration.anyframe.services.AbstractTest;

import javax.sql.DataSource;

import anyframe.core.locator.IServiceLocator;

/**
 * ServiceLocatorService가 제공하는 기능을 테스트하기 위한 샘플 테스트 코드
 */
public class ServiceLocatorServiceTest extends AbstractTest {
	/**
	 * 테스트 수행을 위한 main
	 */
	public static void main(String[] args) throws Exception {

		ServiceLocatorServiceTest locatorTest = new ServiceLocatorServiceTest();

		// 1. initialize context
		locatorTest.setup();
		// 2. test lookup datasource
		locatorTest.testLookupDataSource();
		// 3. test lookup EJB
		locatorTest.testLookupEJBHome();
		// 4. close context
		locatorTest.teardown();

		System.out.println("Successful!!!!!!");
	}

	/**
	 * Service Locator Service를 통해 Naming Server에 정의된 DataSource 객체를 lookup하는
	 * 테스트
	 */
	public void testLookupDataSource() throws Exception {
		IServiceLocator serviceLocator = (IServiceLocator) context
				.getBean("serviceLocator");
		DataSource dataSource = (DataSource) serviceLocator.getDataSource(
				"AnyframeDS", "default");
		if (dataSource == null)
			throw new Exception("fail to lookup datasource.");
		dataSource.getConnection().close();
	}

	/**
	 * Service Locator Service를 통해 Naming Server에 배포된 EJB Home 객체를 lookup하는 테스트
	 */
	public void testLookupEJBHome() throws Exception {
		IServiceLocator serviceLocator = (IServiceLocator) context
				.getBean("serviceLocator");

		Object traderHome = serviceLocator.getEJBHome(
				"ejb20-statefulSession-TraderHome", "default");

		if (traderHome == null)
			throw new Exception("fail to lookup EJB Home.");
	}

	protected String[] getConfigLocations() {
		return new String[] { "classpath*:/common/applicationContext-*.xml",
		"classpath*:/services/locator/applicationContext-*.xml",
		"classpath*:/services/cache/applicationContext-*.xml"};
	}	
}

