How to get ant JUnit result in user defined xml file name? - ant

How to get ant JUnit result in user defined xml file name?
Junit (ant task) by default creates TEST-testcasename.xml for results, is there a way to change this xml file name? I am running same testcase multiple times for different browsers and currently the result for one browser is being overwritten by another browser's result.
Here is my code:
<target name="junit1">
<junit fork="true" forkmode="once" haltonfailure="false" logfailedtests="true">
<classpath refid="classpath.project"/>
<formatter type="xml"/>
<jvmarg value="-Dbrowser=${browser}"/>
<batchtest todir="${dir.report}">
<fileset dir="${dir.src}">
<include name="${testcase}"/>
</fileset>
</batchtest>
</junit>

Why don't you just use a different output directory for each browser, or copy the output directory to another one when tests are finished?
<batchtest todir="${dir.report}/${browser}">
If you need all the files to be in the same directory, rename (or copy) them after each batch using the move (or copy) task with a glob mapper :
<mapper type="glob" from="TEST*.xml" to="${browser}-TEST*.xml"/>

Use the extension attribute of the formatter element
https://ant.apache.org/manual/Tasks/junit.html
<formatter type="xml" extension="${browser}.xml"/>
This will append the browser name to the end of each output file

Related

How can PMD HTML Report's Error Description Link Show Local Folder

I am using PMD source code analyzer (PMD) for my java web project through ant task. The computer is offline (not connected to the Internet). Part of ant task is as follows:
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask">
<classpath>
<fileset dir="E:/pmd-bin-6.41.0/lib">
<include name="*.jar"/>
</fileset>
</classpath>
</taskdef>
<pmd shortfilenames="true" cachelocation="pmd.cache" encoding="UTF-8">
<ruleset>web/resources/category/java/bestpractices.xml</ruleset>
<formatter type="html" tofile="report.html">
</formatter>
<fileset dir="src/java/">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>
When I run pmd target, report.html file is generated ok. The html file basically lists <fileName, lineNumber, description> triplets.
e.g.
foo.java...43...The initializer for variable "tempIDNo" is never used (overwritten on lin 67)
The description in this html file has a link as file:///E:ws/project/${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#unusedassignment which does not work. E:ws/project/ is the folder where my project resides.
As a matter of fact, I have all the necessary html files (such as pmd_rules_java_bestpractices.html) unzipped in E:/pmd-doc-6.41.0 folder.
Could you please help me how to set up description link in html file to show local folder?
Thank you.
Here is the solution I have come up with:
(Using suggestion from (How can I create a link to a local file on a locally-run web page?)) Before ant pmd target define property pmd.website.baseurl
<propery name="pmd.website.baseurl" value="file:///E:/pmd-doc-6.41.0"/>
(Using usage/suggestion from (https://ant.apache.org/manual/Types/filterchain.html#expandproperties), ANT replacing strings in specified files using file with properties ) Change inside target as follows
...
<pmd ...>...
</pmd>
<copy file="report.html" tofile="report2.html">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.ExpandProperties"/>
</filterchain>
</copy> ...
Run the ant target.

Ant: Source and target files are the same. How to detect a change?

We are using JiBX. The important thing to know is that JiBX modifies the already compiled class files.
We do our compile:
<javac destdir="${main.destdir}">
<src path="${main.srcdir}"/>
<classpath refid="main.classpath"/>
</javac>
Then, we call JiBX:
<jibx load="true"
binding="{$binding.file}">
<classpath refid="main.classpath"/>
<classpath refid="main.destdir.classpath"/>
</jibx>
This uses an XML file that updates the classfiles compiled by <javac> above. The problem is how do I know that the files have been compiled, but not processed by JiBX? I'd like to put some logic in my program, so that files are not updated twice by JiBX. Besides, it's bad form to duplicate work that already been done.
After the jibx build step, generate a marker file, e.g.
<touch file="${target.dir}/jibx.marker" />
Only perform the jibx build step if that marker file is older than the .class files (indicating that the javac ran more recently than the last jibx).
For that bit of logic, you can use the traditional ant way:
<uptodate property="jibx.uptodate" targetfile="${target.dir}/jibx.marker">
<srcfiles dir="${main.destdir}" includes="...../*.class" />
</uptodate>
And then use the property with an unless clause when invoking the jixb target.
Or, you can use Antcontrib's outofdate alternative:
<outofdate>
<sourcefiles>
<fileset dir="${main.destdir}" includes="...../*.class" />
</sourcefiles>
<targetfiles>
<fileset dir="${target.dir}" includes="jibx.marker"/>
</targetfiles>
<sequential>
<jibx load="true"
binding="{$binding.file}">
<classpath refid="main.classpath"/>
<classpath refid="main.destdir.classpath"/>
</jibx>
</sequential>
</outofdate>
I'm giving this to Patrice M. because his suggestion put me on the right track. However, it didn't quite work out as he stated. (Sorry, if I got he pronoun wrong, but Patrice can be both a male or female name.)
What I had to do was create two watch files: One for the Java compile, and one for the JiBX changes.
<!-- Check if Javac is out of date. If so, create javac watcher -->
<outofdate verbose="true">
<sourcefiles>
<fileset dir="${main.srcdir}">
<include name="*.java"/>
</fileset>
</sourcefiles>
<mapper type="regexp"
from="${main.srcdir}/(.*)\.java"
to="${main.destdir}/(\1).class"/>
<sequential>
<echo message="Java compiled"/>
<echo message="Java compiled"
file="${target.dir}/${javac.monitor.file}"/>
</sequential>
</outofdate>
<javac destdir="${main.destdir}"
debug="${javac.debug}">
<src path="${main.srcdir}"/>
<classpath refid="main.classpath"/>
</javac>
<!-- Compare javac and jibx monitoring file -->
<!-- If out of date, rerun jibx -->
<outofdate>
<sourcefiles>
<fileset dir="${target.dir}">
<include name="${javac.monitor.file}"/>
</fileset>
</sourcefiles>
<targetfiles>
<fileset dir="${target.dir}">
<include name="${jibx.monitor.file}"/>
</fileset>
</targetfiles>
<sequential>
<jibx load="true"
binding="${target.dir}/binding-gg.xml">
<classpath refid="main.classpath"/>
<classpath refid="main.destdir.classpath"/>
</jibx>
<!-- Create JiBX monitoring file -->
<echo message="Compiled and JiBX"
file="${target.dir}/${jibx.monitor.file}"/>
</sequential>
</outofdate>
I create the javac monitoring file if the source is out of date with the classes because that's when I compile. I have to create the JiBX outofdate monitoring file only when I run JiBX and that's inside the <outofdate> for JiBX.
I guess I could also put a source on the XML JiBX files too just to be sure.

Running PigUnit in a subdirectory with Ant

My problem is with running PigUnit via ant from a parent directory.
I am using the PigUnit example straight off of the PigUnit site.
The ant script I am using is here:
<junit printsummary="on" fork="true" haltonfailure="yes">
<jvmarg value="-Duser.dir=${basedir}"/>
<classpath refid="junit.class.path" />
<formatter type="xml" />
<batchtest todir="${test.report.dir}">
<fileset dir="${test.dir}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
This script works perfectly fine if I run ant in the working directory of the project. However, when I remotely call the script with this line of code in an ant build script in the Pig project's parent directory
<ant dir="${pig.dir}" target="main" inheritall="false" antfile="build.xml"/>
I get a FileNotFoundException:
java.io.FileNotFoundException">java.io.FileNotFoundException: top_queries.pig (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at org.apache.pig.pigunit.PigTest.readFile(PigTest.java:273)
at org.apache.pig.pigunit.PigTest.readFile(PigTest.java:269)
at org.apache.pig.pigunit.PigTest.<init>(PigTest.java:92)
at TopQueriesTest.testTop2Queries(Unknown Source)
This same FNF exception also happens if I run ant from the command line in the parent directory:
ant -f PigJavaTest/build.xml junit
My workaround to helping PigUnit find top_queries.pig file to specifiy the top_queries.pig file's location relative to the Pig project's parent directory, e.g.
PigTest test = new PigTest("PigTestJava/top_queries.pig", args);
but this is not optimal because it breaks when running ant from a directory different from the parent one.
Other JUnit tests will run normally from the parent directory, but PigUnit always throws the same FNF exception. I also tried doing a simple test with PigServer (loading/dumping a file) and the PigServer test behaved just like PigUnit had.
Why does this FileNotFoundException get thrown when calling ant from the parent directory, and how can I fix it?
You have a relative path to your pig file. So if you call the ant script from the working directory of the project it should work. When you call the ant script from the parent project, the relative path now is relative to the parent directory.
http://ant.apache.org/manual/Tasks/ant.html
when you call the ant task here, you are correctly setting the dir property, but that only changes the value of ${basedir} property for the follow in script, not the actual working directory when the junit task runs.
I suggest you use the dir attribute in your junit task to hard set the working directory (i guess that's what you're trying to do with the jvmarg option):
<junit printsummary="on" fork="true" haltonfailure="yes" dir="${basedir}">
<jvmarg value="-Duser.dir=${basedir}"/>
<classpath refid="junit.class.path" />
<formatter type="xml" />
<batchtest todir="${test.report.dir}">
<fileset dir="${test.dir}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
See http://ant.apache.org/manual/Tasks/junit.html for more details

ant junit batchtest from a jar

I'd like to use ant (post 1.7) to run all tests in classes named *Test.class in a certain jar.
Something like the following (although it doesn't actually run any tests):
<junit fork="yes" printsummary="on" haltonfailure="on">
<formatter type="xml"/>
<batchtest fork="yes" todir="${junit.output.dir}">
<resources>
<zipentry zipfile="tests-only.jar" name="**/*Test.class"/>
</resources>
</batchtest>
<classpath refid="testsplus.classpath"/>
</junit>
What is the correct syntax for the resources/zipentry part?
The ant docs say:
batchtest collects the included
resources from any number of nested
Resource Collections. It then
generates a test class name for each
resource that ends in .java or .class.
Any type of Resource Collection is
supported as a nested element, prior
to Ant 1.7 only <fileset> has been
supported.
Instead of zipentry you can probably use the zipfileset datatype:
<zipfileset src="tests-only.jar" includes="**/*Test.class"/>

Running "pure" JUnit 4 tests using ant

We have migrated to both JUnit 4 and ant 1.7
The tests runs fine in eclipse, but the annotations are ignored when running the tests using ant.
According to the Ant junit task documentation:
It also works with JUnit 4.0, including "pure" JUnit 4 tests using only annotations and no JUnit4TestAdapter.
But the documentation doesn't elaborate on how it should be configured.
Is there any special setting required for the junit task? Am I missing something?
We have both Tests that extends TestCase (i.e. 3.8 style) and "pure" Junit 4 tests, could that be the problem?
I am using pure JUnit4 tests with Ant.
Here is the interesting part of my build file:
<junit printsummary="yes" haltonfailure="yes">
<formatter type="xml"/>
<classpath refid="path.test"/>
<batchtest fork="yes" todir="${dir.report.unittests.xml}">
<fileset dir="src">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
Make sure you have the latest version of the junit.jar file in the lib directory of Ant. As far as I know the required version is delivered with ant 1.7 or higher versions...
Ant ships with a version of JUnit 3 by default. JUnit 3 has no support for test annotations.
To use the JUnit 4 annotations from the junit task make sure that you provide the location of a JUnit 4 jar in a nested classpath element of the junit task (see this entry in the ant FAQ).
<junit showoutput="yes" fork="true">
<classpath>
<!-- The location of the JUnit version that you want to use -->
<pathelement location="lib/junit-4.9b1.jar"/>
</classpath>
<formatter type="plain" usefile="false" />
<batchtest>
<fileset dir="${tests.dir}"/>
</batchtest>
</junit>
This is a preferable solution to overwriting the ant-junit.jar in ANT_HOME/lib as it means you can keep your JUnit jar in source control alongside your code making upgrades to later versions straightforward.
Note that whilst I haven't specified any include pattern in my fileset above this does mean that the junit task will attempt to run JUnit against all the classes in that directory structure which might result in a number of classes being included that don't contain any tests depending on how you have structured your source files.
You can finally only find and execute tests with the skipNonTests parameter added in ant 1.9.3+!
This is the code snippet from the accepted answer above (except for the new skipNonTests parameter and getting rid of the "Test" in the filename requirement):
<junit printsummary="yes" haltonfailure="yes">
<formatter type="xml"/>
<classpath refid="path.test"/>
<batchtest skipNonTests="true" fork="yes" todir="${dir.report.unittests.xml}">
<fileset dir="src">
<include name="**/*.java"/>
</fileset>
</batchtest>
</junit>
This happened to me and it was because I was both using annotations and extending TestCase.
public class TestXXX extends TestCase {
#Test
public void testSimpleValidCase() {
// this was running
}
#Test
public void simpleValidCase() {
// this wasn't running
}
}
When you extend TestCase you are assuming JUnit3 style so JUnit4 annotations are ignored.
The solution is to stop extending TestCase.
Verify your classpath definition...
this solved my problem.
<path id="classpath" description="Classpath do Projeto">
<fileset dir="${LIB}">
<include name="**/*.jar" />
<exclude name="**/.SVN/*.*"/>
</fileset>
</path>
This is the relevant part of my generic ant script... not sure if that'll help you or not..
<junit fork="true"
forkmode="once"
haltonfailure="false"
haltonerror="false"
failureproperty="tests.failures"
errorproperty="tests.errors"
includeantruntime="true"
showoutput="true"
printsummary="true">
<classpath>
<path refid="path-id.test.classpath.run"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes"
todir="${dir.build.testresults}">
<fileset dir="${dir.src.tests}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
Apply this annotation to the other classes org.junit.Ignore
I also tried to do tests with JUnit 4.0 without JUnit4TestAdapter, i.e. without method
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(SomeTestClass.class);
}
I use ant 1.9.4.
Running ant test verbose (ant -v ) shows
[junit] Running multiple tests in the same VM
[junit] Implicitly adding /usr/share/java/junit.jar:/usr/sharejava/ant-launcher.jar:/usr/share/java/ant.jar:/usr/share/java/ant/ant-junit.jar to CLASSPATH
Aha, but still there is some ant-junit-task.
Downloading this shows in addition
/usr/share/java/ant/ant-junit4.jar which is not added implicitly.
I just added it explicitly:
<junit printsummary="yes"
fork="yes"
forkmode="once"
maxmemory="1023m"
showoutput="no">
...
<classpath>
<pathelement path="...:${junitJar}:${hamcrestJar}:/usr/share/java/ant/ant-junit4.jar" />
</classpath>
...
</junit>
and it worked. Without: no.
I am aware that this solution is not beautiful at all...
What I ended up doing was adding an Ant to one of my definitions that is used by the task>. Et voila.

Resources