001: /*
002: * Copyright 2004-2005 Sun Microsystems, Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of 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,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package com.sun.syndication.unittest;
018:
019: import com.sun.syndication.io.impl.DateParser;
020: import junit.framework.TestCase;
021:
022: import java.util.Calendar;
023: import java.util.GregorianCalendar;
024: import java.util.TimeZone;
025: import java.util.Date;
026:
027: /**
028: *
029: * Start of tests for DateParser
030: *
031: * @author Nick Lothian
032: *
033: */
034: public class TestDateParser extends TestCase {
035: public void testParse() {
036: Calendar cal = new GregorianCalendar();
037: cal.setTimeZone(TimeZone.getTimeZone("GMT"));
038:
039: // four-digit year
040: String sDate = "Tue, 19 Jul 2005 23:00:51 GMT";
041: cal.setTime(DateParser.parseRFC822(sDate));
042:
043: assertEquals(2005, cal.get(Calendar.YEAR));
044: assertEquals(6, cal.get(Calendar.MONTH)); // month is zero-indexed
045: assertEquals(19, cal.get(Calendar.DAY_OF_MONTH));
046: assertEquals(3, cal.get(Calendar.DAY_OF_WEEK));
047: assertEquals(23, cal.get(Calendar.HOUR_OF_DAY));
048: assertEquals(0, cal.get(Calendar.MINUTE));
049: assertEquals(51, cal.get(Calendar.SECOND));
050:
051: // two-digit year
052: sDate = "Tue, 19 Jul 05 23:00:51 GMT";
053: cal.setTime(DateParser.parseRFC822(sDate));
054:
055: assertEquals(2005, cal.get(Calendar.YEAR));
056: assertEquals(6, cal.get(Calendar.MONTH)); // month is zero-indexed
057: assertEquals(19, cal.get(Calendar.DAY_OF_MONTH));
058: assertEquals(3, cal.get(Calendar.DAY_OF_WEEK));
059: assertEquals(23, cal.get(Calendar.HOUR_OF_DAY));
060: assertEquals(0, cal.get(Calendar.MINUTE));
061: assertEquals(51, cal.get(Calendar.SECOND));
062:
063: // four-digit year
064: sDate = "Tue, 19 Jul 2005 23:00:51 UT";
065: cal.setTime(DateParser.parseRFC822(sDate));
066:
067: assertEquals(2005, cal.get(Calendar.YEAR));
068: assertEquals(6, cal.get(Calendar.MONTH)); // month is zero-indexed
069: assertEquals(19, cal.get(Calendar.DAY_OF_MONTH));
070: assertEquals(3, cal.get(Calendar.DAY_OF_WEEK));
071: assertEquals(23, cal.get(Calendar.HOUR_OF_DAY));
072: assertEquals(0, cal.get(Calendar.MINUTE));
073: assertEquals(51, cal.get(Calendar.SECOND));
074:
075: // two-digit year
076: sDate = "Tue, 19 Jul 05 23:00:51 UT";
077: cal.setTime(DateParser.parseRFC822(sDate));
078:
079: assertEquals(2005, cal.get(Calendar.YEAR));
080: assertEquals(6, cal.get(Calendar.MONTH)); // month is zero-indexed
081: assertEquals(19, cal.get(Calendar.DAY_OF_MONTH));
082: assertEquals(3, cal.get(Calendar.DAY_OF_WEEK));
083: assertEquals(23, cal.get(Calendar.HOUR_OF_DAY));
084: assertEquals(0, cal.get(Calendar.MINUTE));
085: assertEquals(51, cal.get(Calendar.SECOND));
086:
087: //RFC822
088: sDate = "Tue, 19 Jul 2005 23:00:51 GMT";
089: assertNotNull(DateParser.parseDate(sDate));
090:
091: //RFC822
092: sDate = "Tue, 19 Jul 05 23:00:51 GMT";
093: assertNotNull(DateParser.parseDate(sDate));
094:
095: Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
096: c.set(2000, Calendar.JANUARY, 01, 0, 0, 0);
097: Date expectedDate = c.getTime();
098:
099: //W3C
100: sDate = "2000-01-01T00:00:00Z";
101: assertEquals(expectedDate.getTime() / 1000, DateParser
102: .parseDate(sDate).getTime() / 1000);
103:
104: //W3C
105: sDate = "2000-01-01T00:00Z";
106: assertEquals(expectedDate.getTime() / 1000, DateParser
107: .parseDate(sDate).getTime() / 1000);
108:
109: //W3C
110: sDate = "2000-01-01";
111: assertEquals(expectedDate.getTime() / 1000, DateParser
112: .parseDate(sDate).getTime() / 1000);
113:
114: //W3C
115: sDate = "2000-01";
116: assertEquals(expectedDate.getTime() / 1000, DateParser
117: .parseDate(sDate).getTime() / 1000);
118:
119: //W3C
120: sDate = "2000";
121: assertEquals(expectedDate.getTime() / 1000, DateParser
122: .parseDate(sDate).getTime() / 1000);
123:
124: //EXTRA
125: sDate = "18:10 2000/10/10";
126: assertNotNull(DateParser.parseDate(sDate));
127:
128: //INVALID
129: sDate = "X20:10 2000-10-10";
130: assertNull(DateParser.parseDate(sDate));
131:
132: }
133: }
|