Java tutorial
/*$Id: AbstractCommonHibernate3Tester.java 11969 2008-12-03 20:44:56Z jens $*/ /* **************************************************************************** * * * (c) Copyright 2008 ABM-utvikling * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.test.domainmodels.hibernate3; import java.util.Collection; import junit.framework.TestCase; import no.abmu.common.domain.Entity; import no.abmu.test.domainmodels.DeepCompare; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.orm.hibernate3.SessionFactoryUtils; import org.springframework.orm.hibernate3.SessionHolder; import org.springframework.transaction.support.TransactionSynchronizationManager; /** * AbstractCommonHibernate2Tester extends TestCase and add * some use full methods for testing of domain models with * use Hibernate2. * * User of the class has to supply Hibernate2 sessionFactory * for the domain model under test. * * @author Jens Vindvad, Jens.Vindvad@abm-utvikling.no * @author $Author: jens $ * @version $Rev: 11969 $ * @date $Date: 2008-12-03 21:44:56 +0100 (Wed, 03 Dec 2008) $ * @copyright ABM-Utvikling */ public class AbstractCommonHibernate3Tester extends TestCase { private static final Log logger = LogFactory.getLog(AbstractCommonHibernate3Tester.class); protected Session session, session2; protected Transaction transaction; protected SessionFactory sessionFactory; protected void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } protected void setUp() throws Exception { session = sessionFactory.openSession(); session2 = sessionFactory.openSession(); session = SessionFactoryUtils.getSession(sessionFactory, true); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); } protected void tearDown() throws Exception { TransactionSynchronizationManager.unbindResource(sessionFactory); SessionFactoryUtils.releaseSession(session, sessionFactory); SessionFactoryUtils.releaseSession(session2, sessionFactory); } protected void saveEntity(Entity entity) { session.save(entity); } protected void saveEntityFlushAndClear(Entity entity) { session.save(entity); session.flush(); session.clear(); } protected void saveEntities(Collection<Entity> entities) { for (Entity entity : entities) { saveEntity(entity); } } protected void saveEntitiesFlushClear(Collection<Entity> entities) { saveEntities(entities); session.flush(); session.clear(); } protected void saveEntitiesAndDeepCompareTesting(Collection<Entity> entities, DeepCompare deepCompare) throws Exception { saveEntitiesFlushClear(entities); deepCompare(entities, deepCompare); } protected void deepCompare(Collection<Entity> entities, DeepCompare deepCompare) throws Exception { DeepCompareTester.loadAndCompare(session, deepCompare, entities); } protected void flushClear() { session.flush(); session.clear(); } }