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.chrono;
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.DateTimeConstants;
25  import org.joda.time.DateTimeUtils;
26  import org.joda.time.DateTimeZone;
27  
28  /**
29   * This class is a Junit unit test for JulianChronology.
30   *
31   * @author Stephen Colebourne
32   */
33  public class TestJulianChronology extends TestCase {
34  
35      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
36      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
37      private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
38  
39      long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
40                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
41                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
42                       366 + 365;
43      // 2002-06-09
44      private long TEST_TIME_NOW =
45              (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
46  
47      private DateTimeZone originalDateTimeZone = null;
48      private TimeZone originalTimeZone = null;
49      private Locale originalLocale = null;
50  
51      public static void main(String[] args) {
52          junit.textui.TestRunner.run(suite());
53      }
54  
55      public static TestSuite suite() {
56          return new TestSuite(TestJulianChronology.class);
57      }
58  
59      public TestJulianChronology(String name) {
60          super(name);
61      }
62  
63      protected void setUp() throws Exception {
64          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
65          originalDateTimeZone = DateTimeZone.getDefault();
66          originalTimeZone = TimeZone.getDefault();
67          originalLocale = Locale.getDefault();
68          DateTimeZone.setDefault(LONDON);
69          TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
70          Locale.setDefault(Locale.UK);
71      }
72  
73      protected void tearDown() throws Exception {
74          DateTimeUtils.setCurrentMillisSystem();
75          DateTimeZone.setDefault(originalDateTimeZone);
76          TimeZone.setDefault(originalTimeZone);
77          Locale.setDefault(originalLocale);
78          originalDateTimeZone = null;
79          originalTimeZone = null;
80          originalLocale = null;
81      }
82  
83      //-----------------------------------------------------------------------
84      public void testFactoryUTC() {
85          assertEquals(DateTimeZone.UTC, JulianChronology.getInstanceUTC().getZone());
86          assertSame(JulianChronology.class, JulianChronology.getInstanceUTC().getClass());
87      }
88  
89      public void testFactory() {
90          assertEquals(LONDON, JulianChronology.getInstance().getZone());
91          assertSame(JulianChronology.class, JulianChronology.getInstance().getClass());
92      }
93  
94      public void testFactory_Zone() {
95          assertEquals(TOKYO, JulianChronology.getInstance(TOKYO).getZone());
96          assertEquals(PARIS, JulianChronology.getInstance(PARIS).getZone());
97          assertEquals(LONDON, JulianChronology.getInstance(null).getZone());
98          assertSame(JulianChronology.class, JulianChronology.getInstance(TOKYO).getClass());
99      }
100 
101     public void testFactory_Zone_int() {
102         JulianChronology chrono = JulianChronology.getInstance(TOKYO, 2);
103         assertEquals(TOKYO, chrono.getZone());
104         assertEquals(2, chrono.getMinimumDaysInFirstWeek());
105         
106         try {
107             JulianChronology.getInstance(TOKYO, 0);
108             fail();
109         } catch (IllegalArgumentException ex) {}
110         try {
111             JulianChronology.getInstance(TOKYO, 8);
112             fail();
113         } catch (IllegalArgumentException ex) {}
114     }
115 
116     //-----------------------------------------------------------------------
117     public void testEquality() {
118         assertSame(JulianChronology.getInstance(TOKYO), JulianChronology.getInstance(TOKYO));
119         assertSame(JulianChronology.getInstance(LONDON), JulianChronology.getInstance(LONDON));
120         assertSame(JulianChronology.getInstance(PARIS), JulianChronology.getInstance(PARIS));
121         assertSame(JulianChronology.getInstanceUTC(), JulianChronology.getInstanceUTC());
122         assertSame(JulianChronology.getInstance(), JulianChronology.getInstance(LONDON));
123     }
124 
125     public void testWithUTC() {
126         assertSame(JulianChronology.getInstanceUTC(), JulianChronology.getInstance(LONDON).withUTC());
127         assertSame(JulianChronology.getInstanceUTC(), JulianChronology.getInstance(TOKYO).withUTC());
128         assertSame(JulianChronology.getInstanceUTC(), JulianChronology.getInstanceUTC().withUTC());
129         assertSame(JulianChronology.getInstanceUTC(), JulianChronology.getInstance().withUTC());
130     }
131 
132     public void testWithZone() {
133         assertSame(JulianChronology.getInstance(TOKYO), JulianChronology.getInstance(TOKYO).withZone(TOKYO));
134         assertSame(JulianChronology.getInstance(LONDON), JulianChronology.getInstance(TOKYO).withZone(LONDON));
135         assertSame(JulianChronology.getInstance(PARIS), JulianChronology.getInstance(TOKYO).withZone(PARIS));
136         assertSame(JulianChronology.getInstance(LONDON), JulianChronology.getInstance(TOKYO).withZone(null));
137         assertSame(JulianChronology.getInstance(PARIS), JulianChronology.getInstance().withZone(PARIS));
138         assertSame(JulianChronology.getInstance(PARIS), JulianChronology.getInstanceUTC().withZone(PARIS));
139     }
140 
141     public void testToString() {
142         assertEquals("JulianChronology[Europe/London]", JulianChronology.getInstance(LONDON).toString());
143         assertEquals("JulianChronology[Asia/Tokyo]", JulianChronology.getInstance(TOKYO).toString());
144         assertEquals("JulianChronology[Europe/London]", JulianChronology.getInstance().toString());
145         assertEquals("JulianChronology[UTC]", JulianChronology.getInstanceUTC().toString());
146         assertEquals("JulianChronology[UTC,mdfw=2]", JulianChronology.getInstance(DateTimeZone.UTC, 2).toString());
147     }
148 
149     //-----------------------------------------------------------------------
150     public void testDurationFields() {
151         assertEquals("eras", JulianChronology.getInstance().eras().getName());
152         assertEquals("centuries", JulianChronology.getInstance().centuries().getName());
153         assertEquals("years", JulianChronology.getInstance().years().getName());
154         assertEquals("weekyears", JulianChronology.getInstance().weekyears().getName());
155         assertEquals("months", JulianChronology.getInstance().months().getName());
156         assertEquals("weeks", JulianChronology.getInstance().weeks().getName());
157         assertEquals("days", JulianChronology.getInstance().days().getName());
158         assertEquals("halfdays", JulianChronology.getInstance().halfdays().getName());
159         assertEquals("hours", JulianChronology.getInstance().hours().getName());
160         assertEquals("minutes", JulianChronology.getInstance().minutes().getName());
161         assertEquals("seconds", JulianChronology.getInstance().seconds().getName());
162         assertEquals("millis", JulianChronology.getInstance().millis().getName());
163         
164         assertEquals(false, JulianChronology.getInstance().eras().isSupported());
165         assertEquals(true, JulianChronology.getInstance().centuries().isSupported());
166         assertEquals(true, JulianChronology.getInstance().years().isSupported());
167         assertEquals(true, JulianChronology.getInstance().weekyears().isSupported());
168         assertEquals(true, JulianChronology.getInstance().months().isSupported());
169         assertEquals(true, JulianChronology.getInstance().weeks().isSupported());
170         assertEquals(true, JulianChronology.getInstance().days().isSupported());
171         assertEquals(true, JulianChronology.getInstance().halfdays().isSupported());
172         assertEquals(true, JulianChronology.getInstance().hours().isSupported());
173         assertEquals(true, JulianChronology.getInstance().minutes().isSupported());
174         assertEquals(true, JulianChronology.getInstance().seconds().isSupported());
175         assertEquals(true, JulianChronology.getInstance().millis().isSupported());
176         
177         assertEquals(false, JulianChronology.getInstance().centuries().isPrecise());
178         assertEquals(false, JulianChronology.getInstance().years().isPrecise());
179         assertEquals(false, JulianChronology.getInstance().weekyears().isPrecise());
180         assertEquals(false, JulianChronology.getInstance().months().isPrecise());
181         assertEquals(false, JulianChronology.getInstance().weeks().isPrecise());
182         assertEquals(false, JulianChronology.getInstance().days().isPrecise());
183         assertEquals(false, JulianChronology.getInstance().halfdays().isPrecise());
184         assertEquals(true, JulianChronology.getInstance().hours().isPrecise());
185         assertEquals(true, JulianChronology.getInstance().minutes().isPrecise());
186         assertEquals(true, JulianChronology.getInstance().seconds().isPrecise());
187         assertEquals(true, JulianChronology.getInstance().millis().isPrecise());
188         
189         assertEquals(false, JulianChronology.getInstanceUTC().centuries().isPrecise());
190         assertEquals(false, JulianChronology.getInstanceUTC().years().isPrecise());
191         assertEquals(false, JulianChronology.getInstanceUTC().weekyears().isPrecise());
192         assertEquals(false, JulianChronology.getInstanceUTC().months().isPrecise());
193         assertEquals(true, JulianChronology.getInstanceUTC().weeks().isPrecise());
194         assertEquals(true, JulianChronology.getInstanceUTC().days().isPrecise());
195         assertEquals(true, JulianChronology.getInstanceUTC().halfdays().isPrecise());
196         assertEquals(true, JulianChronology.getInstanceUTC().hours().isPrecise());
197         assertEquals(true, JulianChronology.getInstanceUTC().minutes().isPrecise());
198         assertEquals(true, JulianChronology.getInstanceUTC().seconds().isPrecise());
199         assertEquals(true, JulianChronology.getInstanceUTC().millis().isPrecise());
200         
201         DateTimeZone gmt = DateTimeZone.forID("Etc/GMT");
202         assertEquals(false, JulianChronology.getInstance(gmt).centuries().isPrecise());
203         assertEquals(false, JulianChronology.getInstance(gmt).years().isPrecise());
204         assertEquals(false, JulianChronology.getInstance(gmt).weekyears().isPrecise());
205         assertEquals(false, JulianChronology.getInstance(gmt).months().isPrecise());
206         assertEquals(true, JulianChronology.getInstance(gmt).weeks().isPrecise());
207         assertEquals(true, JulianChronology.getInstance(gmt).days().isPrecise());
208         assertEquals(true, JulianChronology.getInstance(gmt).halfdays().isPrecise());
209         assertEquals(true, JulianChronology.getInstance(gmt).hours().isPrecise());
210         assertEquals(true, JulianChronology.getInstance(gmt).minutes().isPrecise());
211         assertEquals(true, JulianChronology.getInstance(gmt).seconds().isPrecise());
212         assertEquals(true, JulianChronology.getInstance(gmt).millis().isPrecise());
213     }
214 
215     public void testDateFields() {
216         assertEquals("era", JulianChronology.getInstance().era().getName());
217         assertEquals("centuryOfEra", JulianChronology.getInstance().centuryOfEra().getName());
218         assertEquals("yearOfCentury", JulianChronology.getInstance().yearOfCentury().getName());
219         assertEquals("yearOfEra", JulianChronology.getInstance().yearOfEra().getName());
220         assertEquals("year", JulianChronology.getInstance().year().getName());
221         assertEquals("monthOfYear", JulianChronology.getInstance().monthOfYear().getName());
222         assertEquals("weekyearOfCentury", JulianChronology.getInstance().weekyearOfCentury().getName());
223         assertEquals("weekyear", JulianChronology.getInstance().weekyear().getName());
224         assertEquals("weekOfWeekyear", JulianChronology.getInstance().weekOfWeekyear().getName());
225         assertEquals("dayOfYear", JulianChronology.getInstance().dayOfYear().getName());
226         assertEquals("dayOfMonth", JulianChronology.getInstance().dayOfMonth().getName());
227         assertEquals("dayOfWeek", JulianChronology.getInstance().dayOfWeek().getName());
228         
229         assertEquals(true, JulianChronology.getInstance().era().isSupported());
230         assertEquals(true, JulianChronology.getInstance().centuryOfEra().isSupported());
231         assertEquals(true, JulianChronology.getInstance().yearOfCentury().isSupported());
232         assertEquals(true, JulianChronology.getInstance().yearOfEra().isSupported());
233         assertEquals(true, JulianChronology.getInstance().year().isSupported());
234         assertEquals(true, JulianChronology.getInstance().monthOfYear().isSupported());
235         assertEquals(true, JulianChronology.getInstance().weekyearOfCentury().isSupported());
236         assertEquals(true, JulianChronology.getInstance().weekyear().isSupported());
237         assertEquals(true, JulianChronology.getInstance().weekOfWeekyear().isSupported());
238         assertEquals(true, JulianChronology.getInstance().dayOfYear().isSupported());
239         assertEquals(true, JulianChronology.getInstance().dayOfMonth().isSupported());
240         assertEquals(true, JulianChronology.getInstance().dayOfWeek().isSupported());
241     }
242 
243     public void testTimeFields() {
244         assertEquals("halfdayOfDay", JulianChronology.getInstance().halfdayOfDay().getName());
245         assertEquals("clockhourOfHalfday", JulianChronology.getInstance().clockhourOfHalfday().getName());
246         assertEquals("hourOfHalfday", JulianChronology.getInstance().hourOfHalfday().getName());
247         assertEquals("clockhourOfDay", JulianChronology.getInstance().clockhourOfDay().getName());
248         assertEquals("hourOfDay", JulianChronology.getInstance().hourOfDay().getName());
249         assertEquals("minuteOfDay", JulianChronology.getInstance().minuteOfDay().getName());
250         assertEquals("minuteOfHour", JulianChronology.getInstance().minuteOfHour().getName());
251         assertEquals("secondOfDay", JulianChronology.getInstance().secondOfDay().getName());
252         assertEquals("secondOfMinute", JulianChronology.getInstance().secondOfMinute().getName());
253         assertEquals("millisOfDay", JulianChronology.getInstance().millisOfDay().getName());
254         assertEquals("millisOfSecond", JulianChronology.getInstance().millisOfSecond().getName());
255         
256         assertEquals(true, JulianChronology.getInstance().halfdayOfDay().isSupported());
257         assertEquals(true, JulianChronology.getInstance().clockhourOfHalfday().isSupported());
258         assertEquals(true, JulianChronology.getInstance().hourOfHalfday().isSupported());
259         assertEquals(true, JulianChronology.getInstance().clockhourOfDay().isSupported());
260         assertEquals(true, JulianChronology.getInstance().hourOfDay().isSupported());
261         assertEquals(true, JulianChronology.getInstance().minuteOfDay().isSupported());
262         assertEquals(true, JulianChronology.getInstance().minuteOfHour().isSupported());
263         assertEquals(true, JulianChronology.getInstance().secondOfDay().isSupported());
264         assertEquals(true, JulianChronology.getInstance().secondOfMinute().isSupported());
265         assertEquals(true, JulianChronology.getInstance().millisOfDay().isSupported());
266         assertEquals(true, JulianChronology.getInstance().millisOfSecond().isSupported());
267     }
268 
269 }