001package fr.ifremer.adagio.core.test;
002
003/*
004 * #%L
005 * SIH-Adagio Core for Allegro
006 * $Id: BaseDaoTest.java 11926 2014-01-13 11:20:10Z bl05b3e $
007 * $HeadURL: https://forge.ifremer.fr/svn/sih-adagio/trunk/adagio/core-allegro/src/test/java/fr/ifremer/adagio/core/dao/BaseDaoTest.java $
008 * %%
009 * Copyright (C) 2012 - 2013 Ifremer
010 * %%
011 * This program is free software: you can redistribute it and/or modify
012 * it under the terms of the GNU Affero General Public License as published by
013 * the Free Software Foundation, either version 3 of the License, or
014 * (at your option) any later version.
015 * 
016 * This program is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
019 * GNU General Public License for more details.
020 * 
021 * You should have received a copy of the GNU Affero General Public License
022 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
023 * #L%
024 */
025
026import javax.sql.DataSource;
027
028import org.apache.commons.lang3.StringUtils;
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.hibernate.Query;
032import org.hibernate.Session;
033import org.hibernate.SessionFactory;
034import org.hibernate.StatelessSession;
035import org.junit.After;
036import org.junit.Before;
037import org.junit.BeforeClass;
038import org.springframework.transaction.PlatformTransactionManager;
039import org.springframework.transaction.TransactionStatus;
040
041import fr.ifremer.adagio.core.service.ServiceLocator;
042
043public abstract class BaseDaoTest {
044
045    /** Logger. */
046    private static final Log log = LogFactory.getLog(BaseDaoTest.class);
047
048    private static PlatformTransactionManager transactionManager;
049
050    private TransactionStatus status;
051    
052    private boolean commitOnTearDown = true;
053
054    @BeforeClass
055    public static void beforeClass() {
056        transactionManager = getBean("transactionManager", PlatformTransactionManager.class);
057    }
058
059    @Before
060    public void setUp() throws Exception {
061        status = transactionManager.getTransaction(null);
062        if (log.isDebugEnabled()) {
063            log.debug("Session and transaction initialized");
064        }
065    }
066
067    @After
068    public void tearDown() throws Exception {
069        if (commitOnTearDown) {
070            transactionManager.commit(status);
071        }
072        else {
073            transactionManager.rollback(status);
074        }
075    }
076    
077    /**
078     * Allow to apply a commit on tear down (by default a rollback is applied)
079     * @param commitOnTearDown true to force to commit after unit test
080     */
081    protected void setCommitOnTearDown(boolean commitOnTearDown) {
082        this.commitOnTearDown = commitOnTearDown;
083    }
084
085    protected static <S> S getBean(String name, Class<S> serviceType) {
086        return ServiceLocator.instance().getService(name, serviceType);
087    }
088
089
090    protected Query createStatelessQuery(String hqlQuery) {
091        SessionFactory sessionFactory = getBean("sessionFactory", SessionFactory.class);
092        StatelessSession session = sessionFactory.openStatelessSession();
093        Query query = session.createQuery(hqlQuery);
094        return query;
095    }
096    
097    protected long countEntities(Class entityClass) {
098        return countEntities(entityClass, null, "");
099    }
100    protected long countEntities(Class entityClass, String alias, String whereClause) {
101        if (StringUtils.isNotBlank(whereClause)) {
102            whereClause = " " + alias + " where " + whereClause;
103        }
104        Query query = createStatelessQuery("select count(*) from " + entityClass.getName() + whereClause);
105        return (Long)query.uniqueResult();
106    }
107
108    protected void commit() {
109        // Commit
110        transactionManager.commit(status);
111        // Then open a new transaction
112        status = transactionManager.getTransaction(null);
113    }
114}