001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.emultest.java.util;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: import java.util.Date;
021:
022: /**
023: * Tests for GWT's emulation of the JRE Date class.
024: */
025: public class DateTest extends GWTTestCase {
026: public static final String CURRENT = "CURRENT";
027: public static final String TO_STRING_PATTERN = "\\w{3} \\w{3} \\d{2} \\d{2}:\\d{2}:\\d{2}( .+)? \\d{4}";
028: public static final long DAY_MILLISECONDS_SHIFT = 27;
029: public static final String FUTURE = "FUTURE";
030: public static final String PAST = "PAST";
031: public static final long SECOND_MILLISECONDS_SHIFT = 10;
032:
033: /** Sets module name so that javascript compiler can operate */
034: public String getModuleName() {
035: return "com.google.gwt.emultest.EmulSuite";
036: }
037:
038: /** Testing for public boolean java.util.Date.after(java.util.Date)* */
039: public void testAfter() {
040:
041: // /////////////////////////////
042: // Current
043: // /////////////////////////////
044: Date accum0 = create();
045: Date arg10 = create();
046: boolean a0 = accum0.after(arg10);
047: assertFalse(a0);
048: // /////////////////////////////
049: // Past
050: // /////////////////////////////
051: Date accum1 = create(PAST);
052: Date arg20 = create();
053: boolean a1 = accum1.after(arg20);
054: assertFalse(a1);
055:
056: // /////////////////////////////
057: // Future
058: // /////////////////////////////
059: Date accum2 = create(FUTURE);
060: Date arg30 = create();
061: boolean a2 = accum2.after(arg30);
062: assertTrue(a2);
063: }
064:
065: /** Testing for public boolean java.util.Date.before(java.util.Date)* */
066: public void testBefore() {
067:
068: // /////////////////////////////
069: // Current
070: // /////////////////////////////
071: Date accum0 = create();
072: Date arg10 = create();
073: boolean a0 = accum0.before(arg10);
074: assertFalse(a0);
075: // /////////////////////////////
076: // Past
077: // /////////////////////////////
078: Date accum1 = create(PAST);
079: Date arg20 = create();
080: boolean a1 = accum1.before(arg20);
081: assertTrue(a1);
082:
083: // /////////////////////////////
084: // Future
085: // /////////////////////////////
086: Date accum2 = create(FUTURE);
087: Date arg30 = create();
088: boolean a2 = accum2.before(arg30);
089: assertFalse(a2);
090: }
091:
092: /** Testing for public java.lang.Object java.util.Date.clone()* */
093: public void testClone() {
094:
095: // /////////////////////////////
096: // Current
097: // /////////////////////////////
098: Date accum0 = create();
099: Object a0 = accum0.clone();
100: assertFalse(a0 == accum0);
101: assertEquals(a0, accum0);
102: // /////////////////////////////
103: // Past
104: // /////////////////////////////
105: Date accum1 = create(PAST);
106: Object a1 = accum1.clone();
107: assertFalse(a1 == accum1);
108: assertEquals(a1, accum1);
109:
110: // /////////////////////////////
111: // Future
112: // /////////////////////////////
113: Date accum2 = create(FUTURE);
114: Object a2 = accum2.clone();
115: assertFalse(a2 == accum2);
116: assertEquals(a2, accum2);
117: }
118:
119: /** Testing for public int java.util.Date.compareTo(java.util.Date)* */
120: public void testCompareTo() {
121:
122: // /////////////////////////////
123: // Current
124: // /////////////////////////////
125: Date accum0 = create();
126: Date arg10 = create();
127: int a0 = accum0.compareTo(arg10);
128: assertEquals(a0, 0);
129: // /////////////////////////////
130: // Past
131: // /////////////////////////////
132: Date accum1 = create();
133: Date arg20 = create(PAST);
134: int a1 = accum1.compareTo(arg20);
135: assertEquals(a1, 1);
136: // /////////////////////////////
137: // Future
138: // /////////////////////////////
139: Date accum2 = create();
140: Date arg30 = create(FUTURE);
141: int a2 = accum2.compareTo(arg30);
142: assertEquals(a2, -1);
143: }
144:
145: /** Testing for public int java.util.Date.getDate()* */
146: public void testGetDate() {
147:
148: // /////////////////////////////
149: // Past
150: // /////////////////////////////
151: Date accum1 = create(PAST);
152: int a1 = accum1.getDate();
153: assertEquals(4, a1);
154: // /////////////////////////////
155: // Future
156: // /////////////////////////////
157: Date accum2 = create(FUTURE);
158: int a2 = accum2.getDate();
159: assertEquals(29, a2);
160: }
161:
162: /** Testing for public int java.util.Date.getDay()* */
163: public void testGetDay() {
164:
165: // /////////////////////////////
166: // Current
167: // /////////////////////////////
168: Date accum0 = create();
169: int a0 = accum0.getDay();
170:
171: // /////////////////////////////
172: // Past
173: // /////////////////////////////
174: Date accum1 = create(PAST);
175: int a1 = accum1.getDay();
176:
177: // /////////////////////////////
178: // Future
179: // /////////////////////////////
180: Date accum2 = create(FUTURE);
181: int a2 = accum2.getDay();
182: }
183:
184: /**
185: * Testing for public int java.util.Date.getHours()
186: */
187: public void testGetHours() {
188: // Cannot be done because each time zone will give a different
189: // answer
190: }
191:
192: /** Testing for public int java.util.Date.getMinutes()* */
193: public void testGetMinutes() {
194:
195: // /////////////////////////////
196: // Past
197: // /////////////////////////////
198: Date accum1 = create(PAST);
199: int a1 = accum1.getMinutes();
200: assertEquals(a1, 0);
201: // /////////////////////////////
202: // Future
203: // /////////////////////////////
204: Date accum2 = create(FUTURE);
205: int a2 = accum2.getMinutes();
206: assertEquals(a2, 4);
207: }
208:
209: /** Testing for public int java.util.Date.getMonth()* */
210: public void testGetMonth() {
211:
212: // /////////////////////////////
213: // Past
214: // /////////////////////////////
215: Date accum1 = create(PAST);
216: int a1 = accum1.getMonth();
217: assertEquals(0, a1);
218:
219: // /////////////////////////////
220: // Future
221: // /////////////////////////////
222: Date accum2 = create(FUTURE);
223: int a2 = accum2.getMonth();
224: assertEquals(11, a2);
225: }
226:
227: /** Testing for public int java.util.Date.getSeconds()* */
228: public void testGetSeconds() {
229:
230: // /////////////////////////////
231: // Past
232: // /////////////////////////////
233: Date accum1 = create(PAST);
234: int a1 = accum1.getSeconds();
235: assertEquals(0, a1);
236:
237: // /////////////////////////////
238: // Future
239: // /////////////////////////////
240: Date accum2 = create(FUTURE);
241: int a2 = accum2.getSeconds();
242: assertEquals(5, a2);
243: }
244:
245: /** Testing for public long java.util.Date.getTime()* */
246: public void testGetTime() {
247:
248: // /////////////////////////////
249: // Past
250: // /////////////////////////////
251: Date accum1 = create(PAST);
252: long a1 = accum1.getTime();
253: assertEquals(-2839795200000L, a1);
254:
255: // /////////////////////////////
256: // Future
257: // /////////////////////////////
258: Date accum2 = create(FUTURE);
259: long a2 = accum2.getTime();
260: assertEquals(1293678245000L, a2);
261: }
262:
263: /** Testing for public int java.util.Date.getTimezoneOffset()* */
264: public void testGetTimezoneOffset() {
265:
266: // /////////////////////////////
267: // Current
268: // /////////////////////////////
269: Date accum0 = create();
270: int a0 = accum0.getTimezoneOffset();
271:
272: // /////////////////////////////
273: // Past
274: // /////////////////////////////
275: Date accum1 = create(PAST);
276: int a1 = accum1.getTimezoneOffset();
277:
278: // /////////////////////////////
279: // Future
280: // /////////////////////////////
281: Date accum2 = create(FUTURE);
282: int a2 = accum2.getTimezoneOffset();
283: }
284:
285: /** Testing for public int java.util.Date.getYear()* */
286: public void testGetYear() {
287:
288: // /////////////////////////////
289: // Past
290: // /////////////////////////////
291: Date accum1 = create(PAST);
292: int a1 = accum1.getYear();
293: assertEquals(a1, -20);
294:
295: // /////////////////////////////
296: // Future
297: // /////////////////////////////
298: Date accum2 = create(FUTURE);
299: int a2 = accum2.getYear();
300: assertEquals(110, a2);
301: }
302:
303: /** Testing for public static long java.util.Date.parse(java.lang.String)* */
304: public void testParse() {
305:
306: // /////////////////////////////
307: // Current
308: // /////////////////////////////
309: Date accum0 = create();
310: String arg10 = createString(CURRENT);
311: long a0 = Date.parse(arg10);
312: assertEquals(roundToDay(accum0.getTime()), roundToDay(a0));
313: // /////////////////////////////
314: // Past
315: // /////////////////////////////
316: Date accum1 = create(PAST);
317: String arg20 = createString(PAST);
318: long a1 = Date.parse(arg20);
319: assertEquals(-2840140800000L, a1);
320:
321: // /////////////////////////////
322: // Future
323: // /////////////////////////////
324: Date accum2 = create(FUTURE);
325: String arg30 = createString(FUTURE);
326: long a2 = Date.parse(arg30);
327: assertEquals(1293678245000L, a2);
328: }
329:
330: /** Testing for public void java.util.Date.setDate(int)* */
331: public void testSetDate() {
332: // We only go through dates from 0-28 here. There are some months that do
333: // not
334: // have 29, 30, or 31 days - so our assertion would be wrong in the cases
335: // where
336: // the current month did not have 29,30,or 31 days
337: for (int i = 1; i < 29; i++) {
338: Date accum0 = create();
339: accum0.setDate(i);
340: assertEquals(accum0.getDate(), i);
341: }
342: }
343:
344: /**
345: * Testing to that if we set the day number to 31 for a month that only has 30
346: * days in it, that the date rolls over to the first day of the next month in
347: * sequence.
348: */
349: public void testInvalidDateForMonth() {
350: int monthNum = 3; // April
351: int numDaysInOldMonth = 30;
352: int newDayNum = 31;
353: Date dateWithThirtyDays = new Date(2006, monthNum, 30);
354: dateWithThirtyDays.setDate(newDayNum);
355: assertEquals(dateWithThirtyDays.getMonth(), monthNum + 1);
356: assertEquals(dateWithThirtyDays.getDate(), newDayNum
357: - numDaysInOldMonth);
358: }
359:
360: /** Testing for public void java.util.Date.setHours(int)* */
361: public void testSetHours() {
362: for (int i = 0; i < 24; i++) {
363: Date accum0 = create();
364: accum0.setHours(i);
365: assertEquals(accum0.getHours(), i);
366: }
367: }
368:
369: /** Testing for public void java.util.Date.setMinutes(int)* */
370: public void testSetMinutes() {
371: for (int i = 0; i < 24; i++) {
372: Date accum0 = create();
373: accum0.setMinutes(i);
374: assertEquals(accum0.getMinutes(), i);
375: }
376: }
377:
378: /** Testing for public void java.util.Date.setMonth(int)* */
379: public void testSetMonth() {
380: for (int i = 0; i < 12; i++) {
381: // We want to use a fixed date here. If we use the current date, the
382: // assertion may fail
383: // when the date is the 29th, 30th, or 31st, and we set the month to one
384: // which does
385: // not have 29, 30, or 31 days in it, respectively.
386: Date accum0 = new Date(2006, 12, 1);
387: accum0.setMonth(i);
388: assertEquals(accum0.getMonth(), i);
389: }
390: }
391:
392: /**
393: * We want to test to see that if we are currently in a month with 31 days and
394: * we set the month to one which has less than 31 days, that the month
395: * returned by the date class will be one higher than the month that we
396: * originally set (according to the spec of java.util.date)
397: */
398: public void testSetInvalidMonthForDate() {
399: int dayNum = 31;
400: int newMonthNum = 1;
401: int numDaysInNewMonth = 28;
402: Date dateWithThirtyOneDays = new Date(2006, 12, dayNum);
403: dateWithThirtyOneDays.setMonth(newMonthNum);
404: assertEquals(dateWithThirtyOneDays.getMonth(), newMonthNum + 1);
405: assertEquals(dateWithThirtyOneDays.getDate(), dayNum
406: - numDaysInNewMonth);
407: }
408:
409: /** Testing for public void java.util.Date.setSeconds(int)* */
410: public void testSetSeconds() {
411: for (int i = 0; i < 24; i++) {
412: Date accum0 = create();
413: accum0.setSeconds(i);
414: assertEquals(accum0.getSeconds(), i);
415: }
416: }
417:
418: /** Testing for public void java.util.Date.setTime(long)* */
419: public void testSetTime() {
420: long[] values = new long[] { -100000000000L, -100L, 0, 100L,
421: 1000000000L };
422: for (int i = 0; i < values.length; i++) {
423: Date accum0 = create();
424: accum0.setTime(values[i]);
425: assertEquals(accum0.getTime(), values[i]);
426: }
427: }
428:
429: /** Testing for public void java.util.Date.setYear(int)* */
430: public void testSetYear() {
431: for (int i = 1880; i < 2050; i++) {
432: // We want to use a fixed date here. If we use the current date, the
433: // assertion may fail
434: // when the date is February 29th, and we set the year to a non-leap year
435: Date accum0 = new Date(2006, 12, 01);
436: accum0.setYear(i);
437: assertEquals(accum0.getYear(), i);
438: }
439: }
440:
441: /**
442: * We want to test to see that if the date is Feb 29th (in a leap year) and we
443: * set the year to a non-leap year, that the month and day will roll over to
444: * March 1st.
445: */
446: public void testSetInvalidYearForDate() {
447: int dayNum = 29;
448: int monthNum = 1; // February
449: int newYearNum = 2005;
450: int numDaysInFebInNewYear = 28;
451: Date leapYearDate = new Date(2004, monthNum, dayNum);
452: leapYearDate.setYear(newYearNum);
453: assertEquals(leapYearDate.getYear(), newYearNum);
454: assertEquals(leapYearDate.getMonth(), monthNum + 1);
455: assertEquals(leapYearDate.getDate(), dayNum
456: - numDaysInFebInNewYear);
457: }
458:
459: /**
460: * We want to test to see that if the date is Feb 29th (in a leap year) and we
461: * set the year to another leap year, that the month and day will be retained
462: */
463: public void testSetValidLeapYearForDate() {
464: int dayNum = 29;
465: int monthNum = 1; // February
466: int yearNum = 2004;
467: int newYearNum = yearNum + 4;
468: Date leapYearDate = new Date(yearNum, monthNum, dayNum);
469: leapYearDate.setYear(newYearNum);
470: assertEquals(leapYearDate.getYear(), newYearNum);
471: assertEquals(leapYearDate.getMonth(), monthNum);
472: assertEquals(leapYearDate.getDate(), dayNum);
473: }
474:
475: /** Testing for public java.lang.String java.util.Date.toGMTString()* */
476: public void testToGMTString() {
477:
478: // /////////////////////////////
479: // Past
480: // /////////////////////////////
481: Date accum1 = create(PAST);
482: String a1 = accum1.toGMTString();
483: assertEquals("5 Jan 1880 00:00:00 GMT", a1);
484:
485: // /////////////////////////////
486: // Future
487: // /////////////////////////////
488: Date accum2 = create(FUTURE);
489: String a2 = accum2.toGMTString();
490: assertEquals("30 Dec 2010 03:04:05 GMT", a2);
491: }
492:
493: /** Testing for public java.lang.String java.util.Date.toLocaleString()* */
494: public void testToLocaleString() {
495:
496: // /////////////////////////////
497: // Past
498: // /////////////////////////////
499: Date accum1 = create(PAST);
500: String a1 = accum1.toLocaleString();
501: assertTrue(a1.indexOf("1880") != -1);
502: // /////////////////////////////
503: // Future
504: // /////////////////////////////
505: Date accum2 = create(FUTURE);
506: String a2 = accum2.toLocaleString();
507: assertTrue(a2.indexOf("2010") != -1);
508: }
509:
510: /** Date docs specify an exact format for toString() */
511: public void testToString() {
512: // /////////////////////////////
513: // Past
514: // /////////////////////////////
515: Date d = create(PAST);
516: String s = d.toString();
517:
518: assertTrue("Bad format " + s, s.matches(TO_STRING_PATTERN));
519: assertEquals("Parsing returned unequal dates from " + s, d,
520: new Date(Date.parse(s)));
521:
522: // /////////////////////////////
523: // Future
524: // /////////////////////////////
525: d = create(FUTURE);
526: s = d.toString();
527:
528: assertTrue("Bad format " + s, s.matches(TO_STRING_PATTERN));
529: assertEquals("Parsing returned unequal dates from " + s, d,
530: new Date(Date.parse(s)));
531: }
532:
533: /** Testing for public static long java.util.Date.UTC(int,int,int,int,int,int)* */
534: public void testUTC() {
535:
536: // /////////////////////////////
537: // Current
538: // /////////////////////////////
539: Date accum0 = create();
540: int arg10 = 0;
541: int arg11 = 0;
542: int arg12 = 0;
543: int arg13 = 0;
544: int arg14 = 0;
545: int arg15 = 0;
546: long a0 = accum0.UTC(arg10, arg11, arg12, arg13, arg14, arg15);
547:
548: // /////////////////////////////
549: // Past
550: // /////////////////////////////
551: Date accum1 = create(PAST);
552: int arg20 = 0;
553: int arg21 = 0;
554: int arg22 = 0;
555: int arg23 = 0;
556: int arg24 = 0;
557: int arg25 = 0;
558: long a1 = accum1.UTC(arg20, arg21, arg22, arg23, arg24, arg25);
559:
560: // /////////////////////////////
561: // Future
562: // /////////////////////////////
563: Date accum2 = create(FUTURE);
564: int arg30 = 0;
565: int arg31 = 0;
566: int arg32 = 0;
567: int arg33 = 0;
568: int arg34 = 0;
569: int arg35 = 0;
570: long a2 = accum2.UTC(arg30, arg31, arg32, arg33, arg34, arg35);
571: }
572:
573: Date create() {
574: return (Date) theDate.clone();
575: }
576:
577: Date create(String s) {
578: if (s.equals(FUTURE)) {
579: return new Date("12/30/2010 3:4:5 GMT");
580: } else if (s.equals(PAST)) {
581: return new Date("1/5/1880 GMT");
582: } else {
583: return (Date) theDate.clone();
584: }
585: }
586:
587: private String createString(String s) {
588: if (s.equals(FUTURE)) {
589: return "12/30/2010 3:4:5 GMT";
590: } else if (s.equals(PAST)) {
591: return "1/1/1880 GMT";
592: } else {
593: return theDate.toLocaleString();
594: }
595: }
596:
597: private long roundToDay(long accum0) {
598: return accum0 >> DAY_MILLISECONDS_SHIFT << DAY_MILLISECONDS_SHIFT;
599: }
600:
601: Date theDate = new Date();
602: }
|