package com.dc.fillmore; import org.apache.log4j.Logger; import org.springframework.mock.jndi.SimpleNamingContextBuilder; import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; import javax.naming.NamingException; import javax.sql.DataSource; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * A base test case for hibernate unit tests. * This base test case serves two main purposes: * */ public abstract class HibernateBaseTestCase extends AbstractTransactionalDataSourceSpringContextTests { private static final String JNDI_DATASOURCE_LOCATION = "java:comp/env/jdbc/DataSource"; private static final Logger log = Logger.getLogger(HibernateBaseTestCase.class); static{ try { SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder(); builder.bind(JNDI_DATASOURCE_LOCATION, new InMemoryHsqlDataSource()); builder.activate(); } catch (NamingException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. throw new RuntimeException(e); } catch (IllegalStateException e) { if (e.getMessage().indexOf("already set")<0) { throw e; } else { // TODO: fix this so it only happens once - dont put in static block? } } } protected void onSetUpInTransaction() throws Exception { super.onSetUpInTransaction(); setComplete(); // signals no need for rollback, // (as we don't care if we leave data in our // ad-hoc/in-memory database instance!) } protected String[] getConfigLocations() { return new String[] {"applicationContext.xml"}; } /** * a dummy datasource that we bind to JNDI in a static block above. */ private static class InMemoryHsqlDataSource implements DataSource { public InMemoryHsqlDataSource() { try { Class.forName("org.hsqldb.jdbcDriver"); } catch (ClassNotFoundException e) { throw new IllegalStateException("hsql not in classpath!",e); } } public Connection getConnection() throws SQLException { return DriverManager.getConnection("jdbc:hsqldb:mem:registration2005_temporaryTestDatabase", "sa", ""); } public Connection getConnection(String username, String password) throws SQLException { return null; //To change body of implemented methods use File | Settings | File Templates. } public PrintWriter getLogWriter() throws SQLException { return null; //To change body of implemented methods use File | Settings | File Templates. } public void setLogWriter(PrintWriter out) throws SQLException { } public void setLoginTimeout(int seconds) throws SQLException { } public int getLoginTimeout() throws SQLException { return 0; //To change body of implemented methods use File | Settings | File Templates. } } }