• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?xml version="1.0" encoding="UTF-8"?>
2
3<!--
4   Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
5   All rights reserved. This program and the accompanying materials
6   are made available under the terms of the Eclipse Public License v1.0
7   which accompanies this distribution, and is available at
8   http://www.eclipse.org/legal/epl-v10.html
9
10   Contributors:
11      Brock Janiczak - initial API and implementation
12      Dominik Stadler - source folder support
13-->
14
15<project name="JaCoCo Report Task Tests" xmlns:au="antlib:org.apache.ant.antunit" xmlns:jacoco="antlib:org.jacoco.ant">
16
17	<target name="setUp">
18		<tempfile property="temp.dir" prefix="jacocoTest" destdir="${java.io.tmpdir}" />
19		<mkdir dir="${temp.dir}"/>
20	</target>
21
22	<target name="tearDown">
23		<delete dir="${temp.dir}" quiet="false" failonerror="true" />
24	</target>
25
26
27	<target name="testReportNoStructureElement">
28		<au:expectfailure expectedMessage="Group name must be supplied">
29			<jacoco:report/>
30		</au:expectfailure>
31	</target>
32
33	<target name="testReportNoStructureName">
34		<au:expectfailure expectedMessage="Group name must be supplied">
35			<jacoco:report>
36				<structure/>
37			</jacoco:report>
38		</au:expectfailure>
39	</target>
40
41	<target name="testReportNoGroupName">
42		<au:expectfailure expectedMessage="Group name must be supplied">
43			<jacoco:report>
44				<structure name="root">
45					<group/>
46				</structure>
47			</jacoco:report>
48		</au:expectfailure>
49	</target>
50
51	<target name="testReportWithExecutiondataFiles">
52		<jacoco:report>
53			<executiondata>
54				<fileset dir="${basedir}/data" includes="*.exec"/>
55			</executiondata>
56			<structure name="root"/>
57		</jacoco:report>
58	</target>
59
60	<target name="testReportInvalidExecutionDataFile">
61		<property name="doesnotexist.file" location="doesnotexist.exec"/>
62		<au:expectfailure expectedMessage="Unable to read execution data file ${doesnotexist.file}">
63			<jacoco:report>
64				<executiondata>
65					<file file="doesnotexist.exec"/>
66				</executiondata>
67				<structure name="root"/>
68			</jacoco:report>
69		</au:expectfailure>
70	</target>
71
72	<target name="testReportWithSourceButNoDebug">
73		<java classname="org.jacoco.ant.RemoveDebugInfos" classpath="${java.class.path}" failonerror="true">
74			<arg value="${org.jacoco.ant.reportTaskTest.classes.dir}/org/jacoco/ant/TestTarget.class" />
75			<arg value="${temp.dir}/TestTarget.class" />
76		</java>
77		<jacoco:report>
78			<structure name="root">
79				<classfiles>
80					<fileset dir="${temp.dir}" id="*.class" />
81				</classfiles>
82				<sourcefiles encoding="UTF-8">
83					<fileset dir="${org.jacoco.ant.reportTaskTest.sources.dir}" />
84				</sourcefiles>
85			</structure>
86		</jacoco:report>
87		<au:assertLogContains level="warn" text="To enable source code annotation class files for bundle 'root' have to be compiled with debug information"/>
88	</target>
89
90	<target name="testReportWithSourceDirButNoDebug">
91		<java classname="org.jacoco.ant.RemoveDebugInfos" classpath="${java.class.path}" failonerror="true">
92			<arg value="${org.jacoco.ant.reportTaskTest.classes.dir}/org/jacoco/ant/TestTarget.class" />
93			<arg value="${temp.dir}/TestTarget.class" />
94		</java>
95		<jacoco:report>
96			<structure name="root">
97				<classfiles>
98					<fileset dir="${temp.dir}" id="*.class" />
99				</classfiles>
100				<sourcefiles encoding="UTF-8">
101					<dirset file="${org.jacoco.ant.reportTaskTest.sources.dir}" />
102				</sourcefiles>
103			</structure>
104		</jacoco:report>
105		<au:assertLogContains level="warn" text="To enable source code annotation class files for bundle 'root' have to be compiled with debug information"/>
106	</target>
107
108	<target name="testReportWithSourceButNoClasses">
109		<jacoco:report>
110			<structure name="root">
111				<sourcefiles encoding="UTF-8">
112					<fileset dir="${org.jacoco.ant.reportTaskTest.sources.dir}" />
113				</sourcefiles>
114			</structure>
115		</jacoco:report>
116		<au:assertLogDoesntContain level="warn" text="source code annotation"/>
117	</target>
118
119	<target name="testReportWithNoMatch">
120		<property name="nomatch.file" location="${basedir}/data/nomatch.exec"/>
121		<jacoco:report>
122			<executiondata>
123				<file file="${nomatch.file}"/>
124			</executiondata>
125			<structure name="root">
126				<classfiles>
127					<path location="${org.jacoco.ant.reportTaskTest.classes.dir}"/>
128				</classfiles>
129			</structure>
130		</jacoco:report>
131		<au:assertLogContains level="warn" text="Classes in bundle 'root' do no match with execution data."/>
132		<au:assertLogContains level="warn" text="For report generation the same class files must be used as at runtime."/>
133		<au:assertLogContains level="warn" text="Execution data for class org/jacoco/ant/TestTarget does not match."/>
134	</target>
135
136
137	<!-- HTML Output -->
138
139	<target name="testReportHtmlNoDestdirOrDestfile">
140		<au:expectfailure expectedMessage="Destination directory or file must be supplied for html report">
141			<jacoco:report>
142				<structure name="root"/>
143				<html/>
144			</jacoco:report>
145		</au:expectfailure>
146	</target>
147
148	<target name="testReportHtmlBothDestdirAndDestfile">
149		<au:expectfailure expectedMessage="Either destination directory or file must be supplied, not both">
150			<jacoco:report>
151				<structure name="root"/>
152				<html destdir="${temp.dir}" destfile="${temp.dir}/report.zip"/>
153			</jacoco:report>
154		</au:expectfailure>
155	</target>
156
157	<target name="testReportHtmlWithClassFileSet">
158		<jacoco:report>
159			<structure name="Test">
160				<group name="Group">
161					<classfiles>
162						<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
163					</classfiles>
164				</group>
165			</structure>
166			<html destdir="${temp.dir}"/>
167		</jacoco:report>
168
169		<au:assertFileExists file="${temp.dir}/Group/org.jacoco.ant/TestTarget.html"/>
170	</target>
171
172	<target name="testReportHtmlWithPath">
173		<jacoco:report>
174			<structure name="Test">
175				<group name="Group">
176					<classfiles>
177						<path location="${org.jacoco.ant.reportTaskTest.classes.dir}"/>
178					</classfiles>
179				</group>
180			</structure>
181			<html destdir="${temp.dir}"/>
182		</jacoco:report>
183
184		<au:assertFileExists file="${temp.dir}/Group/org.jacoco.ant/TestTarget.html"/>
185	</target>
186
187	<target name="testReportHtmlWithJAR">
188		<property name="testReportHtmlWithJAR.jarfile" location="${temp.dir}/testclasses.jar"/>
189		<jar destfile="${testReportHtmlWithJAR.jarfile}">
190			<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
191		</jar>
192		<jacoco:report>
193			<structure name="Test">
194				<group name="Group">
195					<classfiles>
196						<file file="${testReportHtmlWithJAR.jarfile}"/>
197					</classfiles>
198				</group>
199			</structure>
200			<html destdir="${temp.dir}"/>
201		</jacoco:report>
202
203		<au:assertFileExists file="${temp.dir}/Group/org.jacoco.ant/TestTarget.html"/>
204	</target>
205
206	<target name="testReportHtmlFooter">
207		<jacoco:report>
208			<structure name="Test"/>
209			<html footer="ExampleFooter" destdir="${temp.dir}"/>
210		</jacoco:report>
211
212		<loadfile property="testReportHtmlFooter.content" srcfile="${temp.dir}/index.html" encoding="UTF-8"/>
213		<au:assertTrue message="Footer not included in ${testReportHtmlFooter.content}">
214			<contains string="${testReportHtmlFooter.content}" substring="ExampleFooter"/>
215		</au:assertTrue>
216	</target>
217
218	<target name="testReportHtmlEncoding">
219		<jacoco:report>
220			<structure name="Test"/>
221			<html encoding="UTF-16" destdir="${temp.dir}"/>
222		</jacoco:report>
223
224		<loadfile property="testReportHtmlEncoding.content" srcfile="${temp.dir}/index.html" encoding="UTF-16"/>
225		<au:assertTrue message="Encoding not set in ${testReportHtmlEncoding.content}">
226			<contains string="${testReportHtmlEncoding.content}" substring="encoding=&quot;UTF-16&quot;"/>
227		</au:assertTrue>
228	</target>
229
230	<target name="testReportHtmlDefaultTabWidth">
231		<jacoco:report>
232			<structure name="Test">
233				<classfiles>
234					<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
235				</classfiles>
236				<sourcefiles encoding="UTF-8">
237					<fileset dir="${org.jacoco.ant.reportTaskTest.sources.dir}" />
238				</sourcefiles>
239			</structure>
240			<html destdir="${temp.dir}"/>
241		</jacoco:report>
242
243		<loadfile property="testReportHtmlTabWidth.content" srcfile="${temp.dir}/org.jacoco.ant/TestTarget.java.html"/>
244		<au:assertTrue message="Tab width not set in ${testReportHtmlTabWidth.content}">
245			<contains string="${testReportHtmlTabWidth.content}" substring="window['PR_TAB_WIDTH']=4"/>
246		</au:assertTrue>
247	</target>
248
249	<target name="testReportHtmlTabWidth">
250		<jacoco:report>
251			<structure name="Test">
252				<classfiles>
253					<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
254				</classfiles>
255				<sourcefiles encoding="UTF-8" tabwidth="13">
256					<fileset dir="${org.jacoco.ant.reportTaskTest.sources.dir}" />
257				</sourcefiles>
258			</structure>
259			<html destdir="${temp.dir}"/>
260		</jacoco:report>
261
262		<loadfile property="testReportHtmlTabWidth.content" srcfile="${temp.dir}/org.jacoco.ant/TestTarget.java.html"/>
263		<au:assertTrue message="Tab width not set in ${testReportHtmlTabWidth.content}">
264			<contains string="${testReportHtmlTabWidth.content}" substring="window['PR_TAB_WIDTH']=13"/>
265		</au:assertTrue>
266	</target>
267
268	<target name="testReportHtmlInvalidTabWidth">
269		<au:expectfailure expectedMessage="Tab width must be greater than 0">
270			<jacoco:report>
271				<structure name="Test">
272					<classfiles>
273						<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
274					</classfiles>
275					<sourcefiles encoding="UTF-8" tabwidth="0">
276						<fileset dir="${org.jacoco.ant.reportTaskTest.sources.dir}" />
277					</sourcefiles>
278				</structure>
279				<html destdir="${temp.dir}"/>
280			</jacoco:report>
281		</au:expectfailure>
282	</target>
283
284	<target name="testReportHtmlZipFile">
285		<jacoco:report>
286			<structure name="Test">
287				<classfiles>
288					<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
289				</classfiles>
290			</structure>
291			<html destfile="${temp.dir}/report.zip"/>
292		</jacoco:report>
293
294		<au:assertFileExists file="${temp.dir}/report.zip"/>
295	</target>
296
297	<target name="testReportHtmlWithSources">
298		<jacoco:report>
299			<structure name="Test">
300				<classfiles>
301					<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
302				</classfiles>
303				<sourcefiles encoding="UTF-8">
304					<fileset dir="${org.jacoco.ant.reportTaskTest.sources.dir}" />
305				</sourcefiles>
306			</structure>
307			<html destdir="${temp.dir}"/>
308		</jacoco:report>
309
310		<au:assertFileExists file="${temp.dir}/org.jacoco.ant/TestTarget.java.html"/>
311		<au:assertFileExists file="${temp.dir}/default/TestTargetInDefault.java.html"/>
312	</target>
313
314	<target name="testReportHtmlWithSourcesDir">
315		<jacoco:report>
316			<structure name="Test">
317				<classfiles>
318					<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
319				</classfiles>
320				<sourcefiles encoding="UTF-8">
321					<dirset dir="${org.jacoco.ant.reportTaskTest.sources.dir}/.." includes="src" />
322				</sourcefiles>
323			</structure>
324			<html destdir="${temp.dir}"/>
325		</jacoco:report>
326
327		<au:assertFileExists file="${temp.dir}/org.jacoco.ant/TestTarget.java.html"/>
328		<au:assertFileExists file="${temp.dir}/default/TestTargetInDefault.java.html"/>
329	</target>
330
331	<target name="testReportHtmlWithSourceEncoding">
332		<mkdir dir="${temp.dir}/org/jacoco/ant"/>
333		<echo file="${temp.dir}/org/jacoco/ant/TestTarget.java" encoding="UTF-16">Source Code</echo>
334		<jacoco:report>
335			<structure name="Test">
336				<classfiles>
337					<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
338				</classfiles>
339				<sourcefiles encoding="UTF-16">
340					<fileset dir="${temp.dir}" />
341				</sourcefiles>
342			</structure>
343			<html destdir="${temp.dir}"/>
344		</jacoco:report>
345
346		<loadfile property="testReportHtmlWithSourceEncoding.content" srcfile="${temp.dir}/org.jacoco.ant/TestTarget.java.html" encoding="UTF-8"/>
347		<au:assertTrue message="Report does not contain expected text.">
348			<contains string="${testReportHtmlWithSourceEncoding.content}" substring="Source Code"/>
349		</au:assertTrue>
350	</target>
351
352	<target name="testReportHtmlLocale">
353		<jacoco:report>
354			<structure name="Test">
355				<classfiles>
356					<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
357				</classfiles>
358				<sourcefiles encoding="UTF-8">
359					<fileset dir="${org.jacoco.ant.reportTaskTest.sources.dir}" />
360				</sourcefiles>
361			</structure>
362			<html destdir="${temp.dir}" locale="gr"/>
363		</jacoco:report>
364
365		<loadfile property="testReportHtmlLocale.content" srcfile="${temp.dir}/org.jacoco.ant/TestTarget.java.html" encoding="UTF-8"/>
366		<au:assertTrue message="Report does not contain expected language tag.">
367			<contains string="${testReportHtmlLocale.content}" substring="lang=&quot;gr&quot;"/>
368		</au:assertTrue>
369	</target>
370
371	<!-- CSV Output -->
372
373	<target name="testReportCsvNoDestfile">
374		<au:expectfailure expectedMessage="Destination file must be supplied for csv report">
375			<jacoco:report>
376				<structure name="root"/>
377				<csv/>
378			</jacoco:report>
379		</au:expectfailure>
380	</target>
381
382	<target name="testReportCsvEncoding">
383		<property name="testReportCsvEncoding.destfile" location="${temp.dir}/report.csv"/>
384		<jacoco:report>
385			<structure name="Test"/>
386			<csv encoding="UTF-16" destfile="${testReportCsvEncoding.destfile}"/>
387		</jacoco:report>
388
389		<au:assertFileExists file="${testReportCsvEncoding.destfile}"/>
390		<loadfile property="testReportCsvEncoding.content" srcfile="${testReportCsvEncoding.destfile}" encoding="UTF-16"/>
391		<au:assertTrue message="Encoding not set in ${testReportCsvEncoding.content}">
392			<contains string="${testReportCsvEncoding.content}" substring="METHOD_COVERED"/>
393		</au:assertTrue>
394	</target>
395
396
397	<!-- XML Output -->
398
399	<target name="testReportXmlNoDestfile">
400		<au:expectfailure expectedMessage="Destination file must be supplied for xml report">
401			<jacoco:report>
402				<structure name="root"/>
403				<xml/>
404			</jacoco:report>
405		</au:expectfailure>
406	</target>
407
408	<target name="testReportXmlInvalidDestfile">
409		<au:expectfailure expectedMessage="Error while creating report">
410			<jacoco:report>
411				<structure name="root"/>
412				<xml destfile="${temp.dir}"/>
413			</jacoco:report>
414		</au:expectfailure>
415	</target>
416
417	<target name="testReportXmlEncoding">
418		<property name="testReportXmlEncoding.destfile" location="${temp.dir}/report.xml"/>
419		<jacoco:report>
420			<structure name="Test"/>
421			<xml encoding="UTF-16" destfile="${testReportXmlEncoding.destfile}"/>
422		</jacoco:report>
423
424		<au:assertFileExists file="${testReportXmlEncoding.destfile}"/>
425		<loadfile property="testReportXmlEncoding.content" srcfile="${testReportXmlEncoding.destfile}" encoding="UTF-16"/>
426		<au:assertTrue message="Encoding not set in ${testReportXmlEncoding.content}">
427			<contains string="${testReportXmlEncoding.content}" substring="encoding=&quot;UTF-16&quot;"/>
428		</au:assertTrue>
429	</target>
430
431	<!-- Coverage Check -->
432
433	<target name="testReportCheckOk">
434		<jacoco:report>
435			<structure name="Test">
436				<classfiles>
437					<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
438				</classfiles>
439				<sourcefiles encoding="UTF-8">
440					<fileset dir="${org.jacoco.ant.reportTaskTest.sources.dir}" />
441				</sourcefiles>
442			</structure>
443			<check>
444				<rule element="CLASS">
445					<limit counter="METHOD" value="MISSEDCOUNT" maximum="100"/>
446				</rule>
447			</check>
448		</jacoco:report>
449	</target>
450
451	<target name="testReportCheckFailed">
452		<au:expectfailure expectedMessage="Coverage check failed due to violated rules.">
453			<jacoco:report>
454				<structure name="Test">
455					<classfiles>
456						<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
457					</classfiles>
458					<sourcefiles encoding="UTF-8">
459						<fileset dir="${org.jacoco.ant.reportTaskTest.sources.dir}" />
460					</sourcefiles>
461				</structure>
462				<check>
463					<rule element="BUNDLE">
464						<limit counter="INSTRUCTION" value="COVEREDRATIO" minimum="0.90"/>
465					</rule>
466				</check>
467			</jacoco:report>
468		</au:expectfailure>
469		<au:assertLogContains level="error" text="instructions covered ratio is 0.00, but expected minimum is 0.90"/>
470	</target>
471
472	<target name="testReportCheckSetPropertyOnly">
473		<jacoco:report>
474			<structure name="Test">
475				<classfiles>
476					<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
477				</classfiles>
478				<sourcefiles encoding="UTF-8">
479					<fileset dir="${org.jacoco.ant.reportTaskTest.sources.dir}" />
480				</sourcefiles>
481			</structure>
482			<check failonviolation="false" violationsproperty="violation">
483				<rule element="BUNDLE">
484					<limit counter="METHOD" value="COVEREDRATIO" minimum="0.50"/>
485					<limit counter="INSTRUCTION" value="COVEREDRATIO" minimum="0.90"/>
486				</rule>
487			</check>
488		</jacoco:report>
489		<au:assertLogContains level="error" text="methods covered ratio is 0.00, but expected minimum is 0.50"/>
490		<au:assertLogContains level="error" text="instructions covered ratio is 0.00, but expected minimum is 0.90"/>
491		<au:assertTrue message="Property is not set">
492			<contains string="${violation}" substring="methods covered ratio is 0.00, but expected minimum is 0.50"/>
493		</au:assertTrue>
494		<au:assertTrue message="Property is not set">
495			<contains string="${violation}" substring="instructions covered ratio is 0.00, but expected minimum is 0.90"/>
496		</au:assertTrue>
497	</target>
498
499
500</project>