001: package org.springunit.examples.constructor.junit.v4;
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: public class CompositeDateConstructorTest extends TestCase {
012:
013: protected void setUp() throws Exception {
014: this .data = new HashMap<String, Map<String, Object>>();
015:
016: Map<String, Object> testData = new HashMap<String, Object>();
017: testData.put("day", 1);
018: testData.put("month", 1);
019: testData.put("year", 2006);
020: testData.put("expectedDay", 1);
021: testData.put("expectedMonth", 1);
022: testData.put("expectedYear", 2006);
023: this .data.put("testJan01", testData);
024:
025: testData = new HashMap<String, Object>();
026: testData.put("day", 31);
027: testData.put("month", 12);
028: testData.put("year", 2006);
029: testData.put("expectedDay", 31);
030: testData.put("expectedMonth", 12);
031: testData.put("expectedYear", 2006);
032: this .data.put("testDec31", testData);
033:
034: testData = new HashMap<String, Object>();
035: testData.put("day", 29);
036: testData.put("month", 2);
037: testData.put("year", 2000);
038: testData.put("expectedDay", 29);
039: testData.put("expectedMonth", 2);
040: testData.put("expectedYear", 2000);
041: this .data.put("testFeb29Leap2000", testData);
042:
043: testData = new HashMap<String, Object>();
044: testData.put("day", 29);
045: testData.put("month", 2);
046: testData.put("year", 2004);
047: testData.put("expectedDay", 29);
048: testData.put("expectedMonth", 2);
049: testData.put("expectedYear", 2004);
050: this .data.put("testFeb29Leap2004", testData);
051:
052: testData = new HashMap<String, Object>();
053: testData.put("day", 31);
054: testData.put("month", 4);
055: testData.put("year", 2006);
056: this .data.put("testApr31", testData);
057:
058: testData = new HashMap<String, Object>();
059: testData.put("day", 31);
060: testData.put("month", 6);
061: testData.put("year", 2006);
062: this .data.put("testJun31", testData);
063:
064: testData = new HashMap<String, Object>();
065: testData.put("day", 31);
066: testData.put("month", 9);
067: testData.put("year", 2006);
068: this .data.put("testSep31", testData);
069:
070: testData = new HashMap<String, Object>();
071: testData.put("day", 31);
072: testData.put("month", 11);
073: testData.put("year", 2006);
074: this .data.put("testNov31", testData);
075:
076: testData = new HashMap<String, Object>();
077: testData.put("day", 31);
078: testData.put("month", 2);
079: testData.put("year", 2006);
080: this .data.put("testFeb31", testData);
081:
082: testData = new HashMap<String, Object>();
083: testData.put("day", 30);
084: testData.put("month", 2);
085: testData.put("year", 2006);
086: this .data.put("testFeb30", testData);
087:
088: testData = new HashMap<String, Object>();
089: testData.put("day", 29);
090: testData.put("month", 2);
091: testData.put("year", 2003);
092: this .data.put("testFeb29NoLeap", testData);
093:
094: testData = new HashMap<String, Object>();
095: testData.put("day", 29);
096: testData.put("month", 2);
097: testData.put("year", 1900);
098: this .data.put("testFeb29NoLeap1900", testData);
099:
100: }
101:
102: protected void runValid(int year, int month, int day,
103: int expectedYear, int expectedMonth, int expectedDay)
104: throws Exception {
105: CompositeDate subject = new CompositeDate(year, month, day);
106: assertTrue(expectedMonth == subject.getMonth());
107: assertTrue(expectedDay == subject.getDay());
108: assertTrue(expectedYear == subject.getYear());
109: }
110:
111: public void testJan01() throws Exception {
112: Integer day = (Integer) this .data.get(getName()).get("day");
113: Integer month = (Integer) this .data.get(getName()).get("month");
114: Integer year = (Integer) this .data.get(getName()).get("year");
115: Integer expectedDay = (Integer) this .data.get(getName()).get(
116: "expectedDay");
117: Integer expectedMonth = (Integer) this .data.get(getName()).get(
118: "expectedMonth");
119: Integer expectedYear = (Integer) this .data.get(getName()).get(
120: "expectedYear");
121: runValid(year, month, day, expectedYear, expectedMonth,
122: expectedDay);
123: }
124:
125: public void testDec31() throws Exception {
126: Integer day = (Integer) this .data.get(getName()).get("day");
127: Integer month = (Integer) this .data.get(getName()).get("month");
128: Integer year = (Integer) this .data.get(getName()).get("year");
129: Integer expectedDay = (Integer) this .data.get(getName()).get(
130: "expectedDay");
131: Integer expectedMonth = (Integer) this .data.get(getName()).get(
132: "expectedMonth");
133: Integer expectedYear = (Integer) this .data.get(getName()).get(
134: "expectedYear");
135: runValid(year, month, day, expectedYear, expectedMonth,
136: expectedDay);
137: }
138:
139: public void testFeb29Leap2000() throws Exception {
140: Integer day = (Integer) this .data.get(getName()).get("day");
141: Integer month = (Integer) this .data.get(getName()).get("month");
142: Integer year = (Integer) this .data.get(getName()).get("year");
143: Integer expectedDay = (Integer) this .data.get(getName()).get(
144: "expectedDay");
145: Integer expectedMonth = (Integer) this .data.get(getName()).get(
146: "expectedMonth");
147: Integer expectedYear = (Integer) this .data.get(getName()).get(
148: "expectedYear");
149: runValid(year, month, day, expectedYear, expectedMonth,
150: expectedDay);
151: }
152:
153: public void testFeb29Leap2004() throws Exception {
154: Integer day = (Integer) this .data.get(getName()).get("day");
155: Integer month = (Integer) this .data.get(getName()).get("month");
156: Integer year = (Integer) this .data.get(getName()).get("year");
157: Integer expectedDay = (Integer) this .data.get(getName()).get(
158: "expectedDay");
159: Integer expectedMonth = (Integer) this .data.get(getName()).get(
160: "expectedMonth");
161: Integer expectedYear = (Integer) this .data.get(getName()).get(
162: "expectedYear");
163: runValid(year, month, day, expectedYear, expectedMonth,
164: expectedDay);
165: }
166:
167: protected void runInvalid(int year, int month, int day)
168: throws Exception {
169: try {
170: new CompositeDate(year, month, day);
171: fail("Exception not thrown");
172: } catch (InvalidDateException ex) {
173: }
174: }
175:
176: public void testApr31() throws Exception {
177: Integer day = (Integer) this .data.get(getName()).get("day");
178: Integer month = (Integer) this .data.get(getName()).get("month");
179: Integer year = (Integer) this .data.get(getName()).get("year");
180: runInvalid(year, month, day);
181: }
182:
183: public void testJun31() throws Exception {
184: Integer day = (Integer) this .data.get(getName()).get("day");
185: Integer month = (Integer) this .data.get(getName()).get("month");
186: Integer year = (Integer) this .data.get(getName()).get("year");
187: runInvalid(year, month, day);
188: }
189:
190: public void testSep31() throws Exception {
191: Integer day = (Integer) this .data.get(getName()).get("day");
192: Integer month = (Integer) this .data.get(getName()).get("month");
193: Integer year = (Integer) this .data.get(getName()).get("year");
194: runInvalid(year, month, day);
195: }
196:
197: public void testNov31() throws Exception {
198: Integer day = (Integer) this .data.get(getName()).get("day");
199: Integer month = (Integer) this .data.get(getName()).get("month");
200: Integer year = (Integer) this .data.get(getName()).get("year");
201: runInvalid(year, month, day);
202: }
203:
204: public void testFeb31() throws Exception {
205: Integer day = (Integer) this .data.get(getName()).get("day");
206: Integer month = (Integer) this .data.get(getName()).get("month");
207: Integer year = (Integer) this .data.get(getName()).get("year");
208: runInvalid(year, month, day);
209: }
210:
211: public void testFeb30() throws Exception {
212: Integer day = (Integer) this .data.get(getName()).get("day");
213: Integer month = (Integer) this .data.get(getName()).get("month");
214: Integer year = (Integer) this .data.get(getName()).get("year");
215: runInvalid(year, month, day);
216: }
217:
218: public void testFeb29NoLeap() throws Exception {
219: Integer day = (Integer) this .data.get(getName()).get("day");
220: Integer month = (Integer) this .data.get(getName()).get("month");
221: Integer year = (Integer) this .data.get(getName()).get("year");
222: runInvalid(year, month, day);
223: }
224:
225: public void testFeb29NoLeap1900() throws Exception {
226: Integer day = (Integer) this .data.get(getName()).get("day");
227: Integer month = (Integer) this .data.get(getName()).get("month");
228: Integer year = (Integer) this .data.get(getName()).get("year");
229: runInvalid(year, month, day);
230: }
231:
232: private Map<String, Map<String, Object>> data;
233:
234: }
|