View Javadoc
1   package net.sumaris.core.test;
2   
3   /*-
4    * #%L
5    * SUMARiS :: Test shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2018 SUMARiS Consortium
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  
28  import net.sf.ehcache.CacheManager;
29  import org.hibernate.Session;
30  import org.junit.After;
31  import org.junit.Before;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.springframework.beans.factory.annotation.Autowired;
35  import org.springframework.transaction.PlatformTransactionManager;
36  import org.springframework.transaction.TransactionStatus;
37  
38  import javax.persistence.EntityManager;
39  
40  /**
41   * <p>Abstract AbstractDaoTest class.</p>
42   */
43  
44  public abstract class AbstractDaoTest {
45  
46  	/** Logger. */
47  	private static final Logger log = LoggerFactory.getLogger(AbstractDaoTest.class);
48  
49  	private TransactionStatus status;
50  	
51  	private boolean commitOnTearDown = true;
52  
53  	@Autowired
54  	private PlatformTransactionManager transactionManager;
55  
56  	@Autowired
57  	private EntityManager entityManager;
58  
59  	@Autowired(required = false)
60  	protected CacheManager cacheManager;
61  	/**
62  	 * <p>setUp.</p>
63  	 *
64  	 * @throws Exception if any.
65  	 */
66  	@Before
67  	public void setUp() throws Exception {
68  		if (transactionManager != null) {
69  			status = transactionManager.getTransaction(null);
70  			if (log.isDebugEnabled()) {
71  				log.debug("Transaction initialized");
72  			}
73  		}
74  	}
75  
76  	/**
77  	 * <p>tearDown.</p>
78  	 *
79  	 * @throws Exception if any.
80  	 */
81  	@After
82  	public void tearDown() throws Exception {
83  		if (transactionManager != null) {
84  			if (commitOnTearDown) {
85  				transactionManager.commit(status);
86  			} else {
87  				transactionManager.rollback(status);
88  			}
89  		}
90  		// Clear all cache, if any
91  		if (cacheManager != null) {
92  			cacheManager.clearAll();
93  		}
94  	}
95  	
96  	/**
97  	 * Allow to apply a commit on tear down (by default a rollback is applied)
98  	 *
99  	 * @param commitOnTearDown true to force to commit after unit test
100 	 */
101 	protected void setCommitOnTearDown(boolean commitOnTearDown) {
102 		this.commitOnTearDown = commitOnTearDown;
103 	}
104 
105 	/**
106 	 * <p>commit.</p>
107 	 */
108 	protected void commit() {
109 		// Commit
110 		transactionManager.commit(status);
111 		// Then open a new transaction
112 		status = transactionManager.getTransaction(null);
113 	}
114 
115 	/**
116 	 * <p>getEntityManager.</p>
117 	 *
118 	 * @return a {@link Session} object.
119 	 */
120 	protected EntityManager getEntityManager() {
121 		return entityManager;
122 	}
123 }