/*
 * 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 anyframe.core.hibernate.transaction;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

import anyframe.core.hibernate.SetUpInitData;
import anyframe.sample.model.bidirection.Movie;

/**
 * TestCase Name : HibernateJTATransactionManager<br>
 * <br>
 * [Description] : Spring Framework에서 제공하는 JTA Transaction Manager 중의 하나인
 * WebLogicJtaTransactionManager를 이용하여 트랜잭션이 제대로 관리되는지 확인한다.<br>
 * [Main Flow]
 * <ul>
 * <li>#-1 Positive Case : WebLogicJtaTransactionManager Rollback을 이용하여, 초기화
 * 데이터의 입력 작업을 취소시킨 후, 데이터가 제대로 Rollback되었는지 검증한다.</li>
 * <li>#-2 Positive Case : WebLogicJtaTransactionManager Commit을 이용하여, 초기화 데이터의
 * 입력 작업을 DB에 반영시킨 후, 데이터가 제대로 Commit되었는지 검증한다.</li>
 * </ul>
 * 
 * @author SoYon Lim
 */
public class HibernateJTATransactionManagerTest extends
		AbstractTransactionalDataSourceSpringContextTests {
	protected String[] getConfigLocations() {
		return new String[] { "classpath:/anyframe/core/hibernate/transaction/jta/context-*.xml" };
	}

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/**
	 * [Flow #-1] Positive Case : WebLogicJtaTransactionManager Rollback을 이용하여,
	 * 초기화 데이터의 입력 작업을 취소시킨 후, 데이터가 제대로 Rollback되었는지 검증한다.
	 * 
	 * @throws Exception
	 *             throws exception which is from hibernate
	 */
	public void testRollback() throws Exception {
		// 1. insert init data
		Session session = sessionFactory.getCurrentSession();
		SetUpInitData.initializeData(session);

		// 2. rollback transaction
		isRollback();
		endTransaction();

		// 3. begin a new transaction
		startNewTransaction();

		// 4. check if insertion is rollbacked
		Movie movie = (Movie) sessionFactory.getCurrentSession().get(
				Movie.class, "MV-00001");
		assertNull("fail to rollback init data.", movie);
	}

	/**
	 * [Flow #-2] Positive Case : WebLogicJtaTransactionManager Commit을 이용하여,
	 * 초기화 데이터의 입력 작업을 DB에 반영시킨 후, 데이터가 제대로 Commit되었는지 검증한다.
	 * 
	 * @throws Exception
	 *             throws exception which is from hibernate
	 */
	public void testCommit() throws Exception {
		// 1. insert init data
		Session session = sessionFactory.getCurrentSession();
		SetUpInitData.initializeData(session);

		// 2. commit transaction
		setComplete();
		endTransaction();

		// 3. begin a new transaction
		startNewTransaction();

		// 4. check if insertion is successful
		Movie movie = (Movie) sessionFactory.getCurrentSession().get(
				Movie.class, "MV-00001");
		assertNotNull("fail to add a movie.", movie);
	}
}

