/*
 * 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.Country;
import anyframe.sample.model.bidirection.Movie;

/**
 * TestCase Name : HibernateSecondLevelCacheTest<br>
 * <br>
 * [Description] : Hibernate 속성 정의 파일 내에 hibernate.cache.use_second_level_cache,
 * hibernate.cache.provider_class 등을 정의하고, 2LC에 저장되어야 할 객체에 대해 &lt;cache&gt; 속성을
 * 정의한 후, DEBUG 모드에서 테스트케이스를 실행시켜보면서 2nd Level Cache 적용으로 인해 DB에 접근하지 않고도 2LC를
 * 통해 객체가 조회되는 것을 살펴볼 수 있다.<br>
 * [Main Flow]
 * <ul>
 * <li>#-1 Positive Case : COUNTRY 테이블을 대상으로 한건의 Country 정보를 조회한다.</li>
 * <li>#-2 Positive Case : MOVIE 테이블을 대상으로 한건의 Movie 정보를 조회한다.</li>
 * </ul>
 * 
 * @author SoYon Lim
 */
public class HibernateSecondLevelCacheTest extends AbstractConfigurationalTest {
	protected String getHibernateConfigLocation() {
		return "anyframe/core/hibernate/cache/hibernate.cfg.xml";
	}

	/**
	 * [Flow #-1] Positive Case : COUNTRY 테이블을 대상으로 한건의 Country 정보를 조회한다. 한
	 * 어플리케이션 내에서 한 번 조회된 엔티티 정보는 2LC(2 Level Cache)에 저장되므로 다음 조회시 DB에 접근하지 않고도,
	 * Cache를 통해 조회할 수 있다.
	 * 
	 * @throws Exception
	 */
	public void testFindCountry() throws Exception {
		// 1. insert init data. hibernate put initial data into 1LC.
		newSession();
		SetUpInitData.initializeData(session);
		closeSession();

		// 2. find a movie without accessing DB (using 2LC)
		newSession();
		Country country = (Country) session.get(Country.class, "COUNTRY-0001");

		Set movies = country.getMovies();
		movies.iterator();
		closeSession();

		// 3. find a movie again without accessing DB (using 2LC)
		newSession();
		country = (Country) session.get(Country.class, "COUNTRY-0001");

		movies = country.getMovies();
		movies.iterator();
		closeSession();
	}

	/**
	 * [Flow #-2] Positive Case : MOVIE 테이블을 대상으로 한건의 Movie 정보를 조회한다. Movie 관련
	 * Hibernate Mapping XML 파일 내에 2LC 사용 여부를 정의하지 않았으므로, 다른 Session 에서 해당 객체를
	 * 조회할 경우 매번 DB에 접근하게 된다.
	 * 
	 * @throws Exception
	 */
	public void testFindMovie() throws Exception {
		// 1. insert init data. hibernate put initial data into 1LC.
		newSession();
		SetUpInitData.initializeData(session);
		closeSession();

		// 2. find a movie with accessing DB (not using 2LC)
		newSession();
		Movie movie = (Movie) session.get(Movie.class, "MV-00001");

		Set categories = movie.getCategories();
		categories.iterator();
		closeSession();

		// 3. find a movie agagin with accessing DB (not using 2LC)
		newSession();
		movie = (Movie) session.get(Movie.class, "MV-00001");

		categories = movie.getCategories();
		categories.iterator();
		closeSession();
	}
}

