Jnuit and Code files in different locations - unit-testing

I am trying to run ANT script to execute a simple junit test case.
Now I have a requirement.
The executable file with my sample product will be in one location
My junit test cases will be in another location
Still I have to sync up and get the test case executed
Is there a way where I can ask my Junit to look another location for source.

If your junit test cases live in another project then you will need to create a seperate build.xml for that project setup for unit tests. Make sure this new build.xml references your sample product JAR as a dependency.

Yes this is possible.
Compile your source files as usual:
<javac srcdir="../${srcDir}/src" destdir="dist/src">
Compile your test files, including the compiled source in your classpath:
<javac srcdir="../${testDir}/test" destdir="dist/test">
<classpath location="dist/src"/>
</javac>
Then include both these in the JUnit classpath:
<path id="junit.path">
<pathelement location="dist/src" />
<pathelement location="dist/test" />
<fileset file="../Libraries/junit*.jar" />
<fileset file="../Libraries/ant-junit*.jar" />
<fileset dir="externalLibraries">
<include name="**/*.jar" />
</fileset>
</path>
<junit fork="yes" forkmode="once">
<classpath refid="junit.path" />
<batchtest fork="yes" todir="${dist}/testresults">
<fileset dir="dist/test">
<include name="**/*Test.class" />
</fileset>
</batchtest>
</junit>
See also Ant documentation

Related

Rename a file before Copy Task in ant build

I am new to ant build files.
Currently I get a list of files for build as:
a.cls
b.cls
c.cls
but in my local I have to run build on files, in the same directory:
a-meta.cls
b-meta.cls
c-meta.cls
Here meta keyword stays consistent. And I am using the following build.xml file. I am not sure how can I rename filename before actually copying them. I tried replace, mapper and other antlib tasks. But not helpful.
<project name="test" default="compile">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<loadfile property="file" srcfile="filesToMove.txt"/> <!-- these are the list of files, i mentioned earlier -->
<target name="compile">
<echo>${file}</echo> <!-- here i have to rename file name to include -meta -->
<copy file="./classes/${file}" tofile="./src/classes/${file}" overwrite="true"/>
</target>
</project>
How to rename the files before moving them.
The solution to it was replacing the .cls to find only the name and then append the -meta.html. As follows (some portion is changed compared to previous version in the question)
<project name="test" default="compile">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<loadfile property="file" srcfile="filesToMove.txt"/> <!-- these are the list of files, i mentioned earlier -->
<target name="compile">
<echo>${file}</echo> <!-- here i have to rename file name to include -meta -->
<copy file="./classes/${file}" tofile="./src/classes/${file}" overwrite="true"/>
<for param="file">
<path>
<fileset dir="./" includes="*.cls"/>
</path>
<sequential>
<basename file="#{file}" property="#{file}" suffix=".md"/>
<echo message=" ${#{file}}"/>
<copy file="${#{file}}-meta.cls" toDir="test"/>
</sequential>
</for>
</target>
</project>

Ant copy files from dynamic directories further down

I am trying to copy files from some { dynamic } directories X into another location.
Some of the destination directories should be the ( same and static ) as in the source path.
The initial layout looks like this under new directory A/D.
(A)
|_(B)
|_(C)
|_{X}
|_{Y.js}
The final layout should look like this:
A
|_D
|_B
|_C
|_{X}
|_{Y.js}
I tried using a regexmapper, but I am getting messages that entries are skipped because copy doesn't know how to handle it.
<copy toDir="A/D/" verbose="true">
<fileset dir="A/" casesensitive="yes">
<include name="B/C/**/*.js"/>
</fileset>
<regexpmapper from="B/C/([^/]+)/(.+\.js)" to="B/C/\1/\2"/>
</copy>
Edit: I am currently trying on Windows but want this to be portable.
There is no need to use regexmapper and here is the solution I came up with:
<copy toDir="A/D" verbose="true">
<fileset dir="A" casesensitive="no" overwrite="true">
<include name="B/C/**/*.js"/>
</fileset>
</copy>

Remove version number from jar in ant task

I'm trying to copy all JARs from one directory to another. During this process I want to remove version numbers at the end of the file names. (E.g. my-jar-1.2.3.jar to my-jar.jar)
I tried to wrap my head around mapper but I can't find a regexp to get that to work. I've tried this:
<copy todir="lib" flatten="true">
<mapper type="regexp" from="(.*)-[^.]*(\.jar)" to="\1\2" />
<fileset dir="my.files.dir">
<include name="**/*.jar" />
<type type="file" />
</fileset>
</copy>
here is a better version of the below answer:
<project>
<mkdir dir="lib"/>
<copy todir="lib"
verbose="true">
<fileset dir="jars/">
<include name="*.jar"/>
</fileset>
<mapper type="regexp"
from="^(.+?)-[0-9]+.*\.jar$"
to="\1.jar"/>
</copy>
</project>
to handle "That last one is a trouble maker"
This should work properly:
<copy todir="lib" flatten="true">
<mapper type="regexp" from="(.*)-[^-]*(\.jar)" to="\1\2" />
<fileset dir="my.files.dir">
<include name="**/*.jar" />
<type type="file" />
</fileset>
</copy>
THere's a mapper in the maven-ant-task-lib which does just that.
Try this...
<project>
<mkdir dir="lib"/>
<copy todir="lib"
verbose="true">
<fileset dir="jars/">
<include name="*.jar"/>
</fileset>
<mapper type="regexp"
from="^(.+?)-[0-9].*$"
to="\1.jar"/>
</copy>
</project>
In a regex mapper, the from parameter must match the entire name. I use the +? non-greedy pattern matcher. This matches the pattern of . which means any character but not greedily. Normally, this would match the entire line. However, I'm capturing up to the first time a dash followed by a number is found.
The problem happens if the jar has no version number, or it's starts with a non-numeric value. I can successfully, do these:
foo-2.3.2.jar
foo-2r1.jar
But not these:
foo-alpha.jar
foo.jar
So, I tweaked the pattern a bit:
<project>
<mkdir dir="lib"/>
<copy todir="lib"
verbose="true">
<fileset dir="jars/">
<include name="*.jar"/>
</fileset>
<mapper type="regexp"
from="^(.+?)-[0-9]*.*\.jar$"
to="\1.jar"/>
</copy>
</project>
$ ant
[mkdir] Created dir: lib
[copy] Copying 5 files to lib
[copy] Copying jars/bar-3.4.5.jar to lib/bar.jar
[copy] Copying biff-86.4.2.jar to lib/biff.jar
[copy] Copying jars/boff-2.31.2.jar to lib/boff.jar
[copy] Copying jars/foo-1.2.3.jar tolib/foo.jar
[copy] Copying jars/foo-bar-3.3.2.3.jar to lib/foo.jar
That last one is a trouble maker...
It might be worth doing this in two copies: One to take care if a version number is found, and a second to take care of jars without version numbers.

Nant wont recognize namespace.Properties

I'm trying to use nant to build a solution for a c# project however I am having an issue specifying resources in my build file. I get the error code:
CS0234: The type or namespace name 'Properties' does not exists in the namespace 'Darkside'. How can I modify my build file to remove the error?
Here is one of the snippets of code in my source files that is causing the issue:
Image enemyImage = DarkSide.Properties.Resources.DarkSide_Asteroid_Medium_Gray;
where Darkside is the namespace of the project, and the rest accesses the resources
Here is a snippet of my build file:
<target name="build" depends="init" description="compiles the source code">
<property name="build.dir" value="${project::get-base-directory()}/${project::get-name()}/bin/release/${project.version}${basedir.suffix}"/>
<mkdir dir="${build.dir}"/>
<csc target="exe" output="${build.dir}/Darkside.exe" debug="${build.debug}">
<sources>
<include name="Darkside/*.cs"/>
</sources>
<resources >
<include name="${project::get-base-directory()}/${project::get-name()}/Properties/**" />
</resources>
</csc>
</target>
You should probably define the prefix for the resources tag (with dynamicprefix="true").
<target name="build" depends="init" description="compiles the source code">
<property name="build.dir" value="${project::get-base-directory()}/${project::get-name()}/bin/release/${project.version}${basedir.suffix}"/>
<mkdir dir="${build.dir}"/>
<csc target="exe" output="${build.dir}/Darkside.exe" debug="${build.debug}">
<sources>
<include name="Darkside/*.cs"/>
</sources>
<resources dynamicprefix="true" prefix="DarkSide">
<include name="${project::get-base-directory()}/${project::get-name()}/Properties/**" />
</resources>
</csc>
</target>
Indeed the csc task's documentation states:
Note: In order to have <csc> task generate manifest resource names that match those generated by Microsoft Visual Studio.NET, the value of the prefix attribute of the <resources> element should match the "Default Namespace" of the C# project, and the value of the dynamicprefix attribute should be set to "true".

Javac exclusion of package in ANT build.xml

I'm trying to compile all my packages except two of them, here is what my javac in build.xml looks like
<javac srcdir="${src}" destdir="${output}" debug="${debug}" failonerror="yes" >
<exclude name="com/abc/uyyy/**"/>
<exclude name="com/abc/zzz/**"/>
<include name="com/abc/zzz/Text.java"/>
<patternset refid="excluded.from.compilation.abc"/>
<classpath refid="abc.module.classpath"/>
</javac>
But all the files in package are compiled :(.
I've read the documentation (http://ant.apache.org/manual/Tasks/javac.html), but still no success, any help?
NOTE: After the Text.java is compiled, I need to build the WSDL file and then build the excluded packages. I'm using Metro to write and build my WS.
Is it not possbile to compile all the class files into one directory, then use the copy task like below to only copy the ones you want for WSDL?
<target name="copy_all_class_files">
<copy todir="${output}">
<fileset dir="classes">
<include name="com/abc/zzz/Text.class"/>
<exclude name="com/abc/uyyy/**"/>
<exclude name="com/abc/zzz/**"/>
</fileset>
</copy>
</target>
Ok here is what I did,I wrote a new target to compile only the WS file and then generate the classes, it works fine :)
<target name="compile-ws-server">
<javac srcdir="${src}" destdir="${output}"
debug="${debug}" failonerror="yes">
<include name="com/abc/xxx/Text.java"/>
<exclude name="${src}/abc/xxx/**"/>
<classpath refid="abc.module.classpath"/>
</javac>
</target>
All Above is not proper way ...
I have done like
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" excludes="com/company/example/test/**" />
</target>
Here we should avoid placing ${src} and start from inside src folder.