001: package org.springunit.examples.constructor.junit.v3;
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: * Moves test data inputs and expected values into a map.
013: * Note that data values should be stored and retrieved as Objects,
014: * not primitives. Autoboxing is used on the calls to runValid
015: * and runInvalid. If values retrieved from the map are
016: * autoboxed to primitives, then NullPointerExceptions must
017: * be handled at this point, which is far less convenient.
018: *
019: * @author Ted.Velkoff
020: *
021: */
022: public class CompositeDateConstructorTest extends TestCase {
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(int year, int month, int day,
114: int expectedYear, int expectedMonth, int expectedDay)
115: throws Exception {
116: CompositeDate subject = new CompositeDate(year, month, day);
117: assertTrue(expectedMonth == subject.getMonth());
118: assertTrue(expectedDay == subject.getDay());
119: assertTrue(expectedYear == subject.getYear());
120: }
121:
122: public void testJan01() throws Exception {
123: Integer day = (Integer) this .data.get("testJan01").get("day");
124: Integer month = (Integer) this .data.get("testJan01").get(
125: "month");
126: Integer year = (Integer) this .data.get("testJan01").get("year");
127: Integer expectedDay = (Integer) this .data.get("testJan01").get(
128: "expectedDay");
129: Integer expectedMonth = (Integer) this .data.get("testJan01")
130: .get("expectedMonth");
131: Integer expectedYear = (Integer) this .data.get("testJan01")
132: .get("expectedYear");
133: runValid(year, month, day, expectedYear, expectedMonth,
134: expectedDay);
135: }
136:
137: public void testDec31() throws Exception {
138: Integer day = (Integer) this .data.get("testDec31").get("day");
139: Integer month = (Integer) this .data.get("testDec31").get(
140: "month");
141: Integer year = (Integer) this .data.get("testDec31").get("year");
142: Integer expectedDay = (Integer) this .data.get("testDec31").get(
143: "expectedDay");
144: Integer expectedMonth = (Integer) this .data.get("testDec31")
145: .get("expectedMonth");
146: Integer expectedYear = (Integer) this .data.get("testDec31")
147: .get("expectedYear");
148: runValid(year, month, day, expectedYear, expectedMonth,
149: expectedDay);
150: }
151:
152: public void testFeb29Leap2000() throws Exception {
153: Integer day = (Integer) this .data.get("testFeb29Leap2000").get(
154: "day");
155: Integer month = (Integer) this .data.get("testFeb29Leap2000")
156: .get("month");
157: Integer year = (Integer) this .data.get("testFeb29Leap2000")
158: .get("year");
159: Integer expectedDay = (Integer) this .data.get(
160: "testFeb29Leap2000").get("expectedDay");
161: Integer expectedMonth = (Integer) this .data.get(
162: "testFeb29Leap2000").get("expectedMonth");
163: Integer expectedYear = (Integer) this .data.get(
164: "testFeb29Leap2000").get("expectedYear");
165: runValid(year, month, day, expectedYear, expectedMonth,
166: expectedDay);
167: }
168:
169: public void testFeb29Leap2004() throws Exception {
170: Integer day = (Integer) this .data.get("testFeb29Leap2004").get(
171: "day");
172: Integer month = (Integer) this .data.get("testFeb29Leap2004")
173: .get("month");
174: Integer year = (Integer) this .data.get("testFeb29Leap2004")
175: .get("year");
176: Integer expectedDay = (Integer) this .data.get(
177: "testFeb29Leap2004").get("expectedDay");
178: Integer expectedMonth = (Integer) this .data.get(
179: "testFeb29Leap2004").get("expectedMonth");
180: Integer expectedYear = (Integer) this .data.get(
181: "testFeb29Leap2004").get("expectedYear");
182: runValid(year, month, day, expectedYear, expectedMonth,
183: expectedDay);
184: }
185:
186: protected void runInvalid(int year, int month, int day)
187: throws Exception {
188: try {
189: new CompositeDate(year, month, day);
190: fail("Exception not thrown");
191: } catch (InvalidDateException ex) {
192: }
193: }
194:
195: public void testApr31() throws Exception {
196: Integer day = (Integer) this .data.get("testApr31").get("day");
197: Integer month = (Integer) this .data.get("testApr31").get(
198: "month");
199: Integer year = (Integer) this .data.get("testApr31").get("year");
200: runInvalid(year, month, day);
201: }
202:
203: public void testJun31() throws Exception {
204: Integer day = (Integer) this .data.get("testJun31").get("day");
205: Integer month = (Integer) this .data.get("testJun31").get(
206: "month");
207: Integer year = (Integer) this .data.get("testJun31").get("year");
208: runInvalid(year, month, day);
209: }
210:
211: public void testSep31() throws Exception {
212: Integer day = (Integer) this .data.get("testSep31").get("day");
213: Integer month = (Integer) this .data.get("testSep31").get(
214: "month");
215: Integer year = (Integer) this .data.get("testSep31").get("year");
216: runInvalid(year, month, day);
217: }
218:
219: public void testNov31() throws Exception {
220: Integer day = (Integer) this .data.get("testNov31").get("day");
221: Integer month = (Integer) this .data.get("testNov31").get(
222: "month");
223: Integer year = (Integer) this .data.get("testNov31").get("year");
224: runInvalid(year, month, day);
225: }
226:
227: public void testFeb31() throws Exception {
228: Integer day = (Integer) this .data.get("testFeb31").get("day");
229: Integer month = (Integer) this .data.get("testFeb31").get(
230: "month");
231: Integer year = (Integer) this .data.get("testFeb31").get("year");
232: runInvalid(year, month, day);
233: }
234:
235: public void testFeb30() throws Exception {
236: Integer day = (Integer) this .data.get("testFeb30").get("day");
237: Integer month = (Integer) this .data.get("testFeb30").get(
238: "month");
239: Integer year = (Integer) this .data.get("testFeb30").get("year");
240: runInvalid(year, month, day);
241: }
242:
243: public void testFeb29NoLeap() throws Exception {
244: Integer day = (Integer) this .data.get("testFeb29NoLeap").get(
245: "day");
246: Integer month = (Integer) this .data.get("testFeb29NoLeap").get(
247: "month");
248: Integer year = (Integer) this .data.get("testFeb29NoLeap").get(
249: "year");
250: runInvalid(year, month, day);
251: }
252:
253: public void testFeb29NoLeap1900() throws Exception {
254: Integer day = (Integer) this .data.get("testFeb29NoLeap1900")
255: .get("day");
256: Integer month = (Integer) this .data.get("testFeb29NoLeap1900")
257: .get("month");
258: Integer year = (Integer) this .data.get("testFeb29NoLeap1900")
259: .get("year");
260: runInvalid(year, month, day);
261: }
262:
263: private Map<String, Map<String, Object>> data;
264:
265: }
|