1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/tests/test/org/dbforms/util/TestTimeUtil.java,v 1.5 2004/08/18 12:26:11 hkollmann Exp $
3    * $Revision: 1.5 $
4    * $Date: 2004/08/18 12:26:11 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.util;
25  
26  import java.text.SimpleDateFormat;
27  
28  import java.util.Calendar;
29  
30  
31  
32  /***
33   *  Test the Util.java class
34   *
35   * @author     epugh
36   * @created    May 3, 2002
37   */
38  public class TestTimeUtil extends AbstractTestCase {
39     private SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
40  
41     /***
42      * DOCUMENT ME!
43      */
44     public void testSecondsToString() {
45        int k = 4 + (47 * 60) + (3 * 60 * 60) + (1 * 24 * 60 * 60);
46        assertEquals("1:03:47:04", TimeUtil.seconds2String(k));
47     }
48  
49  
50     /***
51      * DOCUMENT ME!
52      */
53     public void testTimeUtil() {
54        Calendar now;
55        now = Calendar.getInstance();
56        now.set(Calendar.HOUR_OF_DAY, 12);
57        now.set(Calendar.MINUTE, 30);
58        now.set(Calendar.SECOND, 0);
59        now.set(Calendar.DAY_OF_MONTH, 12);
60        now.set(Calendar.MONTH, 11);
61        now.set(Calendar.YEAR, 2002);
62        assertTrue(check(now, "12.12.2002 12:30:00"));
63  
64        now = Calendar.getInstance();
65        now.set(Calendar.HOUR_OF_DAY, 0);
66        now.set(Calendar.MINUTE, 0);
67        now.set(Calendar.SECOND, 0);
68        now.set(Calendar.DAY_OF_MONTH, 12);
69        now.set(Calendar.MONTH, 11);
70        now.set(Calendar.YEAR, 2002);
71        assertTrue(check(now, "12.12.2002 00:00:00"));
72  
73        now = Calendar.getInstance();
74        now.set(Calendar.HOUR_OF_DAY, 12);
75        now.set(Calendar.MINUTE, 30);
76        now.set(Calendar.SECOND, 0);
77        now.set(Calendar.DAY_OF_MONTH, 25);
78        now.set(Calendar.MONTH, 9);
79        now.set(Calendar.YEAR, 2003);
80        assertTrue(check(now, "25.10.2003 12:30:00"));
81  
82        now = Calendar.getInstance();
83        now.set(Calendar.HOUR_OF_DAY, 12);
84        now.set(Calendar.MINUTE, 30);
85        now.set(Calendar.SECOND, 0);
86        now.set(Calendar.DAY_OF_MONTH, 27);
87        now.set(Calendar.MONTH, 9);
88        now.set(Calendar.YEAR, 2003);
89        assertTrue(check(now, "27.10.2003 12:30:00"));
90  
91        now = Calendar.getInstance();
92        now.set(Calendar.HOUR_OF_DAY, 12);
93        now.set(Calendar.MINUTE, 30);
94        now.set(Calendar.SECOND, 0);
95        now.set(Calendar.DAY_OF_MONTH, 26);
96        now.set(Calendar.MONTH, 9);
97        now.set(Calendar.YEAR, 2003);
98  
99        // here we have bug in the java daylight switching code!!!!
100       assertFalse(check(now, "26.10.2003 12:30:00"));
101 
102       now = Calendar.getInstance();
103       now.set(Calendar.HOUR_OF_DAY, 12);
104       now.set(Calendar.MINUTE, 30);
105       now.set(Calendar.SECOND, 0);
106       now.set(Calendar.DAY_OF_MONTH, 12);
107       now.set(Calendar.MONTH, 5);
108       assertTrue(check(now, "12.06 12:30"));
109 
110       now = Calendar.getInstance();
111       now.set(Calendar.HOUR_OF_DAY, 12);
112       now.set(Calendar.MINUTE, 30);
113       now.set(Calendar.SECOND, 0);
114       now.set(Calendar.DAY_OF_MONTH, 12);
115       now.set(Calendar.MONTH, 11);
116       assertTrue(check(now, "12.12 12:30"));
117 
118       now = Calendar.getInstance();
119       now.set(Calendar.HOUR_OF_DAY, 12);
120       now.set(Calendar.MINUTE, 30);
121       now.set(Calendar.SECOND, 0);
122       now.set(Calendar.DAY_OF_MONTH, 12);
123       assertTrue(check(now, "12. 12:30"));
124 
125       now = Calendar.getInstance();
126       now.set(Calendar.HOUR_OF_DAY, 0);
127       now.set(Calendar.MINUTE, 0);
128       now.set(Calendar.SECOND, 0);
129       now.set(Calendar.DAY_OF_MONTH, 12);
130       assertTrue(check(now, "12."));
131 
132       now = Calendar.getInstance();
133       now.set(Calendar.HOUR_OF_DAY, 0);
134       now.set(Calendar.MINUTE, 0);
135       now.set(Calendar.SECOND, 0);
136       now.set(Calendar.DAY_OF_MONTH, 12);
137       now.set(Calendar.MONTH, 5);
138       assertTrue(check(now, "12.06"));
139 
140       now = Calendar.getInstance();
141       now.set(Calendar.HOUR_OF_DAY, 0);
142       now.set(Calendar.MINUTE, 0);
143       now.set(Calendar.SECOND, 0);
144       now.set(Calendar.DAY_OF_MONTH, 12);
145       now.set(Calendar.MONTH, 5);
146       now.set(Calendar.YEAR, 2003);
147       assertTrue(check(now, "12.06.03"));
148 
149       now = Calendar.getInstance();
150       now.set(Calendar.HOUR_OF_DAY, 0);
151       now.set(Calendar.MINUTE, 0);
152       now.set(Calendar.SECOND, 0);
153       now.set(Calendar.DAY_OF_MONTH, 12);
154       now.set(Calendar.MONTH, 11);
155       now.set(Calendar.YEAR, 2003);
156       assertTrue(check(now, "12.12.03"));
157 
158       now = Calendar.getInstance();
159       now.set(Calendar.HOUR_OF_DAY, 12);
160       now.set(Calendar.MINUTE, 0);
161       now.set(Calendar.SECOND, 0);
162       now.set(Calendar.DAY_OF_MONTH, 12);
163       assertTrue(check(now, "12. 12:"));
164 
165       now = Calendar.getInstance();
166       now.set(Calendar.HOUR_OF_DAY, 12);
167       now.set(Calendar.MINUTE, 30);
168       now.set(Calendar.SECOND, 0);
169       assertTrue(check(now, "12:30"));
170 
171       now = Calendar.getInstance();
172       now.set(Calendar.HOUR_OF_DAY, 12);
173       now.set(Calendar.MINUTE, 30);
174       now.set(Calendar.SECOND, 0);
175       now.set(Calendar.DAY_OF_MONTH, 12);
176       now.set(Calendar.MONTH, 11);
177       now.set(Calendar.YEAR, 2002);
178       assertTrue(check(now, "2002-12-12 12:30:00"));
179    }
180 
181 
182    private boolean check(Calendar now,
183                          String   s) {
184       String s1 = format.format(now.getTime());
185       System.out.println("parsing results");
186       System.out.println("===============");
187 
188       String s2 = format.format(TimeUtil.parseDate(format, s).getTime());
189       System.out.println(s);
190       System.out.println(s1);
191       System.out.println(s2);
192 
193       return s1.equals(s2);
194    }
195 }