1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH München (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo.util;
24  
25  import java.util.Calendar;
26  import java.util.Date;
27  import java.util.TimeZone;
28  
29  import junit.framework.TestCase;
30  
31  /***
32   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
33   */
34  public final class GregorianTest extends TestCase {
35  
36      /***
37       * @param name the name of the test
38       */
39      public GregorianTest(String name) {
40          super(name);
41      }
42  
43      /***
44       * test Gregorian operations.
45       */
46      public void testGregorian() {
47  
48          // check that new calendars conform to the policies
49          Calendar date = new GregorianDate(2004, Calendar.JULY, 31);
50          Calendar time = new GregorianTime(17, 30, 15);
51          assertDate(date);
52          assertTime(time);
53  
54          // check that modified calendars conform to the policies
55          date.set(Calendar.YEAR, 2005);
56          time.set(Calendar.HOUR, 19);
57          assertDate(date);
58          assertTime(time);
59  
60          // check that invalid modifications don't change the calendars
61          Calendar date2 = new GregorianDate(date);
62          Calendar time2 = new GregorianTime(time);
63          time2.set(Calendar.YEAR, 1999);
64          time2.set(Calendar.MONTH, 0);
65          time2.set(Calendar.DATE, 1);
66          date2.set(Calendar.HOUR, 19);
67          date2.set(Calendar.MINUTE, 19);
68          date2.set(Calendar.SECOND, 19);
69          assertEquals(date, date2);
70          assertEquals(time, time2);
71  
72          // TODO check that setTimeInMillis() doesn't perform invalid modifications
73  
74          System.out.println("Gregorian test successful");
75      }
76  
77      private void assertTime(Calendar time) {
78          assertFalse(time.isSet(Calendar.YEAR));
79          assertFalse(time.isSet(Calendar.MONTH));
80          assertFalse(time.isSet(Calendar.DATE));
81      }
82  
83      private void assertDate(Calendar date) {
84          assertFalse(date.isSet(Calendar.HOUR));
85          assertFalse(date.isSet(Calendar.MINUTE));
86          assertFalse(date.isSet(Calendar.SECOND));
87      }
88  
89      /***
90       * test integrity of class GregorianTime against method setTimeInMillis().
91       */
92      public void testGregorianTimeSetTimeInMillis() {
93          GregorianTime time1 = new GregorianTime();
94          GregorianTime time2 = new GregorianTime();
95          long timeInMillis = time1.getTimeInMillis();
96          // adding one millisecond should not change the time
97          time1.setTimeInMillis(timeInMillis + 1);
98          assertEquals(time1, time2);
99          assertEquals(time1.getTimeInMillis(), time2.getTimeInMillis());
100     }
101 
102     /***
103      * test integrity of class GregorianTime against method setTime().
104      */
105     public void testGregorianTimeSetTime() {
106         Date date = new Date();
107         GregorianTime time1 = new GregorianTime(date);
108         GregorianTime time2 = new GregorianTime(date);
109         // adding one millisecond should not change the time
110         time1.setTime(new Date(date.getTime() + 1));
111         assertEquals(time1, time2);
112         assertEquals(time1.getTimeInMillis(), time2.getTimeInMillis());
113     }
114 
115     /***
116      * test integrity of class GregorianTime against method setTimeZone().
117      */
118     public void testGregorianTimeSetTimeZone() {
119         Date date = new Date();
120         GregorianTime time1 = new GregorianTime(date);
121         GregorianTime time2 = new GregorianTime(date);
122         // changing the time zone must change the time
123         time1.setTimeZone(TimeZone.getTimeZone("GMT+5"));
124         assertEquals(time1, time2);
125         assertTrue(time1.getTimeInMillis() != time2.getTimeInMillis());
126     }
127 
128     /***
129      * test integrity of class GregorianDate against method setTimeInMillis().
130      */
131     public void testGregorianDateSetTimeInMillis() {
132         GregorianDate date1 = new GregorianDate();
133         GregorianDate date2 = new GregorianDate();
134         long timeInMillis = date1.getTimeInMillis();
135         // adding one millisecond should not change the time
136         date1.setTimeInMillis(timeInMillis + 1);
137         assertEquals(date1, date2);
138         assertEquals(date1.getTimeInMillis(), date2.getTimeInMillis());
139     }
140 
141     /***
142      * test integrity of class GregorianDate against method setTime().
143      */
144     public void testGregorianDateSetTime() {
145         Date date = new Date();
146         GregorianDate date1 = new GregorianDate(date);
147         GregorianDate date2 = new GregorianDate(date);
148         // adding one millisecond should not change the time
149         date1.setTime(new Date(date.getTime() + 1));
150         assertEquals(date1, date2);
151         assertEquals(date1.getTimeInMillis(), date2.getTimeInMillis());
152     }
153 
154     /***
155      * test integrity of class GregorianDate against method setTimeZone().
156      */
157     public void testGregorianDateSetTimeZone() {
158         Date date = new Date();
159         GregorianDate date1 = new GregorianDate(date);
160         GregorianDate date2 = new GregorianDate(date);
161         // changing the time zone must not change the date
162         date1.setTimeZone(TimeZone.getTimeZone("GMT+5"));
163         assertEquals(date1, date2);
164         assertEquals(date1.getTimeInMillis(), date2.getTimeInMillis());
165     }
166 }