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