Archive for April, 2006

Niagara Falls Trip

Friday, April 21st, 2006

niagara.jpgWe had a great vacation at Niagara Falls and drove more than 1200 miles with Arvind. We crossed the American border and spent the next couple of days at Niagara Falls, Canada.The weather was quite pleasant in the lower 50s. The hotel stay was nice and from our room we could view and enjoy the falls.

We visited ‘Kingdom of Lost Birds’ - the largest indoor aviary in the world. Arvind had lot of fun chasing all pretty colored birds. We spent long hours at the Falls, walked a lot, took the boat ride, enjoyed the night illumination from the Skylon tower. Niagara is awesome!!

We also had time to visit other places of interests - Wax museum, Guiness world records museum, Ripley’s Believe it or not.

Enjoy pictures and video clippings.

Short Home Videos

Thursday, April 13th, 2006

The last two home videos that I created were about an hour long. While a lot shooting is required to make such movies, the effort and time required is even more significant.

For a change, I’m going to create very short movies (clippping) and make then available on the internet through my site. This way, I would be able to share these immediately with my friends and family.

Check out the Home Video section. I have added Arvind’s petfarm visit to it.

Better jUnit-ing

Sunday, April 2nd, 2006

jUnit has been around for a long time. But there are always better techniques to implement old ways. I have provided below an efficient junit aprroach integrated with DbUnit and Spring with support for transaction and hibernate open session. Significant features are:

  • Integration with DbUnit: DbUnit puts database into a known state between test runs. Pre-loads database with known data set before the test
  • Transaction support using spring: After each test all changes made by the test are rolled back. Hence there is no need to programatically undo channges made by the test. Much cleaner code. (Thanks, Jim)
  • After each test, clear the pre-loaded data set from database
  • Support for open session in view: Web applications using Hibernate with lazy loading need to keep the session open till the view is rendered. (Read more)

/**
* Junit test integrated with DbUnit and spring. Provides support for
* transaction and open session. This wraps each test method with a
* transaction and rollbacks after the method exits. DbUnit
* is used to load up the table with initial set of values.
*/

public class MyTest extends AbstractTransactionalDataSourceSpringContextTests {

private SessionFactory sessionFactory;
private Session session;
private static ConfigurableApplicationContext factory;
private final String[] TABLES = { “EmployeeTable”, “DepartmentTable” };
private final String DATA_FILE = “data.xml”;
private final String[] CONFIG_LOCATIONS = { “spring-context.xml” };

public static Test suite() {
return new TestSuite(MyTest.class);
}

protected String[] getConfigLocations() {
return CONFIG_LOCATIONS;
}

// This method is used to perform any setup operations, such as
// populating a database table, within the transaction.
protected void onSetUpInTransaction() throws Exception {
super.onSetUpInTransaction();
if (factory == null) {
factory = new ClassPathXmlApplicationContext(getConfigLocations());
}
// Load test data using DBUnit
DataSource ds = (DataSource) factory.getBean(”dataSource”);
Connection con = DataSourceUtils.getConnection(ds);
IDatabaseConnection dbUnitCon = new DatabaseConnection(con);
IDataSet dataSet = new FlatXmlDataSet(Thread.currentThread().
getContextClassLoader().getResourceAsStream(DATA_FILE));
try {
DatabaseOperation.CLEAN_INSERT.execute(dbUnitCon, dataSet);
} finally {
DataSourceUtils.releaseConnection(con, ds);
}
// Support of open session
sessionFactory = (SessionFactory) factory.getBean(”sessionFactory”);
session = sessionFactory.openSession();
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}

protected void onTearDownAfterTransaction() throws Exception {
// delete table entries
deleteFromTables(TABLES);
// release session
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
session = holder.getSession();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.releaseSession(session, sessionFactory);
}

public void testSomething() throws Exception {
// …
}

}