Subversion Repository Public Repository

litesoft

Diff Revisions 852 vs 853 for /trunk/Java/core/Server/tests/org/litesoft/core/simpletypes/temporal/SimpleDateTest.java

Diff revisions: vs.
  @@ -23,30 +23,30 @@
23 23 public void test_Contructors_Bad()
24 24 {
25 25 // Bad Strings
26 - chkCB( "y", "1996a" );
27 - chkCB( "yM", "1996/a" );
28 - chkCB( "yMd", "1996/1/a" );
29 - // Missing Parts
30 - chkCB( "yM", "1996" );
31 - chkCB( "yMd", "1996/1" );
26 + chkCB( "1996a" );
27 + chkCB( "1996/a" );
28 + chkCB( "1996/1/a" );
32 29 // Bad Dates
33 - chkCB( "yM", "2000/0" );
34 - chkCB( "yM", "2000/13" );
35 - chkCB( "yMd", "2000/0/1" );
36 - chkCB( "yMd", "2000/13/1" );
37 - chkCB( "yMd", "2000/1/0" );
38 - chkCB( "yMd", "2000/1/32" );
39 - chkCB( "yMd", "2000/4/31" );
40 - chkCB( "yMd", "2100/2/29" ); // exception to Leap Day
30 + chkCB( "2000/0" );
31 + chkCB( "2000/13" );
32 + chkCB( "2000/0/1" );
33 + chkCB( "2000/13/1" );
34 + chkCB( "2000/1/0" );
35 + chkCB( "2000/1/32" );
36 + chkCB( "2000/4/31" );
37 + chkCB( "2100/2/29" ); // exception to Leap Day
38 +
39 + // // Missing Parts
40 + // chkCB( "yM", "1996" );
41 + // chkCB( "yMd", "1996/1" );
41 42 }
42 43
43 - private void chkCB( String pFormat, String pToYMD )
44 + private void chkCB( String pToYMD )
44 45 {
45 - DateFormat zFormat = new DateFormat( pFormat );
46 46 try
47 47 {
48 - SimpleDate.fromYMD( zFormat, pToYMD );
49 - fail( "Did not fail : fromYMD( \"" + pFormat + "\" , \"" + pToYMD + "\" )" );
48 + SimpleDate.fromYMD( pToYMD );
49 + fail( "Did not fail : fromYMD( \"" + pToYMD + "\" )" );
50 50 }
51 51 catch ( IllegalArgumentException expected )
52 52 {
  @@ -70,7 +70,8 @@
70 70 {
71 71 DateFormat zFormat = new DateFormat( pFormat );
72 72 assertEquals( pDateResExpected, zFormat.getDateRes() );
73 - SimpleDate sd = SimpleDate.fromYMD( zFormat, pToYMD );
73 + SimpleDate sd = SimpleDate.fromYMD( pToYMD );
74 + assertEquals( pDateResExpected, sd.getDateRes() );
74 75 assertEquals( pFromYMD, sd.toYMD() );
75 76 }
76 77
  @@ -115,47 +116,49 @@
115 116 chk( "(M/d/y) MMMM d", 1957, 1, 4, "(1/4/1957) January 4", "Mdy", 1957, 1, 4 );
116 117 chk( "(M/d/y) ddMMM", 1957, 1, 4, "(1/4/1957) 04Jan", "Mdy", 1957, 1, 4 );
117 118
118 - chk( new SimpleDate( 1957, 1, 4 ).changeFormat( new DateFormat( "MMMyy" ) ), "Jan57", "My", 1957, 1, 0 );
119 + chk( new DateFormat( "MMMyy" ), new SimpleDate( 1957, 1, 4 ).changeResolution( DateRes.ToMONTH ), "Jan57", "My", 1957, 1, 0 );
119 120 }
120 121
121 122 private void chk( String pDateFormat, String pDateToParse, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
122 123 {
123 - DateFormat zFormat = new DateFormat( pDateFormat );
124 - chk( SimpleDate.fromYMD( zFormat, pDateToParse ), pToString, pFieldOrder, pYear, pMonth, pDay );
124 + chk( new DateFormat( pDateFormat ), SimpleDate.fromYMD( pDateToParse ), pToString, pFieldOrder, pYear, pMonth, pDay );
125 125 }
126 126
127 127 private void chk( String pDateFormat, int pYearSrc, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
128 128 {
129 - DateFormat zFormat = new DateFormat( pDateFormat );
130 - chk( new SimpleDate( zFormat, pYearSrc ), pToString, pFieldOrder, pYear, pMonth, pDay );
129 + chk( new DateFormat( pDateFormat ), new SimpleDate( pYearSrc ), pToString, pFieldOrder, pYear, pMonth, pDay );
131 130 }
132 131
133 - private void chk( String pDateFormat, int pYearSrc, int pMonthSrc, int pDaySrc, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
132 + private void chk( String pDateFormat, int pYearSrc, int pMonthSrc, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
134 133 {
135 - DateFormat zFormat = new DateFormat( pDateFormat );
136 - chk( new SimpleDate( zFormat, pYearSrc, pMonthSrc, pDaySrc ), pToString, pFieldOrder, pYear, pMonth, pDay );
134 + chk( new DateFormat( pDateFormat ), new SimpleDate( pYearSrc, pMonthSrc ), pToString, pFieldOrder, pYear, pMonth, pDay );
137 135 }
138 136
139 - private void chk( String pDateFormat, int pYearSrc, int pMonthSrc, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
137 + private void chk( String pDateFormat, int pYearSrc, int pMonthSrc, int pDaySrc, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
140 138 {
141 - DateFormat zFormat = new DateFormat( pDateFormat );
142 - chk( new SimpleDate( zFormat, pYearSrc, pMonthSrc ), pToString, pFieldOrder, pYear, pMonth, pDay );
139 + chk( new DateFormat( pDateFormat ), new SimpleDate( pYearSrc, pMonthSrc, pDaySrc ), pToString, pFieldOrder, pYear, pMonth, pDay );
143 140 }
144 141
145 142 private void chk( SimpleDate pDate, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
146 143 {
147 - assertEquals( pToString, pDate.toString() );
148 - assertEquals( pFieldOrder, pDate.getFormat().getFieldOrder() );
144 + chk( pDate.getDateRes().getDateFormat(), pDate, pToString, pFieldOrder, pYear, pMonth, pDay );
145 + }
146 +
147 + private void chk( DateFormat pFormat, SimpleDate pDate, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
148 + {
149 + assertEquals( pFormat.getDateRes(), pDate.getDateRes() );
150 +
151 + assertEquals( pFieldOrder, pFormat.getFieldOrder() );
149 152 assertEquals( "Year", pYear, pDate.getYear() );
150 153 assertEquals( "Month", pMonth, pDate.getMonth() );
151 154 assertEquals( "Day", pDay, pDate.getDay() );
155 + assertEquals( pToString, pFormat.format( pDate ) );
156 +
152 157 SimpleDate sd2 = pDate.addDays( 1 ).minusDays( 1 );
153 - assertEquals( pDate, sd2 );
154 - assertEquals( pToString, sd2.toString() );
155 - assertEquals( pFieldOrder, sd2.getFormat().getFieldOrder() );
156 158 assertEquals( "Year", pYear, sd2.getYear() );
157 159 assertEquals( "Month", pMonth, sd2.getMonth() );
158 160 assertEquals( "Day", pDay, sd2.getDay() );
161 + assertEquals( pDate, sd2 );
159 162 }
160 163
161 164 public void test_addYears()
  @@ -164,11 +167,18 @@
164 167 chk_addYears( "2000/01/15", -1, "1999/01/15" );
165 168 chk_addYears( "2000/02/29", 1, "2001/03/01" );
166 169 chk_addYears( "2000/02/29", -1, "1999/03/01" );
170 + chk_addYears( "1957/01", 51, "2008/01" );
171 + chk_addYears( "2000/01", -1, "1999/01" );
172 + chk_addYears( "2000/02", 1, "2001/02" );
173 + chk_addYears( "2000/02", -1, "1999/02" );
174 + chk_addYears( "1957", 51, "2008" );
175 + chk_addYears( "2000", -1, "1999" );
176 + chk_addYears( "2000", 1, "2001" );
167 177 }
168 178
169 179 private void chk_addYears( String pDateYMD, int pAdjustBy, String pExpectedYMD )
170 180 {
171 - SimpleDate sd = SimpleDate.fromYMD( new DateFormat( "yMd" ), pDateYMD ).addYears( pAdjustBy );
181 + SimpleDate sd = SimpleDate.fromYMD( pDateYMD ).addYears( pAdjustBy );
172 182 assertEquals( pExpectedYMD, sd.toYMD() );
173 183 }
174 184
  @@ -178,17 +188,24 @@
178 188 chk_minusYears( "2000/01/15", -1, "2001/01/15" );
179 189 chk_minusYears( "2000/02/29", 1, "1999/02/28" );
180 190 chk_minusYears( "2000/02/29", -1, "2001/02/28" );
191 + chk_minusYears( "2008/01", 51, "1957/01" );
192 + chk_minusYears( "2000/01", -1, "2001/01" );
193 + chk_minusYears( "2000/02", 1, "1999/02" );
194 + chk_minusYears( "2000/02", -1, "2001/02" );
195 + chk_minusYears( "2008", 51, "1957" );
196 + chk_minusYears( "2000", -1, "2001" );
197 + chk_minusYears( "2000", 1, "1999" );
181 198 }
182 199
183 200 private void chk_minusYears( String pDateYMD, int pAdjustBy, String pExpectedYMD )
184 201 {
185 - SimpleDate sd = SimpleDate.fromYMD( new DateFormat( "yMd" ), pDateYMD ).minusYears( pAdjustBy );
202 + SimpleDate sd = SimpleDate.fromYMD( pDateYMD ).minusYears( pAdjustBy );
186 203 assertEquals( pExpectedYMD, sd.toYMD() );
187 204 }
188 205
189 206 public void test_addMonths()
190 207 {
191 - SimpleDate sd = new SimpleDate( new DateFormat( "y" ), 2000 );
208 + SimpleDate sd = new SimpleDate( 2000 );
192 209 try
193 210 {
194 211 sd.addMonths( 1 ); // expected to fail
  @@ -202,18 +219,22 @@
202 219 chk_addMonths( "2000/01/15", -1, "1999/12/15" );
203 220 chk_addMonths( "2000/02/29", 12, "2001/03/01" );
204 221 chk_addMonths( "2000/02/29", -12, "1999/03/01" );
222 + chk_addMonths( "1957/01", 2, "1957/03" );
223 + chk_addMonths( "2000/01", -1, "1999/12" );
224 + chk_addMonths( "2000/02", 12, "2001/02" );
225 + chk_addMonths( "2000/02", -12, "1999/02" );
205 226 }
206 227
207 228 private void chk_addMonths( String pDateYMD, int pAdjustBy, String pExpectedYMD )
208 229 {
209 - SimpleDate sd = SimpleDate.fromYMD( new DateFormat( "yMd" ), pDateYMD );
230 + SimpleDate sd = SimpleDate.fromYMD( pDateYMD );
210 231 sd = sd.addMonths( pAdjustBy );
211 232 assertEquals( pExpectedYMD, sd.toYMD() );
212 233 }
213 234
214 235 public void test_minusMonths()
215 236 {
216 - SimpleDate sd = new SimpleDate( new DateFormat( "y" ), 2000 );
237 + SimpleDate sd = new SimpleDate( 2000 );
217 238 try
218 239 {
219 240 sd.minusMonths( 1 ); // expected failure
  @@ -227,17 +248,21 @@
227 248 chk_minusMonths( "1999/12/15", -1, "2000/01/15" );
228 249 chk_minusMonths( "2000/02/29", 12, "1999/02/28" );
229 250 chk_minusMonths( "2000/02/29", -12, "2001/02/28" );
251 + chk_minusMonths( "1957/03", 2, "1957/01" );
252 + chk_minusMonths( "1999/12", -1, "2000/01" );
253 + chk_minusMonths( "2000/02", 12, "1999/02" );
254 + chk_minusMonths( "2000/02", -12, "2001/02" );
230 255 }
231 256
232 257 private void chk_minusMonths( String pDateYMD, int pAdjustBy, String pExpectedYMD )
233 258 {
234 - SimpleDate sd = SimpleDate.fromYMD( new DateFormat( "yMd" ), pDateYMD ).minusMonths( pAdjustBy );
259 + SimpleDate sd = SimpleDate.fromYMD( pDateYMD ).minusMonths( pAdjustBy );
235 260 assertEquals( pExpectedYMD, sd.toYMD() );
236 261 }
237 262
238 263 public void test_addDays_daysTill()
239 264 {
240 - SimpleDate sd = new SimpleDate( new DateFormat( "y" ), 2000 );
265 + SimpleDate sd = new SimpleDate( 2000 );
241 266 try
242 267 {
243 268 sd.addDays( 1 ); // expected failure
  @@ -247,7 +272,7 @@
247 272 {
248 273 // expected
249 274 }
250 - sd = new SimpleDate( "yM", 2000, 1 );
275 + sd = new SimpleDate( 2000, 1 );
251 276 try
252 277 {
253 278 sd.addDays( 1 ); // expected failure
  @@ -276,15 +301,13 @@
276 301
277 302 private void chk_addDays( String pDateYMD, int pAdjustBy, String pExpectedYMD )
278 303 {
279 - SimpleDate osd = SimpleDate.fromYMD( new DateFormat( "yMd" ), pDateYMD );
280 - assertTrue( "equalsDate", osd.equals( osd.getDate() ) );
281 - SimpleDate nsd = osd.copy();
282 - assertEquals( osd, nsd );
283 - nsd = nsd.addDays( pAdjustBy );
304 + SimpleDate osd = SimpleDate.fromYMD( pDateYMD );
305 + SimpleDate nsd = osd.addDays( pAdjustBy );
284 306 assertEquals( pExpectedYMD, nsd.toYMD() );
285 307 assertEquals( pAdjustBy, osd.daysTill( nsd ) );
286 308 if ( pAdjustBy == 0 )
287 309 {
310 + assertTrue( osd == nsd );
288 311 assertTrue( 0 == osd.compareTo( nsd ) );
289 312 assertFalse( osd.after( nsd ) );
290 313 assertFalse( osd.before( nsd ) );
  @@ -305,7 +328,7 @@
305 328
306 329 public void test_minusDays_daysTill()
307 330 {
308 - SimpleDate sd = new SimpleDate( new DateFormat( "y" ), 2000 );
331 + SimpleDate sd = new SimpleDate( 2000 );
309 332 try
310 333 {
311 334 sd.minusDays( 1 ); // expected failure
  @@ -315,7 +338,7 @@
315 338 {
316 339 // expected
317 340 }
318 - sd = new SimpleDate( new DateFormat( "yM" ), 2000, 1 );
341 + sd = new SimpleDate( 2000, 1 );
319 342 try
320 343 {
321 344 sd.minusDays( 1 ); // expected failure
  @@ -336,15 +359,13 @@
336 359
337 360 private void chk_minusDays( String pDateYMD, int pAdjustBy, String pExpectedYMD )
338 361 {
339 - SimpleDate osd = SimpleDate.fromYMD( new DateFormat( "yMd" ), pDateYMD );
340 - assertTrue( "equalsDate", osd.equals( osd.getDate() ) );
341 - SimpleDate nsd = osd.copy();
342 - assertEquals( osd, nsd );
343 - nsd = nsd.minusDays( pAdjustBy );
362 + SimpleDate osd = SimpleDate.fromYMD( pDateYMD );
363 + SimpleDate nsd = osd.minusDays( pAdjustBy );
344 364 assertEquals( pExpectedYMD, nsd.toYMD() );
345 365 assertEquals( -pAdjustBy, osd.daysTill( nsd ) );
346 366 if ( pAdjustBy == 0 )
347 367 {
368 + assertTrue( osd == nsd );
348 369 assertTrue( 0 == osd.compareTo( nsd ) );
349 370 assertFalse( osd.after( nsd ) );
350 371 assertFalse( osd.before( nsd ) );
  @@ -365,7 +386,7 @@
365 386
366 387 public void test_after_before_null()
367 388 {
368 - SimpleDate sd = new SimpleDate( new DateFormat( "yMd" ), 2000, 1, 1 );
389 + SimpleDate sd = new SimpleDate( 2000, 1, 1 );
369 390 try
370 391 {
371 392 sd.after( (SimpleDate) null );