package org.springunit.examples.constructor.junit.v6;
import java.util.HashMap;
import java.util.Map;
import org.springunit.examples.CompositeDate;
import org.springunit.examples.InvalidDateException;
import junit.framework.TestCase;
/**
* Unifies algorithms for valid and invalid inputs
* in a single method.
*
* @author Ted.Velkoff
*
*/
public class CompositeDateConstructorTest extends TestCase {
protected Object getObject(String name) {
return this.data.get(getName()).get(name);
}
protected void setUp() throws Exception {
this.data = new HashMap<String, Map<String, Object>>();
Map<String, Object> testData = new HashMap<String, Object>();
testData.put("day", 1);
testData.put("month", 1);
testData.put("year", 2006);
testData.put("expectedDay", 1);
testData.put("expectedMonth", 1);
testData.put("expectedYear", 2006);
this.data.put("testJan01", testData);
testData = new HashMap<String, Object>();
testData.put("day", 31);
testData.put("month", 12);
testData.put("year", 2006);
testData.put("expectedDay", 31);
testData.put("expectedMonth", 12);
testData.put("expectedYear", 2006);
this.data.put("testDec31", testData);
testData = new HashMap<String, Object>();
testData.put("day", 29);
testData.put("month", 2);
testData.put("year", 2000);
testData.put("expectedDay", 29);
testData.put("expectedMonth", 2);
testData.put("expectedYear", 2000);
this.data.put("testFeb29Leap2000", testData);
testData = new HashMap<String, Object>();
testData.put("day", 29);
testData.put("month", 2);
testData.put("year", 2004);
testData.put("expectedDay", 29);
testData.put("expectedMonth", 2);
testData.put("expectedYear", 2004);
this.data.put("testFeb29Leap2004", testData);
testData = new HashMap<String, Object>();
testData.put("day", 31);
testData.put("month", 4);
testData.put("year", 2006);
this.data.put("testApr31", testData);
testData = new HashMap<String, Object>();
testData.put("day", 31);
testData.put("month", 6);
testData.put("year", 2006);
this.data.put("testJun31", testData);
testData = new HashMap<String, Object>();
testData.put("day", 31);
testData.put("month", 9);
testData.put("year", 2006);
this.data.put("testSep31", testData);
testData = new HashMap<String, Object>();
testData.put("day", 31);
testData.put("month", 11);
testData.put("year", 2006);
this.data.put("testNov31", testData);
testData = new HashMap<String, Object>();
testData.put("day", 31);
testData.put("month", 2);
testData.put("year", 2006);
this.data.put("testFeb31", testData);
testData = new HashMap<String, Object>();
testData.put("day", 30);
testData.put("month", 2);
testData.put("year", 2006);
this.data.put("testFeb30", testData);
testData = new HashMap<String, Object>();
testData.put("day", 29);
testData.put("month", 2);
testData.put("year", 2003);
this.data.put("testFeb29NoLeap", testData);
testData = new HashMap<String, Object>();
testData.put("day", 29);
testData.put("month", 2);
testData.put("year", 1900);
this.data.put("testFeb29NoLeap1900", testData);
}
protected void runValid() throws Exception {
Integer day = (Integer)getObject("day");
Integer month = (Integer)getObject("month");
Integer year = (Integer)getObject("year");
Integer expectedDay = (Integer)getObject("expectedDay");
Integer expectedMonth = (Integer)getObject("expectedMonth");
Integer expectedYear = (Integer)getObject("expectedYear");
CompositeDate subject = new CompositeDate(year, month, day);
assertTrue(expectedMonth == subject.getMonth());
assertTrue(expectedDay == subject.getDay());
assertTrue(expectedYear == subject.getYear());
}
public void testJan01() throws Exception {
runValid();
}
public void testDec31() throws Exception {
runValid();
}
public void testFeb29Leap2000() throws Exception {
runValid();
}
public void testFeb29Leap2004() throws Exception {
runValid();
}
protected void runInvalid() throws Exception {
Integer day = (Integer)getObject("day");
Integer month = (Integer)getObject("month");
Integer year = (Integer)getObject("year");
try {
new CompositeDate(year, month, day);
fail("Exception not thrown");
}
catch (InvalidDateException ex) {
}
}
public void testApr31() throws Exception {
runInvalid();
}
public void testJun31() throws Exception {
runInvalid();
}
public void testSep31() throws Exception {
runInvalid();
}
public void testNov31() throws Exception {
runInvalid();
}
public void testFeb31() throws Exception {
runInvalid();
}
public void testFeb30() throws Exception {
runInvalid();
}
public void testFeb29NoLeap() throws Exception {
runInvalid();
}
public void testFeb29NoLeap1900() throws Exception {
runInvalid();
}
private Map<String, Map<String, Object>> data;
}
|