Salome HOME
No need in merging user by cascade from ConributorRelation.
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / common / BaseTest.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   12 Oct 2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package test.splat.common;
11
12 import java.util.List;
13 import java.util.Map;
14
15 import javax.sql.DataSource;
16
17 import org.hibernate.SessionFactory;
18 import org.splat.log.AppLogger;
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.beans.factory.annotation.Qualifier;
21 import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
22 import org.springframework.orm.hibernate3.HibernateTemplate;
23 import org.springframework.test.context.ContextConfiguration;
24 import org.springframework.test.context.transaction.TransactionConfiguration;
25 import org.springframework.transaction.TransactionDefinition;
26 import org.springframework.transaction.TransactionStatus;
27 import org.springframework.transaction.support.AbstractPlatformTransactionManager;
28 import org.springframework.transaction.support.DefaultTransactionDefinition;
29 import org.testng.Assert;
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.BeforeMethod;
32 import org.testng.annotations.Test;
33
34 /**
35  * Base Siman Test class. Each test method is transactional by default.
36  * Use <code>NotTransactional</code> annotation to declare test method as not transactional.
37  * By default all operations on database are rolledback after each test method.
38  * To avoid this use <code>Rollback(false)</code> annotation.
39  * 
40  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
41  * 
42  */
43
44 @ContextConfiguration(locations = { 
45                 "/test/spring/ut-applicationContext.xml",
46                 "/test/spring/ut-datasourceContext.xml",
47                 "/spring/businessServiceContext.xml", 
48                 "/spring/daoContext.xml",
49                 "/spring/globalContext.xml",
50                 "/spring/technicalServiceContext.xml" 
51 })
52 @TransactionConfiguration(defaultRollback = true, transactionManager = "txManager")
53 @Test(singleThreaded=true, groups = {"service", "functional", "business"})
54 public class BaseTest extends TestListingAndOrder {
55
56         // ========================================= static methods
57         /**
58          * Logger for the class.
59          */
60         private static final AppLogger LOG = AppLogger.getLogger(BaseTest.class);
61         
62         /**
63          * The helper class to work with hibernate session.
64          */
65         private HibernateTemplate _hibernateTemplate;
66         
67         /**
68          * Get the hibernateTemplate.
69          * @return the hibernateTemplate
70          */
71         @Test(enabled = false)
72         public HibernateTemplate getHibernateTemplate() {
73                 return _hibernateTemplate;
74         }
75
76         // ======================================= static variables
77         /**
78          * {@inheritDoc}
79          * 
80          * @see org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests#setDataSource(javax.sql.DataSource)
81          */
82         @Override
83         @Autowired
84         @Test(enabled = false)
85 //      public void setDataSource(@Qualifier("p6spyiBatisDatasource")
86         @SuppressWarnings("deprecation")
87         public void setDataSource(@Qualifier("simanDatasource")
88         final DataSource dataSource) {
89                 this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
90         }
91
92         /**
93          * Set a hibernate session factory.
94          * @param aSessionFactory the hibernate session factory
95          */
96         @Autowired
97         @Test(enabled = false)
98         public void setSessionFactory(@Qualifier("simanSessionFactory")
99         final SessionFactory aSessionFactory) {
100                 _hibernateTemplate = new HibernateTemplate(aSessionFactory);
101         }
102         
103         /**
104          * Method for the test data creation. It is called before the first test
105          * starting.
106          */
107         @BeforeClass
108         public void initClass() {
109                 try {
110                         // Initialize UserContext
111 //                      initUserContext();
112                         
113                 } catch (Exception e) {
114                         Assert.fail("Exception occured during the testing data creation. ", e);
115                 }
116         }
117         
118         
119         /**
120          * Insert base data before each method : matricule, department, language, reference.
121          */
122         @BeforeMethod
123         public void insertBaseDataBeforeMethod() {
124                 LOG.debug(" >>>>>>>>>>>>>>> COMMON - insertBaseDataBeforeMethod");
125                 
126         }
127
128         // [PRIVATE METHOD] ------------------------------ 
129         
130         // [DATA PROVIDER METHOD : Insert / Select] ------------------------------ 
131         /**
132          * Insert data for test : document. 
133          * @param document BO
134          */
135 //      protected void insertData(final DocumentWithBLOBs document) {
136 //              // Insert document
137 //              _documentDAO.insert(document);
138 //      }
139         
140         /**
141          * Uses the current transaction to execute SQL statement in order to be able to 
142          * see the current state of the database.
143          * @param statement the sql statement
144          */
145         @SuppressWarnings("deprecation")
146         protected void executeAndDisplaySqlStatement(final String statement) {
147                 List<Map<String, Object>> resultSet = simpleJdbcTemplate.queryForList(statement);
148                 displayResultset(resultSet);
149         }
150         
151         /**
152          * Uses the current transaction to execute SQL statement in order to be able to 
153          * see the current state of the database.
154          * @param statement the sql statement
155          * @param args the sql statement parameters
156          */
157         @SuppressWarnings("deprecation")
158         protected void executeAndDisplaySqlStatement(final String statement, final Object...args) {
159                 List<Map<String, Object>> resultSet = simpleJdbcTemplate.queryForList(statement, args);
160                 displayResultset(resultSet);
161         }
162
163         /**
164          * Displays resultset in log.
165          * @param resultSet the resultset to display.
166          */
167         private void displayResultset(final List<Map<String, Object>> resultSet) {
168                 if(!resultSet.isEmpty()) {
169                         Map<String, Object> firstElement = resultSet.get(0);
170                         LOG.debug(firstElement.keySet().toString());
171                         
172                         for (Map<String, Object> line : resultSet) {
173                                 StringBuilder logLine = new StringBuilder("[");
174                                 logLine.append(line.values());
175                                 logLine.append("[");
176                                 LOG.debug(logLine.toString());
177                         }
178                 }
179         }
180         
181         
182         // ==========================================================================
183         // NESTED TRANSACTIONS MANAGEMENT
184         // ==========================================================================
185         
186         /**
187          * Used for nested transactions management.
188          */
189         private transient TransactionStatus _nestedTxStatus;
190         
191         /**
192          * Use this method to start a new transaction within an existing one. 
193      * Consider the following test scenario:
194          *   testXX():
195          *     1: check service.XX on valid data:
196          *        - createInitialData();
197          *        - service.XX(); (performs some changes on initial data)
198          *     2: check service.XX on invalid data:
199          *        - deleteInitialData(); (as data were changed by previous call)
200          *        - createInitialData();
201          *        - adjustInitialData();
202          *        - service.XX();
203          * To make such a test we need to provide deleteInitialData() method and call
204          * createInitialData() twice. The latter might lead to significant increasing of
205          * the test execution time.
206          * 
207          * Consider an alternative approach:
208          *   testXX():
209          *     1: check service.XX on valid data:
210          *        - createInitialData();
211          *        - startNestedTransaction();
212          *        - service.XX();
213          *        - rollbackNestedTransaction();
214          *     2: check service.XX on invalid data:
215          *        - startNestedTransaction();
216          *        - adjustInitialData();
217          *        - service.XX();
218          *        - rollbackNestedTransaction();
219          * Thus you're able to create and manage the nested transactions programmatically.
220          * @see test.areva.hewis.mlst2.service.gpao.rpta.TaBaseService#rollbackNestedTransaction()     
221          */
222         protected void startNestedTransaction() {
223                 AbstractPlatformTransactionManager txManager = 
224                         (AbstractPlatformTransactionManager) applicationContext.getBean("txManager");
225                 
226                 DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
227                 txDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
228                 
229                 _nestedTxStatus = txManager.getTransaction(txDefinition);
230 }
231         
232         /**
233          * Rollbacks the nested transaction.
234          * @see test.areva.hewis.mlst2.service.gpao.rpta.TaBaseService#startNestedTransaction()
235          */
236         protected void rollbackNestedTransaction() {
237                 AbstractPlatformTransactionManager txManager = 
238                         (AbstractPlatformTransactionManager) applicationContext.getBean("txManager");
239                 txManager.rollback(_nestedTxStatus);
240         }
241         
242
243         /**
244          * Rollbacks the nested transaction and start a new one.
245          * @see test.areva.hewis.mlst2.service.gpao.rpta.TaBaseService#startNestedTransaction()
246          * @see test.areva.hewis.mlst2.service.gpao.rpta.TaBaseService#rollbackNestedTransaction()
247          */
248         protected void rollbackAndRestartNestedTransaction() {
249                 rollbackNestedTransaction();
250                 startNestedTransaction();
251         }
252 }