Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Running application with war file compile with clover

Sumeet Kumar May 26, 2013

I want to know that can we run a war (or sar) file compiled with clover on jBoss and if we can, then does clover provide the facility to check the code coverage.

Say we compile an application with clover and deploy it on server. End users work this application and at the end of the day we want to check how much code was covered during this period. Can we do it?

If we can do it then please mention the configuration steps so that we can check the code coverage.

Please note that this question is from end user's perspective who is using the application and not from jUnit perspective.

Thanks,

Sumeet

3 answers

1 accepted

0 votes
Answer accepted
Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 27, 2013

Can anyone please provide a sample target clover.setup for instrumenting the code which is ready to use with tutorial project.

The build_completed.xml file in tutorial contains complete Clover configuration. Just type:

ant clover.all -Dtest.target=test.run

0 votes
Rijuta S May 18, 2014

Thanks Marek, changing permission on directory helped.

0 votes
Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 27, 2013

Hi Sumeet,

Yes, Clover can instrument WAR and SAR applications too. Typical steps are as follows:

  • instrument code using <clover-setup> or <clover-instr>; use "threaded" or "interval" flush policy in case when code will be deployed to an application server
  • package instrumented classes into JAR/WAR/SAR/EAR
  • put clover.jar into server's /lib directory (alternatively: bundle clover.jar in the application's jar)
  • deploy instrumented application + clover.db created during instrumentation (use -Dclover.initstring=/path/to/clover.db JVM option to point to the database file)
  • after tests are finished, take clover.db and coverage data files and generate a report using <clover-setup> task

References:

Cheers
Marek

Sumeet Kumar May 27, 2013
&lt;project name="money_demo" default="code.compile" basedir="."&gt;
    &lt;property name="src" location="src/main/java"/&gt;
    &lt;property name="test.src" location="src/test/java"/&gt;
    &lt;property name="app.build" location="build/app"/&gt;
    &lt;property name="test.build" location="build/test"/&gt;
    &lt;property name="test.result" location="build/testresult"/&gt;
    &lt;property name="junit.jar" location="lib/junit-3.8.2.jar"/&gt;
    &lt;property name="clover.jar" location="../lib/clover.jar"/&gt;
    &lt;taskdef resource="cloverlib.xml" classpath="${clover.jar}"/&gt;
    &lt;path id="build.classpath"&gt;
        &lt;pathelement path="${clover.jar}"/&gt; &lt;!-- I added this to make clover.jar avail in runtime --&gt;
        &lt;pathelement location="${junit.jar}"/&gt;
        &lt;pathelement location="${app.build}"/&gt;
    &lt;/path&gt;
    &lt;path id="testbuild.classpath"&gt;
        &lt;path refid="build.classpath"/&gt;
        &lt;pathelement location="${test.build}"/&gt;
    &lt;/path&gt;
    &lt;target name="-check.clover"&gt;
        &lt;available property="clover.installed" classname="com.cenqua.clover.CloverInstr" /&gt;
    &lt;/target&gt;
    &lt;target name="guard.noclover" depends="-check.clover" unless="clover.installed"&gt;
        &lt;fail message="The target you are attempting to run requires Clover, which doesn't appear to be installed"/&gt;
    &lt;/target&gt;
    &lt;target name="clover.report"&gt;
        &lt;clover-html-report outdir="clover_html" title="Clover Demo"/&gt;
    &lt;/target&gt;
    &lt;target name="record.point"&gt;
        &lt;clover-historypoint historyDir="clover_history"/&gt;
    &lt;/target&gt;
    &lt;target name="code.compile"
            description="Creates ${app.build} directory and compiles sources from ${src} to ${app.build}"&gt;
        &lt;mkdir dir="${app.build}"/&gt;
        &lt;javac includeantruntime="false" srcdir="${src}" destdir="${app.build}" classpathref="build.classpath" source="1.4"/&gt;
    &lt;/target&gt;
    &lt;target name="test.compile" depends="code.compile"
            description="Creates ${test.build} directory and compiles sources from ${test.src} to ${test.build}"&gt;
        &lt;mkdir dir="${test.build}"/&gt;
        &lt;javac includeantruntime="false"
  srcdir="${test.src}"
               destdir="${test.build}"
               classpathref="testbuild.classpath" source="1.5"/&gt;
    &lt;/target&gt;
    &lt;target name="test.run" depends="test.compile" description="Runs the tests"&gt;
        &lt;mkdir dir="${test.result}"/&gt;
        &lt;junit fork="yes" printsummary="true" showoutput="true"&gt;
            &lt;classpath refid="testbuild.classpath"/&gt;
            &lt;formatter type="xml"/&gt;
            &lt;batchtest fork="yes" todir="${test.result}"&gt;
                &lt;fileset dir="${test.src}" includes="**/*Test.java"/&gt;
            &lt;/batchtest&gt;
        &lt;/junit&gt;
    &lt;/target&gt;
    &lt;target name="clean"&gt;
        &lt;delete dir="build"/&gt;
        &lt;clover-clean/&gt; &lt;!-- I added this to delete clover database when clean target is executed --&gt;
    &lt;/target&gt;
    &lt;target name="with.clover"&gt;
   &lt;clover-setup/&gt;
&lt;/target&gt;
&lt;target name="hist.report"&gt;
     &lt;clover-report&gt;
           &lt;historical outfile="historical.pdf"
                  historyDir="clover_history"&gt;
           &lt;format type="pdf"/&gt;
           &lt;/historical&gt;
     &lt;/clover-report&gt;
&lt;/target&gt;
&lt;target name="clover.check" depends="with.clover"&gt;
    &lt;clover-check target="80%"/&gt;
&lt;/target&gt;
&lt;target name="clover.setup" if="clover.enabled"&gt;
        &lt;mkdir dir="${clover.db.dir}" /&gt;
        &lt;mkdir dir="${clover.instr.dir}" /&gt;
        &lt;clover-instr initString="${clover.db.dir}/coverage.db" destdir="${clover.instr.dir}"&gt;
            &lt;fileset dir="${src}" /&gt;
            &lt;testsources dir="${tests}" /&gt;
        &lt;/clover-instr&gt;
        &lt;echo&gt;
            Clover is enabled and will be used for generating test reports
        &lt;/echo&gt;
 &lt;/target&gt;
&lt;/project&gt;

**********************
Hi,
I've added the above mentioned lines (in bold) in the tutorial project and tried the following command ant with.clover clover.setup but nothing happened; not even echo.
Could you please tell me what else do I need to do to make the code compile so that its war could be run on jBoss?
Sumeet Kumar May 27, 2013

<target name="clover.setup">

<echo>

Clover is enabled and will be used for generating test reports

</echo>

<mkdir dir="${clover.db.dir}" />

<mkdir dir="${clover.instr.dir}" />

<clover-instr initString="${clover.db.dir}/coverage.db" destdir="${clover.instr.dir}">

<fileset dir="${src}" />

<testsources dir="${tests}" />

</clover-instr>

<flushpolicy>threaded</flushpolicy>

</target>

Ran the ant build with a slight change in the clover.setup and this time the new directories were created but they are empty.

I was expecting instrumented classes in those directories but it is not.

Can anyone please provide a sample target clover.setup for instrumenting the code which is ready to use with tutorial project.

Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 27, 2013

The build.xml file defined by you contains both <clover-setup> and <clover-instr> tasks. Both tasks are making virtually the same - they instrument the code - but the difference is that <clover-instr> puts instrumented sources into a specified directory, while the <clover-setup> peforms instrumentation "on the fly", by intercepting the <javac> task.

It means that there is no need to have both tasks defined. I recommend using <clover-setup> only. Similarly the 'ant with.clover clover.setup' call was also incorrect, as no compilation (javac) was triggered.

Please try the following:

* remove <target name="clover.setup"> as it is obsolete

* run 'ant clean with.clover test.run clover.report' - it shold enable Clover, compile the code, run tests and generate an HTML report

Rijuta S May 13, 2014

Hi, I am instrumenmting clover with war file using Ant. I see clover related jar and class files when I see war info. This ensures me that clover instrumented war is correct. I am trying to run soapUI tests and generate coverage files. I have corretcly set up path for clover.db in initstring. However, the coverage files are not generated when soapUI tests are run.

I did similar setup using maven and it worked flawlessly. What could be the issue with ant setup ?

Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 13, 2014

How do you run your soapUI tests exactly?

Do you deploy your application to some server or instantiate container on-the-fly?

If yes, then do you shutdown this sever after tests are finished? By default, Clover's coverage recorders will write coverage files to disk at the JVM shutdown (they attach to proper hook).

If not, then did you set the <clover-setup flushpolicy="treaded"> or <clover-setup flushpolicy="interval">? This will change the default flush policy and start writing coverage files periodically, so there's no need to shutdown JVM.

Do you see the clover.db.liverec file when running your tests? This indicates that at least one coverage recorder has started.

Do you see any Clover-related error messages when running your application? For instance, that the coverage recorder could not start because it did not find the database. This may indicate a problem with initstring. Using runtime property -Dclover.initstring=/path/to/clover.db may help.

Do you see any NoClassDefFound exceptions in the log? Especially the ones with classes from the com_cenqua_clover package? This may indicate that the clover.jar packaged into your WAR was not picked up by a class loader. In such case, putting clover.jar into application server's /lib directory should help.

Rijuta S May 13, 2014

Hello Marek. Thank you for your quick response.

I deploy the application war file to Glassfish server. Then I run my soapUI tests by giving the Glassfish server end point address in soapUI (ex: xyz.xx.xy.xz:8080/service).

Intially I noticed error in log files: clover database not found at specified path. Executing with -Dclover.initstring=/path/to/clover.db helped to solve this error.

I also added flushpolicy and flush interval attributes in <clover-setup>. Now I see permission denied error:

[#|2014-05-14T12:04:26.582-0400|SEVERE|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=817;_ThreadName=Thread-2;|ERROR: java.io.FileNotFoundException flushing coverage for recorder /home/user/workspace/cloverDB/clover.dbao50rb_hv6t1omy: /home/user/workspace/cloverDB/clover.dbao50rb_hv6t1omy (Permission denied)|#]

In above error, /home/user/workspace/cloverDB/ is the path I specified in initstring, where clover.db present.

I tried changing permission of clover.db, but it didnt help.

Clover did produce the coverage files when I did not use flush policy and stopped the server. So I think my problem was with clover.db path when initially it did not generate files.

Thank you for all your help.

Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 14, 2014

Could you also change a permission for the directory:

/home/user/workspace/cloverDB/

to allow Glassfish to create new files in it?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events