View Javadoc

1   /*
2    *  Copyright 2001-2005 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.time;
17  
18  import java.util.Locale;
19  import java.util.TimeZone;
20  
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  import org.joda.time.chrono.BuddhistChronology;
25  import org.joda.time.chrono.CopticChronology;
26  import org.joda.time.chrono.EthiopicChronology;
27  import org.joda.time.chrono.GJChronology;
28  import org.joda.time.chrono.GregorianChronology;
29  import org.joda.time.chrono.ISOChronology;
30  import org.joda.time.chrono.IslamicChronology;
31  import org.joda.time.chrono.JulianChronology;
32  import org.joda.time.chrono.LenientChronology;
33  import org.joda.time.chrono.LimitChronology;
34  import org.joda.time.chrono.StrictChronology;
35  import org.joda.time.chrono.ZonedChronology;
36  
37  /**
38   * This class is a Junit unit test for Chronology.
39   *
40   * @author Stephen Colebourne
41   */
42  public class TestChronology extends TestCase {
43      // Test in 2002/03 as time zones are more well known
44      // (before the late 90's they were all over the place)
45  
46  //    private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
47      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
48      
49      long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
50                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
51                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
52                       366 + 365;
53      long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
54                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
55                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
56                       366 + 365 + 365;
57      
58      // 2002-06-09
59      private long TEST_TIME_NOW =
60              (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
61              
62      // 2002-04-05
63      private long TEST_TIME1 =
64              (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
65              + 12L * DateTimeConstants.MILLIS_PER_HOUR
66              + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
67          
68      // 2003-05-06
69      private long TEST_TIME2 =
70              (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
71              + 14L * DateTimeConstants.MILLIS_PER_HOUR
72              + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
73      
74      private DateTimeZone originalDateTimeZone = null;
75      private TimeZone originalTimeZone = null;
76      private Locale originalLocale = null;
77  
78      public static void main(String[] args) {
79          junit.textui.TestRunner.run(suite());
80      }
81  
82      public static TestSuite suite() {
83          return new TestSuite(TestChronology.class);
84      }
85  
86      public TestChronology(String name) {
87          super(name);
88      }
89  
90      protected void setUp() throws Exception {
91          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
92          originalDateTimeZone = DateTimeZone.getDefault();
93          originalTimeZone = TimeZone.getDefault();
94          originalLocale = Locale.getDefault();
95          DateTimeZone.setDefault(LONDON);
96          TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
97          Locale.setDefault(Locale.UK);
98      }
99  
100     protected void tearDown() throws Exception {
101         DateTimeUtils.setCurrentMillisSystem();
102         DateTimeZone.setDefault(originalDateTimeZone);
103         TimeZone.setDefault(originalTimeZone);
104         Locale.setDefault(originalLocale);
105         originalDateTimeZone = null;
106         originalTimeZone = null;
107         originalLocale = null;
108     }
109 
110     //-----------------------------------------------------------------------
111     public void testTest() {
112         assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString());
113         assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString());
114         assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString());
115     }
116 
117     //-----------------------------------------------------------------------
118     public void testEqualsHashCode_ISO() {
119         Chronology chrono1 = ISOChronology.getInstanceUTC();
120         Chronology chrono2 = ISOChronology.getInstanceUTC();
121         Chronology chrono3 = ISOChronology.getInstance();
122         
123         assertEquals(true, chrono1.equals(chrono2));
124         assertEquals(false, chrono1.equals(chrono3));
125         
126         DateTime dt1 = new DateTime(0L, chrono1);
127         DateTime dt2 = new DateTime(0L, chrono2);
128         DateTime dt3 = new DateTime(0L, chrono3);
129         
130         assertEquals(true, dt1.equals(dt2));
131         assertEquals(false, dt1.equals(dt3));
132         
133         assertEquals(true, chrono1.hashCode() == chrono2.hashCode());
134         assertEquals(false, chrono1.hashCode() == chrono3.hashCode());
135     }
136 
137     //-----------------------------------------------------------------------
138     public void testEqualsHashCode_Lenient() {
139         Chronology chrono1 = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
140         Chronology chrono2 = LenientChronology.getInstance(ISOChronology.getInstanceUTC());
141         Chronology chrono3 = LenientChronology.getInstance(ISOChronology.getInstance());
142         
143         assertEquals(true, chrono1.equals(chrono2));
144         assertEquals(false, chrono1.equals(chrono3));
145         
146         DateTime dt1 = new DateTime(0L, chrono1);
147         DateTime dt2 = new DateTime(0L, chrono2);
148         DateTime dt3 = new DateTime(0L, chrono3);
149         
150         assertEquals(true, dt1.equals(dt2));
151         assertEquals(false, dt1.equals(dt3));
152         
153         assertEquals(true, chrono1.hashCode() == chrono2.hashCode());
154         assertEquals(false, chrono1.hashCode() == chrono3.hashCode());
155     }
156 
157     //-----------------------------------------------------------------------
158     public void testEqualsHashCode_Strict() {
159         Chronology chrono1 = StrictChronology.getInstance(ISOChronology.getInstanceUTC());
160         Chronology chrono2 = StrictChronology.getInstance(ISOChronology.getInstanceUTC());
161         Chronology chrono3 = StrictChronology.getInstance(ISOChronology.getInstance());
162         
163         assertEquals(true, chrono1.equals(chrono2));
164         assertEquals(false, chrono1.equals(chrono3));
165         
166         DateTime dt1 = new DateTime(0L, chrono1);
167         DateTime dt2 = new DateTime(0L, chrono2);
168         DateTime dt3 = new DateTime(0L, chrono3);
169         
170         assertEquals(true, dt1.equals(dt2));
171         assertEquals(false, dt1.equals(dt3));
172         
173         assertEquals(true, chrono1.hashCode() == chrono2.hashCode());
174         assertEquals(false, chrono1.hashCode() == chrono3.hashCode());
175     }
176 
177     //-----------------------------------------------------------------------
178     public void testEqualsHashCode_Limit() {
179         DateTime lower = new DateTime(0L);
180         DateTime higherA = new DateTime(1000000L);
181         DateTime higherB = new DateTime(2000000L);
182         
183         Chronology chrono1 = LimitChronology.getInstance(ISOChronology.getInstanceUTC(), lower, higherA);
184         Chronology chrono2A = LimitChronology.getInstance(ISOChronology.getInstanceUTC(), lower, higherA);
185         Chronology chrono2B = LimitChronology.getInstance(ISOChronology.getInstanceUTC(), lower, higherB);
186         Chronology chrono3 = LimitChronology.getInstance(ISOChronology.getInstance(), lower, higherA);
187         
188         assertEquals(true, chrono1.equals(chrono2A));
189         assertEquals(false, chrono1.equals(chrono2B));
190         assertEquals(false, chrono1.equals(chrono3));
191         
192         DateTime dt1 = new DateTime(0L, chrono1);
193         DateTime dt2A = new DateTime(0L, chrono2A);
194         DateTime dt2B = new DateTime(0L, chrono2B);
195         DateTime dt3 = new DateTime(0L, chrono3);
196         
197         assertEquals(true, dt1.equals(dt2A));
198         assertEquals(false, dt1.equals(dt2B));
199         assertEquals(false, dt1.equals(dt3));
200         
201         assertEquals(true, chrono1.hashCode() == chrono2A.hashCode());
202         assertEquals(false, chrono1.hashCode() == chrono2B.hashCode());
203         assertEquals(false, chrono1.hashCode() == chrono3.hashCode());
204     }
205 
206     //-----------------------------------------------------------------------
207     public void testEqualsHashCode_Zoned() {
208         DateTimeZone zoneA = DateTimeZone.forID("Europe/Paris");
209         DateTimeZone zoneB = DateTimeZone.forID("Asia/Tokyo");
210         
211         Chronology chrono1 = ZonedChronology.getInstance(ISOChronology.getInstanceUTC(), zoneA);
212         Chronology chrono2 = ZonedChronology.getInstance(ISOChronology.getInstanceUTC(), zoneA);
213         Chronology chrono3 = ZonedChronology.getInstance(ISOChronology.getInstanceUTC(), zoneB);
214         
215         assertEquals(true, chrono1.equals(chrono2));
216         assertEquals(false, chrono1.equals(chrono3));
217         
218         DateTime dt1 = new DateTime(0L, chrono1);
219         DateTime dt2 = new DateTime(0L, chrono2);
220         DateTime dt3 = new DateTime(0L, chrono3);
221         
222         assertEquals(true, dt1.equals(dt2));
223         assertEquals(false, dt1.equals(dt3));
224         
225         assertEquals(true, chrono1.hashCode() == chrono2.hashCode());
226         assertEquals(false, chrono1.hashCode() == chrono3.hashCode());
227     }
228 
229     //-----------------------------------------------------------------------
230     public void testToString() {
231         DateTimeZone paris = DateTimeZone.forID("Europe/Paris");
232         ISOChronology isoParis = ISOChronology.getInstance(paris);
233         
234         assertEquals("ISOChronology[Europe/Paris]", isoParis.toString());
235         assertEquals("GJChronology[Europe/Paris]", GJChronology.getInstance(paris).toString());
236         assertEquals("GregorianChronology[Europe/Paris]", GregorianChronology.getInstance(paris).toString());
237         assertEquals("JulianChronology[Europe/Paris]", JulianChronology.getInstance(paris).toString());
238         assertEquals("BuddhistChronology[Europe/Paris]", BuddhistChronology.getInstance(paris).toString());
239         assertEquals("CopticChronology[Europe/Paris]", CopticChronology.getInstance(paris).toString());
240         assertEquals("EthiopicChronology[Europe/Paris]", EthiopicChronology.getInstance(paris).toString());
241         assertEquals("IslamicChronology[Europe/Paris]", IslamicChronology.getInstance(paris).toString());
242         
243         assertEquals("LenientChronology[ISOChronology[Europe/Paris]]", LenientChronology.getInstance(isoParis).toString());
244         assertEquals("StrictChronology[ISOChronology[Europe/Paris]]", StrictChronology.getInstance(isoParis).toString());
245         assertEquals("LimitChronology[ISOChronology[Europe/Paris], NoLimit, NoLimit]", LimitChronology.getInstance(isoParis, null, null).toString());
246         assertEquals("ZonedChronology[ISOChronology[UTC], Europe/Paris]", ZonedChronology.getInstance(isoParis, paris).toString());
247     }
248 
249 }