Sunday, June 01, 2008

Testing Data Access Objects

A month or two ago we introduced a new DAO testing framework to my current project.  It still made use of DB Unit but had a different approach to that of AppFuse which the project took advantage of as base.  Before, the DB Unit Ant task would insert data from an XML file and would run all the tests.  We had three problems with this:

  1. Data is only inserted once, at the start of the test which is used by one or more tests.  We need to be extra careful that the database state is maintained.  Typical situation is when testing a save method - either remove the additional rows that were inserted or combine the add-save-remove tests into one.  I do understand though that loading data can take a long time, which I experienced in my last major project.
  2. Does not really work well when running inside an IDE like Eclipse.  When a test fails there is an additional reason that we consider to explain the failure, and that is the database state is not the expected one before the start of the test.  Thus a test is not really repeatable in the strictest sense.
  3. Development pretty much involves writing migration scripts to the current version.  Our strategy for this to write the migration script when we introduce a change to the database.  This avoids having to write all the migration scripts near release (continuous integration?) but easily prevents us from having Hibernate generate the schema nor have the DAO test (over)write the data in the database.

The problems above pretty much resulted in a list of requirements when we started working on the new test framework:

  1. Should maintain database state at the start of each testing method.
  2. Should not be slow (possible side effect of #1).
  3. Should be executable inside an IDE.
  4. Should execute tests in a separate database to not disturb the (large) development data.

To address the first two requirements we had the test super class load only a subset of the data, that which the test case being executed needed, before each test was executed.  We forced a method getDataSetLocations() that returned a String array of XML file paths to load.  We are then free to either specify data on per table or per test case basis, and to mix and match test data to avoid duplication.  Below is a straightforward implementation, with the XML paths stored inside a constant/interface:

public abstract class BaseDaoTestCase extends AbstractTransactionalDataSourceSpringContextTests {

@Override
protected String[] getConfigLocations() {
// Specify Spring XML files
}

protected abstract String[] getDataSetLocations();

@Override
protected void onSetUpInTransaction() throws Exception {
super.onSetUpBeforeTransaction();

Connection conn = DriverManager.getConnection(...);
IDatabaseConnection connection = new DatabaseConnection(conn);

try {
for (String dataSetLocation : getDataSetLocations()) {
IDataSet dataSet = new XmlDataSet(
new FileReader(new File(dataSetLocation)));
DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
}
} finally {
connection.close();
}
}
}

public class FooTest extends BaseDaoTestCase {
private FooDao fooDao;

public void setFooDao(FooDao fooDao) {
this.fooDao = fooDao; // autowired by Spring
}

@Override
protected String[] getDataSetLocations() {
return new String[] {
DataSetLocations.TBL_USER, // data from tbl_user table
DataSetLocations.FOO_DAO // specific to FooDaoTest
}
}

public void testFoo() {
...
}
}

We hit a bit of a snag when implementing the third requirement, as the test could not find the Spring context files as they were moved for the directory they reside in to a location visible in the classpath. Ended up adding the said directories into Eclipse's classpath while adding exceptions to other file types other than XML.


The last requirement was satisfied by switching to the test database during test time.  This is a similar approach to what Rails uses for it's separate production, development, and testing database setup.  hbm2ddl was set to generate the test database schema during the `setup` Ant task to ensure that it is always present.  We then modified our build file to do the necessary switching and made sure that Eclipse loaded the context file that pointed to the test database first.

Saturday, May 31, 2008

PinoyJUG talk on Scala and Seam

I attended the PinoyJUG talk last Wednesday.  Topics discussed were Scala by Nestor Pestelos, followed by JBoss Seam by Paolo Melendres.  The talk was held at the Orange & Bronze office and it was packed with people! 

P5280528

I'm a bit familiar with both topics so there wasn't much to add during the talk, though the talk on Seam is of more relevance to me in this point in time.  I'm sort-of looking through different web frameworks and Seam's enhancements to JSF such as annotations and REST are very welcomed.  Unfortunately they are still Seam-specific at this point.  I would've wanted to see more demos though: for example, that Ealden's bikini brief store sounds like a good idea.  There was also the occasional jab on Struts 1 and Spring, though "bijection" did get picked on as a term.

Also saw Diana Lacson and Joy Perez(-Melendres hehe) and finally met Melvin Vivas and Jessie Evangelista.  The talk ended at around 9:30 in the evening and after a little detour I headed to SBC Valero to meet up with Diana, Melvin, and Butch for a little chit-chat.  Called it a night at around midnight.

More pictures, which were taken by Donard So Kua, on my Flickr site.

Sunday, May 11, 2008

New project team room

A little over a month ago my team moved into the new project room, 405. The room is very bright with it's large open windows and white walls which should prevent grumpy, moody teammates. I always liked a place that is almost as bright as sunlight which is what we have now. We're also on a different floor now and almost self sufficient which keeps us focused. Below is a picture of the room early in the morning.

405

I decided to go with a combination of couches and desks for the room. Chelle, our Business Development Head and interior design specialist (among other roles) helped me look for new furniture. I, on the other hand, field tested them with great passion. We also added some floor pillow things and bamboo floor things giving anyone the option to work on either the desks, couches, or the floor.

The couches were arranged in such a way that we could still use our whiteboards for discussions. The wall opposite to the whiteboards, and where the desks are nudged against, is reserved for diagrams on the project. Power strips were mounted on walls for easy access especially for those on desks. Desks have a 22" LCD, a laptop dock, and an ergonomic keyboard. Plants c/o Sally our Office Manager add color to the place. The lone electric fan beside my desk is there as the aircon does not reach me and I need a reasonably cold environment or else I'll fall asleep.

We extended the setup to the other offices after people started to hang out in the room. Pictures of the other rooms are posted in my Flickr account.

Thursday, April 17, 2008

Two years

Today is my second year in Orange & Bronze. Unlike last year, I didn't get stuck in Makati traffic and didn't have to endure some shuttle with a malfunctioning aircon. I've started on this "morning person" routine wherein I go to the office early in the morning to avoid traffic and have a few hours to myself to code, etc. This also allows me to avoid the 9-10 am office rush when people get in and issues start piling up. I definitely feel more productive, though I really should do something with my time of sleep.

Another first was that everyone managed to convince me to buy merienda to celebrate this event. There seems to be this commercial from McDo that involves a desperate person and cheeseburgers, but I haven't personally seen it as I stopped watching TV around November of last year. Everyone was chanting "burger! burger! burger!" which was just creepy. Mental note: check YouTube. Ended disappointing everyone especially Kathy when we got Pizza. Good thing I was able to convince JM to share the treat load. Much thanks to Dale for the palm card.

Also, I turned 1 year on my current project yesterday. This is my longest assignment to date and it continues to interest me until now. Today, specifically, we went into a code freeze just before lunch in order to polish the build for an iteration release tomorrow. We then spent the next 2 hours going through the application looking for bugs and filing them. We then divide the issues and started working on them. More on this in the next few days.

Managed to leave the office early today in order to meet up with my family in the Fort. My sister is taking her internship in one of the offices there and my parents decided to fetch her in the afternoon. The difference in the time it takes to commute from Makati in public transportation from this is huge. Fell asleep when I got home though I managed to wake up an hour before midnight to read mail, write this entry, and eat California maki.

That's about it for today. Much thanks for the greetings from everyone. It's been an interesting 2 years and hopefully it will continue to do so.

Wednesday, March 19, 2008

23

Just turned 23 around an hour ago.  I am now spending the first few hours of the year with my officemates here in the office.  I had a joint celebration at McDonalds Greenbelt earlier with Joshua, Chelle's son, who just turned 4 yesterday.  The party turned out to be a cosplay of sorts, with characters from Death Note and Spiderman attending.  Most of my barkada were able to go.  Watch out of the pictures at my Flickr site, as I am in the process of uploading them.

We trooped to Time Zone Greenbelt right after the party and shot some hoops and raced a few laps.  Also went to the videoke booth where I sang (or think I sang) Wannabe by the Spice Girls, to which I specialize on.  Headed back to the office a little after 11, changed to something much more comfortable, and opened gifts, posing after each one for documentation purposes.  Sponge Bob seems to be the theme this time around:

Sponge Bob on my chair

We have just started watching a movie and I'm missing it little by little as I write this entry.  Not a bad way to start a new year.