Archive for June, 2009

Instrumenting an ear with cobertura and ant

Using cobertura with ant is very simple, this is the build xml I’ve used:

<?xml version="1.0" encoding="UTF-8"?>

<project name="labs.cobertura.generic" basedir=".">

Generic file to instrument a an already compiled jar
precondition: having a jar normally compiled
usege: after the instrumentation, we have to insert the instrumentated jar,
containing the same classes as the original jar, into a location where it will be loaded.
(e.g. in the classpath startup of the application server)
author: Germano Giudici

<property file="" />

<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="*.jar" />

<taskdef classpathref="cobertura.classpath" resource="" />

<target name="instrument-jar">
<mkdir dir="${instrumented.dir}" />
<cobertura-instrument datafile="${basedir}/cobertura.ser" todir="${instrumented.dir}">
<fileset dir=".">
<include name="${original-jar-name}" />

<target name="coverage-reports">
<mkdir dir="${coverage.xml.dir}" />
<cobertura-report datafile="${basedir}/cobertura.ser" srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />

<mkdir dir="${coverage.html.dir}" />
<cobertura-report datafile="${basedir}/cobertura.ser" destdir="${coverage.html.dir}">
<fileset dir="${src.dir}">
<include name="**/*.java" />

<target name="clean">
<delete dir="${instrumented.dir}" />
<delete dir="${reports.dir}" />
<delete file="cobertura.log" />
<delete file="cobertura.ser" />
<delete file="cobertura.ser.lock" />

# The path to cobertura.jar


# the path to the instrumented classes' sources
# The name of the jar to be instrumented. It must reside in the same sirectory of this file

I’ve used this to instrument a jar inside an ear, deployed to weblogic 8.1.4.

For default cobertura writes all stats whenever the jvm ends… to semplify this task I’ve deployed a simple servlet containing this code:

String className = "net.sourceforge.cobertura.coveragedata.ProjectData";
String methodName = "saveGlobalProjectData";
Class saveClass = Class.forName(className);
java.lang.reflect.Method saveMethod = saveClass.getDeclaredMethod(methodName, new Class[0]);
saveMethod.invoke(null, new Object[0]);

So when I want to flush the statistics gained by cobertura, all I’ve to do is invoke that servlet.

It’s important to start weblogic (or even wathever other AS) with the cobertura.jar in the classpath.

The statistics flush whill generate a file called cobertura.sar in the domain’s directory, then you can use it to generate the report (with coverage-reports task ).

Categories: Uncategorized

No more “Unknown source” on exception stacktraces with code compiled with ant

Add this to javac tag: <javac srcdir=”${my-source}” destdir=”${my-dest}/classes” debug=”true” debuglevel=”lines,vars,source”>

Categories: Uncategorized