View Javadoc
1   package fr.ifremer.quadrige3.core.test;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.hibernate.Session;
30  import org.hibernate.SessionFactory;
31  import org.junit.After;
32  import org.junit.Before;
33  import org.junit.BeforeClass;
34  import org.springframework.transaction.PlatformTransactionManager;
35  import org.springframework.transaction.TransactionStatus;
36  
37  /**
38   * <p>Abstract AbstractDaoTest class.</p>
39   */
40  public abstract class AbstractDaoTest {
41  
42  	/** Logger. */
43  	private static final Log log = LogFactory.getLog(AbstractDaoTest.class);
44  
45  	private static PlatformTransactionManager transactionManager;
46  
47  	private TransactionStatus status;
48  	
49  	private boolean commitOnTearDown = true;
50  
51  	/**
52  	 * <p>beforeClass.</p>
53  	 */
54  	@BeforeClass
55  	public static void beforeClass() {
56  		transactionManager = Tests.getBean("transactionManager", PlatformTransactionManager.class);
57  	}
58  
59  	/**
60  	 * <p>setUp.</p>
61  	 *
62  	 * @throws Exception if any.
63  	 */
64  	@Before
65  	public void setUp() throws Exception {
66  		status = transactionManager.getTransaction(null);
67  		if (log.isDebugEnabled()) {
68  			log.debug("Session and transaction initialized");
69  		}
70  	}
71  
72  	/**
73  	 * <p>tearDown.</p>
74  	 *
75  	 * @throws Exception if any.
76  	 */
77  	@After
78  	public void tearDown() throws Exception {
79  		if (commitOnTearDown) {
80  			transactionManager.commit(status);
81  		}
82  		else {
83  			transactionManager.rollback(status);
84  		}
85  	}
86  	
87  	/**
88  	 * Allow to apply a commit on tear down (by default a rollback is applied)
89  	 *
90  	 * @param commitOnTearDown true to force to commit after unit test
91  	 */
92  	protected void setCommitOnTearDown(boolean commitOnTearDown) {
93  		this.commitOnTearDown = commitOnTearDown;
94  	}
95  
96  	/**
97  	 * <p>commit.</p>
98  	 */
99  	protected void commit() {
100 		// Commit
101 		transactionManager.commit(status);
102 		// Then open a new transaction
103 		status = transactionManager.getTransaction(null);
104 	}
105 
106 	/**
107 	 * <p>getSession.</p>
108 	 *
109 	 * @return a {@link org.hibernate.Session} object.
110 	 */
111 	protected Session getSession() {
112 		SessionFactory sessionFactory = Tests.getBean("sessionFactory", SessionFactory.class);
113 		return sessionFactory.getCurrentSession();
114 	}
115 }