Oops Null Pointer

Java programming related

Monthly Archives: April 2010

Creating UML Sequence Diagrams from Debug / Stack traces

I found a good tool to create sequence / class interaction diagrams from Java stack traces or debug stacks using a free Eclipse plugin called AmaterasUML. Install it in Eclipse and you can open a “Sequence diagram from stacktrace” view, paste in a stacktrace and it will generate a nice looking sequence diagram.

With a bit of text manipulation you can modify a debug trace (from right-click “Copy Stack” on the debug window) to look like a stack trace and generate the UML from a debug breakpoint. The steps are:

  • Add “at ” to the start of every line
  • Replace method args with the class name.java:line number
  • Clean up $2$1 type class suffixes

E.g.

EventRegistryImpl.fireEvent(Event) line: 47
BaseFolderNavigator$2.handleEvent(Event) line: 79
CoverageNavigatorViewGXT(BaseObservable).fireEvent(Event) line: 27
BaseNavigatorViewGXT$2$1.execute() line: 149

becomes

at EventRegistryImpl.fireEvent(EventRegistryImpl.java:47)
at BaseFolderNavigator.handleEvent(BaseFolderNavigator.java:79)
at CoverageNavigatorViewGXT(BaseObservable).fireEvent(CoverageNavigatorViewGXT.java:27)
at BaseNavigatorViewGXT.execute(BaseNavigatorViewGXT.java:149)

Below is a gawk command (via UnxUtils) to convert a stack file (x.txt) to a stacktrace

gawk "{str = $0; sub(/^\t/, \"^\", str);match(str, /(.*)\./, arr); sub(/^/, \"^at \", str);sub(/\(.*\./,\".\",str);sub(/\([^\)]*\)/, \"(\", str);sub(/ line: /, arr[1]\".java:\", str);sub(/$/, \")\", str);sub(/\t\)$/, \"\)\", str);print str}" x.txt

Capturing Integration Testing Input/Output with XStream

A co-worker was looking for a way to capture input and results during a debug session for certain scenarios that were occurring at run time. Ideally they were looking for a tool that could be used in Eclipse and capture parameters and return values from frames in the debug session and then be able to reuse those values in jUnit tests.

A quick search couldn’t find any tools that find the bill, but as a quick and dirty method you can use XStream in an Eclipse “Display” view to dump out input and return variables. Then in a jUnit test you unserialise the XML back into objects and tests the inputs against the results.

The XML format gives you two instant advantages:

  • No need to reconstruct the input / result objects from scratch (which can be difficult, especially in complex or legacy systems)
  • Easy tweaking of the the XML to get the results you want

A disadvantage is that if the objects structure changes then re-factoring tools will miss the XML data and the test may no longer work.

To use this technique:

  • Put XStream in your project classpath
  • Show the Display view in eclipse (Widnow->Show View-> Display)
  • Put breakpoints in the code to capture input and result objects
  • At a breakpoint run the following code in the Display view to serialise the objects to XML
(new com.thoughtworks.xstream.XStream()).toXML(someObjectToSerialise)
  • Save the XML to files and load the files into a jUnit test (say with Commons FileUtils.readFileToString())

If you know of any other tools to do this in a more automated fashion then let me know (it would be cool to do this with a few clicks via a plugin – even generate the test classes).