001: package org.springunit.examples.constructor.junit.v6;
002:
003: import java.util.HashMap;
004: import java.util.Map;
005:
006: import org.springunit.examples.CompositeDate;
007: import org.springunit.examples.InvalidDateException;
008:
009: import junit.framework.TestCase;
010:
011: /**
012: * Unifies algorithms for valid and invalid inputs
013: * in a single method.
014: *
015: * @author Ted.Velkoff
016: *
017: */
018: public class CompositeDateConstructorTest extends TestCase {
019:
020: protected Object getObject(String name) {
021: return this .data.get(getName()).get(name);
022: }
023:
024: protected void setUp() throws Exception {
025: this .data = new HashMap<String, Map<String, Object>>();
026:
027: Map<String, Object> testData = new HashMap<String, Object>();
028: testData.put("day", 1);
029: testData.put("month", 1);
030: testData.put("year", 2006);
031: testData.put("expectedDay", 1);
032: testData.put("expectedMonth", 1);
033: testData.put("expectedYear", 2006);
034: this .data.put("testJan01", testData);
035:
036: testData = new HashMap<String, Object>();
037: testData.put("day", 31);
038: testData.put("month", 12);
039: testData.put("year", 2006);
040: testData.put("expectedDay", 31);
041: testData.put("expectedMonth", 12);
042: testData.put("expectedYear", 2006);
043: this .data.put("testDec31", testData);
044:
045: testData = new HashMap<String, Object>();
046: testData.put("day", 29);
047: testData.put("month", 2);
048: testData.put("year", 2000);
049: testData.put("expectedDay", 29);
050: testData.put("expectedMonth", 2);
051: testData.put("expectedYear", 2000);
052: this .data.put("testFeb29Leap2000", testData);
053:
054: testData = new HashMap<String, Object>();
055: testData.put("day", 29);
056: testData.put("month", 2);
057: testData.put("year", 2004);
058: testData.put("expectedDay", 29);
059: testData.put("expectedMonth", 2);
060: testData.put("expectedYear", 2004);
061: this .data.put("testFeb29Leap2004", testData);
062:
063: testData = new HashMap<String, Object>();
064: testData.put("day", 31);
065: testData.put("month", 4);
066: testData.put("year", 2006);
067: this .data.put("testApr31", testData);
068:
069: testData = new HashMap<String, Object>();
070: testData.put("day", 31);
071: testData.put("month", 6);
072: testData.put("year", 2006);
073: this .data.put("testJun31", testData);
074:
075: testData = new HashMap<String, Object>();
076: testData.put("day", 31);
077: testData.put("month", 9);
078: testData.put("year", 2006);
079: this .data.put("testSep31", testData);
080:
081: testData = new HashMap<String, Object>();
082: testData.put("day", 31);
083: testData.put("month", 11);
084: testData.put("year", 2006);
085: this .data.put("testNov31", testData);
086:
087: testData = new HashMap<String, Object>();
088: testData.put("day", 31);
089: testData.put("month", 2);
090: testData.put("year", 2006);
091: this .data.put("testFeb31", testData);
092:
093: testData = new HashMap<String, Object>();
094: testData.put("day", 30);
095: testData.put("month", 2);
096: testData.put("year", 2006);
097: this .data.put("testFeb30", testData);
098:
099: testData = new HashMap<String, Object>();
100: testData.put("day", 29);
101: testData.put("month", 2);
102: testData.put("year", 2003);
103: this .data.put("testFeb29NoLeap", testData);
104:
105: testData = new HashMap<String, Object>();
106: testData.put("day", 29);
107: testData.put("month", 2);
108: testData.put("year", 1900);
109: this .data.put("testFeb29NoLeap1900", testData);
110:
111: }
112:
113: protected void runValid() throws Exception {
114: Integer day = (Integer) getObject("day");
115: Integer month = (Integer) getObject("month");
116: Integer year = (Integer) getObject("year");
117: Integer expectedDay = (Integer) getObject("expectedDay");
118: Integer expectedMonth = (Integer) getObject("expectedMonth");
119: Integer expectedYear = (Integer) getObject("expectedYear");
120: CompositeDate subject = new CompositeDate(year, month, day);
121: assertTrue(expectedMonth == subject.getMonth());
122: assertTrue(expectedDay == subject.getDay());
123: assertTrue(expectedYear == subject.getYear());
124: }
125:
126: public void testJan01() throws Exception {
127: runValid();
128: }
129:
130: public void testDec31() throws Exception {
131: runValid();
132: }
133:
134: public void testFeb29Leap2000() throws Exception {
135: runValid();
136: }
137:
138: public void testFeb29Leap2004() throws Exception {
139: runValid();
140: }
141:
142: protected void runInvalid() throws Exception {
143: Integer day = (Integer) getObject("day");
144: Integer month = (Integer) getObject("month");
145: Integer year = (Integer) getObject("year");
146: try {
147: new CompositeDate(year, month, day);
148: fail("Exception not thrown");
149: } catch (InvalidDateException ex) {
150: }
151: }
152:
153: public void testApr31() throws Exception {
154: runInvalid();
155: }
156:
157: public void testJun31() throws Exception {
158: runInvalid();
159: }
160:
161: public void testSep31() throws Exception {
162: runInvalid();
163: }
164:
165: public void testNov31() throws Exception {
166: runInvalid();
167: }
168:
169: public void testFeb31() throws Exception {
170: runInvalid();
171: }
172:
173: public void testFeb30() throws Exception {
174: runInvalid();
175: }
176:
177: public void testFeb29NoLeap() throws Exception {
178: runInvalid();
179: }
180:
181: public void testFeb29NoLeap1900() throws Exception {
182: runInvalid();
183: }
184:
185: private Map<String, Map<String, Object>> data;
186:
187: }
|