New QDox lead – Robert Scholte

I’d like to announce the new lead for QDox, Robert Scholte.

Anyone who’s been following the project for the past year would have seen that Robert has made a tremendous number of contributions to all areas of QDox, including bug fixes, build, docs/website, releasing, user support and admin. If you follow the project further back, you’ll see that Robert has actually done more on the project in the past year than was done for the combined 6 years before that…. impressive.

I originally wrote QDox back in 2002, as a stop gap until annotations were added to the Java language. It was quickly picked up tools such as XDoclet. I always thought (and hoped) that once annotations made it into the language, it would be redundant. Yet it lived on. Unlike the JDK Annotation Processing Tool (APT), it could process source code rather than byte code in a simpler and faster manner. And it provided access to information that the JDK didn’t make available (e.g. parameter names). And it was useful where annotations still did not exist, like in JavaME. So, even in 2010, it still lives on and is used in tools such as Maven and Eclipse. But I cannot invest the time into it that I once could.

Given Robert’s commitment to the project, I can think of no better person to lead the project. He’s done an astounding job. Robert has kindly accepted this role and I’m delighted to hand it over to him.

I’d also like to use this opportunity to thank Paul Hammant, Mauro Talevi and Dennis Lundberg for their ongoing efforts on the project.

JUnit/TestNG tip: Diffing Lists

A little cheap and cheerful JUnit/TestNG tip…

Problem: You’ve got two lists that you want to compare using assertEquals(). But when the assertion fails, the error message kinda sucks.

java.lang.AssertionError: expected:<[Person{name=Joe,starsign=Virgo,sex=Male}, Person{name=Jaimie,starsign=Libra,sex=Female}, Per
son{name=Ewan,starsign=Capricorn,sex=Male}]> but was:<[Person{name=Joe,starsign=Virgo,sex=Male}, Person{name=Jaimie,starsign=Scor
pio,sex=Female}]>
   at org.junit.Assert.fail(Assert.java:74)
   at org.junit.Assert.failNotEquals(Assert.java:448)
   at org.junit.Assert.assertEquals(Assert.java:102)
   ...

It’s really tricky to see exactly where the differences are in the list.

Solution: Join both the lists into a string, delimited by newlines and assert that instead. That will force your IDE’s multiline differ to kick in.

junit-diff(click image to see the rest of it)

This relies on having a sensible toString() method on your list items.

If your IDE doesn’t do this, or you can’t run your tests from your IDE, you should really get that seen to.

Here’s a method to do it:

public static <T> void assertList(List<T> actual, T... expected) {
  assertEquals(join(expected, "\n"), join(actual, "\n"));
}

That’s all. Now get back to work.

SiteMesh 3 preview

I’m pleased to announce a preview of the next generation of SiteMesh.

Before I go on, here’s a big disclaimer: This is not a stable release version yet… it’s not even a beta… it’s an alpha… a first alpha.

But anyway, I decided to launch it in this state, as I think it’s a huge improvement over SiteMesh 2, and I’d like to get your feedback earlier so it can be fed into the final product.

Quick links, for the impatient:

So what’s new? Well, everything. The 10 year old code base has been thrown away and rebuilt from the ground up. At the core of it is a new content processing architecture that performs roughly three times as fast as SiteMesh 2 with half the memory usage.

Along with that:

  • Decorator chaining. Compose pages from multiple layouts and decorators.
  • Decoupled from templating systems. Decorators can be produced with any technologies – you are free from JSP.
  • Offline site generation. Apply decorators as part of a build step. Accessible through Java API, Ant task and command line tool.
  • Simplified configuration. Use XML, Java or plug in your own system.
  • Clean API provides extension points for adding custom processing rules (e.g. on the fly page transformations).
  • Licensed under Apache Software License v2.0.

So what’s the same? The values of SiteMesh have not changed – simplicity, robustness and performance.

You can learn more about SiteMesh 3 on its new website: www.sitemesh.org

What about compatibility with SiteMesh 2?

This was a hard decision to make, but SiteMesh 3 is not backwards compatible with SiteMesh 2. Upgrading will require work beyond just upgrading a library. Over the coming weeks, I shall produce some guides and tools to make this transition easier. Though this may be frustrating to many users, it was becoming too hard to make changes to SiteMesh 2 based on limitations to its design. SiteMesh 3 addresses these and will make way for a lot of great features and improvements in years to come.

Meanwhile, SiteMesh 2 will continue to be supported until 2011 – though this will mostly be critical bug fixes only. The majority of development effort will be focussed on SiteMesh 3.

So give it a go, and give us feedback. But please remember that it’s only an alpha!

SiteMesh logo design contest

I’m looking for a gorgeous new logo for SiteMesh, so I”ve just launched  a logo design contest on 99designs.com. It will run for 7 days and has a prize of $400 – of course, the real prize is the good feeling of contributing back to the SiteMesh project ;).

http://99designs.com/contests/23286

The brief…

Theme:
Clean, simple, modern.

Font:
Feel free to use whatever font you like, or create your own. Remember that SiteMesh is associated with clean and simple. If you use (or derive from) an existing font, please specify what font this is and where it can be obtained.

Background:
The logo should be on a flat (single color) background, so it is easy to place in different places. If the background is not white, please also supply a variation of the logo that has a white background (e.g. to use in print outs).

Color scheme:
Up to you.

Variations:
Would like two variations of the logo:
1. The worded logo: This focus of this must be the word ‘SiteMesh’. It will typically be used in the header of every page of the website, documentation, etc.
2. The icon based logo: This would typically appear in small square placements (e.g. a desktop icon). It does not need to contain the text ‘SiteMesh’ but should be easy to recognize as associated with the worded logo. Should look good at small sizes (e.g. 42×42).

Format:
Please supply original high-resolution files of whatever tool you use to create the logo (e.g PSD), so I can make modifications and rescale in the future as necessary.

http://99designs.com/contests/23286

Running SiteMesh on AppEngine

Does SiteMesh work on Google AppEngine (Java preview)?

Short answer: Yes.

Longer answer: Yes. It does. So long as you check 2 things…

1. Upgrade

You need SiteMesh version 2.4.2 or greater. Earlier versions do not support AppEngine.

Download here

2. Disable static file serving

If you want decorators to be applied to static content (e.g. to .html files), the following needs to be added to WEB-INF/appengine-web.xml:

<static-files>
  <exclude path="**"/>
</static-files>

This forces the static resources to be served by the Servlet engine, rather than a separate static web-server. Without this, the files served by the static web server will not be decorated.

Happy AppEngining.

SiteMesh 2.4 released

After a long wait, SiteMesh 2.4 has just been released.

This mostly consists of minor bug fixes and improvements.

Our Marmites

Marmite is a spread available in the UK and a few other countries. If you’ve come across Vegemite, it’s similar. The taste is very distinctive, to the point where anyone who’s ever tried it either loves it or hates it. There is no in between. Whereas representing a persons feelings for certain types of food may require a complicated data structure, for Marmite you’d only ever need a single bit. You get the idea.

The marketing slogan in the UK is “Marmite: Love it or Hate it?”.

I often ask different people what they think about a certain product (for example, a web framework). Usually I’ll get varying feedback, but once in a while I’ll come across something that only gets two categories of answer – “Love it” or “Hate it”. It’s usually accompanied this person getting very excited or angry, going a bit red in the face and thumping the table (in a positive or negative way). There are never any inbetweeners. If people have not actually used a product, I’ll discard their opinion.

These are the Marmites of our world.

Here are some of the Marmites I’ve come across, with typical explainations I get from lovers and haters.

Maven is the Marmite of the build tool world.

  • Lovers: Consistency across projects. Common build layout and tasks. Dependency management. Does lots.
  • Haters: Poor documentation. Need the head a size of a planet to understand it.

Mac OS X is the Marmite of the desktop OS world.

  • Lovers: Beautiful. Stable. Less clutter. Less hassle. Unixy + usability. It’s not Windows.
  • Haters: Forever staring at the spinny beachball.

Ruby on Rails is the Marmite of the web application framework world.

  • Lovers: Ruby. And web sites. And ActiveRecord. And it’s oh so agile.
  • Haters: Irritating fan club. Lack of abstractions can make it hard to evolve larger systems.

Google Web Toolkit is the Marmite of the AJAX world.

  • Lovers: Allows you to use Java instead of JavaScript for building apps.
  • Haters: Allows you to use Java instead of JavaScript for building apps.

ReiserFS is the Marmite of the file system world.

  • Lovers: Excellent performance with many files and big disks. Scales well. Fast recovery through journaling. Uses space effectivley.
  • Haters: Numerous problems with fsck operations corrupting the filesystem. Future uncertain since Mr Reiser was convicted of murdering his wife.

IBM Visual Age for Java is the Marmite of the Java IDE world.

  • Lovers: A magical image of objects that can be navigating, interacted with and modified on the fly. Like Smalltalk.
  • Haters: Uhhmm. WTF is this? Where’s my source code? How do I use this thing? Isn’t this supposed to be Java?

Behaviour Driven Development is the Marmite of the development process world.

  • Lovers: It bridges the gap from requirements understanding to a live running system, with a simple metaphor.
  • Haters: It’s what now? You’ve changed the prefix from ‘test’ to ‘should’ and introduced a few words. Get away from me.

The thing to remember is to never get into a conversation with anyone about a Marmite. Either you will agree with someone and learn very little from it, or you’ll get into a fight.

So to help others avoid these awkward moments, contribute your own list of Marmites here (comments below, or trackback). Criteria: Must be something that everyone who has experience of either hates or loves – there can be no inbetween.

Qt, now a viable option for Java

Today it was announced that Qt would be made available under the LGPL license. Qt is well known in the Linux community thanks to KDE, but Qt based applications run very well on Windows and OS X, as well as many embedded platforms such as WinCE and S60.

I’m a closet Qt fan, because:

  • It has a clean OO programming model. Simple, extensible, yet not over the top.
  • It has easy to use bindings for a wide variety languages, including C++, Java, Python, Ruby, Perl, PHP, and C#.
  • It’s mature with a rich set of widgets. Including typical windowing components, a WebKit based browser, media playback, OpenGL, etc.
  • The resulting apps look good, feel fast and fit in with the native platform.
  • It’s very extensible/flexible.

Of course, on each of those points alone there are other frameworks that can beat Qt – it’s the combination of all of these that makes it so compelling.

Oh yeah, Java…

Jambi is the Qt binding layer for Java. It’s actually been very well thought out, allowing you to use pretty much any of the functionality in the C++ layer, with a simple Java API and set of conventions. It also includes bindings allowing Qt and Swing components to seamlessly co-exist in the same UI.

There are parts of the Qt Java API that make me cringe slightly (namely using reflection for event callbacks), but overall the benefits outweigh these – I’ll just have to get over the cringey bits and live with them.

I’ve used Swing and SWT extensively on some big (and small) projects. I’ve only dabbled with Qt, but from that I’ve been productive very quickly and built some solid UIs in little time. The reason why I’ve previously chosen not to use it on ‘real projects’ was due to licensing. Now that it’s LGPL, that changes everything.

If I were to start a new project tomorrow and needed a serious Java UI, Qt would be my first choice.

Ok, that wasn’t really a convincing argument. How about checking out:

Creative uses of Hamcrest matchers

The matcher API of Hamcrest is typically associated with assertThat() or mocks. I always knew other people would find good uses for it, but I never really knew what.

I particularly like these:

Collection processing

Håkan Råberg blogged about how Hamcrest can be used with iterators:

List<Integer> numbers = Arrays.asList(-1, 0, 1, 2);
List<Integer> positiveNumbers = detect(numbers, greaterThan(0)));

List<String> words = Arrays.asList("cheese", "lemon", "spoon");
List<String> wordsWithoutE = reject(words, containingString("e"));

Nothing rocket-sciencey about it. But simple and useful because it reduces boilerplate code and get to use the ever growing library of Hamcrest matchers.

On top of that, combining Hamcrest with a CGLib generated proxy, he has built a staticly typed query API:

List<Person> employees = ...;
List<Integer> allAges
= collect(from(employees).getAge());
List<Person> allBosses
= collect(from(employees).getDepartment().getBoss());
List<Person> allAccountants
= select(from(employees).getDepartment().getName(),
containingString("Accounts"));

This is nice alternative to a string based query language as you get your IDE completions, refactoring, compile time checking etc, without the noise of boilerplate code.

Web testing

Robert Chatley has taken some of the concepts of his LiFT framework and reimplemented them using Hamcrest and WebDriver for performing web testing.

public void testHasLotsOfLinks() {
goTo("http://some/url");
assertPresenceOf(greaterThan(15), links());
assertPresenceOf(atLeast(1), link().with(text(containingString("Sign in"))));

clickOn(link().with(text(containingString("Sign in"))));
assertPresenceOf(exactly(1), title().with(text(equalTo("Sign in page"))));
}

Now initially this seems a bit wordy and strange. Robert has designed this as a literate API. If you adjust the syntax highlighting of your API and make the Java keywords and syntax less visible, you get this:

goTo "http://some/url"
assertPresenceOf greaterThan 15 links
assertPresenceOf atLeast 1 link with text containingString "Sign in"

clickOn link with text containingString "Sign in"
assertPresenceOf exactly 1 title with text equalTo "Sign in page"

The motivation here is that the API usage is self documenting and could be useful to non-programmers. The flip-side to this is that it’s actually quite hard to write APIs like this and the usage can take quite a bit of getting used to.

Robert also introduced a Finder interface (the link() and title() methods return Finder implementations). This allows you to factor out your own UI specific components:

assertPresenceOf(atLeast(1), signInLink());
clickOn(signInLink());
assertPresenceOf(exactly(1),
blogLink().with(urlParameter("name", containingString("joe"))));

This is the bit I really like.

Allowing abstractions of components and matching rules to be combined in many different ways, so tests can check exactly what they need to, resulting in reduced less brittle tests that are easier to maintain.

Other uses

As I hear of other uses I’m listing them on the Hamcrest wiki.

When it goes bad

Of course, like any technology, it’s easy to get carried away.

Here’s an example of Hamcrest gone bad:

assertThat(myNumber, anyOf(equalTo(0), allOf(greaterThan(5), lessThan(10))));

I’m not a LISP programmer, so I find that really hard to understand. Just because we have an assertTHAT() method, we don’t have to use it all the time. In this case it’s much simpler to use plain old assertTRUE():

assertTrue("myNumber should be 0 or between 5 and 10",
myNumber == 0 || (myNumber > 5 && myNumber < 10));

Even though the non-Matcher version is longer (it could be shortened by leaving out the message and using a shorter variable name, but that would make it harder to understand), I find it much easier to understand.

But, what if you actually needed to use a matcher (e.g. for the web testing or collection processing examples above)?

One approach is you could use higher level matcher that are composed of other matchers:

matcher = anyOf(equalTo(0), allOf(greaterThan(5), lessThan(10)))
// simplifies to
matcher = anyOf(equalTo(0), between(5, 10))

Complete tangent: An alternative to between(5, 10) is between(5).and(10). The latter makes for more literate code, but is harder to implement – again a design tradeoff.

Another approach is to create a one-off anonymous matcher implementation:

matcher = new CustomMatcher() {
public boolean matchesSafely(Integer n) {
return n == 0 || (n > 5 && n < 10);
}
}

What are you doing with Hamcrest?

Updates:

  1. JUnit 4.4 now comes with Hamcrest and assertThat().

Hamcrest 1.1 released

http://code.google.com/p/hamcrest