KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > parser > DateParserTest


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.parser;
37
38 import java.util.Date JavaDoc;
39 import java.util.GregorianCalendar JavaDoc;
40 import java.util.TimeZone JavaDoc;
41
42 import junit.framework.Assert;
43 import junit.framework.TestCase;
44
45 public class DateParserTest extends TestCase {
46
47     /**
48      * Constructor for DateParserTest.
49      * @param arg0
50      */

51     public DateParserTest(String JavaDoc arg0) {
52         super(arg0);
53     }
54
55     public void testParseString1() {
56 // day, month, year, hour, minute
57
String JavaDoc testData = "07 Mar 2003 19:20";
58
59         Date JavaDoc date = null;
60         try {
61             date = DateParser.parse(testData);
62         } catch (ParserException e) {
63             Assert.assertTrue(false);
64         }
65
66         GregorianCalendar JavaDoc c = new GregorianCalendar JavaDoc();
67         c.clear();
68         c.setTimeZone(TimeZone.getTimeZone("GMT"));
69         // year, month, date, hour, second
70
c.set(2003, 2, 7, 19, 20, 0);
71         Date JavaDoc testDate = c.getTime();
72
73         Assert.assertTrue( testDate.equals(date) );
74     }
75
76     public void testParseString7() {
77 // day, month, year, hour, minute
78
String JavaDoc testData = "Wed May 25 12:59:16 2005 +0100";
79
80         Date JavaDoc date = null;
81         try {
82             date = DateParser.parse(testData);
83         } catch (ParserException e) {
84             Assert.assertTrue(false);
85         }
86
87         GregorianCalendar JavaDoc c = new GregorianCalendar JavaDoc();
88         c.clear();
89         c.setTimeZone(TimeZone.getTimeZone("GMT+0100"));
90         // year, month, date, hour, second
91
c.set(2005, 4, 25, 12, 59, 16);
92         Date JavaDoc testDate = c.getTime();
93
94         Assert.assertEquals( testDate, date );
95     }
96     
97     
98     public void testParseString2() {
99         String JavaDoc testStr = "Thu, 6 Feb 2003 11:05:12 +0100";
100         
101         Date JavaDoc date = null;
102         try {
103             date = DateParser.parse(testStr);
104         } catch (ParserException e) {
105             Assert.assertTrue(false);
106         }
107         GregorianCalendar JavaDoc c = new GregorianCalendar JavaDoc();
108         c.clear();
109         c.setTimeZone(TimeZone.getTimeZone("GMT+0100"));
110         // year, month, date, hour, second
111
c.set(2003, 1, 6, 11,5,12);
112         Date JavaDoc testDate = c.getTime();
113         
114         assertTrue(testDate.equals(date));
115     }
116     
117     public void testParseString3() {
118         String JavaDoc testStr = "19 Jun 2003 09:46 PDT";
119         
120         Date JavaDoc date = null;
121         try {
122             date = DateParser.parse(testStr);
123         } catch (ParserException e) {
124             Assert.assertTrue(false);
125         }
126         GregorianCalendar JavaDoc c = new GregorianCalendar JavaDoc();
127         c.clear();
128         c.setTimeZone(TimeZone.getTimeZone("PDT"));
129         // year, month, date, hour, second
130
c.set(2003, 5, 19, 9, 46, 0);
131         Date JavaDoc testDate = c.getTime();
132         assertTrue(testDate.equals(date));
133     }
134     
135     public void testParseString4() {
136         String JavaDoc testStr = "Thu, 17 Apr 03 10:06 -0400";
137         Date JavaDoc date = null;
138         try {
139             date = DateParser.parse(testStr);
140         } catch (ParserException e) {
141             Assert.assertTrue(false);
142         }
143
144         GregorianCalendar JavaDoc c = new GregorianCalendar JavaDoc();
145         c.clear();
146         c.setTimeZone(TimeZone.getTimeZone("GMT-0400"));
147
148         // year, month, date, hour, second
149
c.set(2003, 3, 17, 10, 6, 0);
150         Date JavaDoc testDate = c.getTime();
151
152         assertTrue(testDate.equals(date));
153     }
154
155     public void testParseString5() {
156         String JavaDoc testStr = "Wed, 2 July 2003 18:12:33 +0000";
157         
158         Date JavaDoc date = null;
159         try {
160             date = DateParser.parse(testStr);
161         } catch (ParserException e) {
162             Assert.assertTrue(false);
163         }
164         GregorianCalendar JavaDoc c = new GregorianCalendar JavaDoc();
165         c.clear();
166         c.setTimeZone(TimeZone.getTimeZone("GMT"));
167         // year, month, date, hour, second
168
c.set(2003, 6, 2, 18, 12, 33);
169         Date JavaDoc testDate = c.getTime();
170         assertTrue(testDate.equals(date));
171     }
172     
173     public void testParseString6() {
174         String JavaDoc testStr = " Wed, 2\tJuly 2003 18:12:33 +0000 ";
175         
176         Date JavaDoc date = null;
177         try {
178             date = DateParser.parse(testStr);
179         } catch (ParserException e) {
180             Assert.assertTrue(false);
181         }
182         GregorianCalendar JavaDoc c = new GregorianCalendar JavaDoc();
183         c.clear();
184         c.setTimeZone(TimeZone.getTimeZone("GMT"));
185         // year, month, date, hour, second
186
c.set(2003, 6, 2, 18, 12, 33);
187         Date JavaDoc testDate = c.getTime();
188         assertTrue(testDate.equals(date));
189     }
190
191     public void testParseLeapYear1() {
192         String JavaDoc testStr = "Tue, 2 Mar 2004 18:12:33 +0000 ";
193         
194         Date JavaDoc date = null;
195         try {
196             date = DateParser.parse(testStr);
197         } catch (ParserException e) {
198             Assert.assertTrue(false);
199         }
200         GregorianCalendar JavaDoc c = new GregorianCalendar JavaDoc();
201         c.clear();
202         c.setTimeZone(TimeZone.getTimeZone("GMT"));
203         // year, month, date, hour, second
204
c.set(2004, 2, 2, 18, 12, 33);
205         Date JavaDoc testDate = c.getTime();
206         assertEquals(testDate, date);
207     }
208
209     public void testParseLeapYear2() {
210         String JavaDoc testStr = "Tue, 29 Feb 2004 18:12:33 +0000 ";
211         
212         Date JavaDoc date = null;
213         try {
214             date = DateParser.parse(testStr);
215         } catch (ParserException e) {
216             Assert.assertTrue(false);
217         }
218         GregorianCalendar JavaDoc c = new GregorianCalendar JavaDoc();
219         c.clear();
220         c.setTimeZone(TimeZone.getTimeZone("GMT"));
221         // year, month, date, hour, second
222
c.set(2004, 1, 29, 18, 12, 33);
223         Date JavaDoc testDate = c.getTime();
224         assertEquals(testDate, date);
225     }
226 }
227
Popular Tags