Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 73: Test your Java/CORBA server from the inside

Learn how to test your server classes from within the running server by dynamically loading and executing test classes inside the server's virtual machine

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Testing complex server code can be a difficult task. Often the code you want to test depends heavily on other parts of the system. It can be torturous to try to isolate that piece in order to really exercise it well. Often the code you want to test is not even directly reachable from the client side. By executing your test within the same virtual machine in which the server class executes, you can enjoy the fullest access to the features of that server class. You could instantiate the server class in a freestanding test program, but this approach may not provide the same context the server class would encounter when it runs within the server. It would be nice if you could test such classes in the context in which they normally reside: the running server.

When we write our server code in Java, we have easy access to dynamic loading on a very granular scale. This dynamic loading is the key to a solution for our testing challenge. A few lines of extra code in the server enables thorough testing of the server while the server is running. A test sequence we wish to perform can be placed in a test class which the server can be directed to dynamically load and execute by name.

Figure 1 shows a set of objects within a CORBA server. The lollipop or jack is a simple way to denote an interface -- a set of related operations. The arrow flowing into the jack shows usage of that interface. Imagine an object on the server side that is not directly exposed to the ORB. In this figure, you'll find just such an object, an object of the CustomerStorage class. Thorough testing of the CustomerStorage class from the client side may be difficult or impossible. But to instantiate objects of the CustomerStorage class from a freestanding test program also may be undesirable due to its dependency on the Customer class.

We don't have to live with the limits of either approach. We can test our server code in place, while the server is running! Again, the key is Java's ability to dynamically load a class and to instantiate new objects of that class. In effect, we will inject our test code into the server. The first step is to define a Java interface that our test classes will implement. Our CORBA server will know nothing of the test classes, but it will know of this new interface. We define this interface to eliminate static dependency of the server to specific tests. We can write as many test classes as we like, without ever having to recompile the server.

To keep things simple, we define a test interface called InjectableProgram, with a single operation called start:

      interface InjectableProgram {
            void start();
      }


Each test class must implement this interface. Believe it or not, half our work is done. A test class named CustomerStorageTest that implements InjectableCode, can be loaded and executed with the following code fragment. We will use a more flexible variation if this fragment is in our server.

      Class c = Class.forName("CustomerStorageTest");
      InjectableProgram program = (InjectableProgram) c.newInstance();
      program.start();


It's almost that simple!

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • See Bill Venners's Design Techniques (January 1999) "Design with dynamic extension" http://www.javaworld.com/javaworld/jw-01-1999/jw-01-techniques.html
  • Class Unloading in Clarifications and Amendments to The Java Language Specification http://java.sun.com/docs/books/jls/clarify.html
  • java.lang.Class class http://java.sun.com/products/jdk/1.2/docs/api/java/lang/Class.html
  • java.lang.ClassLoader class http://java.sun.com/products/jdk/1.2/docs/api/java/lang/ClassLoader.html
  • java.lang.reflect package http://java.sun.com/products/jdk/1.2/docs/api/java/lang/reflect/package-summary.html
  • Sun's Java IDL compiler can be obtained here http://developer.java.sun.com/developer/earlyAccess/jdk12/idltojava.html