|
@@ -3,7 +3,9 @@ |
3 |
3 |
|
|
4 |
4 |
|
import junit.framework.*; |
5 |
5 |
|
|
6 |
|
- |
public class CalendarYMDTest extends AbstractTemporalTestSupport<CalendarYMD> |
|
6 |
+ |
import org.litesoft.core.typeutils.*; |
|
7 |
+ |
|
|
8 |
+ |
public class CalendarYMDTest extends TestCase |
7 |
9 |
|
{ |
8 |
10 |
|
public static TestSuite suite() |
9 |
11 |
|
{ |
|
@@ -26,21 +28,22 @@ |
26 |
28 |
|
chkCB( "1996a" ); |
27 |
29 |
|
chkCB( "1996/a" ); |
28 |
30 |
|
chkCB( "1996/1/a" ); |
29 |
|
- |
|
30 |
|
- |
// Missing Parts |
31 |
|
- |
chkCB( "1996" ); |
32 |
|
- |
chkCB( "1996/1" ); |
33 |
|
- |
|
34 |
|
- |
// Too Many Parts |
35 |
|
- |
chkCB( "1996/2/1 0" ); |
36 |
|
- |
|
37 |
31 |
|
// Bad Dates |
|
32 |
+ |
chkCB( "2000/0" ); |
|
33 |
+ |
chkCB( "2000/13" ); |
38 |
34 |
|
chkCB( "2000/0/1" ); |
39 |
35 |
|
chkCB( "2000/13/1" ); |
40 |
36 |
|
chkCB( "2000/1/0" ); |
41 |
37 |
|
chkCB( "2000/1/32" ); |
42 |
38 |
|
chkCB( "2000/4/31" ); |
43 |
39 |
|
chkCB( "2100/2/29" ); // exception to Leap Day |
|
40 |
+ |
|
|
41 |
+ |
// Missing Parts |
|
42 |
+ |
chkCB( "1996" ); |
|
43 |
+ |
chkCB( "1996/1" ); |
|
44 |
+ |
|
|
45 |
+ |
// Too Many Parts |
|
46 |
+ |
chkCB( "1996/2/1/0" ); |
44 |
47 |
|
} |
45 |
48 |
|
|
46 |
49 |
|
private void chkCB( String pToYMD ) |
|
@@ -129,14 +132,17 @@ |
129 |
132 |
|
|
130 |
133 |
|
private void chk( DateFormat pFormat, String pFieldOrder, CalendarYMD pDate, String pDateFormatToString, int pYear, int pMonth, int pDay ) |
131 |
134 |
|
{ |
|
135 |
+ |
String zExpectedStringFormY = Integers.zeroPadIt( 4, pYear ); |
|
136 |
+ |
String zExpectedStringFormM = Integers.zeroPadIt( 2, pMonth ); |
|
137 |
+ |
String zExpectedStringFormD = Integers.zeroPadIt( 2, pDay ); |
132 |
138 |
|
assertEquals( pFieldOrder, pFormat.getFieldOrder() ); |
133 |
139 |
|
assertEquals( "Year", pYear, pDate.getYear() ); |
134 |
140 |
|
assertEquals( "Month", pMonth, pDate.getMonth() ); |
135 |
141 |
|
assertEquals( "Day", pDay, pDate.getDay() ); |
136 |
142 |
|
assertEquals( pDateFormatToString, pFormat.format( pDate ) ); |
137 |
|
- |
assertEquals( "toYMD", toYMD( pYear, pMonth, pDay ), pDate.toYMD() ); |
|
143 |
+ |
assertEquals( "toYMB", zExpectedStringFormY + "/" + zExpectedStringFormM + "/" + zExpectedStringFormD, pDate.toYMD() ); |
138 |
144 |
|
assertEquals( "fromYMD", pDate, CalendarYMD.fromYMD( pDate.toYMD() ) ); |
139 |
|
- |
assertEquals( "toSQLvalue", toSQLvalueYMD( pYear, pMonth, pDay ), pDate.toSQLvalue() ); |
|
145 |
+ |
assertEquals( "toSQLvalue", zExpectedStringFormY + "-" + zExpectedStringFormM + "-" + zExpectedStringFormD, pDate.toSQLvalue() ); |
140 |
146 |
|
assertEquals( "fromSQLvalue", pDate, CalendarYMD.fromSQLvalue( pDate.toSQLvalue() ) ); |
141 |
147 |
|
|
142 |
148 |
|
CalendarYMD sd2 = pDate.addDays( 1 ).minusDays( 1 ); |
|
@@ -160,7 +166,25 @@ |
160 |
166 |
|
CalendarYMD od = CalendarYMD.fromYMD( pDateYMD ); |
161 |
167 |
|
CalendarYMD nd = od.addYears( pAdjustBy ); |
162 |
168 |
|
assertEquals( pExpectedYMD, nd.toYMD() ); |
163 |
|
- |
compareCheck( add( pAdjustBy ), od, nd ); |
|
169 |
+ |
if ( pAdjustBy == 0 ) |
|
170 |
+ |
{ |
|
171 |
+ |
assertTrue( od == nd ); |
|
172 |
+ |
assertTrue( 0 == od.compareTo( nd ) ); |
|
173 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
174 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
175 |
+ |
} |
|
176 |
+ |
else if ( pAdjustBy > 0 ) |
|
177 |
+ |
{ |
|
178 |
+ |
assertTrue( 0 > od.compareTo( nd ) ); |
|
179 |
+ |
assertFalse( od.afterOrEqual( nd ) ); |
|
180 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
181 |
+ |
} |
|
182 |
+ |
else // pAdjustBy < 0 |
|
183 |
+ |
{ |
|
184 |
+ |
assertTrue( 0 < od.compareTo( nd ) ); |
|
185 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
186 |
+ |
assertFalse( od.beforeOrEqual( nd ) ); |
|
187 |
+ |
} |
164 |
188 |
|
} |
165 |
189 |
|
|
166 |
190 |
|
public void test_minusYears() |
|
@@ -177,7 +201,25 @@ |
177 |
201 |
|
CalendarYMD od = CalendarYMD.fromYMD( pDateYMD ); |
178 |
202 |
|
CalendarYMD nd = od.minusYears( pAdjustBy ); |
179 |
203 |
|
assertEquals( pExpectedYMD, nd.toYMD() ); |
180 |
|
- |
compareCheck( minus( pAdjustBy ), od, nd ); |
|
204 |
+ |
if ( pAdjustBy == 0 ) |
|
205 |
+ |
{ |
|
206 |
+ |
assertTrue( od == nd ); |
|
207 |
+ |
assertTrue( 0 == od.compareTo( nd ) ); |
|
208 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
209 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
210 |
+ |
} |
|
211 |
+ |
else if ( pAdjustBy < 0 ) |
|
212 |
+ |
{ |
|
213 |
+ |
assertTrue( 0 > od.compareTo( nd ) ); |
|
214 |
+ |
assertFalse( od.afterOrEqual( nd ) ); |
|
215 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
216 |
+ |
} |
|
217 |
+ |
else // pAdjustBy > 0 |
|
218 |
+ |
{ |
|
219 |
+ |
assertTrue( 0 < od.compareTo( nd ) ); |
|
220 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
221 |
+ |
assertFalse( od.beforeOrEqual( nd ) ); |
|
222 |
+ |
} |
181 |
223 |
|
} |
182 |
224 |
|
|
183 |
225 |
|
public void test_addMonths() |
|
@@ -194,7 +236,25 @@ |
194 |
236 |
|
CalendarYMD od = CalendarYMD.fromYMD( pDateYMD ); |
195 |
237 |
|
CalendarYMD nd = od.addMonths( pAdjustBy ); |
196 |
238 |
|
assertEquals( pExpectedYMD, nd.toYMD() ); |
197 |
|
- |
compareCheck( add( pAdjustBy ), od, nd ); |
|
239 |
+ |
if ( pAdjustBy == 0 ) |
|
240 |
+ |
{ |
|
241 |
+ |
assertTrue( od == nd ); |
|
242 |
+ |
assertTrue( 0 == od.compareTo( nd ) ); |
|
243 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
244 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
245 |
+ |
} |
|
246 |
+ |
else if ( pAdjustBy > 0 ) |
|
247 |
+ |
{ |
|
248 |
+ |
assertTrue( 0 > od.compareTo( nd ) ); |
|
249 |
+ |
assertFalse( od.afterOrEqual( nd ) ); |
|
250 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
251 |
+ |
} |
|
252 |
+ |
else // pAdjustBy < 0 |
|
253 |
+ |
{ |
|
254 |
+ |
assertTrue( 0 < od.compareTo( nd ) ); |
|
255 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
256 |
+ |
assertFalse( od.beforeOrEqual( nd ) ); |
|
257 |
+ |
} |
198 |
258 |
|
} |
199 |
259 |
|
|
200 |
260 |
|
public void test_minusMonths() |
|
@@ -211,7 +271,25 @@ |
211 |
271 |
|
CalendarYMD od = CalendarYMD.fromYMD( pDateYMD ); |
212 |
272 |
|
CalendarYMD nd = od.minusMonths( pAdjustBy ); |
213 |
273 |
|
assertEquals( pExpectedYMD, nd.toYMD() ); |
214 |
|
- |
compareCheck( minus( pAdjustBy ), od, nd ); |
|
274 |
+ |
if ( pAdjustBy == 0 ) |
|
275 |
+ |
{ |
|
276 |
+ |
assertTrue( od == nd ); |
|
277 |
+ |
assertTrue( 0 == od.compareTo( nd ) ); |
|
278 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
279 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
280 |
+ |
} |
|
281 |
+ |
else if ( pAdjustBy < 0 ) |
|
282 |
+ |
{ |
|
283 |
+ |
assertTrue( 0 > od.compareTo( nd ) ); |
|
284 |
+ |
assertFalse( od.afterOrEqual( nd ) ); |
|
285 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
286 |
+ |
} |
|
287 |
+ |
else // pAdjustBy > 0 |
|
288 |
+ |
{ |
|
289 |
+ |
assertTrue( 0 < od.compareTo( nd ) ); |
|
290 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
291 |
+ |
assertFalse( od.beforeOrEqual( nd ) ); |
|
292 |
+ |
} |
215 |
293 |
|
} |
216 |
294 |
|
|
217 |
295 |
|
public void test_addDays_daysTill() |
|
@@ -239,7 +317,25 @@ |
239 |
317 |
|
CalendarYMD nd = od.addDays( pAdjustBy ); |
240 |
318 |
|
assertEquals( pExpectedYMD, nd.toYMD() ); |
241 |
319 |
|
assertEquals( pAdjustBy, od.daysTill( nd ) ); |
242 |
|
- |
compareCheck( add( pAdjustBy ), od, nd ); |
|
320 |
+ |
if ( pAdjustBy == 0 ) |
|
321 |
+ |
{ |
|
322 |
+ |
assertTrue( od == nd ); |
|
323 |
+ |
assertTrue( 0 == od.compareTo( nd ) ); |
|
324 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
325 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
326 |
+ |
} |
|
327 |
+ |
else if ( pAdjustBy > 0 ) |
|
328 |
+ |
{ |
|
329 |
+ |
assertTrue( 0 > od.compareTo( nd ) ); |
|
330 |
+ |
assertFalse( od.afterOrEqual( nd ) ); |
|
331 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
332 |
+ |
} |
|
333 |
+ |
else // pAdjustBy < 0 |
|
334 |
+ |
{ |
|
335 |
+ |
assertTrue( 0 < od.compareTo( nd ) ); |
|
336 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
337 |
+ |
assertFalse( od.beforeOrEqual( nd ) ); |
|
338 |
+ |
} |
243 |
339 |
|
} |
244 |
340 |
|
|
245 |
341 |
|
public void test_minusDays_daysTill() |
|
@@ -259,11 +355,47 @@ |
259 |
355 |
|
CalendarYMD nd = od.minusDays( pAdjustBy ); |
260 |
356 |
|
assertEquals( pExpectedYMD, nd.toYMD() ); |
261 |
357 |
|
assertEquals( -pAdjustBy, od.daysTill( nd ) ); |
262 |
|
- |
compareCheck( minus( pAdjustBy ), od, nd ); |
|
358 |
+ |
if ( pAdjustBy == 0 ) |
|
359 |
+ |
{ |
|
360 |
+ |
assertTrue( od == nd ); |
|
361 |
+ |
assertTrue( 0 == od.compareTo( nd ) ); |
|
362 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
363 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
364 |
+ |
} |
|
365 |
+ |
else if ( pAdjustBy < 0 ) |
|
366 |
+ |
{ |
|
367 |
+ |
assertTrue( 0 > od.compareTo( nd ) ); |
|
368 |
+ |
assertFalse( od.afterOrEqual( nd ) ); |
|
369 |
+ |
assertTrue( od.beforeOrEqual( nd ) ); |
|
370 |
+ |
} |
|
371 |
+ |
else // pAdjustBy > 0 |
|
372 |
+ |
{ |
|
373 |
+ |
assertTrue( 0 < od.compareTo( nd ) ); |
|
374 |
+ |
assertTrue( od.afterOrEqual( nd ) ); |
|
375 |
+ |
assertFalse( od.beforeOrEqual( nd ) ); |
|
376 |
+ |
} |
263 |
377 |
|
} |
264 |
378 |
|
|
265 |
379 |
|
public void test_after_before_null() |
266 |
380 |
|
{ |
267 |
|
- |
chkAfterBeforeNull( new CalendarYMD( 2000, 1, 1 ) ); |
|
381 |
+ |
CalendarYMD sd = new CalendarYMD( 2000, 1, 1 ); |
|
382 |
+ |
try |
|
383 |
+ |
{ |
|
384 |
+ |
sd.afterOrEqual( null ); |
|
385 |
+ |
fail( "Did Not fail on '" + sd + "': afterOrEqual( null )" ); |
|
386 |
+ |
} |
|
387 |
+ |
catch ( IllegalArgumentException expected ) |
|
388 |
+ |
{ |
|
389 |
+ |
// expected |
|
390 |
+ |
} |
|
391 |
+ |
try |
|
392 |
+ |
{ |
|
393 |
+ |
sd.beforeOrEqual( null ); |
|
394 |
+ |
fail( "Did Not fail on '" + sd + "': beforeOrEqual( null )" ); |
|
395 |
+ |
} |
|
396 |
+ |
catch ( IllegalArgumentException expected ) |
|
397 |
+ |
{ |
|
398 |
+ |
// expected |
|
399 |
+ |
} |
268 |
400 |
|
} |
269 |
401 |
|
} |