Spring and Hypersonic/Hibernate tests
Follow-up to Files project dev server from Kieran's blog
Having been away on holiday for 2 weeks and having quite a bit of catching up to do with other stuff, we’ve not made huge leaps in the last few weeks. However, we’re building up steam again now and have finally sorted out after a few restarts the domain model we’re going to be going for around the key aspects of accounts, files, folders, etc…
Up until now for speed of prototyping, we’ve been working with Spring, but not yet involved a database as we can quite easily just talk directly to the file system for now. However, now is the time to start getting more complex and we need somewhere to store all the metadata of all kinds about the files and accounts.
As usual, we’ll try and incrementally do the Hibernate mappings and start to build the database scheme. To do this quickly we’ll be building against some tests and a hypersonic database to start with. Spring provides the handy “AbstractTransactionalDataSourceSpringContextTests” class which allow easy binding of Spring objects and also a simple way to plug in transactional capabilities to your tests.
By coupling these test with the Hypersonic database which can be built and torn down in memory in just milliseconds, we can prototype the database very quickly.
Hibernate session-factory config
<session-factory>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="use_outer_join">true</property>
<property name="hbm2ddl.auto">create-drop</property>
<mapping resource="......hbm.xml"/>
</session-factory>
Spring sessionfactory and datasource
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"><value>hypersonic-hibernate.cfg.xml</value></property>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:.</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
So based on your mappings files, the database schema gets created in a new hypersonic database for each test giving you a working and clear schema to test against. Magic.
public class HypersonicTests extends AbstractTransactionalDataSourceSpringContextTests {
protected String[] getConfigLocations() {
return new String[] { "file:apps/webinterface/src/applicationContext.xml","file:apps/webinterface/test-src/hypersonic-db-context.xml"};
}
}
public class DbConnectionTests extends HypersonicTests {
private SessionFactory _sessionFactory;
public final void testDbConnection() throws Exception {
Session session = getSessionFactory().openSession();
session.save(new AccountImpl(null, null, "Test", null));
session.flush();
List accounts = session.createCriteria(Account.class).list();
assertEquals(1, accounts.size());
}
public SessionFactory getSessionFactory() {
return _sessionFactory;
}
public void setSessionFactory(final SessionFactory sessionFactory) {
_sessionFactory = sessionFactory;
}
}
Add a comment
You are not allowed to comment on this entry as it has restricted commenting permissions.