Monthly Archive for November, 2006

Life without enum; handling typesafe constants pre-Java 5, AKA The Typesafe Enum Pattern

Introduction
Java 5 has been around for quite some time now, and many of us have grown addicted to the use of some of the snazzy features we were treated to in that release. Me for one, I have a hard time coping without generics and enums. But as a consultant, I often find myself at clients running older versions of the JDK due to various reasons. This fact, coupled with said appreciation of features I now am unable to use, one tries to find ways in which to utilize the strengths of the new features without their presence. This brings about the re-discovery of the existing patterns which led to the new feature.

One such pattern is the typesafe enum pattern, or descriptor pattern as I’ve also seen it called. This is a rather simple pattern for handling constants in a typesafe, clear and consistent manner. The alternatives to this pattern tends to use constants of simple types (typically int or String) defined as class constants or using an Interface to make them easily accessible. Continue reading ‘Life without enum; handling typesafe constants pre-Java 5, AKA The Typesafe Enum Pattern’

ClassNotFoundException using XMLUnit with JDK 1.4

I recently set up a testcase for validating generated XML and in doing so, decided to use the XMLUnit extensions to jUnit.

When running my testcase, I got the following stacktrace:

java.lang.ClassNotFoundException: org.w3c.dom.ranges.DocumentRange
	at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:141)
	at com.client.MapHandlerTest.testMarshal(MapHandlerTest.java:79)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:324)
	at junit.framework.TestCase.runTest(TestCase.java:154)
	at junit.framework.TestCase.runBare(TestCase.java:127)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:118)
	at junit.framework.TestSuite.runTest(TestSuite.java:208)
	at junit.framework.TestSuite.run(TestSuite.java:203)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

This client uses a fairly old release of Java 1.4, and it appears the

org.w3c.dom.ranges.DocumentRange

class (and family) hadn’t entered the JDK at that time.

No worries, just downloaded a more recent XML parser (Xerces 2.8.1), added xml-apis.jar to my classpath and the tests run smoothly again. :)