View Javadoc
1   package org.itracker;
2   
3   import org.apache.log4j.Logger;
4   import org.dbunit.database.DatabaseConfig;
5   import org.dbunit.database.DatabaseConnection;
6   import org.dbunit.dataset.CompositeDataSet;
7   import org.dbunit.dataset.IDataSet;
8   import org.dbunit.dataset.xml.XmlDataSet;
9   import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
10  import org.dbunit.operation.DatabaseOperation;
11  import org.hibernate.Session;
12  import org.hibernate.SessionFactory;
13  import org.itracker.core.ApplicationInitialization;
14  import org.itracker.services.ConfigurationService;
15  import org.junit.After;
16  import org.junit.Before;
17  import org.junit.internal.runners.JUnit4ClassRunner;
18  import org.junit.runner.RunWith;
19  import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
20  import org.springframework.orm.hibernate3.SessionHolder;
21  import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
22  import org.springframework.transaction.support.TransactionSynchronizationManager;
23  
24  import javax.sql.DataSource;
25  import java.sql.SQLException;
26  
27  @RunWith(JUnit4ClassRunner.class)
28  public abstract class AbstractDependencyInjectionTest extends
29          AbstractDependencyInjectionSpringContextTests {
30  
31      private static final Logger log = Logger
32              .getLogger(AbstractDependencyInjectionSpringContextTests.class);
33      private DataSource dataSource;
34      private LocalSessionFactoryBean sessionFactoryBean;
35      public ClassLoader classLoader;
36      public IDataSet dataSet;
37      private SessionFactory sessionFactory;
38  
39      protected AbstractDependencyInjectionTest() {
40          classLoader = getClass().getClassLoader();
41      }
42  
43      @Override
44      public void onSetUp() throws Exception {
45          sessionFactory = (SessionFactory) applicationContext
46                  .getBean("sessionFactory");
47          Session session = sessionFactory.openSession();
48          TransactionSynchronizationManager.bindResource(sessionFactory,
49                  new SessionHolder(session));
50  
51          dataSet = getDataSet();
52          DatabaseConnection dbConnection = null;
53          try {
54  
55              resetConfiguration();
56  
57              dbConnection = new DatabaseConnection(getDataSource().getConnection());
58              dbConnection.getConfig().setProperty(
59                      DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
60                      new HsqldbDataTypeFactory());
61  
62              if (dataSet != null) {
63                  DatabaseOperation.CLEAN_INSERT.execute(dbConnection, dataSet);
64              }
65  
66              if (!dbConnection.getConnection().getAutoCommit()) {
67                  dbConnection.getConnection().commit();
68              }
69          } catch (Exception e) {
70              log.error("onSetUp: failed to set up datasets", e);
71              throw e;
72          } finally {
73              if (null != dbConnection) {
74                  try {
75                      dbConnection.close();
76                  } catch (SQLException e) {
77                      log.warn("onSetUp: failed to close connection", e);
78                  }
79              }
80          }
81      }
82  
83      @Override
84      public void onTearDown() throws Exception {
85          DatabaseConnection dbConnection = null;
86          try {
87              dbConnection = new DatabaseConnection(getDataSource()
88                      .getConnection());
89  
90              if (dataSet != null) {
91                  DatabaseOperation.DELETE_ALL.execute(dbConnection, dataSet);
92              }
93  
94              if (!dbConnection.getConnection().getAutoCommit()) {
95                  dbConnection.getConnection().commit();
96              }
97  
98          } catch (Exception e) {
99              log.error("onTearDown: failed to tear down datasets", e);
100             throw e;
101         } finally {
102             TransactionSynchronizationManager.unbindResource(sessionFactory);
103             sessionFactoryBean.destroy();
104 
105 
106             if (null != dbConnection) {
107                 try {
108                     dbConnection.close();
109                 } catch (SQLException e) {
110                     log.warn("onTearDown: failed to close connection", e);
111                 }
112             }
113         }
114     }
115 
116     final protected void resetConfiguration() {
117         ConfigurationService configurationService = (ConfigurationService) applicationContext
118                 .getBean("configurationService");
119 
120         ApplicationInitialization.initializeAllLanguages(configurationService, true);
121         configurationService.initializeConfiguration();
122 
123         configurationService.resetConfigurationCache();
124     }
125 
126     private IDataSet getDataSet() throws Exception {
127         final String[] aDataSet = getDataSetFiles();
128         final IDataSet[] dataSets = new IDataSet[aDataSet.length];
129 
130         for (int i = 0; i < aDataSet.length; i++) {
131             dataSets[i] = new XmlDataSet(classLoader
132                     .getResourceAsStream(aDataSet[i]));
133         }
134         return new CompositeDataSet(dataSets);
135     }
136 
137     /**
138      * must make sure, that the order is correct, so no constraints will be
139      * violated.
140      */
141     protected abstract String[] getDataSetFiles();
142 
143     public LocalSessionFactoryBean getSessionFactoryBean() {
144         return sessionFactoryBean;
145     }
146 
147     public void setSessionFactoryBean(LocalSessionFactoryBean sessionFactoryBean) {
148         this.sessionFactoryBean = sessionFactoryBean;
149     }
150 
151     public DataSource getDataSource() {
152         return dataSource;
153     }
154 
155     public void setDataSource(DataSource dataSource) {
156         this.dataSource = dataSource;
157     }
158 
159     @Before
160     public final void callSetup() throws Exception {
161         super.setUp();
162     }
163 
164     @After
165     public final void callTeardown() throws Exception {
166         super.tearDown();
167     }
168 
169 }