Ant execute arbitrary programs and terminate at end of junit task - java

I am attempting to write unit tests against a series of RMI interfaces / implementations and am relying on Ant + Junit to facilitate these tests. The main problem is that I need to have the rmiregistry start up at the beginning of my Junit task and close down at the end of execution whether that be failure,error, or success. Ant script:
<project>
<target name="test">
<javac srcdir="Test/src" destdir="Test/bin" />
<junit fork="true" printsummary="true">
<batchtest>
<fileset dir="Test/test">
<include name="**/*Test.*"/>
</fileset>
</batchtest>
</junit>
</target>
</project>

Basically, just set haltonerror="false" and failonerror="false" for the junit task.
This way junit won't fail due to tests. Starting and stopping is done via the exec task. I used taskkill here to kill the rmiregistry.exe.
<exec executable="start"> <--your start command here -->
<arg value="rmiregistry"/>
</exec>
<junit fork="true" printsummary="true" haltonerror="false" failonerror="false" errorproperty="juniterrors" failureproperty="junitfailures">
...
</junit>
<exec executable="taskkill"> <--your shutdown command here -->
<arg value="/IM"/>
<arg value="rmiregistry.exe"/>
</exec>
<fail if="juniterrors"/> <!-- ant can fail now, if desired -->
<fail if="junitfailures"/>
Another option is to use try/catch from ant-contrib, but I don't think this is needed here.

Related

ant jdb return immediately from a task

I'm wanting use ant to run a class and then the debugger (jdb) or the other way round
Whichever way round I do it I need one to return immediately as the other needs to attach...
here's the two tasks I'm working on at the moment... (where debug is the target run)
<target
name="run-debug-target"
depends="compile" >
<java
fork="true"
classname="uk.co.bedroomcoders.ple.desktop.DesktopLauncher"
classpath="bin:libs/gdx-backend-lwjgl.jar:libs/gdx-backend-lwjgl-natives.jar:libs/gdx.jar:libs/gdx-natives.jar" >
<jvmarg line="-agentlib:jdwp=transport=dt_socket,address=localhost:6000,server=y,suspend=y" />
</java>
</target>
<target
name="debug"
depends="run-debug-target"
description="debugs the project compiling if needed" >
<exec spawn="true" executable="jdb">
<arg value="-listen" />
<arg value="localhost:6000"/>
</exec>
</target>
https://ant.apache.org/manual/Tasks/java.html
See the spawn property:
if enabled allows to start a process which will outlive ant.
Requires fork=true, and not compatible with timeout, input, output, error, result attributes.
So..
<java
fork="true"
spawn="true"
classname="uk.co.bedroomcoders.ple.desktop.DesktopLauncher"
classpath="bin:libs/gdx-backend-lwjgl.jar:libs/gdx-backend-lwjgl-natives.jar:libs/gdx.jar:libs/gdx-natives.jar" >
<jvmarg line="-agentlib:jdwp=transport=dt_socket,address=localhost:6000,server=y,suspend=y" />
</java>
In this way, <java> task will start a new java process running the java class and return immediately without waiting for the process to return.

Java Ant wont complete task after running a Jar

I got some junit tests (in Tester App) that i want to run on a service (Service.jar running parallely). As soon as ant runs Service.jar, it wouldn't return to perform the junit test as expected but just sits there running the Service app. Am i doing something wrong? My build.xml looks like this below..
<target name="compile" depends="clean">
<javac srcdir="${srcdir}" destdir="${testdir}"
verbose="${full-compile}">
<classpath refid="classpath.test"/>
</javac>
</target>
<target name="test" depends="compile,run">
<junit fork="no" haltonfailure="yes">
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<test name="xyz.Tester.Test1" outfile="result" >
<formatter type="plain"/>
<formatter type="xml"/>
</test>
</junit>
</target>
<target name="run">
<java jar="Service.jar" fork="yes">
</java>
</target>
Moreover, i would like to know if there's a way i can run the Service.jar just within Ant (fork="no") if it's possible as ant wouldn't let me (Cannot execute a jar in non-forked mode. Please set fork='true'.)
Any help would be appreciated. Thanks
By default, ant tasks are executed in sequence. When you need to run something in parallel, you need to explicitly use task parallel - see the samples in its docs - something like:
...
<parallel>
<java jar="Service.jar" fork="yes"/>
<sequential>
<sleep seconds="30"/>
<junit fork="true" forkmode="once" ... >
<java jar="ServiceStopper.jar" fork="yes"/>
</sequential>
</parallel>
...
Alternatively, you can use the spawn attribute on the java task. It causes that your service is spawned in a separate process, and processing continues with next ant command.
Concerning the non-forked jar execution - you can avoid forking if you specify classpath and classname attributes.

Using <java> tag in Apach Ant, can I run both Server and the ProxyServer class

I would like to start Server and the ProxyServer class simultaneously, using Ant tag, is it possible to run the wo classes?
Here is the code I tried but Ant only starts the Server class and does not do anything there after, not sure if there is away in ant to achieve this.
Appreciate your help.
<target name="pxyServer" depends="server">
<echo>Executing Target - Run ProxyServer</echo>
<java classname="pxy.ProxyServer">
<classpath path="staging" />
</java>
</target>
<target name="server">
<echo>Executing Target - RunServer</echo>
<java classname="pxy.Server">
<classpath path="staging" />
</java>
</target>
Your targets are executed sequentially, and since the first one keeps running, the second one never gets the chance to start.
For parallel execution, you can use ant's "parallel" task:
http://ant.apache.org/manual/Tasks/parallel.html
Your modified script should probably look something like this:
<target name="startServerAndProxy">
<echo>Running server and proxy...</echo>
<parallel>
<java classname="pxy.Server">
<classpath path="staging" />
</java>
<java classname="pxy.ProxyServer">
<classpath path="staging" />
</java>
</parallel>
</target>
(Of course, if you're trying to start some third application in parallel, a client for example, then you should also include that one in the "parallel".)
UPDATE:
To start the server and the proxy each in its own console, I don't know if it can be done with the "java" Ant task, but I just tested that it can be done with "exec":
<target name="doit">
<parallel>
<exec executable="cmd" dir="staging">
<arg line="/k start java.exe pxy.Server"/>
</exec>
<exec executable="cmd" dir="staging">
<arg line="/k start java.exe pxy.ProxyServer"/>
</exec>
</parallel>
</target>

Cucumber-JVM ant task

How do i tell ant to execute all cucumber tests (features, implementations) in a folder?
I'm stuck using this example
<target name="runcukes" depends="compile-test">
<mkdir dir="target/cucumber-junit-report"/>
<java classname="cucumber.cli.Main" fork="true" failonerror="false" resultproperty="cucumber.exitstatus">
<classpath refid="classpath"/>
<arg value="--format"/>
<arg value="junit:target/cucumber-junit-report/allcukes.xml"/>
<arg value="--format"/>
<arg value="pretty"/>
<arg value="--format"/>
<arg value="html:target/cucumber-html-report"/>
<arg value="--glue"/>
<arg value="cucumber.examples.java.helloworld"/>
<arg value="src/test/resources"/>
</java>
<junitreport todir="target/cucumber-junit-report">
<fileset dir="target/cucumber-junit-report">
<include name="allcukes.xml"/>
</fileset>
<report format="frames" todir="target/cucumber-junit-report"/>
</junitreport>
<fail message="Cucumber failed">
<condition>
<not>
<equals arg1="${cucumber.exitstatus}" arg2="0"/>
</not>
</condition>
</fail>
</target>
Usage: java cucumber.cli.Main [options] [ [FILE|DIR][:LINE[:LINE]*] ]+
Options:
-g, --glue PATH Where glue code (step definitions and hooks) is loaded from.
-f, --format FORMAT[:OUT] How to format results. Goes to STDOUT unless OUT is specified.
Available formats: junit, html, pretty, progress, json, json-pretty.
-t, --tags TAG_EXPRESSION Only run scenarios tagged with tags matching TAG_EXPRESSION.
-n, --name REGEXP Only run scenarios whose names match REGEXP.
-d, --dry-run Skip execution of glue code.
-m, --monochrome Don't colour terminal output.
--dotcucumber Where to write out runtime information.
-v, --version Print version.
-h, --help You're looking at it.
In your case - change the <arg value="src/test/resources"/> to directory you want to run against. Note that you can specify multiple individual files and directories space-separated.
I also faced same problem and solved by declaring top package name like in you case the package name is "cucumber.examples.java.helloworld" Just declare only cucumber like this
<arg value="--glue"/>
<arg value="cucumber"/>
in you ant task. It's working at me end.
Instead of using the build file provided along with the samples from github, you can write your own build file.
Try running the Junit test file from ant using ant's Junit task. Set "format" of "#Cucumber.Options" annotation to point to the directory having your features.
Example
To run the Junit test,add the following target to your build file,
<junit printsummary="yes" failureproperty="junit.failure">
<classpath refid="id_of_the_path_to_find_JARS/.class files"></classpath>
<test name="com.example.YourTestClass" haltonfailure="no" outfile="result" >
<formatter type="plain"/>
<formatter type="xml"/>
</test>
</junit>
The Juint test specified in the <test name="com.example.YourTestClass" haltonfailure="no" outfile="result" > will be executed. The YourTestClass.java will look like this...
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#Cucumber.Options(format = {"html:target/cucumber-html","junit:target/cucumber-junit/Webpage.xml"},features = "src/test/features")
public class YourTestClass {
}
All the feature files kept in src/test/features will run while executing the build file

Ant macrodef compilation task

I have a compiler (and language) I am building that is normally invoked thus:
java -jar nc.jar \
-p some/referenced/package.nc \
-p framework.nc \
source1.ns source2.ns sourceN.ns \
-o output/package.nc
I'd like to include a task in my ANT build file that invokes the compiler to compile the standard library and all test cases, but specifying each separate compiler invocation as a <java> task is painful:
<target name="framework" depends="compiler" description="Build the n framework">
<!-- More compile steps -->
<java jar="nc.jar" fork="true">
<arg value="-p"/>
<arg path="../nframework/build/n.core.nc"/>
<arg path="../nframework/n/debug/DebugPrint.ns"/>
<arg path="../nframework/n/debug/Trace.ns"/>
<arg value="-o"/>
<arg path="../nframework/build/n.debug.nc"/>
</java>
<!-- More compile steps -->
</target>
I would like to create an ANT task of some sort that can simplify this into something like:
<target name="framework" depends="compiler" description="Build the n framework">
<!-- More compile steps -->
<nc output="../nframework/build/n.debug.nc">
<link-package path="../nframework/build/n.core.nc"/>
<src>
<fileset dir="../nframework/n/debug" includes="**/*.ns"/>
</src>
</nc>
<!-- More compile steps -->
</target>
To this end, I tried macrodef:
<macrodef name="nc">
<attribute name="output"/>
<element name="link-package"/>
<element name="src"/>
<sequential>
<java jar="nc.jar" fork="true">
<arg value="-p"/>
<!-- This doesn't do what I want -->
<link-package/>
<!-- Neither does this -->
<src/>
<arg value="-o"/>
<arg path="#{output}"/>
</java>
</sequential>
</macrodef>
I've tried several variations on the above, but each errors out with something like:
/home/jwarner/code/nlang/nc/build.xml:55: java doesn't support nested "fileset" element.
Is there a way to do this without extending ANT itself? Alternatively, would it be fairly easy to add an ant task to my compiler? I'm not terribly picky about the syntax of the final <nc> task.
I have had a similar problem in the past where the out-of-the-box Ant tasks didn't quite do what I wanted them to do. I found that it was very easy to write my own Ant task.
The documentation is concise but does a good job of explaining what you need to do.
http://ant.apache.org/manual/develop.html#writingowntask

Categories

Resources