Cobertura Java Ant-Maven Eclipse Example


In this example we will discussed how to add jar file's instead of class. Why this is require, as most of time we are generating the .ear, .war file for application deployment and that time we won't find the any class dir in project.

Requirement:
1. Maven
2. Ant
3. Java
4. Eclipse

> Create the java project with maven command
C:\>mvn archetype:generate -DgroupId=com.tutorialbyexample -DartifactId=mavenjavaexample -DarchetypeArtifactId
=maven-archetype-quickstart -DinteractiveMode=false
Note: assuming you have set the path and class path of maven, ant and java
e.g: 
set classpath=%;C:\apache-maven-3.2.1\lib;
set path=%;C:\apache-maven-3.2.1\bin;

> Now with help of above mavenjavaexample project will created

mavenjavaexample 
- src
 -- main
   ---java
 --test
    ---java
- pom.xml

App.java
package com.tutorialbyexample;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}


AppTest.java

package com.tutorialbyexample;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}





What we need now just create another project for cobertura:

Create simple project in Eclipse:Cobetura-Java-Coverage-Report-mvn
More more  details refer the http://www.tutorialbyexample.com/2015/03/cobertura-java-ant-eclipse-example.html

Now do the required change in:
cobertura.properties:
lib.dir=C:/Cobetura-Java-Coverage-Report-mvn/lib
instrumented.dir=C:/Cobetura-Java-Coverage-Report-mvn/instrumented
coverage.html.dir=C:/Cobetura-Java-Coverage-Report-mvn/htmlReports
src.module1.dir=C:/mavenjavaexample/src/main/java
target.module1.dir=C:/mavenjavaexample/target

build.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="Cobetura-Java-Coverage-Report" default="GeneratingCoberturaFiles" basedir=".">
<property file="cobertura.properties" />
<property name="lib.dir" value="${lib.dir}" />

<property name="instrumented.dir" value="${instrumented.dir}" />
<property name="coverage.html.dir" value="${coverage.html.dir}" />
<property name="src.module1.dir" value="${src.module1.dir}" />
<property name="target.module1.dir" value="${target.module1.dir}" />
<path id="cobertura.classpath">
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
<target name="init">
<delete dir="${instrumented.dir}" />
<delete dir="${coverage.html.dir}" />
<delete file="cobertura.ser" />
<mkdir dir="${instrumented.dir}" />
<mkdir dir="${coverage.html.dir}" />
</target>
<target name="GeneratingCoberturaFiles" depends="init" description="Generating the .ser file with .class file">
<cobertura-instrument todir="${instrumented.dir}" datafile="cobertura.ser">
<ignore regex="org.apache.log4j.*" />
<!--<fileset dir="${target.module1.dir}">
<include name="**/*.class" />
</fileset>-->
<fileset dir="${target.module1.dir}">
<include name="*.jar" />
</fileset>
</cobertura-instrument>
</target>
<target name="GenerateHTMLReport" description="Generate coverage report">
<delete dir="${coverage.html.dir}" />
<mkdir dir="${coverage.html.dir}" />
<cobertura-report format="HTML" destdir="${coverage.html.dir}" datafile="cobertura.ser">
<fileset dir="${src.module1.dir}">
<include name="**/*.java" />
</fileset>
</cobertura-report>
</target>
</project>


Now open the build.xml in ant view and run it ant GeneratingCoberturaFiles

Buildfile: C:\Cobetura-Java-Coverage-Report-mvn\build.xml
init:
   [delete] Deleting directory C:\\Cobetura-Java-Coverage-Report-mvn\instrumented
   [delete] Deleting directory C:\Cobetura-Java-Coverage-Report-mvn\htmlReports
   [delete] Deleting: C:\Cobetura-Java-Coverage-Report-mvn\cobertura.ser
    [mkdir] Created dir: C:\Cobetura-Java-Coverage-Report-mvn\instrumented
    [mkdir] Created dir: C:\Cobetura-Java-Coverage-Report-mvn\htmlReports
GeneratingCoberturaFiles:
[cobertura-instrument] 17:42:50,465 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
[cobertura-instrument] 17:42:50,465 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
[cobertura-instrument] 17:42:50,466 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/C:/Users/extvkx/workspace/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-instrument] 17:42:50,467 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
[cobertura-instrument] 17:42:50,467 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-instrument] 17:42:50,467 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1.jar!/logback.xml]
[cobertura-instrument] 17:42:50,523 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@ad3fd4 - URL [jar:file:/C:/Users/extvkx/workspace/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml] is not of type file
[cobertura-instrument] 17:42:50,562 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
[cobertura-instrument] 17:42:50,574 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
[cobertura-instrument] 17:42:50,590 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
[cobertura-instrument] 17:42:50,619 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
[cobertura-instrument] 17:42:50,660 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [net.sourceforge.cobertura] to INFO
[cobertura-instrument] 17:42:50,660 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
[cobertura-instrument] 17:42:50,661 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
[cobertura-instrument] 17:42:50,661 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
[cobertura-instrument] 17:42:50,662 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@398f3c - Registering current configuration as safe fallback point
[cobertura-instrument] 17:42:51,441 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
[cobertura-instrument] 17:42:51,441 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
[cobertura-instrument] 17:42:51,441 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-instrument] 17:42:51,441 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
[cobertura-instrument] 17:42:51,441 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-instrument] 17:42:51,441 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1.jar!/logback.xml]
[cobertura-instrument] 17:42:51,457 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@193b604 - URL [jar:file:/C:/Users/extvkx/workspace/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml] is not of type file
[cobertura-instrument] 17:42:51,500 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
[cobertura-instrument] 17:42:51,507 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
[cobertura-instrument] 17:42:51,513 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
[cobertura-instrument] 17:42:51,528 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
[cobertura-instrument] 17:42:51,554 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [net.sourceforge.cobertura] to INFO
[cobertura-instrument] 17:42:51,554 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
[cobertura-instrument] 17:42:51,554 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
[cobertura-instrument] 17:42:51,554 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
[cobertura-instrument] 17:42:51,555 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@92e96c - Registering current configuration as safe fallback point
[cobertura-instrument] Cobertura 2.1.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
[cobertura-instrument] [INFO] Cobertura: Saved information on 1 classes.
[cobertura-instrument] [INFO] Cobertura: Saved information on 1 classes.
BUILD SUCCESSFUL
Total time: 2 seconds


Again run the ant GenerateHTMLReport

Buildfile: C:\Cobetura-Java-Coverage-Report-mvn\build.xml
GenerateHTMLReport:
   [delete] Deleting directory C:\Cobetura-Java-Coverage-Report-mvn\htmlReports
    [mkdir] Created dir: C:\Cobetura-Java-Coverage-Report-mvn\htmlReports
[cobertura-report] 17:44:47,435 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
[cobertura-report] 17:44:47,436 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
[cobertura-report] 17:44:47,436 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-report] 17:44:47,437 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
[cobertura-report] 17:44:47,437 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-report] 17:44:47,437 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1.jar!/logback.xml]
[cobertura-report] 17:44:47,475 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@b6dfa8 - URL [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml] is not of type file
[cobertura-report] 17:44:47,510 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
[cobertura-report] 17:44:47,517 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
[cobertura-report] 17:44:47,529 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
[cobertura-report] 17:44:47,549 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
[cobertura-report] 17:44:47,601 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [net.sourceforge.cobertura] to INFO
[cobertura-report] 17:44:47,601 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
[cobertura-report] 17:44:47,601 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
[cobertura-report] 17:44:47,602 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
[cobertura-report] 17:44:47,604 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@15a68d - Registering current configuration as safe fallback point
[cobertura-report] 17:44:47,781 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
[cobertura-report] 17:44:47,781 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
[cobertura-report] 17:44:47,781 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-report] 17:44:47,782 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
[cobertura-report] 17:44:47,782 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-report] 17:44:47,782 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1.jar!/logback.xml]
[cobertura-report] 17:44:47,797 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@8bf135 - URL [jar:file:/C:/Cobetura-Java-Coverage-Report-mvn/lib/cobertura-2.1.1-sources.jar!/logback.xml] is not of type file
[cobertura-report] 17:44:47,845 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
[cobertura-report] 17:44:47,854 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
[cobertura-report] 17:44:47,860 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
[cobertura-report] 17:44:47,876 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
[cobertura-report] 17:44:47,906 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [net.sourceforge.cobertura] to INFO
[cobertura-report] 17:44:47,906 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
[cobertura-report] 17:44:47,907 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
[cobertura-report] 17:44:47,907 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
[cobertura-report] 17:44:47,908 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@1890c67 - Registering current configuration as safe fallback point
[cobertura-report] Cobertura 2.1.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
[cobertura-report] [INFO] Cobertura: Loaded information on 1 classes.
[cobertura-report] Report time: 338ms
BUILD SUCCESSFUL
Total time: 1 second

Now time to see the report:



















Reports:








Have good day ahead!!!

Reference:
https://github.com/cobertura/cobertura/wiki/Releasing-a-new-Cobertura
Maven official site.

No comments:

Post a Comment