Sunday, March 23, 2008

Ant: Simple JUnit Test

This is the final part of the so called Ant series I have been writing in this blog, about Apache Ant. This is an extremely brief tutorial on JUnit. The goal is to test Ant and JUnit framework integration .

The previous posts on Ant can be found here:

  1. Apache Ant
  2. Ant: Installation
  3. Ant: Hello World in Ant
  4. Ant: Java Hello World in Ant
  5. Ant: Java Swing Hello World in Ant

Prerequisites

In order to do this we will need to have a Java compiler, Ant, and JUnit installed.

Java Classes

First, we need a Java class to test. I had to make this example too simple according to my Java knowledge. This lacks any Java Docs or comments, but it's a pretty simple bit of code. :)

public class Math {
static public int add(int a, int b) {
return a + b;
}

static public int multiply ( int a, int b) {
return a * b;
}
}

Then to test this code, we need a second Java class that will

  1. import junit.framework.*
  2. extend TestCase

There are two important points to note in the sample. First, the routine is named testAdd. This convention tells that the routine is supposed to be a test and that it's targeting the add functionality. Here's the matching example.

import junit.framework.*;

public class TestMath extends TestCase {

protected void setUp() {
// put common setup code in here
}

protected void tearDown() {
// put common cleanup code in here



}

public void testAdd() {
int num1 = 3;
int num2 = 2;
int total = 5;
int sum = 0;
sum = Math.add(num1, num2);
assertEquals(sum, total);
}

public void testMulitply() {



int num1 = 3;
int num2 = 7;
int total = 21;
int sum = 0;
sum = Math.multiply(num1, num2);
assertEquals("Problem with multiply", sum, total);

num1 = 5;
num2 = 4;



total = 20;
sum = Math.multiply(num1, num2);
assertEquals("Problem with multiply", sum, total);
}
}

And both these files could be kept in a sub-folder named src

Ant Script

The last step is how to run your JUnit tests using Ant. Create a file called build.xml and place it in the main folder. The core of this scripts is the following, which does the JUnit tests.

<junit printsummary="yes" haltonfailure="yes" showoutput="yes" >
<classpath>
<pathelement path="${build}"/>
<fileset dir="lib">



<include name="**/*.jar"/>
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="${src}">



<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>

The file I used is this one. (The inner details of this scripts are Ant Scripting and XML, which is not explained in this document, but really straightforward to understand)

Output

Run ant test in command line:

[root@nimal junit-sample]# ant test
Buildfile: build.xml

init:

compile:
[javac] Compiling 2 source files to /opt/junit-sample/bin

run-tests:
[junit] Running TestMath

[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.321 sec

test:
[junitreport] Processing /opt/junit-sample/reports/TESTS-TestSuites.xml to /tmp/null1227203872
[junitreport] Loading stylesheet jar:file:/usr/local/ant/lib/ant-junit.jar!/org/apache/tools/ant



/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 1714ms
[junitreport] Deleting: /tmp/null1227203872

BUILD SUCCESSFUL
Total time: 6 seconds

Ant will also do nice things like create nice HTML reports! I've linked to a simple Ant script that will compile all the code in your "src" folder, run all the tests named "test*" and then create an HTML report in your "reports\html" folder.

I've linked to a zip file (ant-junit-sample.zip) that contains the build.xml file, the source code and the test class. (If you try to run it and get errors about JUnit not being found, remember to add that junit.jar to your Ant lib folder.)

Attachments


Thursday, March 6, 2008

Ant: Java Swing Hello World in Ant

This is the fourth part of the so called Ant series I have been writing in this blog, about Apache Ant. This post is mainly about building and running a Java Swing program using Ant.

The previous posts on Ant can be found here:

   1. Apache Ant
   2. Ant: Installation
   3. Ant: Hello World in Ant

   4. Ant: Java Hello World in Ant

Java Swing Program

Create the following simple swing program and save it as HelloWorldSwing.java in a sub folder src:

import javax.swing.JFrame;
import javax.swing.JLabel;

public class HelloWorldSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("HelloWorldSwing"); final JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true); }
}

Ant Script

Use a text editor to create a file called build.xml and place it in the main folder:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Run Test" default="run" basedir=".">

<target name="clean"> <delete dir="build"/>
</target>

<target name="compile" depends="clean">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/> </target>

<target name="jar" depends="compile">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorldSwing.jar" basedir="build/classes"> <manifest>
<attribute name="Main-Class" value="HelloWorldSwing"/>
</manifest>
</jar>
</target>

<target name="run" depends="jar"> <java jar="build/jar/HelloWorldSwing.jar" fork="true"/>
</target>

</project>

Outputs and Problems

When we run this is text mode it, it compiles but fails to run. But when in GUI it runs.

[root@sanjaya-vm ant-swing]# ant
Buildfile: build.xml

clean:
[delete] Deleting directory /opt/ant-swing/build

compile:
[mkdir] Created dir: /opt/ant-swing/build/classes
[javac] Warning: HelloWorldSwing.java modified in the future.
[javac] Compiling 1 source file to /opt/ant-swing/build/classes

jar:
[mkdir] Created dir: /opt/ant-swing/build/jar
[jar] Building jar: /opt/ant-swing/build/jar/HelloWorldSwing.jar

run:
[java] Exception in thread "main" java.awt.HeadlessException:
[java] No X11 DISPLAY variable was set, but this program performed an operation which requires it.
[java] at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
...
...
 [java] at java.awt.Window.<init>(Window.java:406)
[java] at java.awt.Frame.<init>(Frame.java:402)
[java] at javax.swing.JFrame.<init>(JFrame.java:207)
[java] at HelloWorldSwing.main(Unknown Source) [java] Java Result: 1

BUILD SUCCESSFUL
Total time: 3 seconds
This is one another basic level post and I strongly advice you to refer to any more advanced document, if you are going to do this for any production system. :)

Feedback and questions are welcome via comment or you can email me at talkout AT SPAMFREE gmail DOT com