JUnit Unit Testing Module

Public Targets

TargetDescription
testrun unit test(s)

Source: utest.xml

				<?xml version="1.0" encoding="UTF-8"?>
<project name="utest">
	<description>
  + ==\_/============J=A=V=A=G=E=N===A=N=T===M=O=D=U=L=E=S================= +
  +  \(_)/                                                                  +
  +  -(_)-                JUnit Unit Testing Module                         +
  +  /(_)\                                                                  +
  + ======================================================================= +
  +    Copyright (c) 2002-2005 Outsource Cafe, Inc.  All rights reserved.   +
  + ======================================================================= +
  +
  + Run all JUnit classes in test directory ending with the 'Test' suffix.  
  +
  + Public targets: 
  +   test - runs unit test(s), optionaly generating report
  +
  + Optional properties:
  +   utestcase - when set allows single unit test to be run instead of batchtest
  +   testreports - when set test results are sent to HEML report
  +   target.classes.dir - classes to test, defaults to build.classes.dir
  +
  + Prerequisit properties:
  +   test.folder - folder name associated with unit tests (test)
  +   src.test.dir - Java unit test source code directory
  +   gen.test.dir - Generated Java unit test source code directory
  +   build.test.dir - destination directory of temporary test artifacts
  +   build.test.classes.dir - destination directory of compiled classes
  +   build.test.metainf.dir - META-INF directory
  +   build.test.reports.dir - temp reports directory 
  +   build.test.docs.dir - test reports directory
  +
  + Note: the 'utest' prefix used to avoid target name confilcts with main build file.
  +
  + Todo: might get more flexibility from a utest.macro ?
  +
  + developer: RichardEasterling@OutsourceCafe.com
  + ======================================================================= +
  + Licensed under the Apache License, Version 2.0 (the "License");
  + you may not use this file except in compliance with the License.
  + You may obtain a copy of the License at
  +
  +   http://www.apache.org/licenses/LICENSE-2.0
  +
  + Unless required by applicable law or agreed to in writing, software
  + distributed under the License is distributed on an "AS IS" BASIS,
  + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  + See the License for the specific language governing permissions and
  + limitations under the License.
  + =J=A=M===========================================J=A=V=A=G=E=N=.=C=O=M= +
	</description>
	<property name="UTEST.PRESENT" value="true"></property>
	<!-- =================================================================== -->
	<!-- Public targets: -->
	<!-- =================================================================== -->
	<target name="test" depends="utest.uptodate, utest.compile, utest.resources, utest.console, utest.report" description="run unit test(s)"></target>
	<!-- ===================================================================== -->
	<!-- local properties: -->
	<!-- ===================================================================== -->
	<property name="testtool.name" value="JUnit"></property>
	<property name="utest.fork" value="false"></property>
	<property name="utest.last.failed.file" location="${build.dir}/.lastutestsfailed"></property>
	<property name="target.classes.dir" location="${build.classes.dir}"></property>
	<!-- optional properties set in build file:
   <property name="utestcase" value="AllTests"/>
	<property name="testreports" value="true"/>
	-->
	<!-- ===================================================================== -->
	<!-- test classpaths -->
	<!-- ===================================================================== -->
	<path id="utest.build.classpath">
		<pathelement path="${java.class.path}"></pathelement>
		<path refid="dependencies.classpath"></path>
		<pathelement location="${junit.jar}"></pathelement>
		<pathelement location="${target.classes.dir}"></pathelement>
		<pathelement location="${build.test.classes.dir}"></pathelement>
	</path>
	<!-- ===================================================================== -->
	<!-- patternset to identify unit test classes to execute -->
	<!-- ===================================================================== 
	THIS MOVED TO classpath.xml. SET IT IN THE POM, FOR EXAMPLE:
	<build>
		<unitTest>
			<includes>
				<include>**/*Test.class</include>
			</includes>
		</unitTest>
	</build>
	
	<property name="test.class.include" value="**/*Test.class"/>
	<patternset id="utest.class.patternset">
		<include name="${test.class.include}"/>
	</patternset>
	-->
	<!-- ===================================================================== -->
	<!-- sanity checks -->
	<!-- ===================================================================== -->
	<condition property="utest.properties.ok">
		<and>
			<available file="${junit.jar}"></available>
		</and>
	</condition>
	<fail message="JUnit properties error (check jars, etc.)" unless="utest.properties.ok"></fail>
	<!-- ===================================================================== -->
	<!-- show settings-this can be appended to an overriden 'setup' target: -->
	<!-- <target name="setup" depends="init, settings, utsettings"/> -->
	<!-- ===================================================================== -->
	<target name="utest.settings" depends="">
		<echo>
utestcase            = ${utestcase}
testreports          = ${testreports}
+ ============================================================= +
    </echo>
	</target>
	<!-- ===================================================================== -->
	<!-- list unit test build classpath -->
	<!-- ===================================================================== -->
	<target name="utest.cp">
		<pathconvert refid="utest.build.classpath" property="utest.classpath.list" pathsep="${CR}"></pathconvert>
		<echo message="+ utest.build.classpath================================= +"></echo>
		<echo message="${utest.classpath.list}"></echo>
	</target>
	<!-- =================================================================== -->
	<!-- compiles the source code                                            -->
	<!-- =================================================================== -->
	<target name="utest.compile">
		<mkdir dir="${gen.test.dir}"></mkdir>
		<mkdir dir="${src.test.dir}"></mkdir>
		<mkdir dir="${build.test.classes.dir}"></mkdir>
		<javac destdir="${build.test.classes.dir}" source="${compiler.source}" target="${compiler.target}" classpathref="utest.build.classpath" debug="${compiler.debug}" deprecation="${compiler.deprecation}" optimize="${compiler.optimize}" compiler="${compiler.type}" excludes="${compiler.excludes}">
			<src path="${gen.test.dir}"></src>
			<src path="${src.test.dir}"></src>
		</javac>
	</target>
	<!-- =================================================================== -->
	<!-- 'utest.resources' target - copy resources to target/test/classpath  -->
	<!-- =================================================================== -->
	<!-- the 'utest.resources' target, found in classpath.xml, is generated 
	     from the POM. The default project.xml entry for EJBs is:
	<build>
		<sourceDirectory>src/java</sourceDirectory>
		<unitTestSourceDirectory>src/test</unitTestSourceDirectory>
		<unitTest>
			<includes>
				<include>**/*Test.java</include>
			</includes>
			<resources>
				<resource>
					<directory>src/test</directory>
					<excludes>
						<exclude>**/*.java</exclude>
					</excludes>
				</resource>
				<resource>
					<directory>src-gen/test</directory>
					<excludes>
						<exclude>**/*.java</exclude>
					</excludes>
				</resource>
			</resources>
		</unitTest>
	</build>
	-->
	<!-- =================================================================== -->
	<!-- run unit test(s) sending results to console -->
	<!-- =================================================================== -->
	<target name="utest.console" unless="skip.utest.console">
		<echo>JUnit Console utestcase = ${utestcase}</echo>
		<junit printsummary="true" errorProperty="utest.failed" failureProperty="utest.failed" fork="${utest.fork}">
			<classpath refid="utest.build.classpath"></classpath>
			<formatter type="plain" usefile="false"></formatter>
			<test name="${utestcase}" if="utestcase"></test>
			<batchtest todir="${build.test.reports.dir}" unless="utestcase">
				<fileset dir="${src.test.dir}">
					<patternset refid="utest.batchtest.patternset"></patternset>
				</fileset>
				<fileset dir="${gen.test.dir}">
					<patternset refid="utest.batchtest.patternset"></patternset>
				</fileset>
			</batchtest>
		</junit>
		<echo message="last build failed tests" file="${utest.last.failed.file}"></echo>
		<fail message="Unit test(s) failed. Check log and/or reports directory." if="utest.failed"></fail>
		<!-- Remove test failed file, as these tests succeeded -->
		<delete file="${utest.last.failed.file}" failonerror="false"></delete>
	</target>
	<!-- =================================================================== -->
	<!-- run unit test(s) sending results to HTML report -->
	<!-- =================================================================== -->
	<target name="utest.report" unless="skip.utest.reports">
		<delete dir="${build.test.docs.dir}" failonerror="false"></delete>
		<delete dir="${build.test.reports.dir}" failonerror="false"></delete>
		<mkdir dir="${build.test.docs.dir}"></mkdir>
		<mkdir dir="${build.test.reports.dir}"></mkdir>
		<echo>JUnit Report utestcase = ${utestcase}</echo>
		<junit printsummary="true" errorProperty="utest.failed" failureProperty="utest.failed" fork="${utest.fork}">
			<classpath refid="utest.build.classpath"></classpath>
			<formatter type="xml"></formatter>
			<test name="${utestcase}" todir="${build.test.reports.dir}" if="utestcase"></test>
			<batchtest todir="${build.test.reports.dir}" unless="utestcase">
				<fileset dir="${src.test.dir}">
					<patternset refid="utest.batchtest.patternset"></patternset>
				</fileset>
				<fileset dir="${gen.test.dir}">
					<patternset refid="utest.batchtest.patternset"></patternset>
				</fileset>
			</batchtest>
		</junit>
		<junitreport todir="${build.test.docs.dir}">
			<fileset dir="${build.test.reports.dir}">
				<include name="TEST-*.xml"></include>
			</fileset>
			<report format="noframes" todir="${build.test.docs.dir}"></report>
		</junitreport>
		<echo message="last build failed tests" file="${utest.last.failed.file}"></echo>
		<fail message="Unit test(s) failed. Check log and/or reports directory." if="utest.failed"></fail>
		<!-- Remove test failed file, as these tests succeeded -->
		<delete file="${utest.last.failed.file}" failonerror="false"></delete>
	</target>
	<!-- =================================================================== -->
	<!-- Logic to skip running unit tests if src and test(s) are uptodate.   -->
	<!-- Note: Many users can't figure out why their tests are not running   -->
	<!-- when uptodate is set.  To avoid confusion the tests run on every    -->
	<!-- call unless you explicitly set 'test.uptodate.on' to 'true'.        -->
	<!-- =================================================================== -->
	<target name="utest.uptodate">
		<mkdir dir="${gen.test.dir}"></mkdir>
		<mkdir dir="${src.test.dir}"></mkdir>
		<mkdir dir="${target.classes.dir}"></mkdir>
		<condition property="utests.uptodate">
			<and>
				<!-- always run tests unless this property has been set -->
				<isset property="test.uptodate.on"></isset>
				<uptodate>
					<!--if no changes in target source - compare *.java to *.class timestamps-->
					<srcfiles dir="${src.java.dir}" includes="**/*.java"></srcfiles>
					<srcfiles dir="${gen.java.dir}" includes="**/*.java"></srcfiles>
					<srcfiles dir="${xdoclet.build.java.dir}" includes="**/*.java"></srcfiles>
					<mapper type="glob" from="*.java" to="${target.classes.dir}/*.class"></mapper>
				</uptodate>
				<uptodate>
					<!--if no changes in unit test source - compare *.java to *.class timestamps-->
					<srcfiles dir="${src.test.dir}" includes="**/*.java"></srcfiles>
					<srcfiles dir="${gen.test.dir}" includes="**/*.java"></srcfiles>
					<mapper type="glob" from="*.java" to="${build.test.classes.dir}/*.class"></mapper>
				</uptodate>
				<uptodate>
					<!--if no changes in target resources (non-source) - compare timestamps on files in src and classes directories -->
					<srcfiles dir="${src.test.dir}" excludes="**/*.java"></srcfiles>
					<srcfiles dir="${gen.test.dir}" excludes="**/*.java"></srcfiles>
					<mapper type="glob" from="*" to="${build.test.classes.dir}/*"></mapper>
				</uptodate>
				<not>
					<!--always run tests if single unit test class spacified -->
					<isset property="utestcase"></isset>
				</not>
				<or>
					<not>
						<!-- this check is not applicable to console tests -->
						<isset property="testreports"></isset>
					</not>
					<uptodate>
						<!-- run test if any report fragments (compared with test source) are missing or not uptodate-->
						<srcfiles dir="${src.test.dir}" includes="**/*.java"></srcfiles>
						<srcfiles dir="${gen.test.dir}" includes="**/*.java"></srcfiles>
						<mapper type="package" from="*Test.java" to="${build.test.reports.dir}/TEST-*Test.xml"></mapper>
					</uptodate>
				</or>
				<not>
					<!--rerun tests if last test failed (indicated by this file not being erased)-->
					<available file="${utest.last.failed.file}"></available>
				</not>
			</and>
		</condition>
		<echo>utests.uptodate = ${utests.uptodate}, testreports = ${testreports}</echo>
		<!-- determine which target to run based on 'testreports' property -->
		<condition property="skip.utest.console">
			<or>
				<isset property="testreports"></isset>
				<isset property="utests.uptodate"></isset>
			</or>
		</condition>
		<condition property="skip.utest.reports">
			<or>
				<not>
					<isset property="testreports"></isset>
				</not>
				<isset property="utests.uptodate"></isset>
			</or>
		</condition>
	</target>
</project>