Java and .NET RESTful interoperability with XStream

My ex-colleagues Paul Hammant and Ian Cartwright have written an article on their experiences of building SOA applications using RESTful services in .NET and Java that could interoperate over web services and message queues. XStream made this possible.

Buzzwordtastic.

http://www.infoq.com/articles/REST-INTEROP

I’ve joined Google

Woooo….. this looks fun.

OSCon: SiteMesh, SiteMesh, SiteMesh, SiteMesh

Just got back from the O’Reilly Open Source Convention in Portland. Fantastic conference – met lots of really interesting people (and the odd nutter).

It was a good conference for SiteMesh. It opened my eyes to two things:

# SiteMesh rocks. People who have tried SiteMesh, love it and don’t turn back. Their preferred choice for web framework changes, but SiteMesh remains constant.
# Our marketing sucks. Despite it being around for 5 years, most of the Java web app community have never felt the need to try it.

I was there to present a session on SiteMesh but a lot of other speakers beat me to it. It kept slipping into other sessions…

h4. Using AppFuse for Test driven Web Development, Matt Raible (details)

Matt gave an overview of the technology stack used in his AppFuse application. Despite having 5 versions of his app that use different frameworks (Struts, WebWork, Tapestry, Spring MVC and JavaServer Faces), all used SiteMesh. Good!

h4. Integrate: Building a Site from Open Source Gems, Erik Hatcher (details)

Erik walked us through the open source products he used to build his Lucene Book website and what customizations he made. The focus, of course, was Lucene and I learned a lot of great tricks about Lucene that hadn’t occurred to me before – such as using “sounds like” queries with soundex and indexing images by colors. I continue to love Lucene.

A great point that Erik mentioned was the need to become intimate with the projects you use. If you truely want to make the most of your frameworks, understand how they work, join the community and extend them.

Erik chose Tapestry to build the site but he also had Blojsom and some static content, so SiteMesh was useful to integrate these and he created some custom code to build SiteMesh decorators with Tapestry.

He pointed out that despite submitting this useful Tapestry integration to the SiteMesh project, nothing had made it into the SiteMesh release. Feeling embarressed, I committed his changes immediately, inadvertently breaking the build and providing great ammunition for Eric Pugh’s session on the importance of continuous integration.

h4. WebWork vs Spring MVC Smackdown, Matthew Porter and Matt Raible (details)

The basic plot was this… Matthew Porter was arguing why Spring MVC sucks and WebWork rocks. Matt Raible was arguing why Spring MVC rocks and WebWork sucks. The only thing they both agreed on was SiteMesh rocked. A fairly heated and passionate debate – great fun to watch. I would have opted for more violence though.

Matthew Porter got the final laugh when he pointed out that he compared the Spring MVC and WebWork versions of Matt Raible’s AppFuse framework and the Spring MVC version had about 25% (I think) more code, not including comments.

(more)

h4. The Evolution of Web Application Architectures, Craig McClanahan (details)

This was an interesting session where Craig compared the approaches taken by Struts, WebWork, Spring MVC, Tapestry and JavaServer Faces. He had done detailed research and, despite his heavy involvement with Struts and JSF, gave a very fair and objective view of the pros and cons of each.

This work could be useful for people evaluating which frameworks to choose and possibly could be overlayed with a guide based on values. The bottom line is there’s no single ‘ultimate’ web framework and depending on your needs and values you should choose the most suitable. I think it would be beneficial to all to have a guide indicating which values each of these frameworks are suited/not-suited for.

So, my question is this: Which values are more important to you when choosing a web framework and in which priority?

These are some example values that spring to mind: commercial support, testability (unit and functional), popularity, extensibility/customization, integration with other frameworks, rich widget support, REST friendlyness, simplicity vs magicness, AJAX friendlyness, learning curve, configuration, etc.

Anyhoo, SiteMesh was probably mentioned enough times to attract another load of people to the SiteMesh session.


I’m really glad these people mentioned SiteMesh and said such kind words about it – it resulted in a lot of interest and a full house for the SiteMesh session. I hope to get these presentations online shortly and write a bit more about how Subversion, Microsoft Word and SiteMesh can be combined to create a rich Content Management System.

h2. Improving SiteMesh’s marketing

The fact still remains that SiteMesh has terrible marketing. I’d love some ideas of how to spread the word more and encourage more people to try it but I honestly have no idea what to do. Any suggestions?

Flexible JUnit assertions with assertThat()

Over time I’ve found I end up with a gazillion permutation of assertion methods in JUnit: assertEquals, assertNotEquals, assertStringContains, assertArraysEqual, assertInRange, assertIn, etc.

Here’s a nicer way. jMock contains a constraint library for specifying precise expectations on mocks that can be reused in your own assertion method (and that’s the last time I’m going to mention mocks today, I promise – despite the frequent references to the jMock library).

By making a simple JUnit assertion method that takes a Constraint, it provides a replacement for all the other assert methods.

I call mine assertThat() because I think it reads well. Combined with the jMock syntactic sugar, you can use it like this:

assertThat(something, eq("Hello"));
assertThat(something, eq(true));
assertThat(something, isA(Color.class));
assertThat(something, contains("World"));
assertThat(something, same(Food.CHEESE));
assertThat(something, NULL);
assertThat(something, NOT_NULL);

Okay, that’s nice but nothing radical. A bunch of assert methods have been replaced with different methods that return constraint objects. But there’s more…

h3. Combining constraints

Constraints can be chained making it possible to combine them in different permutations. For instance, for virtually every assertion I do, I usually find that I need to test the negative equivalent at some point:

assertThat(something, not(eq("Hello")));
assertThat(something, not(contains("Cheese")));

Or maybe combinations of assertions:

assertThat(something, or(contains("color"), contains("colour")));

h3. Readable failure messages

The previous example can be written using the vanilla JUnit assert methods like this:

assertTrue(something.indexOf("color") > -1 || something.indexOf("colour") > -1);

Fine, the constraint based one is easier to read. But the real beauty is the failure message.

The vanilla JUnit assert fails with:

junit.framework.AssertionFailedError:

Useless! Means you have to put an explicit error message in the assertion:

assertTrue(something.indexOf("color") > -1 || something.indexOf("colour") > -1,
"Expected a string containing 'color' or 'colour'");

But the jMock constraint objects are self describing. So with this assertion:

assertThat(something, or(contains("color"), contains("colour")));

I get this useful failure message, for free:

junit.framework.AssertionFailedError:
Expected: (a string containing "color" or a string containing "colour")
but got : hello world

h3. Implementing it

The simplest way is to grab jMock and create your own base test class that extends MockObjectTestCase. This brings in convenience methods for free (I’m still not talking about mocks, honest). If you don’t want to extend this class, you can easily reimplement these methods yourself – it’s no biggie.

import org.jmock.MockObjectTestCase;
import org.jmock.core.Constraint;

public abstract class MyTestCase extends MockObjectTestCase {

protected void assertThat(Object something, Constraint matches) {
if (!matches.eval(something)) {
StringBuffer message = new StringBuffer("nExpected: ");
matches.describeTo(message);
message.append("nbut got : ").append(something).append('n');
fail(message.toString());
}
}

}

Now ensure all your test cases extend this instead of junit.framework.TestCase and you’re done.

h4. Defining custom constraints

Creating new constraints is easy. Let’s say I want something like:

assertThat(something, between(10, 20));

To do that I need to create a method that returns a Constraint object, requiring two methods; eval() for performing the actual assertion, and describeTo() for the self describing error message. This is something that can live in the base test class.

public Constraint between(final int min, final int max) {
return new Constraint() {
public boolean eval(Object object) {
if (!object instanceof Integer) {
return false;
}
int value = ((Integer)object).intValue();
return value > min && value < max;
}
public StringBuffer describeTo(StringBuffer buffer) {
return buffer.append("an int between ").append(min).append(" and ").append(max);
}
}
}

This can be combined with other constraints and still generate decent failure messages.

assertThat(something, or(eq(50), between(10, 20));
junit.framework.AssertionFailedError:
Expected: (50 or an int between 10 and 20)
but got : 43

In practice I find I only need to create a few of these constraints as the different combinations gives me nearly everything I need.

More about this in the jMock documentation.

h4. Summary

Since using this one assert method I’ve found my tests to be much easier to understand because of lack of noise and I’ve spent a lot less time creating ‘yet another assertion’ method for specific cases. And in most cases I never need to write a custom failure message as the failures are self describing.

h4. Updates

  1. The matchers from jMock have been pulled out into a new project, Hamcrest.
  2. A follow up to this post shows some creative uses of matchers, and talks a bit about when you shouldn’t use them.
  3. JUnit 4.4 now comes with assertThat()!

SiteMesh and Content Management @ O’Reilly OpenSource Conference

I’m talking at the O’Reilly OpenSource Conference (OSCON) – Wednesday Aug 3, Portland, Oregon.

Come and say hi.

A problem faced in every web application is how to separate style from content. SiteMesh is a framework that provides an elegant solution to this, resulting in a clean separation that is straightforward to work with, complements other web frameworks, and is easily applied to existing applications.

The first part of this session introduces SiteMesh, including an overview of the architecture and patterns, comparisons with other approaches, and how it can complement existing web frameworks (such as WebWork, Spring, and Struts).

The second part of this session demonstrates how SiteMesh can be blended with other technologies to form the foundation of a rich content management system that distinguishes between the specialized roles of users, their skills, and the most suitable tools. Content writers can use a word processor, web designers can use a WYSIWYG web development tool, and developers can use their IDE.

Allowing these different roles and tools to come together to produce one website is a trivial task with SiteMesh–allowing content management to be easily introduced to existing applications.

Finally, some of the advanced features of SiteMesh are discussed, such as real world tips and tricks, how to create custom strategies for which look and feel to apply, assembling pages from components and building portal style applications.

And for the first time, new features in SiteMesh 3 will be demonstrated, including extending the HTML processor, using it outside of SiteMesh, and offline support.

http://conferences.oreillynet.com/os2005/

XStream 1.1.2 released. Java 5 Enums, JavaBeans, field aliasing, StAX, and more…

New features:

  • Java 5 Enum support.
  • JavaBeanConverter for serialization using getters and setters.
  • Aliasing of fields.
  • StAX integration, with namespaces.
  • Improved support on JDK 1.3 and IBM JDK.

Changelog:
http://xstream.codehaus.org/changes.html

Full download:
http://dist.codehaus.org/xstream/distributions/xstream-1.1.2.zip

Jar only:
http://dist.codehaus.org/xstream/jars/xstream-1.1.2.jar

VB.Net is the bestest

I was happily coding away in VB.Net today (grrr) when I noticed a little weirdity in the intellisense popup.

stupid.gif

Documentation says:

The NotOverridable modifier defines a method of a base class that cannot be overridden in derived classes. All methods are NotOverridable unless marked with the Overridable modifier. You can use the NotOverridable modifier when you do not want to allow an overridden method to be overridden again in a derived class.

Makes C++ look simple.