Wednesday, April 25, 2007

Bandwidth saver

I never miss an opportunity to push for Freemarker, the most powerful template engine in the world, so here's a quick tip on how to strip all whitespace from your HTML pages:

<#assign out>
<#compress>
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>

// Your page here

</BODY>
</HTML>
</#compress>
</#assign>
${out?replace('\n','')}


It assigns the compressed body of the outer assign macro to the variable "out", and the finally writes the value of "out" with all newlines stripped.

The entire page is now a singe line of HTML - a developer's nightmare, but a nice a bandwidth saver.

Combined with Sitemesh, another one of my favorite tools, this is easily and transparently applied to all your pages.

Tuesday, April 17, 2007

A quick peek at SpringIDE 2.0

There are a number of cool new features coming in SpringIDE 2.0, currently at milestone 3. One example is the new SpringExplorer, which is a comprehensive tree view of a Spring context, another is content assist for custom namespaces.

But the by far neatest new feature is the ability to evaluate AspectJ expressions in XML on the fly, and displaying what beans will be advised! Take a close look at this screenshot...an <aop:advisor> element is highlighted on the left, with an AspectJ pointcut expression. On the right, you can see what methods will be advised (the "advises" arrow). A godsend!

Thursday, April 12, 2007

One more thing...

I forgot one thing about Test-NG...they switched the order of "expected value"/"actual value" in the assert statements! So after finally having learned the hard way that the "expected" value should be first (JUnit 3), I now have to revert to (the more intuitive but less standard way of) putting the "actual" value first :-)

Today I'm struggling with an issue related to this: http://forum.springframework.org/archive/index.php/t-36086.html

Basically, an @Around expression with "&& args(foo)" works fine when the Eclipse compiler is being used, but not when Maven compiles the sources with Sun Javac 1.6, no matter how much debug info I turn on :-/

Wednesday, April 11, 2007

A week with Test-NG

On my latest assignment (I work as a consultant), we've decided to let go of the old workhorse that is JUnit 3, and go with one of the new, fancy @Nnotations-driven frameworks JUnit 4 or Test-NG. After a short evaluation period, which consisted of verifying basic Eclipse and Maven support and skimming a few tutorials, the feature sheet of Test-NG looked a bit stronger. The two major selling points of Test-NG over JUnit 4 were the ability to group tests and that Test-NG can run only the tests that failed the previous round.

However, the first week of day-to-day usage of Test-NG has been less than stellar, mostly (but not limited to) the poor IDE integration. My list of pros and cons versus the trusted old JUnit 3, when used inside Eclipse:

Pros:
  • The test class is only instantiated once, not once for each method in the test class. As it should be.
  • Better setup granularity with both @BeforeClass and @BeforeMethod entry points
  • @DataProvider is a nice feature, especially in conjunction with Selenium.
And the cons:
  • No built-in macro expansion in Eclipse that creates a test method - in a class that extends the JUnit 3 class TestCase, just type "test" and press Ctrl-Enter, and you have a method skeleton. Removing the need to inherit means more work in this case.
  • Keyboard shortcut Ctrl-Alt-X N does not work, so I have to use the mouse every time I want to run a test.
  • Can't run more than one test class at once, using multi-select in the file tree browser.
  • Can't run an entire package hierarchy of test classes in one shot.
  • Can't run a single test method by right-clicking a method name in the "green bar" view.
  • Using JMock is a lot more work, either by re-inventing half of MockObjectTestCase manually, or by using plain old inheritance.
  • No automatic static imports (a weakness of Eclipse's, to be fair), which forces me to write Assert.assertEquals() instead of just assertEquals() or write the static import manually.
So, a lot of annoyances on the ground level, and we haven't really started to see the benefits of large-scale features such as grouping and run-failed-tests-only. The verdict so far is that Test-NG is as good as or worse than JUnit 3, when counting in IDE integration.

And really, what is so fantastic about not having to inherit TestCase and naming your methods testXXX? Here's the number of times that has been a limiting factor in my work as a Java developer: 0.

Also, annotations are good for a lot of things (@Entity and @Transactional are great), but sometimes you get the feeling that various framework authors are going "OK, we now have annotations in Java, how can we possibly shoe-horn that into JMyFramework4J 2.0?"