Thursday, February 3, 2011

Extend TestCase and use annotations in JUnit 4

JUnit 3 required that test classes extend, directly or indirectly, the class junit.framework.TestCase, and that each test method begin with the word "test". In contrast, JUnit 4 uses @Test annotations for each test method and does not require the test class to extend junit.framework.TestCase. But what if we have some JUnit 3 and some JUnit 4 tests? JUnit 4 automatically treats classes that extend TestCase as a JUnit 3 test, and therefore ignores the @Test notation. So let's say there is a class like the following.

public class MyTests extends TestCase
{
@Test
public testOne()
{
//do stuff
}

@Test
public anotherTest()
{
//do stuff
}
}

Using JUnit 4, it would appear that both these tests are run, but actually only the first one runs. Why? Because the class extends TestCase, so JUnit 4 will handle this class using a JUnit 3 compatibility mode. This means that the @Test tag is ignored and the method names are examined for the pattern "test*". This can be fixed in two ways. The first is by removing "extends TestCase". This will cause JUnit 4 to run the test in normal mode instead of backwards compat mode.

The second option is to use a class annotation to force the class to be treated as a JUnit 4 test case. It looks like this:

@RunWith(JUnit4.class)
public class MyTests extends TestCase
{
...
}

This forces JUnit 4 to locate tests using annotations instead of using the older method name convention.

The solution using @RunWith was found in the JUnit yahoo group.

Friday, February 26, 2010

No %%Pages: comment in header!

After being able to print successfully for several months, I got this error message today while printing from my laptop with Fedora 10: "No %%Pages: comment in header!". I have no idea what it means, but after searching around there was a simple fix for me. From the printer properties GUI I noticed that the "Print Test Page" button was disabled. In the policies tab the printer state "enabled" box was unchecked. I checked the box to re-enable the printer, and voila, I was able to print again.

Monday, February 15, 2010

MojoFailureException vs. MojoExecutionException

There are two types of wrapper exceptions commonly used in Maven plugins, the MojoFailureException and MojoExecutionException. It's not obvious which one is the correct one to use, so here is a simple explanation. A MojoExecutionException typically means that an unexpected and unrecoverable error happened while running the plugin. This would be something like a NullPointerException or other RuntimeException that the plugin developer did not expect and/or can not handle. It's basically a plugin crash.

A MojoFailureException means that there was a problem in the build which the plugin detected, and the plugin is now notifying you of that failure. This would be something like a Java source file that couldn't compile. The build can not continue, but the plugin is still working normally.

More information can be found in the Sonatype Maven book.

Wednesday, January 6, 2010

Perl multiline search/replace

Quick perl program to do a multi-line search and replace.

open(INPUT,"<$ARGV[0]") or die;
$/ = "%%\n";

$theFile=<INPUT>;
#print $theFile;
close(INPUT);

#$theFile =~ s# \true\<\/optional\>\n##ig ;
$theFile =~ s# \<\/dependency\># \true\<\/optional\>\n \<\/dependency\>#ig;

open(OUTPUT,">$ARGV[1]") or die;
print(OUTPUT $theFile);
close(OUTPUT);

Thursday, December 3, 2009

Rsync with central Maven repository

For reference here is the rsync command to copy files from the central Maven repository.

rsync -v -t -l -r mirrors.ibiblio.org::maven2/subdir /local/path

Sunday, November 22, 2009

XUbuntu 9.10

I have an old Fujitsu laptop (pentium 3, 256 MB RAM, 20 GB disk) that has gone through a few iterations of various linux distributions. I used XUbuntu 7.04 for a while and it was ok, but it was just too slow. The tricky part is that it has an old Cisco Aironet 360 wireless adapter which only works with a 2.4 kernel (or at least I thought this was the case). I tried out Damn Small Linux, and it's great for what it does, but for my laptop I couldn't get the wireless card or the touchpad working. I tried out various combinations of older versions of Fedora and Ubuntu, but they were always either painfully slow or unable to handle the old wireless card. Fedora 12 was unbearably slow.

I didn't think a recent version of XUbuntu would work, because in the past I was only able to get the wireless working when using a 2.4 kernel. But I'm really glad I gave it a try anyway, because it works! XUbuntu 9.10 was able to find and configure everything on the laptop right away, including my old wireless card. The install process is improved since previous versions, the boot time is faster, and the gnome interface seems more responsive all around.

Big thanks to the XUbuntu guys for all their work on this. Now I have an extra working laptop again!

Friday, July 24, 2009

Ant refid resolution 1.5, 1.6, 1.7

I noticed some behaviour changes in Ant related to refids. In Ant 1.5.x a fileset refid has to be defined before being used. So for example if I define a fileset in target "a" and then call target "b" which uses the refid, the build will fail because target "a" was never run and never set up the refid.

This behaviour changed in Ant 1.6.x, so that Ant tries to be more intelligent and automatically searches for refids that are defined in the build, but not necessarily run. So in the above example, Ant silently succeeds and the refid is found in target "a" even though that target was never actually called. This seems like strange behaviour to me, because it's not well documented and seems like it could produce some tricky bugs. I guess the Ant developers also thought this behaviour was not ideal because in Ant 1.7 the same automatic refid resolution works but now produces a warning. I briefly tried to track down some discussion related to this in the ant developer mailing list archives, but I was unsuccessful in finding anything.