Excluding the setup method of a junit test using ant - ant

Hi I am new to junit and ant.
I want to prevent the JUnit #Before* annotated methods from execution.
Is that possible with Ant's "exclude"?
Are other possibilities to prevent these annotated methods from execution?
thx

No you can't do that with Ant's exclude.
You could use Ant to change the annotation in all files.
http://ant.apache.org/manual/Tasks/replace.html
<replace dir="${tests}" token="#Before" value="#BeforeREMOVED">
<include name="**/*.java"/>
<exclude name="**/*Test*"/>
</replace>
You can adjust/omit the include and exclude tags as necessary to match the files you want.
You can alway change the files back
<replace dir="${tests}" token="#BeforeREMOVED" value="#Before">
<include name="**/*.java"/>
<exclude name="**/*Test*"/>
</replace>
Otherwise you can extend the tests with a new class that overrides the methods. Seems like it might be a lot of work though.

Related

Why does ANT update the contents of a fileset after it was created, and can I override this?

I think this may be easiest explained by an example, so here goes:
<target name="test">
<fileset id="fileset" dir="target">
<include name="*"/>
</fileset>
<echo>${toString:fileset}</echo>
<touch file="target/test"/>
<echo>${toString:fileset}</echo>
</target>
Outputs:
test:
[echo]
[touch] Creating target/test
[echo] test
What I ideally want is to have the fileset stay the same so I can have a before/after set (in order to get a changed set using <difference>, so if you know of a way to skip right to that...).
I've tried using <filelist> instead, but I can't get this correctly populated and compared in the <difference> task (they're also hard to debug since I can't seem to output their contents). I also tried using <modified/> to select files in the fileset, but it doesn't seem to work at all and always returns nothing.
Even if there is an alternative approach I would appreciate a better understanding of what ANT is doing in the example above and why.
The path selector is evaluated on the fly. When a file is added, it will reflect in the set when you use it.
You may able to evaluate and keep it in variable using pathconvert. Then this can be converted back to filest using pathtofilest
A fileset is something like a selector. It's a set of "instructions" (inclusions, exclusions, patterns) allowing to get a set of files.
Each time you actually do something with the fileset (like printing the files it "references"), the actual set of files is computed based on the "instructions" contained in the fileset.
As Jayan pointed out it might be worth posting the final outcome as an answer, so here's a simplified version with the key parts:
<fileset id="files" dir="${target.dir}"/>
<pathconvert property="before.files" pathsep=",">
<fileset refid="files"/>
</pathconvert>
<!-- Other Ant code changes the file-system. -->
<pathconvert property="after.files" pathsep=",">
<fileset refid="files"/>
</pathconvert>
<filelist id="before.files" files="${before.files}"/>
<filelist id="after.files" files="${after.files}"/>
<difference id="changed.files">
<filelist refid="before.files"/>
<filelist refid="after.files"/>
</difference>

Creating an empty placeholder fileset in Ant

So, here's the situation: I have a parent buildfile that defines a compilation task, and I want child buildfiles to optionally be able to add more JARs (which could be wherever) on to the classpath used by that compilation task.
Not all child buildfiles will have these additional dependencies, so I don't want to force them to define the additional dependency fileset. They should just be able to include the parent, and the compile task should just work.
(Obviously there are other required properties that configure the source directory and so on, but they don't enter into this. Also, the actual include/inheritance problem is a good bit more complicated, but hopefully whatever the right thing is for the simple case will work in the complex case too.)
I have something that works: The compile task in the parent buildfile refers to the additional dependency fileset regardless:
<target name="compile" depends="init-additional-dependencies">
<fileset id="global.dependency.fileset" dir="${global.library.directory}">
<include name="**/*.jar"/>
</fileset>
<javac ...>
<classpath>
<!-- should be the same for all buildfiles -->
<fileset refid="global.dependency.fileset"/>
<!-- should be populated by child buildfiles -->
<fileset refid="additional.dependency.fileset"/>
</classpath>
</javac>
</target>
...and the parent buildfile also has a task that creates this fileset, empty, so that javac doesn't blow up. However, the way I'm creating the empty fileset is dopey:
<target name="init-additional-dependencies">
<!-- override me! -->
<fileset id="additional.dependency.fileset" dir=".">
<include name="placeholder.does.not.exist.so.fileset.is.empty"/>
</fileset>
</target>
This works, but seems dumb, and it's hard to believe there isn't a better approach. What is that better approach?
I don't think there's been much discussion about this, so no 'convention' as such exists. The way that fileset works though, exclusions 'trump' inclusions, thus
<fileset refid="additional.dependency.fileset" dir="." excludes="**" />
should always be empty. That seems slightly preferable to both your placeholder file name technique, and the placeholder directory name and erroronmissingdir method.
The problem arises because, by default, there is an implicit include of all files beneath the parent directory of a fileset. Another option - perhaps not of direct use in your case - is to use a filelist instead. Because filelists are constructed from explicitly named files, if you don't name any, they are empty.
<filelist id="additional.dependency.filelist" />
By generalising, you can mix filesets and filelists, if you modify your classpath to use resources:
<filelist id="additional.dependency.resources" />
...
<classpath>
<!-- should be the same for all buildfiles -->
<fileset refid="global.dependency.fileset"/>
<!-- should be populated by child buildfiles -->
<resources refid="additional.dependency.resources"/>
</classpath>
the reference additional.dependency.resources can be either a fileset or a filelist (including the empty filelist), or any other file-based resource collection.
In the parent build file add:
<fileset id="additional.dependency.fileset" erroronmissingdir="false" dir="noop" />
For children that require additional artifacts to be added, define the fileset in the child build file:
<fileset id="additional.dependency.fileset" dir="..." includes="..." />

Is there a way to skip a specific test case via the command line?

I am using apache ant, and do not want to change my source tests, but I'd like the option to turn one off. I know that it is possible to only RUN tests you choose (-Dtestcase=whatever), but I'm not sure if you can exclude one.
You might use ant -DexcludedTest=SomeExcludedTest and have your batch test configured like this:
<batchtest ...>
<!-- define the excludedTest property to an unexisting test name in case
nothing is passed as a system property -->
<property name="excludedTest" value="THIS_TEST_NAME_DOES_NOT_EXIST"/>
<fileset dir="${src.tests}">
<include name="**/*Test.java"/>
<exclude name="**/${excludedTest}.java"/>
</fileset>
</batchtest>

Ant exclude file based on it's content

Is there any way to exclude files from an ant fileset based on the file content?
We do have test servers where code files are mixed up with files that have been generated by a CMS.
Usually, the files are placed in different folders, but there is a risk that real code files are in the middle of generated code.
The only way to differentiate generated files is to open the files and look at it's content. If the file contains a keyword, it should be excluded.
Does anyone know a way to perform this with Ant?
From the answer provided by Preet Sangha, Ishould use a filterchain. However, I'm missing a step here.
Let's say I load a text file of exclusions to be performed:
<loadfile property="exclusions" srcFile="exclusions.txt" />
But I don't know how to integrate it into my current copy task:
<copy todir="${test.dir}">
<fileset dir="${src.dir}">
</fileset>
</copy>
I tried to add the following exclude to the fileset but it does not do anything:
<exclude name="${exclusions}"/>
I'm sure I'm missing a simple step...
Have a look at the not and contains selectors.
The not selector contains an example of pretty much exactly what you're trying to do.
<copy todir="${test.dir}">
<fileset dir="${src.dir}">
<not>
<contains text="your-keyword-here"/>
</not>
</fileset>
</copy>
There's also the containsregexp selector which might be useful if your criteria for exclusion is more complicated.
There's a load more selectors you can use to refine your selection if needed.
I don't know ant but reading the docs....
Can you build a files list using a filterchain, and put this into the excludefiles of a fileset?
or
perhaps create a fileset with a filterchain that uses a filterreader and linecontainsregexp

Filter a fileset referenced using a refid

I have a fileset (which is returned from the Maven Ant task), and it contains all the jars I need to repack. This fileset is referenced by a refid. I only want to include our own jars, so I would like to filter that. But Ant filesets don't support any further attributes or nested tags if a refid is used.
For example, if the fileset is:
org.foo.1.jar
org.foo.2.jar
log4j.jar
and I want to have a fileset which contains only
org.foo*.jar
How would I do that?
Try using a restrict resource collection, which you can use like a fileset in any task that uses resource collections to select the groups of files to operate on.
For example, for a fileset returned from your Maven task referenced via an id called dependency.fileset you can declare a restrict resource collection like so:
<restrict id="filtered.dependencies">
<fileset refid="dependency.fileset"/>
<rsel:name name="org.foo*.jar"/>
</restrict>
Note you'll have to declare the resource selector namespace as it isn't part of the built-in Ant namespace:
<project xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">
...
</project>
From here you can reference your restrict resource collection in a similar fashion to how you would reference your fileset. For example, to create backups of your filtered set of files:
<copy todir=".">
<restrict refid="filtered.dependencies"/>
<globmapper from="*" to="*.bak"/>
</copy>
Of course you can inline your restrict resource collection if you so desire:
<copy todir=".">
<restrict>
<fileset refid="dependency.fileset"/>
<rsel:name name="org.foo*.jar"/>
</restrict>
<globmapper from="*" to="*.bak"/>
</copy>
Have a look at the Ant documentation on resource collections for further information.
I think you'll need to write an ant task for that. They're pretty easy to write though.
See http://ant.apache.org/manual/develop.html#writingowntask
In your task, you'll need to call getProject() and ask it to give you the fileset, walk through it, and create a new one.
I 'm using Ant with Ivy. With the help of Ivy it is possible to filter dependencies for retrieval, with the following code in ivy.xml:
<dependency name="Project1" rev="latest.integration" transitive="true" conf="modlibs">
<exclude name="${exclusionRegEx}" matcher="regexp" />
</dependency>
<dependency name="Project2" rev="latest.integration" transitive="false" conf="modules"/>
Maybe a quick look at the Ivy source 'll help?
If you are using a sufficiently recent version of Ant and the JDK, for example, Ant 1.7 and JDK 6, then you can use the optional script task to do what you want. (Earlier versions may also work.) The page I linked to, if you scroll down to the text "The goal is to list the filesizes" then you'll see a sample script that creates a Fileset.
This isn't for the faint of heart, and a custom ant task you write yourself will probably be more flexible. But I wanted to point out the option.

Resources