Saturday, August 28, 2010

JUnit, DateFormat, Calendar and ANT

It is not necessary to test Java's DateFormat class but, as a means of verifying our knowledge, this code does just that.

DateAndTimeFormattingTest.java

import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;
import org.junit.*;
import static org.junit.Assert.*;

public class DateAndTimeFormattingTest {
    DateFormat dateOnly = DateFormat.getDateInstance(DateFormat.MEDIUM);
    DateFormat dateAndTime = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
 
    @Test 
    public void testDateOnly() {
        Calendar cal = new GregorianCalendar(2010, 11, 31);
        Date date = cal.getTime();
        assertEquals("Dec 31, 2010", dateOnly.format(date));
    }
 
    @Test 
    public void testDateAndTime() {
        Calendar cal = new GregorianCalendar(2010, 11, 31);
        Date date = cal.getTime();
        assertEquals("Dec 31, 2010 12:00:00 AM", dateAndTime.format(date));
    }
}

build.xml

<project name="calendar_test" default="test" basedir=".">
    <property name="src.dir"       location="src/main/java"/> 
    <property name="test.src.dir"  location="src/test/java"/>
    <property name="build.dir"     location="build"/>
    <property name="classes.dir"   location="build/classes"/>
    <property name="tests.dir"     location="build/tests"/>
    <property name="reports.dir"   location="build/reports"/>
    <property name="junit.lib"     location="lib/junit/junit-4.8.2.jar"/>
    <path id="classpath">
        <pathelement location="${classes.dir}"/>
    </path>

    <path id="test.classpath">
        <pathelement path="${classpath}"/>
        <pathelement location="${junit.lib}"/>
        <pathelement location="${tests.dir}"/>
    </path>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="init" depends="clean">
        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${tests.dir}"/>
        <mkdir dir="${reports.dir}"/>
    </target>

    <target name="compile" depends="init">
        <javac srcdir="${src.dir}" 
             destdir="${classes.dir}" 
             classpathref="classpath"/>
        <javac srcdir="${test.src.dir}" 
             destdir="${tests.dir}" 
             classpathref="test.classpath"/>
    </target>
  
    <target name="test" depends="compile">
        <junit fork="yes" printsummary="yes">
            <classpath refid="test.classpath"/>
            <formatter type="plain" usefile="false"/>
            <test name="DateAndTimeFormattingTest"/>
        </junit>
    </target>

</project>

Running ANT

[greg:DateAndTimeFormattingTest] ant
Buildfile: build.xml

clean:
   [delete] Deleting directory /Users/greghelton/dev/src/java/DateAndTimeFormattingTest/build

init:
    [mkdir] Created dir: /Users/greghelton/dev/src/java/DateAndTimeFormattingTest/build/classes
    [mkdir] Created dir: /Users/greghelton/dev/src/java/DateAndTimeFormattingTest/build/tests
    [mkdir] Created dir: /Users/greghelton/dev/src/java/DateAndTimeFormattingTest/build/reports

compile:
    [javac] Compiling 1 source file to /Users/greghelton/dev/src/java/DateAndTimeFormattingTest/build/tests

test:
    [junit] Running DateAndTimeFormattingTest
    [junit] Testsuite: DateAndTimeFormattingTest
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.023 sec
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.023 sec
    [junit] 
    [junit] Testcase: testDateOnly took 0.009 sec
    [junit] Testcase: testDateAndTime took 0 sec

BUILD SUCCESSFUL
Total time: 1 second
[greg:DateAndTimeFormattingTest]