Ant to Gradle: JUnit (4.11) Test conversion - java

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.

Related

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

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

FindBugs refuses to find bcel jar on classpath

For the life of me, I am trying to get FindBugs (2.0.1) to run as part of my command-line Ant build. I downloaded the FindBugs JAR and extracted it to /home/myuser/java/repo/umd/findbugs/2.0.1/findbugs-2.0.1:
As you can see in the screenshot, under /home/myuser/java/repo/umd/findbugs/2.0.1/findbugs-2.0.1/lib there is a JAR called bcel-1.0.jar, and if you open it, you can see that I have drilled down to a class called org.apache.bcel.classfile.ClassFormatException. Hold that thought.
I then copied /home/myuser/java/repo/umd/findbugs/2.0.1/findbugs-2.0.1/lib/findbugs-ant.jar to ${env.ANT_HOME}/lib to make it accessible to the version of Ant that is ran from the command-line (instead of the Ant instance that comes built-into Eclipse).
My project directory structure is as follows:
/home/myuser/sandbox/workbench/eclipse/workspace/myapp/
src/
main/
java/
test/
java/
build/
build.xml
build.properties
gen/
bin/
main/ --> where all main Java class files compiled to
test/ --> where all test Java class files compiled to
audits/
qual/
staging/
Inside build.xml:
<project name="myapp-build" basedir=".." default="package"
xmlns:fb="antlib:edu.umd.cs.findbugs">
<path id="findbugs.source.path">
<fileset dir="src/main/java">
<include name="**.*java"/>
</fileset>
<fileset dir="src/main/test">
<include name="**.*java"/>
</fileset>
</path>
<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask"
uri="antlib:edu.umd.cs.findbugs"/>
<!-- Other Ant target omitted for brevity. -->
<target name="run-findbugs">
<!-- Create a temp JAR that FindBugs can use for analysis. -->
<property name="fb.tmp.jar" value="gen/staging/${ant.project.name}-findbugs-temp.jar"/>
<echo message="Creating ${fb.tmp.jar} for FindBugs."/>
<jar destfile="gen/staging/${ant.project.name}-findbugs-temp.jar">
<fileset dir="gen/bin/main" includes="**/*.class"/>
<fileset dir="gen/bin/test" includes="**/*.class"/>
</jar>
<echo message="Conducting code quality tests with FindBugs."/>
<fb:findbugs home="/home/myuser/java/repo/umd/findbugs/2.0.1/findbugs-2.0.1"
output="html" outputFile="gen/audits/qual/findbugs.html" stylesheet="fancy-hist.xsl" failOnError="true">
<sourcePath refid="findbugs.source.path"/>
<class location="${fb.tmp.jar}"/>
</fb:findbugs>
</target>
<target name="echoMsg" depends="run-findbugs">
<echo message="The build is still alive!!!"/>
</target>
</project>
But when I run ant -buildfile build.xml echoMsg from the command-line, I get an error in FindBugs:
run-findbugs:
[echo] Creating gen/staging/myapp-build-findbugs-temp.jar for FindBugs.
[jar] Building jar: /home/myuser/sandbox/workbench/eclipse/workspace/myapp/gen/staging/myapp-build-findbugs-temp.jar
[echo] Conducting code quality tests with FindBugs.
[fb:findbugs] Executing findbugs from ant task
[fb:findbugs] Running FindBugs...
[fb:findbugs] Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/bcel/classfile/ClassFormatException
[fb:findbugs] Caused by: java.lang.ClassNotFoundException: org.apache.bcel.classfile.ClassFormatException
[fb:findbugs] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[fb:findbugs] at java.security.AccessController.doPrivileged(Native Method)
[fb:findbugs] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[fb:findbugs] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[fb:findbugs] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[fb:findbugs] at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[fb:findbugs] Could not find the main class: edu.umd.cs.findbugs.FindBugs2. Program will exit.
[fb:findbugs] Java Result: 1
[fb:findbugs] Output saved to gen/audits/qual/findbugs.html
echoMsg:
[echo] The build is still alive!!!
Here's what has me amazed:
Even with failOnError="true", FindBugs is not halting the build even when this runtime exception is encountered
The last piece of output "Output saved to gen/audits/qual/findbugs.html" is a lie! There is nothing in gen/audits/qual!
The bcel-1.0.jar is absolutely under FindBugs home, just like every other JAR in the lib/ directory.
Please note: the findbugs-ant.jar is definitely copied to ANT_HOME/lib; otherwise I would be getting a failed build complaining that it couldn't find the Ant tasks. As a sanity check, I went ahead and did this (I deleted the findbugs-ant.jar from ANT_HOME/lib and got a failed build). This build doesn't fail (it succeeds!). It just doesn't run findbugs.
Can anyone spot what is going on here? Thanks in advance!
You can debug where BCEL is being loaded from using the -verbose:class argument to the jvm.
To pass this argument to the jvm running findbugs, use the jvmargs flag on the find bugs plugin
jvmargs
Optional attribute. It specifies any arguments that should be
passed to the Java virtual machine used to run FindBugs. You may need
to use this attribute to specify flags to increase the amount of
memory the JVM may use if you are analyzing a very large program.
How did you populate the find bugs lib jar? When I download findbugs.zip, I get a lib directory which looks very different than what you show. In particular, mine contains a bcel with a version of 5.3, not 1.0 as you show.
Funny thing because I am using the same version of Findbugs and the jar file is named bcel.jar not bcel-1.0.jar. I am also running Findbugs from an Ant script. As crazy as it might sound, try to download the Findbugs once again, unpack it in the place of your current one and run your script once again.
My guess is that you actually have BCEL in the classpath twice. And the file is being loaded from the jar outside the FindBugs library. Then, when FindBugs tries to load the jar, it finds the BCEL in the FindBugs library and cannot load it, because it's already loaded.
The solution would be to find where else BCEL exists in the classpath and remove it.
You might have to define a AuxClasspath to include the classpath that your <javac> task used when compiling your class files.
You don't show how the compile took place, so I am assuming your created a compile.classapath classpath reference:
<javac destdir="gen/bin/main"
srcdir="src/main/java"
classpathref="compile.classpath"/>
<fb:findbugs home="/home/myuser/java/repo/umd/findbugs/2.0.1/findbugs-2.0.1"
output="html" outputFile="gen/audits/qual/findbugs.html" stylesheet="fancy-hist.xsl" failOnError="true">
<auxClasspath refid="compile.classpath"/>
<sourcePath refid="findbugs.source.path"/>
<class location="${fb.tmp.jar}"/>
</fb:findbugs>
I don't see from your Ant script that bcel is landing on any classpath that the findbugs task would be able to load it from. You might want to try making your taskdef explicitly include everything findbugs needs.
<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask"
uri="antlib:edu.umd.cs.findbugs">
<classpath>
<fileset dir="/home/java/repo/umd/findbugs/2.0.1/findbugs-2.0.1">
<include name="*.jar"/>
</fileset>
</classpath>
</taskdef>
most classpath problems you can debug with tattle. it will report you all jars in your project. all duplicated classes etc. saved me a lot of time.
there is also ant task ready: http://docs.jboss.org/tattletale/userguide/1.2/en-US/html/ant.html

Ant javac returning message "A.java added as A.class doesn't exist"

I'm writing an ANT task that is behaving very oddly. I've tried a variety of google searches with no clear answer.
I have a compile target in ANT that calls the javac command:
<target name="compile">
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}:${unittest.src.dir}" destdir="${classes.dir}">
<classpath refid="classpath" />
<exclude name="**/plugins/**" />
<exclude name="**/outbound/**" />
</javac>
</target>
That generates no errors, but when I run ant with the -verbose and -debug tags I get the following in my logs:
[javac] package/MyClass.java added as package/MyClass.class doesn't exist
So it's not erroring out, but it is giving me this odd result (with the "doesn't exist" appended at the end). Obviously this causes later targets to fail when it tries to run the classes (in this case, my junit target).
Web searches seem to indicate that I'm either missing a java_home env variable or that there is a versioning issue somewhere along the line (this error tended to occur when the source attribute on javac was set, but I removed that and am still getting the odd result). I believe we've got those possibilities covered.
Any thoughts or advice on this would be wonderful. Thanks.
I don't know too much about ant, but I would almost guess this error message is a red herring. I think it's just reporting that MyClass.java was added to the list of files to compile, because the compiled version of the class was not found on the class path. This seems to be supported by the fact that the task does not fail.
What are the later errors you are getting?

Change working directory in ant junit task

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

Categories

Resources