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
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.core.simpletypes.temporal;

import junit.framework.*;

import org.litesoft.core.typeutils.*;

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

    public CalendarYMDTest( 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( "1996" );
        chkCB( "1996/1" );

        // Too Many Parts
        chkCB( "1996/2/1/0" );
    }

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

    public void test_rtYMD()
    {
        chkRTYMD( "2000/2/1", "2000/02/01" );
        chkRTYMD( "2000/0012/1", "2000/12/01" );
        chkRTYMD( "2000/1/0001", "2000/01/01" );
        chkRTYMD( "2000/001/31", "2000/01/31" );
        chkRTYMD( "2000/12/31", "2000/12/31" );
        chkRTYMD( "2000/2/29", "2000/02/29" ); // exception-exception Leap Day
    }

    private void chkRTYMD( String pInputForFrom, String pToOutput )
    {
        CalendarYMD sd = CalendarYMD.fromYMD( pInputForFrom );
        assertEquals( pToOutput, sd.toYMD() );
    }

    public void test_rtSQL()
    {
        chkRTSQL( "2000-2-1", "2000-02-01" );
        chkRTSQL( "2000-0012-1", "2000-12-01" );
        chkRTSQL( "2000-1-0001", "2000-01-01" );
        chkRTSQL( "2000-001-31", "2000-01-31" );
        chkRTSQL( "2000-12-31", "2000-12-31" );
        chkRTSQL( "2000-2-29", "2000-02-29" ); // exception-exception Leap Day
    }

    private void chkRTSQL( String pInputForFrom, String pToOutput )
    {
        CalendarYMD sd = CalendarYMD.fromSQLvalue( pInputForFrom );
        assertEquals( pToOutput, sd.toSQLvalue() );
    }

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

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

    public void test_ContructorsAndToString()
            throws Exception
    {
        chk( "dd MMM yyyy", "dMy", 2004, 2, 29, "29 Feb 2004" ); // Leap Day
        chk( "dd MMM yyyy", "dMy", 2000, 2, 29, "29 Feb 2000" ); // exception-exception Leap Day
        chk( "dd MMM yyyy", "dMy", 1957, 1, 4, "04 Jan 1957" );
        chk( "MM/dd/yy", "Mdy", 1957, 1, 4, "01/04/57" );
        chk( "init y/M/d fini", "yMd", 1957, 1, 4, "init 1957/1/4 fini" );
        chk( "dMMM", "dM", 1957, 1, 4, "4Jan" );
        chk( "dMMMyy", "dMy", 1957, 1, 4, "4Jan57" );
        chk( "dMMMyy", "dMy", 1957, 1, 4, "4Jan57" );
        chk( "d MMMM y", "dMy", 1957, 1, 4, "4 January 1957" );
        chk( "MMMM d, y", "Mdy", 1957, 1, 4, "January 4, 1957" );
        chk( "(M/d/y) MMMM d", "Mdy", 1957, 1, 4, "(1/4/1957) January 4" );
        chk( "(M/d/y) ddMMM", "Mdy", 1957, 1, 4, "(1/4/1957) 04Jan" );
    }

    private void chk( String pDateFormat, String pFieldOrder, int pYear, int pMonth, int pDay, String pDateFormatToString )
    {
        chk( new DateFormat( pDateFormat ), pFieldOrder, new CalendarYMD( pYear, pMonth, pDay ), pDateFormatToString, pYear, pMonth, pDay );
    }

    private void chk( DateFormat pFormat, String pFieldOrder, CalendarYMD pDate, String pDateFormatToString, int pYear, int pMonth, int pDay )
    {
        String zExpectedStringFormY = Integers.zeroPadIt( 4, pYear );
        String zExpectedStringFormM = Integers.zeroPadIt( 2, pMonth );
        String zExpectedStringFormD = Integers.zeroPadIt( 2, pDay );
        assertEquals( pFieldOrder, pFormat.getFieldOrder() );
        assertEquals( "Year", pYear, pDate.getYear() );
        assertEquals( "Month", pMonth, pDate.getMonth() );
        assertEquals( "Day", pDay, pDate.getDay() );
        assertEquals( pDateFormatToString, pFormat.format( pDate ) );
        assertEquals( "toYMB", zExpectedStringFormY + "/" + zExpectedStringFormM + "/" + zExpectedStringFormD, pDate.toYMD() );
        assertEquals( "fromYMD", pDate, CalendarYMD.fromYMD( pDate.toYMD() ) );
        assertEquals( "toSQLvalue", zExpectedStringFormY + "-" + zExpectedStringFormM + "-" + zExpectedStringFormD, pDate.toSQLvalue() );
        assertEquals( "fromSQLvalue", pDate, CalendarYMD.fromSQLvalue( pDate.toSQLvalue() ) );

        CalendarYMD 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/02/28" );
        chk_addYears( "2000/02/29", 0, "2000/02/29" );
        chk_addYears( "2000/02/29", -1, "1999/02/28" );
    }

    private void chk_addYears( String pDateYMD, int pAdjustBy, String pExpectedYMD )
    {
        CalendarYMD od = CalendarYMD.fromYMD( pDateYMD );
        CalendarYMD nd = od.addYears( pAdjustBy );
        assertEquals( pExpectedYMD, nd.toYMD() );
        if ( pAdjustBy == 0 )
        {
            assertTrue( od == nd );
            assertTrue( 0 == od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else if ( pAdjustBy > 0 )
        {
            assertTrue( 0 > od.compareTo( nd ) );
            assertFalse( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else // pAdjustBy < 0
        {
            assertTrue( 0 < od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertFalse( od.beforeOrEqual( nd ) );
        }
    }

    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", 0, "2000/02/29" );
        chk_minusYears( "2000/02/29", -1, "2001/02/28" );
    }

    private void chk_minusYears( String pDateYMD, int pAdjustBy, String pExpectedYMD )
    {
        CalendarYMD od = CalendarYMD.fromYMD( pDateYMD );
        CalendarYMD nd = od.minusYears( pAdjustBy );
        assertEquals( pExpectedYMD, nd.toYMD() );
        if ( pAdjustBy == 0 )
        {
            assertTrue( od == nd );
            assertTrue( 0 == od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else if ( pAdjustBy < 0 )
        {
            assertTrue( 0 > od.compareTo( nd ) );
            assertFalse( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else // pAdjustBy > 0
        {
            assertTrue( 0 < od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertFalse( od.beforeOrEqual( nd ) );
        }
    }

    public void test_addMonths()
    {
        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/02/28" );
        chk_addMonths( "2000/02/29", 0, "2000/02/29" );
        chk_addMonths( "2000/02/29", -12, "1999/02/28" );
    }

    private void chk_addMonths( String pDateYMD, int pAdjustBy, String pExpectedYMD )
    {
        CalendarYMD od = CalendarYMD.fromYMD( pDateYMD );
        CalendarYMD nd = od.addMonths( pAdjustBy );
        assertEquals( pExpectedYMD, nd.toYMD() );
        if ( pAdjustBy == 0 )
        {
            assertTrue( od == nd );
            assertTrue( 0 == od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else if ( pAdjustBy > 0 )
        {
            assertTrue( 0 > od.compareTo( nd ) );
            assertFalse( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else // pAdjustBy < 0
        {
            assertTrue( 0 < od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertFalse( od.beforeOrEqual( nd ) );
        }
    }

    public void test_minusMonths()
    {
        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", 0, "2000/02/29" );
        chk_minusMonths( "2000/02/29", -12, "2001/02/28" );
    }

    private void chk_minusMonths( String pDateYMD, int pAdjustBy, String pExpectedYMD )
    {
        CalendarYMD od = CalendarYMD.fromYMD( pDateYMD );
        CalendarYMD nd = od.minusMonths( pAdjustBy );
        assertEquals( pExpectedYMD, nd.toYMD() );
        if ( pAdjustBy == 0 )
        {
            assertTrue( od == nd );
            assertTrue( 0 == od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else if ( pAdjustBy < 0 )
        {
            assertTrue( 0 > od.compareTo( nd ) );
            assertFalse( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else // pAdjustBy > 0
        {
            assertTrue( 0 < od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertFalse( od.beforeOrEqual( nd ) );
        }
    }

    public void test_addDays_daysTill()
    {
        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 )
    {
        CalendarYMD od = CalendarYMD.fromYMD( pDateYMD );
        CalendarYMD nd = od.addDays( pAdjustBy );
        assertEquals( pExpectedYMD, nd.toYMD() );
        assertEquals( pAdjustBy, od.daysTill( nd ) );
        if ( pAdjustBy == 0 )
        {
            assertTrue( od == nd );
            assertTrue( 0 == od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else if ( pAdjustBy > 0 )
        {
            assertTrue( 0 > od.compareTo( nd ) );
            assertFalse( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else // pAdjustBy < 0
        {
            assertTrue( 0 < od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertFalse( od.beforeOrEqual( nd ) );
        }
    }

    public void test_minusDays_daysTill()
    {
        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 )
    {
        CalendarYMD od = CalendarYMD.fromYMD( pDateYMD );
        CalendarYMD nd = od.minusDays( pAdjustBy );
        assertEquals( pExpectedYMD, nd.toYMD() );
        assertEquals( -pAdjustBy, od.daysTill( nd ) );
        if ( pAdjustBy == 0 )
        {
            assertTrue( od == nd );
            assertTrue( 0 == od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else if ( pAdjustBy < 0 )
        {
            assertTrue( 0 > od.compareTo( nd ) );
            assertFalse( od.afterOrEqual( nd ) );
            assertTrue( od.beforeOrEqual( nd ) );
        }
        else // pAdjustBy > 0
        {
            assertTrue( 0 < od.compareTo( nd ) );
            assertTrue( od.afterOrEqual( nd ) );
            assertFalse( od.beforeOrEqual( nd ) );
        }
    }

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

Commits for litesoft/trunk/Java/core/Anywhere/tests/org/litesoft/core/simpletypes/temporal/CalendarYMDTest.java

Diff revisions: vs.
Revision Author Commited Message
912 Diff Diff GeorgeS picture GeorgeS Fri 28 Jun, 2013 06:48:05 +0000

Revert to mixed & Working (PEDS & Prioritizer) code!

897 Diff Diff GeorgeS picture GeorgeS Mon 25 Feb, 2013 02:04:34 +0000
895 Diff Diff GeorgeS picture GeorgeS Mon 04 Feb, 2013 01:12:49 +0000
893 Diff Diff GeorgeS picture GeorgeS Mon 14 Jan, 2013 01:18:30 +0000
873 Diff Diff GeorgeS picture GeorgeS Tue 27 Nov, 2012 17:41:11 +0000

!

869 GeorgeS picture GeorgeS Mon 26 Nov, 2012 01:33:04 +0000