import org.wamblee.persistence.hibernate.HibernateMappingFiles;
/**
- * Test case support class for spring tests.
+ * Test case support class for spring tests.
*/
public class SpringTestCase extends MockObjectTestCase {
- private Log LOG = LogFactory.getLog(SpringTestCase.class);
+ private static final Log LOG = LogFactory.getLog(SpringTestCase.class);
/**
* Session factory bean name.
* Schema pattern.
*/
private static final String SCHEMA_PATTERN = "%";
-
+
/**
* List of (String) configuration file locations for spring.
*/
private String[] _configLocations;
-
+
/**
* Application context for storing bean definitions that vary on a test by
* test basis and cannot be hardcoded in the spring configuration files.
*/
private GenericApplicationContext _parentContext;
-
+
/**
- * Cached spring application context.
+ * Cached spring application context.
*/
- private ApplicationContext _context;
+ private ApplicationContext _context;
public SpringTestCase(Class<? extends SpringConfigFiles> aSpringFiles,
Class<? extends HibernateMappingFiles> aMappingFiles) {
SpringConfigFiles springFiles = aSpringFiles.newInstance();
_configLocations = springFiles.toArray(new String[0]);
} catch (Exception e) {
- fail("Could not construct spring config files class '" + aSpringFiles.getName() + "'");
+ fail("Could not construct spring config files class '"
+ + aSpringFiles.getName() + "'");
}
-
+
// Register the Hibernate mapping files as a bean.
_parentContext = new GenericApplicationContext();
BeanDefinition lDefinition = new RootBeanDefinition(aMappingFiles);
* @return Spring context.
*/
protected synchronized ApplicationContext getSpringContext() {
- if ( _context == null ) {
+ if (_context == null) {
_context = new ClassPathXmlApplicationContext(
- (String[]) _configLocations, _parentContext);
+ (String[]) _configLocations, _parentContext);
assertNotNull(_context);
}
- return _context;
+ return _context;
}
/**
* @return Hibernate session factory.
*/
protected SessionFactory getSessionFactory() {
- SessionFactory factory = (SessionFactory) getSpringContext().getBean(SESSION_FACTORY);
- assertNotNull(factory);
- return factory;
+ SessionFactory factory = (SessionFactory) getSpringContext().getBean(
+ SESSION_FACTORY);
+ assertNotNull(factory);
+ return factory;
}
protected void setUp() throws Exception {
LOG.info("Performing setUp()");
super.setUp();
-
- _context = null; // make sure we get a new application context for every
- // new test.
- BeanKernel
- .overrideBeanFactory(new TestSpringBeanFactory(getSpringContext()));
-
- cleanDatabase();
+ _context = null; // make sure we get a new application context for
+ // every
+ // new test.
+
+ BeanKernel.overrideBeanFactory(new TestSpringBeanFactory(
+ getSpringContext()));
+
+ cleanDatabase();
}
/*
*/
protected PlatformTransactionManager getTransactionManager() {
PlatformTransactionManager manager = (PlatformTransactionManager) getSpringContext()
- .getBean(TRANSACTION_MANAGER);
- assertNotNull(manager);
- return manager;
+ .getBean(TRANSACTION_MANAGER);
+ assertNotNull(manager);
+ return manager;
}
/**
* @return Hibernate template.
*/
protected HibernateTemplate getTemplate() {
- HibernateTemplate template = (HibernateTemplate) getSpringContext().getBean(HibernateTemplate.class.getName());
- assertNotNull(template);
- return template;
+ HibernateTemplate template = (HibernateTemplate) getSpringContext()
+ .getBean(HibernateTemplate.class.getName());
+ assertNotNull(template);
+ return template;
}
/**
}
public void cleanDatabase() throws SQLException {
-
- if (! isDatabaseConfigured() ) {
- return;
+
+ if (!isDatabaseConfigured()) {
+ return;
}
-
+
String[] tables = getTableNames();
try {
SQLException exc = new SQLException(e.getMessage());
exc.initCause(e);
throw exc;
- }
+ }
}
/**
*/
public String[] getTableNames() throws SQLException {
- List result = new ArrayList();
+ List<String> result = new ArrayList<String>();
LOG.debug("Getting database table names to clean (schema: '"
+ SCHEMA_PATTERN + "'");
JdbcTemplate template = new JdbcTemplate(getDataSource());
int result = template.update(aSql, aArgs);
- Map map = new TreeMap();
+ Map<String, Integer> map = new TreeMap<String, Integer>();
map.put("result", new Integer(result));
return map;
+ " statement: " + aStatement);
}
}
-
- private boolean isDatabaseConfigured() {
- try {
- getDataSource();
- } catch (NoSuchBeanDefinitionException e ) {
- return false;
+
+ private boolean isDatabaseConfigured() {
+ try {
+ getDataSource();
+ } catch (NoSuchBeanDefinitionException e) {
+ return false;
}
- return true;
+ return true;
}
/**
* @return Returns the dataSource.
*/
public DataSource getDataSource() {
- DataSource ds = (DriverManagerDataSource) getSpringContext().getBean(DATA_SOURCE);
- assertNotNull(ds);
- return ds;
+ DataSource ds = (DriverManagerDataSource) getSpringContext().getBean(
+ DATA_SOURCE);
+ assertNotNull(ds);
+ return ds;
}
/**
return count;
}
-
- protected int countResultSet(ResultSet aResultSet) throws SQLException {
+
+ protected int countResultSet(ResultSet aResultSet) throws SQLException {
int count = 0;
while (aResultSet.next()) {