Change working directory in ant junit task - java

I have a ant file that runs JUnits tests. These tests rely on a relative path to certain configuration files. I've tried to set the working directory for the batch test, but fail.
I want the working directory to be ${plugins.dir}/${name}
The JUnit part of the ant script:
<junit haltonfailure="no" printsummary="on" fork="true" showoutput="true" dir="${plugins.dir}/${name}">
<jvmarg value="-Duser.dir=${plugins.dir}/${name}"/>
<classpath>
<path refid="project.classpath"/>
<pathelement location="${plugins.dir}/${dependency}/#dot/"/>
<pathelement location="${plugins.dir}/${name}/" />
</classpath>
<formatter type="xml" />
<sysproperty key="basedir" value="${plugins.dir}/${name}"/>
<sysproperty key="dir" value="${plugins.dir}/${name}"/>
<batchtest todir="${junit.output}">
<fileset dir="${dir}">
<include name="**\*AllTests.class" />
</fileset>
</batchtest>
</junit>
I've googled and searched but the workarounds I've found have been to set the "dir", "sysproperty" or "jvmarg". As you can see I've tried them all :)
Is there a way to print the current dir in the tag? It doesnt support . That would allow me to verify if the dir is actually changed and to what.
One wildcard in this equation is that this is being run in Hudson that starts upp an eclipse process that starts antrunner. This is so we can run both junit and eclipse plugin junit tests. Shouldn't have anything to do with the problem I think.

I think you are right with setting the basedir property (see projects attributes). However, since it is a property of ANT (and not of the JVM) it is READ ONLY!
Does it effect other target if you set the basedir when calling your ant task? See Command Line reference.
ant -Dbasedir=somedir
Alternatively, span a new ant process to call your junit target. See the AntCall task or Ant task. Following examples assumes that the junit target contains your junit task. I used this task for other properties (never needed the basedir property so far.
<antcall target="junit">
<param name="basedir" value="${plugins.dir}/${name}"/>
</antcall>
<ant dir="${plugins.dir}/${name}" target="junit" />

I had the same scenario and in my case I saw that dir="...." is ignored if run in same jvm so I simply added fork='true' and it worked.
Quote from Apache's documentation site "dir - The directory in which to invoke the VM. Ignored if fork is disabled.". More here.

I'm using NetBeans. When I add to Ant properties (from Tools, Options, Java, Ant) work.dir=C:/MyWorkingDir/ it executes ant with the following command and changes the working dir to C:\MyWorkingDir:
ant -f D:\\workspace\\lib\\project -Dfork=true -Djavac.includes=com/myapp/MyTest.java -Dtest.includes=com/myapp/MyTest.java "-Dwork.dir=C:/MyWorkingDir/" test-single

Related

Ant to Gradle: JUnit (4.11) Test conversion

I'm not very familiar with JUnit, but I'm attempting to translate an ant target that makes use of junit into the gradle equivalent. It's not going so hot, since I'm getting some failures on the gradle side -- I'm under the impression that it's due to inputs not being found somehow/somewhere, but I can't confirm, since it's not readily present in the ant target.
Here's the ant script:
<target name="testing">
<junit printsummary="yes" showoutput="yes">
<classpath refid="classpath"/>
<formatter type="xml"/>
<batchtest fork="yes" todir="someOutputLocation">
<fileset dir="${base}/testCode">
<include name="**/included.java"/>
<exclude name="**/excluded.java"/>
</fileset>
</batchtest>
</junit>
</target>
And here's (one of the variations) of my gradle attempt:
task testing(type:Test){
useJUnit()
testClassesDir = file("testCodeCompiled")
include '**/included.class'
exclude '**/excluded.class'
classpath = classpath
}
The two errors I get:
junit.framework.AssertionFailedError
java.lang.NullPointerException
The AssertionFailedError is supposedly caused by multiple JUnit dependencies, which I don't have. I import a local version of only junit-4.11.
I really don't know why it's not working, though I suspect it's due to some of gradle's complexities. I've seen people mention an ant-junit library, which I may try to use to at least replicate the results from within gradle.
EDIT: A thought occurs: I found JUnit within some of gradle's src files. By calling useJUnit(), I may be using that instead? If so, there could be a double dependency after all? Nope. Got rid of useJUnit() and the local jar separately. The former behaved as it did before whereas the latter exploded.
MORE INFO: The cause may likely be that the compiledTestCode is missing several of the directories/data that testCode contains. I probably have to copy over the relevant files. Alternatively, is there a way to make gradle's JUnit use .java files instead of .class?
For those of you wondering, the code is fine. I'm just dumb.

Exclude a file or package from the Clover report using ant build script

I am using a short ant script to generate my Clover coverage report. I want to exclude certain source files from the report. The code is already fully instrumented, it's not feasible for me to exclude the file during instrumentation.
Excerpt of the ant build xml (trimmed):
<project name="Clover Coverage" default="clover.report" basedir="${basedir}">
<target name="clover.report">
<clover-report initstring="${cloverdb}" >
<current outfile="${reportdir}" title="${title}" >
<format type="html"/>
<sourcepath>
<pathelement path="${srcdir1}"/>
</sourcepath>
</current>
</clover-report>
</target>
</project>
I've tried to exclude this using fileset, but when I do this, Clover gives an error message saying that no coverage info could be found. But if I remove the fileset then it works fine.
My attempted fix that doesn't work:
<current outfile="${reportdir}" title="${title}" >
<format type="html"/>
<fileset dir="${srcdir1}">
<exclude name="**/ExcludeThisClass.java"/>
</fileset>
<sourcepath>
<pathelement path="${srcdir1}"/>
</sourcepath>
</current>
Is the exclusion of files from the Clover report not possible?
Please run your Ant with debug logging (ant -d). The <clover-report> task should print more details then. The most probably you instrumented and/or executed your source code not at once but in several build/test sessions.
And it's probable that Clover rejected some of recording files because it found them out of date.
See these knowledge base articles:
https://confluence.atlassian.com/cloverkb/ignoring-coverage-recording-files-300816998.html
https://confluence.atlassian.com/cloverkb/no-coverage-recordings-found-no-report-will-be-generated-611812757.html

Cannot execute <sequential> task with Ant

Since I updated my Java from JDK7u55 to JDK7u60, I am facing an issue while running my build. I am using Ant 1.6.5 on Windows Server 2003 Standard Edition. Below is the Ant task which causes a problem.
<!-- RUN JUNIT TASK -->
<target name="run_junit" description="Runs all JUnit tests in another JVM">
<sequential>
<move file="${MyProject}\bin\myApp.jar" tofile="${MyProject}\bin\myApp_original.jar"/>
<move file="${MyProject}\bin\myApp_test.jar" tofile="${MyProject}\bin\myApp.jar"/>
<exec executable="WinTail" spawn="true">
<arg value="${MyProject}\junit.log"/>
</exec>
<java classname="myProject.test.AllTests"
maxmemory="256m"
fork="true"
output="${MyProject}\junit.log"
dir="${MyProject}\bin"
append="true">
<jvmarg value="-Djdk.lang.Process.allowAmbigousCommands=true"/>
<arg value="${MyProject}\bin"/>
<classpath>
<fileset dir="${MyProject}\bin">
<include name="myApp.jar"/>
</fileset>
<pathelement path="${MyProject}\bin"/>
</classpath>
</java>
<move file="${MyProject}\bin\myApp.jar" tofile="${MyProject}\bin\myApp_test.jar"/>
<move file="${MyProject}\bin\myApp_original.jar" tofile="${MyProject}\bin\myApp.jar"/>
</sequential>
</target>
Once two JARs have been renamed, I start running all the tests using <java> task. The AllTests.java uses seperate thread to run each test. There are some tests which takes bit longer to finish and some actually don't respond. I used to kill them manually by using ProcessExplorer. Once that was done it used to execute next command which renames back the JARs to their original names. It used to work when I was using JDK7u55.
Now since I upgraded Java to JDK7u60, instead of waiting for all the tests to be finished, the last two <move> commands are attempted. This causes the build failure as <move> caanot proceed because the underlying JAR files are being used by the test classes. I get below error:
BUILD FAILED
C:\MyProject\build.xml:579: Unable to delete file C:\MyProject\bin\myApp.jar
I am wondering what has changed in JDK7u60 which caused this behavior. I checked the Release Notes of JDK7u60 but didn't get anything. Can someone please shed some light on this?
Firstly on windows it's not uncommon for files belonging to another process or thread to prevent the deletion of a file.
Secondly what exactly is the purpose of the sequential task in your ANT target? The documentation describes the tasks as follows:
Sequential is a container task - it can contain other Apache Ant
tasks. The nested tasks are simply executed in sequence. Sequential's
primary use is to support the sequential execution of a subset of
tasks within the parallel task
In your case there's no use of parallel...
Why don't you try to remove the sequential task and see if that fixes the problem.

Why does Eclipse ant editor warn me

I am having some trouble (annoyance really) with ANT editor in Eclipse where it is displaying me a warning "Reference build.classpath not found." on the following block:
<target name="generate" depends="..., mvn-depends">
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
<classpath refid="build.classpath" />
</taskdef>
...
</target>
With mvn-depends target looking like this:
<target name="mvn-depends">
<artifact:dependencies pathId="build.classpath">
<pom refid="my.pom" />
</artifact:dependencies>
</target>
The rest of the references to build.classpath in the build file are not throwing any warnings and the and build runs just fine without any errors, so it does not seem to be amounting to much.
Still, ignoring a warning makes me feel sort of dirty every time I have to edit that file. Specifically, not knowing if this is a bug in Eclipse ant build file validation code or a potential problem in the way the build file has been structured, that Eclipse has identified.
If anyone has any ideas on why this warning is being shown and whether it is safe to ignore or maybe even disable from preferences and would care to share that knowledge, I would definitely be grateful for the knowledge.
Edit:
As requested, here is an example of a reference to build.classpath that does not cause any warnings:
<javac deprecation="off" debug="on" source="1.7" target="1.7" encoding="UTF-8"
includeantruntime="false" memoryMaximumSize="512M" fork="true">
<classpath refid="build.classpath" />
</javac>
As the editor could recognize refids and other elements that Ant specifies, I guess that the editor does something similar to Ant's build file parsing process.
That is, parse this ant build file into a Project object, and references in <taskdef> may be checked, while <javac> may not.
Since the build.classpath is set during runtime and it is set by something other than things like <classpath>, Eclipse may not find it.
I don't have strong prove about this. But something can be done to make us know more.
First, copy the <javac> to the same target where the warned
<taskdef> exists, to see if the <javac> gets a warning;
Then, copy the <taskdef> to the same target where the presetdefed
<javac> exists, to see if the <taskdef> still gets a warning;
Third, in target "generate", comment out the <taskdef> part, and
check if the <xjc ... /> call gets a warning.
For the first one, I expect "NO", while for the other two, I expect "YES". Otherwise, my guess is wrong.
And it makes sense that it is just a warning -- things Eclipse can't find in editing time could exist during runtime.
Make sure you have given the path element location correctly as bellow.
<property name="dependencyfinder.home" value="C:/DependencyFinder"/>
<path id="dependencyfinder">
<pathelement location="${dependencyfinder.home}/lib/aaa.jar"/>
</path>
<taskdef classname="com.sun.tools.xjc.XJCTask">
<classpath refid="dependencyfinder"/>
</taskdef>
Note: DependencyFinder has a folder lib and lib has aaa.jar
Please check the bellow link for more information
Click here

Trying to run ANT JUnit target in debug mode in Eclipse

Here is my ANT JUnit target
<target name="test" depends="compile" >
<junit failureProperty="test.failure" >
<jvmarg value="-Xdebug" />
<jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5432" />
<classpath>
<pathelement path="${basedir}\..\Core\lib\junit-4.10.jar"/>
<pathelement path="${basedir}\..\Suggestion\lib\ssce.jar"/>
<pathelement path="C:\Java\javamail-1.4.1\mail.jar"/>
<pathelement path="C:\Java\commons-net-2.0\commons-net-ftp-2.0.jar"/>
<pathelement path="${basedir}\WebContent\WEB-INF\lib\gson-2.2.1.jar"/>
<pathelement path="${tomcatLibs}\servlet-api.jar"/>
</classpath>
<classpath>
<pathelement path="${build}"/>
</classpath>
<formatter type="brief" usefile="false" />
<test name="com.server.junit.ServerTestSuite" />
<test name="com.junit.DictionaryTestSuite" />
<test name="com.util.junit.SuggestionTestSuite" />
</junit>
<fail message="Unit test failed" if="test.failure"/>
</target>
My unit tests pass fine if run through Eclipse but fail if I laund them from ANT.
I want it to stop at my break point in a Unit test.
From documentation I know I need to add these jvmarg but can't get it to stop so I obviously don't have them in the right place.
Also, I don't think I have the port correct but what port should I use? I didn't have to set up any debug port when debugging JUnits through eclipse, it just worked
You need to forget a moment that you can run JUnit tests and ANT targets from within Eclipse. What you want is to debug a Java application that happens to have the main class org.apache.tools.ant.Main and which can be started with ant from the command line.
You have now two options: You can create a launch config that invokes org.apache.tools.ant.Main but that's pretty complicated to set up (you will have to replicate everything that the ant script does at startup).
The other alternative is to configure ant correctly. In your case, the tests run within the ant process but I know no simple way to pass -Xdebug to Ant itself. Therefore, you must run the tests in a new process. Add this to the junit task:
<junit fork="yes" forkmode="once" ...>
Without this, jvmarg parameters will be ignored.
The next step is to create a debug configuration in Eclipse. This article explains this in detail. For you, only the last part right before "Conclusion" is important.
Detailed instructions:
In Eclipse, navigate to Run | Debug.
Select Remote Java Application, on the left column. Click New, on the bottom of the same column.
In the Create configuration screen you'll be prompted to enter some values. Start with a meaningful name. For Project, select the Java project that contains the source code you want to debug. Leave Connection Type in default, i.e. Standard (Socket Attach) . For Host , enter localhost. If you want to debug a remote server, enter its hostname or IP address. For port, enter 5432.
Click Apply.
Make sure your tests is running in debug mode. In the same screen click Debug . Eclipse should automatically take you to the Debug perspective and you should see a stack trace in the Debug view.
If you are not automatically taken to the Debug perspective, select Window | Open Perspective | Other and then click Debug.
Taken from here.

Categories

Resources