Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.core.simpletypes.temporal;

import junit.framework.*;

public class SimpleDateTest extends TestCase
{
    public static TestSuite suite()
    {
        return new TestSuite( SimpleDateTest.class );
    }

    public SimpleDateTest( String name )
    {
        super( name );
    }

    public static void main( String[] args )
    {
        junit.textui.TestRunner.run( suite() );
    }

    public void test_Contructors_Bad()
    {
        // Bad Strings
        chkCB( "1996a" );
        chkCB( "1996/a" );
        chkCB( "1996/1/a" );
        // Bad Dates
        chkCB( "2000/0" );
        chkCB( "2000/13" );
        chkCB( "2000/0/1" );
        chkCB( "2000/13/1" );
        chkCB( "2000/1/0" );
        chkCB( "2000/1/32" );
        chkCB( "2000/4/31" );
        chkCB( "2100/2/29" ); // exception to Leap Day

        //    // Missing Parts
        //    chkCB( "yM", "1996" );
        //    chkCB( "yMd", "1996/1" );
    }

    private void chkCB( String pToYMD )
    {
        try
        {
            SimpleDate.fromYMD( pToYMD );
            fail( "Did not fail : fromYMD( \"" + pToYMD + "\" )" );
        }
        catch ( IllegalArgumentException expected )
        {
            // Expected
        }
    }

    public void test_rtYMD()
    {
        chkRT( "y", "1996", "1996", DateRes.ToYEAR );
        chkRT( "yM", "2000/2", "2000/02", DateRes.ToMONTH );
        chkRT( "yMd", "2000/2/1", "2000/02/01", DateRes.ToDAY );
        chkRT( "yMd", "2000/12/1", "2000/12/01", DateRes.ToDAY );
        chkRT( "yMd", "2000/1/1", "2000/01/01", DateRes.ToDAY );
        chkRT( "yMd", "2000/1/31", "2000/01/31", DateRes.ToDAY );
        chkRT( "yMd", "2000/12/31", "2000/12/31", DateRes.ToDAY );
        chkRT( "yMd", "2000/2/29", "2000/02/29", DateRes.ToDAY ); // exception-exception Leap Day
    }

    private void chkRT( String pFormat, String pToYMD, String pFromYMD, DateRes pDateResExpected )
    {
        DateFormat zFormat = new DateFormat( pFormat );
        assertEquals( pDateResExpected, zFormat.getDateRes() );
        SimpleDate sd = SimpleDate.fromYMD( pToYMD );
        assertEquals( pDateResExpected, sd.getDateRes() );
        assertEquals( pFromYMD, sd.toYMD() );
    }

    public void test_fromYMDAndToString()
            throws Exception
    {
        chk( DateFormat.DEFAULT_YMD_FORMAT, "1957/01/4", "04 Jan 1957", "dMy", 1957, 1, 4 );
        chk( "yy", "1996", "96", "y", 1996, 0, 0 );
        chk( "/yy", "1996", "/96", "y", 1996, 0, 0 );
        chk( "/ y:", "1996", "/ 1996:", "y", 1996, 0, 0 );
        chk( "/ yyy:", "1996", "/ 1996:", "y", 1996, 0, 0 );
        chk( "MM/dd/yy", "1957/01/04", "01/04/57", "Mdy", 1957, 1, 4 );
        chk( "init y/M/d fini", "1957/1/04", "init 1957/1/4 fini", "yMd", 1957, 1, 4 );
        chk( "dMMM", "1957/1/4", "4Jan", "dM", 1957, 1, 4 );
        chk( "dMMMyy", "1957/1/4", "4Jan57", "dMy", 1957, 1, 4 );
        chk( "dMMMyy", "1957/1/4", "4Jan57", "dMy", 1957, 1, 4 );
        chk( "d MMMM y", "1957/1/4", "4 January 1957", "dMy", 1957, 1, 4 );
        chk( "MMMMMM y", "1957 / 1", "January 1957", "My", 1957, 1, 0 );
    }

    public void test_ContructorsAndToString()
            throws Exception
    {
        // Leap Day
        chk( new SimpleDate( 2004, 2, 29 ), "29 Feb 2004", "dMy", 2004, 2, 29 );
        // exception-exception Leap Day
        chk( new SimpleDate( 2000, 2, 29 ), "29 Feb 2000", "dMy", 2000, 2, 29 );

        chk( new SimpleDate( 1957, 1, 4 ), "04 Jan 1957", "dMy", 1957, 1, 4 );
        chk( "yy", 1996, "96", "y", 1996, 0, 0 );
        chk( "/yy", 1996, "/96", "y", 1996, 0, 0 );
        chk( "/ y:", 1996, "/ 1996:", "y", 1996, 0, 0 );
        chk( "/ yyy:", 1996, "/ 1996:", "y", 1996, 0, 0 );
        chk( "MM/dd/yy", 1957, 1, 4, "01/04/57", "Mdy", 1957, 1, 4 );
        chk( "init y/M/d fini", 1957, 1, 4, "init 1957/1/4 fini", "yMd", 1957, 1, 4 );
        chk( "dMMM", 1957, 1, 4, "4Jan", "dM", 1957, 1, 4 );
        chk( "dMMMyy", 1957, 1, 4, "4Jan57", "dMy", 1957, 1, 4 );
        chk( "dMMMyy", 1957, 1, 4, "4Jan57", "dMy", 1957, 1, 4 );
        chk( "d MMMM y", 1957, 1, 4, "4 January 1957", "dMy", 1957, 1, 4 );
        chk( "MMMMMM y", 1957, 1, "January 1957", "My", 1957, 1, 0 );
        chk( "MMMM d, y", 1957, 1, 4, "January 4, 1957", "Mdy", 1957, 1, 4 );
        chk( "(M/d/y) MMMM d", 1957, 1, 4, "(1/4/1957) January 4", "Mdy", 1957, 1, 4 );
        chk( "(M/d/y) ddMMM", 1957, 1, 4, "(1/4/1957) 04Jan", "Mdy", 1957, 1, 4 );

        chk( new DateFormat( "MMMyy" ), new SimpleDate( 1957, 1, 4 ).changeResolution( DateRes.ToMONTH ), "Jan57", "My", 1957, 1, 0 );
    }

    private void chk( String pDateFormat, String pDateToParse, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
    {
        chk( new DateFormat( pDateFormat ), SimpleDate.fromYMD( pDateToParse ), pToString, pFieldOrder, pYear, pMonth, pDay );
    }

    private void chk( String pDateFormat, int pYearSrc, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
    {
        chk( new DateFormat( pDateFormat ), new SimpleDate( pYearSrc ), pToString, pFieldOrder, pYear, pMonth, pDay );
    }

    private void chk( String pDateFormat, int pYearSrc, int pMonthSrc, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
    {
        chk( new DateFormat( pDateFormat ), new SimpleDate( pYearSrc, pMonthSrc ), pToString, pFieldOrder, pYear, pMonth, pDay );
    }

    private void chk( String pDateFormat, int pYearSrc, int pMonthSrc, int pDaySrc, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
    {
        chk( new DateFormat( pDateFormat ), new SimpleDate( pYearSrc, pMonthSrc, pDaySrc ), pToString, pFieldOrder, pYear, pMonth, pDay );
    }

    private void chk( SimpleDate pDate, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
    {
        chk( pDate.getDateRes().getDateFormat(), pDate, pToString, pFieldOrder, pYear, pMonth, pDay );
    }

    private void chk( DateFormat pFormat, SimpleDate pDate, String pToString, String pFieldOrder, int pYear, int pMonth, int pDay )
    {
        assertEquals( pFormat.getDateRes(), pDate.getDateRes() );

        assertEquals( pFieldOrder, pFormat.getFieldOrder() );
        assertEquals( "Year", pYear, pDate.getYear() );
        assertEquals( "Month", pMonth, pDate.getMonth() );
        assertEquals( "Day", pDay, pDate.getDay() );
        assertEquals( pToString, pFormat.format( pDate ) );

        if ( pDay != 0 )
        {
            SimpleDate sd2 = pDate.addDays( 1 ).minusDays( 1 );
            assertEquals( "Year", pYear, sd2.getYear() );
            assertEquals( "Month", pMonth, sd2.getMonth() );
            assertEquals( "Day", pDay, sd2.getDay() );
            assertEquals( pDate, sd2 );
        }
    }

    public void test_addYears()
    {
        chk_addYears( "1957/01/04", 51, "2008/01/04" );
        chk_addYears( "2000/01/15", -1, "1999/01/15" );
        chk_addYears( "2000/02/29", 1, "2001/03/01" );
        chk_addYears( "2000/02/29", -1, "1999/03/01" );
        chk_addYears( "1957/01", 51, "2008/01" );
        chk_addYears( "2000/01", -1, "1999/01" );
        chk_addYears( "2000/02", 1, "2001/02" );
        chk_addYears( "2000/02", -1, "1999/02" );
        chk_addYears( "1957", 51, "2008" );
        chk_addYears( "2000", -1, "1999" );
        chk_addYears( "2000", 1, "2001" );
    }

    private void chk_addYears( String pDateYMD, int pAdjustBy, String pExpectedYMD )
    {
        SimpleDate sd = SimpleDate.fromYMD( pDateYMD ).addYears( pAdjustBy );
        assertEquals( pExpectedYMD, sd.toYMD() );
    }

    public void test_minusYears()
    {
        chk_minusYears( "2008/01/04", 51, "1957/01/04" );
        chk_minusYears( "2000/01/15", -1, "2001/01/15" );
        chk_minusYears( "2000/02/29", 1, "1999/02/28" );
        chk_minusYears( "2000/02/29", -1, "2001/02/28" );
        chk_minusYears( "2008/01", 51, "1957/01" );
        chk_minusYears( "2000/01", -1, "2001/01" );
        chk_minusYears( "2000/02", 1, "1999/02" );
        chk_minusYears( "2000/02", -1, "2001/02" );
        chk_minusYears( "2008", 51, "1957" );
        chk_minusYears( "2000", -1, "2001" );
        chk_minusYears( "2000", 1, "1999" );
    }

    private void chk_minusYears( String pDateYMD, int pAdjustBy, String pExpectedYMD )
    {
        SimpleDate sd = SimpleDate.fromYMD( pDateYMD ).minusYears( pAdjustBy );
        assertEquals( pExpectedYMD, sd.toYMD() );
    }

    public void test_addMonths()
    {
        SimpleDate sd = new SimpleDate( 2000 );
        try
        {
            sd.addMonths( 1 ); // expected to fail
            fail( "Did Not fail on '" + sd + "': addMonths( 1 )" );
        }
        catch ( IllegalArgumentException expected )
        {
            // expected
        }
        chk_addMonths( "1957/01/04", 2, "1957/03/04" );
        chk_addMonths( "2000/01/15", -1, "1999/12/15" );
        chk_addMonths( "2000/02/29", 12, "2001/03/01" );
        chk_addMonths( "2000/02/29", -12, "1999/03/01" );
        chk_addMonths( "1957/01", 2, "1957/03" );
        chk_addMonths( "2000/01", -1, "1999/12" );
        chk_addMonths( "2000/02", 12, "2001/02" );
        chk_addMonths( "2000/02", -12, "1999/02" );
    }

    private void chk_addMonths( String pDateYMD, int pAdjustBy, String pExpectedYMD )
    {
        SimpleDate sd = SimpleDate.fromYMD( pDateYMD );
        sd = sd.addMonths( pAdjustBy );
        assertEquals( pExpectedYMD, sd.toYMD() );
    }

    public void test_minusMonths()
    {
        SimpleDate sd = new SimpleDate( 2000 );
        try
        {
            sd.minusMonths( 1 ); // expected failure
            fail( "Did Not fail on '" + sd + "': minusMonths( 1 )" );
        }
        catch ( IllegalArgumentException expected )
        {
            // expected
        }
        chk_minusMonths( "1957/03/04", 2, "1957/01/04" );
        chk_minusMonths( "1999/12/15", -1, "2000/01/15" );
        chk_minusMonths( "2000/02/29", 12, "1999/02/28" );
        chk_minusMonths( "2000/02/29", -12, "2001/02/28" );
        chk_minusMonths( "1957/03", 2, "1957/01" );
        chk_minusMonths( "1999/12", -1, "2000/01" );
        chk_minusMonths( "2000/02", 12, "1999/02" );
        chk_minusMonths( "2000/02", -12, "2001/02" );
    }

    private void chk_minusMonths( String pDateYMD, int pAdjustBy, String pExpectedYMD )
    {
        SimpleDate sd = SimpleDate.fromYMD( pDateYMD ).minusMonths( pAdjustBy );
        assertEquals( pExpectedYMD, sd.toYMD() );
    }

    public void test_addDays_daysTill()
    {
        SimpleDate sd = new SimpleDate( 2000 );
        try
        {
            sd.addDays( 1 ); // expected failure
            fail( "Did Not fail on '" + sd + "': addDays( 1 )" );
        }
        catch ( IllegalArgumentException expected )
        {
            // expected
        }
        sd = new SimpleDate( 2000, 1 );
        try
        {
            sd.addDays( 1 ); // expected failure
            fail( "Did Not fail on '" + sd + "': addDays( 1 )" );
        }
        catch ( IllegalArgumentException expected )
        {
            // expected
        }
        chk_addDays( "1957/01/04", 0, "1957/01/04" );
        chk_addDays( "1957/01/04", 2, "1957/01/06" );
        chk_addDays( "2000/01/15", -1, "2000/01/14" );
        chk_addDays( "2000/02/29", 12, "2000/03/12" );
        chk_addDays( "2000/02/29", 32, "2000/04/01" );
        chk_addDays( "2000/02/29", -60, "1999/12/31" );
        chk_addDays( "1857/01/04", 365, "1858/01/04" );
        chk_addDays( "1857/01/04", 3 * 365, "1860/01/04" );
        chk_addDays( "1857/01/04", 4 * 365 + 1, "1861/01/04" );
        chk_addDays( "1857/01/04", 20 * 365 + 5, "1877/01/04" );
        chk_addDays( "1857/01/04", 40 * 365 + 10, "1897/01/04" );
        chk_addDays( "1857/01/04", 44 * 365 + 10, "1901/01/04" );
        chk_addDays( "1857/01/04", 60 * 365 + 14, "1917/01/04" );
        chk_addDays( "1857/01/04", 80 * 365 + 19, "1937/01/04" );
        chk_addDays( "1857/01/04", 100 * 365 + 24, "1957/01/04" );
    }

    private void chk_addDays( String pDateYMD, int pAdjustBy, String pExpectedYMD )
    {
        SimpleDate osd = SimpleDate.fromYMD( pDateYMD );
        SimpleDate nsd = osd.addDays( pAdjustBy );
        assertEquals( pExpectedYMD, nsd.toYMD() );
        assertEquals( pAdjustBy, osd.daysTill( nsd ) );
        if ( pAdjustBy == 0 )
        {
            assertTrue( osd == nsd );
            assertTrue( 0 == osd.compareTo( nsd ) );
            assertFalse( osd.after( nsd ) );
            assertFalse( osd.before( nsd ) );
        }
        else if ( pAdjustBy > 0 )
        {
            assertTrue( 0 > osd.compareTo( nsd ) );
            assertFalse( osd.after( nsd ) );
            assertTrue( osd.before( nsd ) );
        }
        else
        {
            assertTrue( 0 < osd.compareTo( nsd ) );
            assertTrue( osd.after( nsd ) );
            assertFalse( osd.before( nsd ) );
        }
    }

    public void test_minusDays_daysTill()
    {
        SimpleDate sd = new SimpleDate( 2000 );
        try
        {
            sd.minusDays( 1 ); // expected failure
            fail( "Did Not fail on '" + sd + "': minusDays( 1 )" );
        }
        catch ( IllegalArgumentException expected )
        {
            // expected
        }
        sd = new SimpleDate( 2000, 1 );
        try
        {
            sd.minusDays( 1 ); // expected failure
            fail( "Did Not fail on '" + sd + "': minusDays( 1 )" );
        }
        catch ( IllegalArgumentException expected )
        {
            // expected
        }
        chk_minusDays( "1957/01/04", 0, "1957/01/04" );
        chk_minusDays( "1957/01/06", 2, "1957/01/04" );
        chk_minusDays( "2000/01/14", -1, "2000/01/15" );
        chk_minusDays( "2000/03/12", 12, "2000/02/29" );
        chk_minusDays( "2000/04/01", 32, "2000/02/29" );
        chk_minusDays( "1999/12/31", -60, "2000/02/29" );
        chk_minusDays( "1957/01/04", 100 * 365 + 24, "1857/01/04" );
    }

    private void chk_minusDays( String pDateYMD, int pAdjustBy, String pExpectedYMD )
    {
        SimpleDate osd = SimpleDate.fromYMD( pDateYMD );
        SimpleDate nsd = osd.minusDays( pAdjustBy );
        assertEquals( pExpectedYMD, nsd.toYMD() );
        assertEquals( -pAdjustBy, osd.daysTill( nsd ) );
        if ( pAdjustBy == 0 )
        {
            assertTrue( osd == nsd );
            assertTrue( 0 == osd.compareTo( nsd ) );
            assertFalse( osd.after( nsd ) );
            assertFalse( osd.before( nsd ) );
        }
        else if ( pAdjustBy < 0 )
        {
            assertTrue( 0 > osd.compareTo( nsd ) );
            assertFalse( osd.after( nsd ) );
            assertTrue( osd.before( nsd ) );
        }
        else
        {
            assertTrue( 0 < osd.compareTo( nsd ) );
            assertTrue( osd.after( nsd ) );
            assertFalse( osd.before( nsd ) );
        }
    }

    public void test_after_before_null()
    {
        SimpleDate sd = new SimpleDate( 2000, 1, 1 );
        try
        {
            sd.after( (SimpleDate) null );
            fail( "Did Not fail on '" + sd + "': after( null )" );
        }
        catch ( IllegalArgumentException expected )
        {
            // expected
        }
        try
        {
            sd.before( (SimpleDate) null );
            fail( "Did Not fail on '" + sd + "': before( null )" );
        }
        catch ( IllegalArgumentException expected )
        {
            // expected
        }
    }
}

Commits for litesoft/trunk/Java/core/Server/tests/org/litesoft/core/simpletypes/temporal/SimpleDateTest.java

Diff revisions: vs.
Revision Author Commited Message
857 Diff Diff GeorgeS picture GeorgeS Sun 04 Nov, 2012 16:01:37 +0000
853 Diff Diff GeorgeS picture GeorgeS Sun 04 Nov, 2012 15:18:21 +0000
852 Diff Diff GeorgeS picture GeorgeS Tue 30 Oct, 2012 03:34:07 +0000
851 Diff Diff GeorgeS picture GeorgeS Mon 08 Oct, 2012 00:05:32 +0000

Breaking the code as Temporal changes are implemented...

151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000