Java tutorial
/* * Copyright (c) 2009 - 2010. School of Information Technology and Electrical * Engineering, The University of Queensland. This software is being developed * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project. * PODD is a National e-Research Architecture Taskforce (NeAT) project * co-funded by ANDS and ARCS. * * PODD 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 3 of the License, or * (at your option) any later version. * * PODD 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. * * You should have received a copy of the GNU General Public License * along with PODD. If not, see <http://www.gnu.org/licenses/>. */ package podd.dataaccess; import static podd.util.db.HibernateTestUtil.clearDB; import static podd.util.db.HibernateTestUtil.populateUser; import static podd.util.db.HibernateTestUtil.setupDefaultRepoRoles; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.Before; import org.springframework.orm.hibernate3.SessionFactoryUtils; import org.springframework.orm.hibernate3.SessionHolder; import org.springframework.transaction.support.TransactionSynchronizationManager; import podd.model.EntityFactory; import podd.model.entity.PoddEntity; import podd.model.user.User; import podd.model.user.search.UserSearchCriteria; import podd.model.validation.EntityValidator; import podd.owl.OntologyHelper; import podd.util.PoddApplicationContext; import podd.util.fedora.FedoraRepositoryUtil; import podd.util.fedora.PIDGenerator; /** * @author Yuan-Fang Li * @version $Id$ */ public abstract class AbstractDAOUnitTest<T> { protected static final PoddApplicationContext PODD_CONTEXT = PoddApplicationContext.getPoddContext(); protected T entity; protected DAO<T> dao; protected User user; protected DAO<User> userDAO; protected EntityDAO pidDao; protected PIDGenerator pidGenerator; protected EntityFactory entityFactory; protected OntologyHelper ontologyHelper; protected EntityValidator entityValidator; protected FedoraRepositoryUtil fedoraUtil; @Before public void setUp() throws Exception { // To avoid LazyInitializationException SessionFactory sessionFactory = PODD_CONTEXT.getSessionFactory(); if (!TransactionSynchronizationManager.hasResource(sessionFactory)) { Session session = SessionFactoryUtils.getSession(sessionFactory, true); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); } pidDao = PODD_CONTEXT.getPIDHolderDAO(); fedoraUtil = PODD_CONTEXT.getFedoraUtil(); entityFactory = PODD_CONTEXT.getEntityFactory(); ontologyHelper = PODD_CONTEXT.getOntologyHelper(); entityValidator = PODD_CONTEXT.getEntityValidator(); pidGenerator = PODD_CONTEXT.getHibernatePIDGenerator(); clearDB(PoddEntity.class); clearDB(UserSearchCriteria.class); clearDB(User.class); clearDB("RepositoryRole"); setupDefaultRepoRoles(); user = entityFactory.createUser(null); populateUser(user); userDAO = getUserDAO(); userDAO.save(user); entity = getEntity(); } public void tearDown() throws Exception { // Release the session SessionFactory sessionFactory = PODD_CONTEXT.getSessionFactory(); SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .unbindResource(sessionFactory); SessionFactoryUtils.releaseSession(sessionHolder.getSession(), sessionFactory); } protected DAO<User> getUserDAO() { return PODD_CONTEXT.getHibernateUserDAO(); } protected abstract T getEntity() throws Exception; }