You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Current »

JCOV

JCov is a pure java implementation of a code coverage tool which provides a means to measure and analyze dynamic code coverage of Java programs.

Preparation

Unfortunately, at the time of writing, the jcov tool is not present in Maven and we need to download and build the project ourselves. Luckily, this is an easy process, and once prepared, the jar file could be reused as many times as wanted.

The following is the procedure to download and build jcov:

export JCOV_HOME=~/jcoverage

#Prepare base directory
mkdir $JCOV_HOME
cd $JCOV_HOME

#Build jtreg
git clone https://github.com/openjdk/jtreg.git
cd jtreg
bash make/build.sh --jdk $JAVA_HOME
cd -

#Build jcov
git clone https://github.com/openjdk/jcov.git
cd jcov/build

#Download dependencies
mvn dependency:get -Dartifact=org.ow2.asm:asm:9.1 -Dtransitive=false -Ddest=asm-9.1.jar
mvn dependency:get -Dartifact=org.ow2.asm:asm-util:9.1 -Dtransitive=false -Ddest=asm-util-9.1.jar
mvn dependency:get -Dartifact=org.ow2.asm:asm-tree:9.1 -Dtransitive=false -Ddest=asm-tree-9.1.jar

ant clean
ant -v -f build.xml -Dasm.jar=$PWD/asm-9.1.jar -Dasm-util.jar=$PWD/asm-util-9.1.jar -Dasm-tree.jar=$PWD/asm-tree-9.1.jar -Djavatestjar=$JCOV_HOME/jtreg/build/images/jtreg/lib/javatest.jar

Build

With jcov, the classes are instrumented after compilation, so we can build as usual and instrument the jar files later. After a regular compilation, you need to choose the jar files you'd like to use for coverage and instrument them:

mkdir coverage_reports
cd coverage_reports

#Identify jcov jar file
export JCOV_JAR=$JCOV_HOME/jcov/JCOV_BUILD/jcov_3.0/jcov.jar

#Instrument java classes
find $ACSROOT/lib -name "*.jar" | xargs java -cp $JCOV_JAR:$(acsMakeJavaClasspath -eclipse) com.sun.tdk.jcov.Instr -t coverage_instr.xml

Execute

There are two ways of performing coverage for the executions. One is to run the classes and generate intermediate files that would need to be combined at a later stage and the second is to instance a server that will receive the information from all instrumented Java classes and combine the data on-the-fly.

For simplicity we choose the later:

#Identify jcov jar file
export JCOV_JAR=$JCOV_HOME/jcov/JCOV_BUILD/jcov_3.0/jcov.jar

cd coverage_reports

#Start Grabber
nohup java -cp $JCOV_JAR com.sun.tdk.jcov.Grabber -o coverage_result.xml -t coverage_instr.xml &
java -cp $JCOV_JAR com.sun.tdk.jcov.GrabberManager -wait

After this we run the application code with a small change:

#Identify jcov jar files
export JCOV_JAR=$JCOV_HOME/jcov/JCOV_BUILD/jcov_3.0/jcov.jar
export JCOV_SAVER_JAR=$JCOV_HOME/jcov/JCOV_BUILD/jcov_3.0/jcov_network_saver.jar

#Option #1
java -cp $JCOV_SAVER_JAR:$CLASSPATH <usual_command>

#Option #2
java -javaagent:$JCOV_JAR=grabber <usual_command>

Coverage

After all your tests have been executed with one of the options above, you need to stop the grabber and generate the report:

#Identify jcov jar file
export JCOV_JAR=$JCOV_HOME/jcov/JCOV_BUILD/jcov_3.0/jcov.jar

cd coverage_reports

#Kill Grabber
java -cp $JCOV_JAR com.sun.tdk.jcov.GrabberManager -kill

#Merge Templates
java -cp $JCOV_JAR com.sun.tdk.jcov.Merger -o coverage_merged.xml -t coverage_instr.xml coverage_result.xml

Report

#Identify jcov jar file
export JCOV_JAR=$JCOV_HOME/jcov/JCOV_BUILD/jcov_3.0/jcov.jar

cd coverage_reports

#Generate Report
java -cp $JCOV_JAR com.sun.tdk.jcov.RepGen coverage_merged.xml

The result will be a report in XML with the coverage information.

Example

  • No labels