Adjust build.xml to run JaCoCo coverage when running unit tests when

flag coverage.enabled is set to true, download necessary jars, add jacoco:coverage
to junit targets and a testcoveragereport target which produces the coverage report.
Also fix a test that is affected by JaCoCo and add slightly more coverage in another test.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1510243 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2013-08-04 16:38:01 +00:00
parent ee39ef2d73
commit ffc31302a4
4 changed files with 218 additions and 81 deletions

140
build.xml
View File

@ -151,6 +151,12 @@ under the License.
<property name="ooxml.jsr173.jar" location="${ooxml.lib}/stax-api-1.0.1.jar"/> <property name="ooxml.jsr173.jar" location="${ooxml.lib}/stax-api-1.0.1.jar"/>
<property name="ooxml.jsr173.url" value="${repository.m2}/maven2/stax/stax-api/1.0.1/stax-api-1.0.1.jar"/> <property name="ooxml.jsr173.url" value="${repository.m2}/maven2/stax/stax-api/1.0.1/stax-api-1.0.1.jar"/>
<!-- coverage libs -->
<property name="jacoco.zip" location="${main.lib}/jacoco-0.6.2.201302030002.zip"/>
<property name="jacoco.url" value="${repository.m2}/maven2/org/jacoco/jacoco/0.6.2.201302030002/jacoco-0.6.2.201302030002.zip"/>
<property name="asm.jar" location="${main.lib}/asm-all-4.0.jar"/>
<property name="asm.url" value="${repository.m2}/maven2/org/ow2/asm/asm-all/4.0/asm-all-4.0.jar"/>
<!-- See http://www.ecma-international.org/publications/standards/Ecma-376.htm --> <!-- See http://www.ecma-international.org/publications/standards/Ecma-376.htm -->
<!-- "Copy these file(s), free of charge" --> <!-- "Copy these file(s), free of charge" -->
<property name="ooxml.xsds.ozip" location="${ooxml.lib}/OfficeOpenXML-Part4.zip"/> <property name="ooxml.xsds.ozip" location="${ooxml.lib}/OfficeOpenXML-Part4.zip"/>
@ -164,6 +170,11 @@ under the License.
<property name="maven.ooxml.xsds.version.id" value="1.0"/> <property name="maven.ooxml.xsds.version.id" value="1.0"/>
<property name="maven.ooxml.xsds.jar" value="ooxml-schemas-${maven.ooxml.xsds.version.id}.jar"/> <property name="maven.ooxml.xsds.jar" value="ooxml-schemas-${maven.ooxml.xsds.version.id}.jar"/>
<!-- Coverage -->
<property name="coverage.dir" value="build/coverage"/>
<!-- Exclude some uninteresting classes from coverage-instrumentation as we do not want to measure coverage in those packages anyway -->
<property name="coverage.excludes" value="org.openxmlformats.*:com.*:org.junit.*:junit.*:"/>
<!-- build and distro settings --> <!-- build and distro settings -->
<property name="jar.name" value="poi"/> <property name="jar.name" value="poi"/>
<property name="build.site" location="build/tmp/site/build/site"/> <property name="build.site" location="build/tmp/site/build/site"/>
@ -255,6 +266,13 @@ under the License.
<pathelement location="${main.output.test.dir}"/> <pathelement location="${main.output.test.dir}"/>
</path> </path>
<path id="lib.jacoco">
<fileset dir="lib">
<include name="org.jacoco*.jar" />
<include name="asm-all-4*.jar" />
</fileset>
</path>
<!-- Prints POI's Ant usage help --> <!-- Prints POI's Ant usage help -->
<target name="help" description="Prints Apache POI's Ant usage help"> <target name="help" description="Prints Apache POI's Ant usage help">
<echo> <echo>
@ -316,6 +334,8 @@ under the License.
<available file="${main.junit.jar}"/> <available file="${main.junit.jar}"/>
<available file="${main.hamcrest.jar}"/> <available file="${main.hamcrest.jar}"/>
<available file="${main.ant.jar}"/> <available file="${main.ant.jar}"/>
<available file="${asm.jar}"/>
<available file="${jacoco.zip}"/>
</and> </and>
<isset property="disconnected"/> <isset property="disconnected"/>
</or> </or>
@ -349,6 +369,19 @@ under the License.
<param name="sourcefile" value="${main.ant.url}"/> <param name="sourcefile" value="${main.ant.url}"/>
<param name="destfile" value="${main.ant.jar}"/> <param name="destfile" value="${main.ant.jar}"/>
</antcall> </antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${asm.url}"/>
<param name="destfile" value="${asm.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${jacoco.url}"/>
<param name="destfile" value="${jacoco.zip}"/>
</antcall>
<unzip src="${jacoco.zip}" dest=".">
<patternset>
<include name="lib/*.jar"/>
</patternset>
</unzip>
</target> </target>
<target name="check-ooxml-jars"> <target name="check-ooxml-jars">
@ -622,9 +655,77 @@ under the License.
<delete file="${version.java}"/> <delete file="${version.java}"/>
</target> </target>
<target name="test" depends="compile,test-main,test-scratchpad,test-ooxml,test-excelant" <target name="jacocotask" depends="">
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath refid="lib.jacoco"/>
</taskdef>
</target>
<target name="test" depends="compile,jacocotask,test-main,test-scratchpad,test-ooxml,test-excelant"
description="Tests main, scratchpad and ooxml"/> description="Tests main, scratchpad and ooxml"/>
<target name="test-all" depends="test,test-ooxml-lite"/> <target name="test-all" depends="test,test-ooxml-lite,testcoveragereport"/>
<target name="testcoveragereport" depends="jacocotask" description="create test-report" xmlns:jacoco="antlib:org.jacoco.ant" if="${coverage.enabled}">
<delete dir="${coverage.dir}"/>
<mkdir dir="${coverage.dir}"/>
<jacoco:report>
<executiondata>
<fileset dir="build">
<include name="*.exec"/>
</fileset>
</executiondata>
<structure name="Apache POI">
<group name="Main">
<classfiles>
<fileset dir="${main.output.dir}">
<exclude name=""/>
</fileset>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${main.src}"/>
</sourcefiles>
</group>
<group name="Scratchpad">
<classfiles>
<fileset dir="${scratchpad.output.dir}"/>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${scratchpad.src}"/>
</sourcefiles>
</group>
<group name="OOXML">
<classfiles>
<fileset dir="${ooxml.output.dir}">
<exclude name=""/>
</fileset>
<fileset dir="${ooxml.lite.output.dir}">
<exclude name="org/openxmlformats/**"/>
<exclude name="schema*/**"/>
</fileset>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${ooxml.src}"/>
<!--fileset dir="${ooxml.lite.src}"/-->
</sourcefiles>
</group>
<group name="Excelant">
<classfiles>
<fileset dir="${excelant.output.dir}"/>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${excelant.src}"/>
</sourcefiles>
</group>
</structure>
<html destdir="${coverage.dir}"/>
<xml destfile="${coverage.dir}/coverage.xml"/>
</jacoco:report>
<echo message="Coverage results are available at coverage\index.html, coverage/coverage.xml" />
</target>
<target name="-test-main-check"> <target name="-test-main-check">
<uptodate property="main.test.notRequired" targetfile="${main.testokfile}"> <uptodate property="main.test.notRequired" targetfile="${main.testokfile}">
@ -634,7 +735,8 @@ under the License.
</target> </target>
<target name="test-main" unless="main.test.notRequired" <target name="test-main" unless="main.test.notRequired"
depends="compile-main, -test-main-check"> depends="compile-main, -test-main-check,jacocotask" xmlns:jacoco="antlib:org.jacoco.ant">
<jacoco:coverage enabled="${coverage.enabled}" excludes="${coverage.excludes}" destfile="build/jacoco-main.exec">
<junit fork="yes" forkmode="once" printsummary="yes" haltonfailure="${halt.on.test.failure}" <junit fork="yes" forkmode="once" printsummary="yes" haltonfailure="${halt.on.test.failure}"
failureproperty="main.test.failed" showoutput="true"> failureproperty="main.test.failed" showoutput="true">
<classpath refid="test.classpath"/> <classpath refid="test.classpath"/>
@ -643,6 +745,7 @@ under the License.
<jvmarg value="-ea"/> <jvmarg value="-ea"/>
<jvmarg value="-Xmx256m"/> <jvmarg value="-Xmx256m"/>
<formatter type="plain"/> <formatter type="plain"/>
<formatter type="xml"/>
<batchtest todir="${main.reports.test}"> <batchtest todir="${main.reports.test}">
<fileset dir="${main.src.test}"> <fileset dir="${main.src.test}">
<include name="**/${testpattern}.java"/> <include name="**/${testpattern}.java"/>
@ -652,6 +755,7 @@ under the License.
</fileset> </fileset>
</batchtest> </batchtest>
</junit> </junit>
</jacoco:coverage>
<delete file="${main.testokfile}"/> <delete file="${main.testokfile}"/>
<antcall target="-test-main-write-testfile"/> <antcall target="-test-main-write-testfile"/>
</target> </target>
@ -672,8 +776,9 @@ under the License.
</uptodate> </uptodate>
</target> </target>
<target name="test-scratchpad" depends="compile-main,compile-scratchpad,-test-scratchpad-check" <target name="test-scratchpad" depends="compile-main,compile-scratchpad,-test-scratchpad-check,jacocotask"
unless="scratchpad.test.notRequired"> unless="scratchpad.test.notRequired" xmlns:jacoco="antlib:org.jacoco.ant">
<jacoco:coverage enabled="${coverage.enabled}" excludes="${coverage.excludes}" destfile="build/jacoco-scratchpad.exec">
<junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}" <junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}"
failureproperty="scratchpad.test.failed"> failureproperty="scratchpad.test.failed">
<classpath refid="test.scratchpad.classpath"/> <classpath refid="test.scratchpad.classpath"/>
@ -687,6 +792,7 @@ under the License.
--> -->
<jvmarg value="-Xmx256M"/> <jvmarg value="-Xmx256M"/>
<formatter type="plain"/> <formatter type="plain"/>
<formatter type="xml"/>
<batchtest todir="${scratchpad.reports.test}"> <batchtest todir="${scratchpad.reports.test}">
<fileset dir="${scratchpad.src.test}"> <fileset dir="${scratchpad.src.test}">
<include name="**/${testpattern}.java"/> <include name="**/${testpattern}.java"/>
@ -694,6 +800,7 @@ under the License.
</fileset> </fileset>
</batchtest> </batchtest>
</junit> </junit>
</jacoco:coverage>
<delete file="${scratchpad.testokfile}"/> <delete file="${scratchpad.testokfile}"/>
<antcall target="-test-scratchpad-write-testfile"/> <antcall target="-test-scratchpad-write-testfile"/>
</target> </target>
@ -709,16 +816,19 @@ under the License.
</uptodate> </uptodate>
</target> </target>
<macrodef name="ooxml-test-runner"> <macrodef name="ooxml-test-runner" xmlns:jacoco="antlib:org.jacoco.ant">
<attribute name="classpath"/> <attribute name="classpath"/>
<attribute name="type"/>
<sequential> <sequential>
<jacoco:coverage enabled="${coverage.enabled}" excludes="${coverage.excludes}" destfile="build/jacoco-@{type}.exec">
<junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}" <junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}"
failureproperty="ooxml.test.failed"> failureproperty="ooxml.test.failed">
<classpath refid="@{classpath}"/> <classpath refid="@{classpath}"/>
<syspropertyset refid="junit.properties"/> <syspropertyset refid="junit.properties"/>
<jvmarg value="${poi.test.locale}"/> <jvmarg value="${poi.test.locale}"/>
<!-- <jvmarg value="-ea"/> --> <!-- <jvmarg value="-ea"/> -->
<formatter type="plain"/> <formatter type="plain"/>
<formatter type="xml"/>
<batchtest todir="${ooxml.reports.test}"> <batchtest todir="${ooxml.reports.test}">
<fileset dir="${ooxml.src.test}"> <fileset dir="${ooxml.src.test}">
<include name="**/${testpattern}.java"/> <include name="**/${testpattern}.java"/>
@ -727,11 +837,12 @@ under the License.
</fileset> </fileset>
</batchtest> </batchtest>
</junit> </junit>
</jacoco:coverage>
</sequential> </sequential>
</macrodef> </macrodef>
<target name="test-ooxml" depends="compile-main,compile-ooxml,-test-ooxml-check" unless="ooxml.test.notRequired"> <target name="test-ooxml" depends="compile-main,compile-ooxml,-test-ooxml-check,jacocotask" unless="ooxml.test.notRequired">
<ooxml-test-runner classpath="test.ooxml.classpath"/> <ooxml-test-runner classpath="test.ooxml.classpath" type="ooxml"/>
<delete file="${ooxml.testokfile}"/> <delete file="${ooxml.testokfile}"/>
<antcall target="-test-ooxml-write-testfile"/> <antcall target="-test-ooxml-write-testfile"/>
</target> </target>
@ -754,10 +865,10 @@ under the License.
</java> </java>
</target> </target>
<target name="test-ooxml-lite"> <target name="test-ooxml-lite" depends="jacocotask">
<delete file="${ooxml.testokfile}"/> <delete file="${ooxml.testokfile}"/>
<echo message="Running ooxml tests against 'poi-ooxml-schemas'"/> <echo message="Running ooxml tests against 'poi-ooxml-schemas'"/>
<ooxml-test-runner classpath="ooxml-lite.classpath"/> <ooxml-test-runner classpath="ooxml-lite.classpath" type="ooxml-lite"/>
</target> </target>
<target name="-test-excelant-check"> <target name="-test-excelant-check">
@ -771,8 +882,9 @@ under the License.
<echo file="${excelant.testokfile}" append="false" message="testok"/> <echo file="${excelant.testokfile}" append="false" message="testok"/>
</target> </target>
<target name="test-excelant" depends="compile-excelant,-test-excelant-check" <target name="test-excelant" depends="compile-excelant,-test-excelant-check,jacocotask"
unless="excelant.test.notRequired"> unless="excelant.test.notRequired" xmlns:jacoco="antlib:org.jacoco.ant">
<jacoco:coverage enabled="${coverage.enabled}" excludes="${coverage.excludes}" destfile="build/jacoco-excelant.exec">
<junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}" <junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}"
failureproperty="excelant.test.failed"> failureproperty="excelant.test.failed">
<classpath refid="test.excelant.classpath"/> <classpath refid="test.excelant.classpath"/>
@ -780,12 +892,14 @@ under the License.
<jvmarg value="${poi.test.locale}"/> <jvmarg value="${poi.test.locale}"/>
<jvmarg value="-ea"/> <jvmarg value="-ea"/>
<formatter type="plain"/> <formatter type="plain"/>
<formatter type="xml"/>
<batchtest todir="${excelant.reports.test}"> <batchtest todir="${excelant.reports.test}">
<fileset dir="${excelant.src.test}"> <fileset dir="${excelant.src.test}">
<include name="**/${testpattern}.java"/> <include name="**/${testpattern}.java"/>
</fileset> </fileset>
</batchtest> </batchtest>
</junit> </junit>
</jacoco:coverage>
<delete file="${excelant.testokfile}"/> <delete file="${excelant.testokfile}"/>
<antcall target="-test-excelant-write-testfile"/> <antcall target="-test-excelant-write-testfile"/>
</target> </target>

View File

@ -30,23 +30,19 @@ public class ExcelAntWorkbookUtilTestHelper extends ExcelAntWorkbookUtil {
public ExcelAntWorkbookUtilTestHelper(String fName) { public ExcelAntWorkbookUtilTestHelper(String fName) {
super(fName); super(fName);
// TODO Auto-generated constructor stub
} }
public ExcelAntWorkbookUtilTestHelper(Workbook wb) { public ExcelAntWorkbookUtilTestHelper(Workbook wb) {
super(wb); super(wb);
// TODO Auto-generated constructor stub
} }
@Override @Override
public UDFFinder getFunctions() { public UDFFinder getFunctions() {
// TODO Auto-generated method stub
return super.getFunctions(); return super.getFunctions();
} }
@Override @Override
public FormulaEvaluator getEvaluator(String excelFileName) { public FormulaEvaluator getEvaluator(String excelFileName) {
// TODO Auto-generated method stub
return super.getEvaluator(excelFileName); return super.getEvaluator(excelFileName);
} }

View File

@ -16,14 +16,21 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.ss.excelant.util; package org.apache.poi.ss.excelant.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.examples.formula.CalculateMortgageFunction; import org.apache.poi.ss.examples.formula.CalculateMortgageFunction;
import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.tools.ant.BuildException;
public class TestExcelAntWorkbookUtil extends TestCase { public class TestExcelAntWorkbookUtil extends TestCase {
@ -44,6 +51,17 @@ public class TestExcelAntWorkbookUtil extends TestCase {
} }
public void testWorkbookConstructor() throws InvalidFormatException, IOException {
File workbookFile = new File(mortgageCalculatorFileName);
FileInputStream fis = new FileInputStream(workbookFile);
Workbook workbook = WorkbookFactory.create(fis);
fixture = new ExcelAntWorkbookUtilTestHelper( workbook ) ;
assertNotNull( fixture ) ;
}
public void testAddFunction() { public void testAddFunction() {
fixture = new ExcelAntWorkbookUtilTestHelper( fixture = new ExcelAntWorkbookUtilTestHelper(
mortgageCalculatorFileName ) ; mortgageCalculatorFileName ) ;

View File

@ -46,6 +46,11 @@ public final class TestDocumentProperties
for (int x = 0; x < fields.length; x++) for (int x = 0; x < fields.length; x++)
{ {
// JaCoCo Code Coverage adds it's own field, don't look at this one here
if(fields[x].getName().equals("$jacocoData")) {
continue;
}
if (!fields[x].getType().isArray()) if (!fields[x].getType().isArray())
{ {
assertEquals(fields[x].get(_documentProperties), assertEquals(fields[x].get(_documentProperties),
@ -53,6 +58,10 @@ public final class TestDocumentProperties
} }
else else
{ {
// ensure that the class was not changed/enhanced, e.g. by code instrumentation like coverage tools
assertEquals("Invalid type for field: " + fields[x].getName(),
"[B", fields[x].getType().getName());
byte[] buf1 = (byte[])fields[x].get(_documentProperties); byte[] buf1 = (byte[])fields[x].get(_documentProperties);
byte[] buf2 = (byte[])fields[x].get(newDocProperties); byte[] buf2 = (byte[])fields[x].get(newDocProperties);
Arrays.equals(buf1, buf2); Arrays.equals(buf1, buf2);