Tuesday, November 25, 2008

Looking for Tips on Formatting Code in Blogger

OK, if you read my last post, you probably noticed that the formatting of the Java and (especially) XML code was, well, not so great. And it took me some time just to get it that good. I have some code-to-highlighted-HTML tools that I like to use, but when I push it into Blogger, it gets mangled in various ways. For all its many faults, Spaces at least didn't give me a lot of trouble in this department. I searched around a bit for techniques and tools to improve this situation. Offhand, I didn't find anything that really impressed me as a good solution. I'll keep looking and trying new things, but in the meantime, if you have some good tips here, please share them.

dbUnit and HSQLDB Databases with Schema Names

There are several ways to manage unit tests that involve a data layer. Someday I'll enumerate them in another post. But when I'm doing Java, one of my favorites is using HSQLDB as a fast, in-memory stand-in for a more heavyweight database. I also like dbUnit to manage setting and resetting the data in the in-memory database. I ran into an interesting problem when trying to use dbUnit with an HSQLDB database that employed named schemas (like, say, Oracle). In case you've never had to create your own schemas in DDL before, you can create the schema "SCOTTSCHEMA" like this:
Statement ddl = conn.createStatement(); ddl.executeUpdate("CREATE SCHEMA SCOTTSCHEMA AUTHORIZATION DBA");

Now, when you get ready to use dbUnit and create your IDatabaseConnection, you need to pass the schema name:

IDatabaseConnection dbUnitConn = new DatabaseConnection(conn, "SCOTTSCHEMA");

Now I should be able to load up XML files using dbUnit that look like this for a hypothetical "CONTACTS" table:

<?xml version="1.0" encoding="UTF-8"?> <dataset> <SCOTTSCHEMA.CONTACTS FIRST_NAME="Scott" LAST_NAME="McMaster"/> <SCOTTSCHEMA.CONTACTS FIRST_NAME="Tracy" LAST_NAME="McMaster"/> </dataset>

It turns out that passing the schema name in the second parameter is necessary, but not sufficient: If you try to run a database operation against dbUnitConn at this point, you'll get the following exception:

org.dbunit.dataset.NoSuchTableException: SCOTTSCHEMA.CONTACTS at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:222) at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109) at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79) at ...

When I first hit this, it was easier to figure out what was going on from the dbUnit code rather than the documentation. I discovered rather quickly that you also need to set FEATURE_QUALIFIED_TABLE_NAMES in the IDatabaseConnection's config:

dbUnitConn.getConfig().setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);

Now dbUnit will build its internal map of database tables prefixed with the schema name and will be able to find the table metadata when you execute an operation. It seems like it would make sense to default this feature to "true" when you create a database connection with a schema name...

Saturday, November 22, 2008

My New Blog Location

Well, I finally did it. My old buddy Chad has been on me about moving my blog to a different platform. I was reluctant, because I feel like I've built up a pretty good body of content on my old blog, and I didn't want to risk losing regular readers or anyone who found me while looking for solutions to their technical problems on Google. But some time ago, I turned off comments due to an endless stream of spam appearing on ancient posts. I'm tired of not having comments. I want comments. And I want a whole lot of other features that Windows Live Spaces does not provide, even after all these years. So here I am on Blogger. I plan to keep the content the same -- book and article reviews and suggestions, discussion of the software development tools that I use, sample code in Java, .NET, Ruby, and anything else that I find interesting. So I'm glad you found this and hope you'll stick with me while I get this blog going. Of course, you can always find my previous work here.