001: package org.springunit.examples.constructor.junit.v7;
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: * Add syntactic sugar to make for easier retrieval of items
013: * from data map.
014: *
015: * Issue #1: The data map is set up and torn down with every test
016: * Issue #2: Is inline population of a Map the most appealing way
017: * to capture the key data for testing?
018: *
019: * @author Ted.Velkoff
020: *
021: */
022: public class CompositeDateConstructorTest extends TestCase {
023:
024: protected Object getObject(String name) {
025: return this .data.get(getName()).get(name);
026: }
027:
028: protected void setUp() throws Exception {
029: this .data = new HashMap<String, Map<String, Object>>();
030:
031: Map<String, Object> testData = new HashMap<String, Object>();
032: testData.put("day", 1);
033: testData.put("month", 1);
034: testData.put("year", 2006);
035: testData.put("expectedDay", 1);
036: testData.put("expectedMonth", 1);
037: testData.put("expectedYear", 2006);
038: this .data.put("testJan01", testData);
039:
040: testData = new HashMap<String, Object>();
041: testData.put("day", 31);
042: testData.put("month", 12);
043: testData.put("year", 2006);
044: testData.put("expectedDay", 31);
045: testData.put("expectedMonth", 12);
046: testData.put("expectedYear", 2006);
047: this .data.put("testDec31", testData);
048:
049: testData = new HashMap<String, Object>();
050: testData.put("day", 29);
051: testData.put("month", 2);
052: testData.put("year", 2000);
053: testData.put("expectedDay", 29);
054: testData.put("expectedMonth", 2);
055: testData.put("expectedYear", 2000);
056: this .data.put("testFeb29Leap2000", testData);
057:
058: testData = new HashMap<String, Object>();
059: testData.put("day", 29);
060: testData.put("month", 2);
061: testData.put("year", 2004);
062: testData.put("expectedDay", 29);
063: testData.put("expectedMonth", 2);
064: testData.put("expectedYear", 2004);
065: this .data.put("testFeb29Leap2004", testData);
066:
067: testData = new HashMap<String, Object>();
068: testData.put("day", 31);
069: testData.put("month", 4);
070: testData.put("year", 2006);
071: testData.put("expectedException", new InvalidDateException());
072: this .data.put("testApr31", testData);
073:
074: testData = new HashMap<String, Object>();
075: testData.put("day", 31);
076: testData.put("month", 6);
077: testData.put("year", 2006);
078: testData.put("expectedException", new InvalidDateException());
079: this .data.put("testJun31", testData);
080:
081: testData = new HashMap<String, Object>();
082: testData.put("day", 31);
083: testData.put("month", 9);
084: testData.put("year", 2006);
085: testData.put("expectedException", new InvalidDateException());
086: this .data.put("testSep31", testData);
087:
088: testData = new HashMap<String, Object>();
089: testData.put("day", 31);
090: testData.put("month", 11);
091: testData.put("year", 2006);
092: testData.put("expectedException", new InvalidDateException());
093: this .data.put("testNov31", testData);
094:
095: testData = new HashMap<String, Object>();
096: testData.put("day", 31);
097: testData.put("month", 2);
098: testData.put("year", 2006);
099: testData.put("expectedException", new InvalidDateException());
100: this .data.put("testFeb31", testData);
101:
102: testData = new HashMap<String, Object>();
103: testData.put("day", 30);
104: testData.put("month", 2);
105: testData.put("year", 2006);
106: testData.put("expectedException", new InvalidDateException());
107: this .data.put("testFeb30", testData);
108:
109: testData = new HashMap<String, Object>();
110: testData.put("day", 29);
111: testData.put("month", 2);
112: testData.put("year", 2003);
113: testData.put("expectedException", new InvalidDateException());
114: this .data.put("testFeb29NoLeap", testData);
115:
116: testData = new HashMap<String, Object>();
117: testData.put("day", 29);
118: testData.put("month", 2);
119: testData.put("year", 1900);
120: testData.put("expectedException", new InvalidDateException());
121: this .data.put("testFeb29NoLeap1900", testData);
122:
123: }
124:
125: protected void runConstructor() throws Exception {
126: Integer day = (Integer) getObject("day");
127: Integer month = (Integer) getObject("month");
128: Integer year = (Integer) getObject("year");
129: Integer expectedDay = (Integer) getObject("expectedDay");
130: Integer expectedMonth = (Integer) getObject("expectedMonth");
131: Integer expectedYear = (Integer) getObject("expectedYear");
132: Exception expectedException = (Exception) getObject("expectedException");
133: try {
134: CompositeDate subject = new CompositeDate(year, month, day);
135: if (expectedException != null) {
136: fail("Exception not thrown");
137: }
138: assertTrue(expectedMonth == subject.getMonth());
139: assertTrue(expectedDay == subject.getDay());
140: assertTrue(expectedYear == subject.getYear());
141: } catch (Exception ex) {
142: if (!expectedException.getClass().isAssignableFrom(
143: ex.getClass())) {
144: throw ex;
145: }
146: }
147: }
148:
149: public void testJan01() throws Exception {
150: runConstructor();
151: }
152:
153: public void testDec31() throws Exception {
154: runConstructor();
155: }
156:
157: public void testFeb29Leap2000() throws Exception {
158: runConstructor();
159: }
160:
161: public void testFeb29Leap2004() throws Exception {
162: runConstructor();
163: }
164:
165: public void testApr31() throws Exception {
166: runConstructor();
167: }
168:
169: public void testJun31() throws Exception {
170: runConstructor();
171: }
172:
173: public void testSep31() throws Exception {
174: runConstructor();
175: }
176:
177: public void testNov31() throws Exception {
178: runConstructor();
179: }
180:
181: public void testFeb31() throws Exception {
182: runConstructor();
183: }
184:
185: public void testFeb30() throws Exception {
186: runConstructor();
187: }
188:
189: public void testFeb29NoLeap() throws Exception {
190: runConstructor();
191: }
192:
193: public void testFeb29NoLeap1900() throws Exception {
194: runConstructor();
195: }
196:
197: private Map<String, Map<String, Object>> data;
198:
199: }
|