/*
 * 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.cache;

import java.util.Set;

import anyframe.core.hibernate.AbstractConfigurationalTest;
import anyframe.core.hibernate.SetUpInitData;
import anyframe.sample.model.bidirection.Movie;

/**
 * TestCase Name : HibernateFirstLevelCacheTest<br>
 * <br>
 * [Description] : 하나의 트랜잭션 내에서는 Hibernate에서 기본 제공하는 1LC를 통해 한 번 조회된 객체에 대해서는 다시
 * DB에 접근하지 않고도 조회될 수 있음을 확인할 수 있다.<br>
 * [Main Flow]
 * <ul>
 * <li>#-1 Positive Case : MOVIE 테이블을 대상으로 한건의 Movie 정보를 조회한다.</li>
 * </ul>
 * 
 * @author SoYon Lim
 */
public class HibernateFirstLevelCacheTest extends AbstractConfigurationalTest {
	protected String getHibernateConfigLocation() {
		return "anyframe/core/hibernate/cache/hibernate.cfg.xml";
	}

	/**
	 * [Flow #-1] Positive Case : MOVIE 테이블을 대상으로 한건의 Movie 정보를 조회한다. 동일한
	 * Session 내에서 한 번 조회된 엔티티 정보는 1LC(1 Level Cache)에 저장되므로 다음 조회시 DB에 접근하지
	 * 않고도, Cache를 통해 조회할 수 있다.
	 * 
	 * @throws Exception
	 */
	public void testFindMovie() throws Exception {
		// 1. insert init data. hibernate put initial data into 1LC.
		newSession();
		SetUpInitData.initializeData(session);

		// 2. find a movie without accessing DB (using 1LC)
		Movie movie = (Movie) session.get(Movie.class, "MV-00001");

		Set categories = movie.getCategories();
		categories.iterator();

		// 3. find a movie again without accessing DB (using 1LC)
		movie = (Movie) session.get(Movie.class, "MV-00001");

		categories = movie.getCategories();
		categories.iterator();
		closeSession();
	}
}

