[DRAFT VERSION]
Cobertura settings
1) Cobertura maven plugin
There is a cobertura maven plugin at http://mojo.codehaus.org/cobertura-maven-plugin/ which works well for code coverage of tests.
This plugin measures code coverage of unit tests and not coverage of integration tests.
There are some workarounds which can be used:
a) There is a patch for the maven plugin (patch can be found at this page http://jira.codehaus.org/browse/MCOBERTURA-86 )
- Since that patch is not in current release of cobertura maven plugin, this workaround can be used only on local environment of developer.
b) Run integration tests together with unit tests (http://blog.jayway.com/2008/12/13/getting-coverage-for-integration-tests/)
- This workaround breaks the benefit of maven lifecycle phases.
c) Use maven antrun plugin and create an ant script
- This is solution and not just workaround.
Benefit is also that cobertura merge task, which is not implemented in cobertura maven plugin, is implemented for ant.
2) Ant script
To use the code coverage in multiple maven modules projects, maven profile "cobertura" was created.
Maven antrun plugin is used for running the ant script. (version 1.1 must be specified)
The profile can be activated by using param -Pcobertura in mvn command.
a) Instrumentation
The instrumentation task needs to run before tests. We need to run the instrumentation task :
- For integration tests in maven lifecycle phase pre-integration-test
- for unit tests it is phase process-test-classes.
-- dependency
Because of dependency conflicts the dependencies are declared in profile -> plugin -> dependencies.
To use the cobertura ant tasks we need to specify the task definition. We can use the classpath reference to maven plugin classpath which contains all necessary dependencies.
(it is done by code <taskdef classpathref="maven.plugin.classpath" resource="tasks.properties" />)
-- folder structure
The folder structure is the same as the folder structure generated by cobertura maven plugin.(So that we will be able to use cobertura maven plugin when it will be ready)
-- data file location
"When you instrument your classes, Cobertura generates a .ser file containing basic information about each class.
As your tests run, Cobertura adds additional information to this same data file. If the instrumented classes can not find the data file when running
then they will create a new one. It is important that you use the same cobertura.ser file when instrumenting, running, and generating reports.
The best way to do this is to specify the location of the data file when running your tests. You should pass the
-Dnet.sourceforge.cobertura.datafile=${basedir}/cobertura.ser sysproperty to the JUnit task"
[text from http://cobertura.sourceforge.net/faq.html]
To use this solution it would be neccessary to update the settings of tests. There is a second choice which is used by maven plugin and our script.
Ant script creates a file cobertura.properties in classpath which contains path to the data file. (There is a note about this in coberturas release notes from 5 June 2007)
b) Report
The results of code coverage is stored in folder {project.path}/target/cobertura/site. The basic overview can be found in file index.html.
-- backup
The instrumented classes should not be deployed to local repository. To avoid such situation the compiled classes are backuped before the tests runs and restored after test run finishes.
c) Merge
To generate one final report for all projects cobertura merge task must be called. This task will merge data from specified coberturas data files into one file.
Than we report task which will generete the html/ xml report from data file is called. To generate the report we must specify the path to source codes as parameter.
For each source code folder there must be defined these parameters --basedir [{project.source.folder}/src/main/java] [relative from specified folder to tested class]..
I wrote a groovy script which is doing this work it can be found in file {rhq.project}/modules/enterprise/server/container/pom.xml.
Currently this task is implemented only in module enterprise/server/container and generates the report in xml format because of hudson task.
Groovy script
import java.util.regex.Pattern
import java.util.regex.Matcher
def repClos
def list = new ArrayList()
Pattern sourceCodeFolder = Pattern.compile(".*/src/main/java")
Pattern sourceCodeFile = Pattern.compile('.*/src/main/java/(.*.java)')
list.add("--format")
list.add("xml")
list.add("--datafile")
list.add("${project.build.directory}/cobertura/cobertura.ser")
list.add("--destination")
list.add("${project.build.directory}/site/cobertura")
repClosFile = {
Matcher matcher = sourceCodeFile.matcher(it.canonicalPath)
if (matcher.matches()){
list.add(matcher[0][1])
}
if (it.isDirectory()){
it.eachFile(repClosFile)
}
}
repClosDir = {
Matcher matcher = sourceCodeFolder.matcher(it.canonicalPath)
if (matcher.matches()){
list.add("--basedir")
list.add(it.canonicalPath)
it.eachFile(repClosFile)
}
it.eachDir(repClosDir);
}
def ear = new File("${project.build.directory}/../../../../")
repClosDir(ear)
net.sourceforge.cobertura.reporting.Main.main((String[]) list)