1   package com.atlassian.user.generic;
2   
3   import org.apache.log4j.Logger;
4   import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
5   import org.springframework.dao.DataAccessException;
6   
7   import java.util.Iterator;
8   import java.util.List;
9   
10  /**
11   * Common base class for all test cases using spring's context and transaction management
12   */
13  public abstract class AbstractSpringTest extends AbstractTransactionalDataSourceSpringContextTests
14  {
15      private static final Logger log = Logger.getLogger(AbstractSpringTest.class);
16  
17      public AbstractSpringTest()
18      {
19          init();
20      }
21  
22      public AbstractSpringTest(String name)
23      {
24          super(name);
25          init();
26      }
27  
28      private void init()
29      {
30          setAutowireMode(AUTOWIRE_BY_NAME);
31          setDependencyCheck(false);
32      }
33  
34      public void runBare() throws Throwable
35      {
36          super.runBare();
37          // this check is performed to make sure tests don't leave data behind in the database
38          // (ie they commit but don't clean up properly)
39          // if your test leaves databehind this check will cause a failure and try to delete it so other test are not affected
40          if (jdbcTemplate != null)
41          {
42              JdbcHelper db = new JdbcHelper();
43              List list = db.getNonEmtpyTables(jdbcTemplate);
44              if (list.size() > 0)
45              {
46                  String message = "Test [ " + getClass().getName() + "." + getName() + " ] left commited data in the database";
47                  log.error(message);
48                  for (Iterator i = list.iterator(); i.hasNext();)
49                  {
50                      String tableName = (String) i.next();
51                      int count = db.getTableCount(jdbcTemplate,tableName);
52                      log.error("Test [ " + getClass().getName() + "." + getName() + " ] left commited data in table [ "+tableName+" ] rows [ "+count+" ]");
53                      // force a clean up so not to affect other tests
54                      try
55                      {
56                          db.deleteTable(jdbcTemplate, tableName);
57                      }
58                      catch (DataAccessException e)
59                      {
60                          // its possible this will fail because of foreign key constraints
61                          // i have given a good attempt at trying to delete the data, if this fails the test
62                          // will need to be fixed anyway and the failure below will indicate this
63                      }
64                  }
65                  fail(message);
66              }
67          }
68      }
69  }