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 cd3b2e42e9
commit cb5ff8f4cf
4 changed files with 218 additions and 81 deletions

268
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,24 +735,27 @@ 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">
<junit fork="yes" forkmode="once" printsummary="yes" haltonfailure="${halt.on.test.failure}" <jacoco:coverage enabled="${coverage.enabled}" excludes="${coverage.excludes}" destfile="build/jacoco-main.exec">
failureproperty="main.test.failed" showoutput="true"> <junit fork="yes" forkmode="once" printsummary="yes" haltonfailure="${halt.on.test.failure}"
<classpath refid="test.classpath"/> failureproperty="main.test.failed" showoutput="true">
<syspropertyset refid="junit.properties"/> <classpath refid="test.classpath"/>
<jvmarg value="${poi.test.locale}"/> <syspropertyset refid="junit.properties"/>
<jvmarg value="-ea"/> <jvmarg value="${poi.test.locale}"/>
<jvmarg value="-Xmx256m"/> <jvmarg value="-ea"/>
<formatter type="plain"/> <jvmarg value="-Xmx256m"/>
<batchtest todir="${main.reports.test}"> <formatter type="plain"/>
<fileset dir="${main.src.test}"> <formatter type="xml"/>
<include name="**/${testpattern}.java"/> <batchtest todir="${main.reports.test}">
<exclude name="**/All*Tests.java"/> <fileset dir="${main.src.test}">
<exclude name="**/TestUnfixedBugs.java"/> <include name="**/${testpattern}.java"/>
<exclude name="**/TestcaseRecordInputStream.java"/> <exclude name="**/All*Tests.java"/>
</fileset> <exclude name="**/TestUnfixedBugs.java"/>
</batchtest> <exclude name="**/TestcaseRecordInputStream.java"/>
</junit> </fileset>
</batchtest>
</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,28 +776,31 @@ 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">
<junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}" <jacoco:coverage enabled="${coverage.enabled}" excludes="${coverage.excludes}" destfile="build/jacoco-scratchpad.exec">
failureproperty="scratchpad.test.failed"> <junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}"
<classpath refid="test.scratchpad.classpath"/> failureproperty="scratchpad.test.failed">
<syspropertyset refid="junit.properties"/> <classpath refid="test.scratchpad.classpath"/>
<jvmarg value="${poi.test.locale}"/> <syspropertyset refid="junit.properties"/>
<jvmarg value="-ea"/> <jvmarg value="${poi.test.locale}"/>
<!-- <jvmarg value="-ea"/>
YK: ensure that JUnit has enough memory to run tests. <!--
Without the line below tests fail on Mac OS X with jdk-1.6.26 YK: ensure that JUnit has enough memory to run tests.
and on Windows with jdk-1.5.22 Without the line below tests fail on Mac OS X with jdk-1.6.26
--> and on Windows with jdk-1.5.22
<jvmarg value="-Xmx256M"/> -->
<formatter type="plain"/> <jvmarg value="-Xmx256M"/>
<batchtest todir="${scratchpad.reports.test}"> <formatter type="plain"/>
<fileset dir="${scratchpad.src.test}"> <formatter type="xml"/>
<include name="**/${testpattern}.java"/> <batchtest todir="${scratchpad.reports.test}">
<exclude name="**/AllTests.java"/> <fileset dir="${scratchpad.src.test}">
</fileset> <include name="**/${testpattern}.java"/>
</batchtest> <exclude name="**/AllTests.java"/>
</junit> </fileset>
</batchtest>
</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,29 +816,33 @@ 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>
<junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}" <jacoco:coverage enabled="${coverage.enabled}" excludes="${coverage.excludes}" destfile="build/jacoco-@{type}.exec">
failureproperty="ooxml.test.failed"> <junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}"
<classpath refid="@{classpath}"/> failureproperty="ooxml.test.failed">
<syspropertyset refid="junit.properties"/> <classpath refid="@{classpath}"/>
<jvmarg value="${poi.test.locale}"/> <syspropertyset refid="junit.properties"/>
<!-- <jvmarg value="-ea"/> --> <jvmarg value="${poi.test.locale}"/>
<formatter type="plain"/> <!-- <jvmarg value="-ea"/> -->
<batchtest todir="${ooxml.reports.test}"> <formatter type="plain"/>
<fileset dir="${ooxml.src.test}"> <formatter type="xml"/>
<include name="**/${testpattern}.java"/> <batchtest todir="${ooxml.reports.test}">
<exclude name="**/TestUnfixedBugs.java"/> <fileset dir="${ooxml.src.test}">
<exclude name="**/All*Tests.java"/> <include name="**/${testpattern}.java"/>
</fileset> <exclude name="**/TestUnfixedBugs.java"/>
</batchtest> <exclude name="**/All*Tests.java"/>
</junit> </fileset>
</batchtest>
</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,21 +882,24 @@ 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">
<junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}" <jacoco:coverage enabled="${coverage.enabled}" excludes="${coverage.excludes}" destfile="build/jacoco-excelant.exec">
failureproperty="excelant.test.failed"> <junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}"
<classpath refid="test.excelant.classpath"/> failureproperty="excelant.test.failed">
<syspropertyset refid="junit.properties"/> <classpath refid="test.excelant.classpath"/>
<jvmarg value="${poi.test.locale}"/> <syspropertyset refid="junit.properties"/>
<jvmarg value="-ea"/> <jvmarg value="${poi.test.locale}"/>
<formatter type="plain"/> <jvmarg value="-ea"/>
<batchtest todir="${excelant.reports.test}"> <formatter type="plain"/>
<fileset dir="${excelant.src.test}"> <formatter type="xml"/>
<include name="**/${testpattern}.java"/> <batchtest todir="${excelant.reports.test}">
</fileset> <fileset dir="${excelant.src.test}">
</batchtest> <include name="**/${testpattern}.java"/>
</junit> </fileset>
</batchtest>
</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 {
@ -43,6 +50,17 @@ public class TestExcelAntWorkbookUtil extends TestCase {
assertNotNull( fixture ) ; assertNotNull( fixture ) ;
} }
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(

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);