View Javadoc

1   /*
2    *  Copyright 2001-2010 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  
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  import org.joda.time.chrono.CopticChronology;
24  import org.joda.time.chrono.LenientChronology;
25  import org.joda.time.chrono.StrictChronology;
26  
27  /**
28   * This class is a Junit unit test for YearMonth.
29   *
30   * @author Stephen Colebourne
31   */
32  public class TestYearMonth_Properties extends TestCase {
33  
34      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
35      private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS);
36  
37      private long TEST_TIME_NOW =
38              (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
39              
40      private long TEST_TIME1 =
41          (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
42          + 12L * DateTimeConstants.MILLIS_PER_HOUR
43          + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
44          
45      private long TEST_TIME2 =
46          (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
47          + 14L * DateTimeConstants.MILLIS_PER_HOUR
48          + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
49          
50      private DateTimeZone zone = null;
51  
52      private Locale systemDefaultLocale = null;
53  
54      public static void main(String[] args) {
55          junit.textui.TestRunner.run(suite());
56      }
57  
58      public static TestSuite suite() {
59          return new TestSuite(TestYearMonth_Properties.class);
60      }
61  
62      public TestYearMonth_Properties(String name) {
63          super(name);
64      }
65  
66      protected void setUp() throws Exception {
67          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
68          zone = DateTimeZone.getDefault();
69          DateTimeZone.setDefault(DateTimeZone.UTC);
70          systemDefaultLocale = Locale.getDefault();
71          Locale.setDefault(Locale.ENGLISH);
72      }
73  
74      protected void tearDown() throws Exception {
75          DateTimeUtils.setCurrentMillisSystem();
76          DateTimeZone.setDefault(zone);
77          zone = null;
78          Locale.setDefault(systemDefaultLocale);
79          systemDefaultLocale = null;
80      }
81  
82      //-----------------------------------------------------------------------
83      public void testPropertyGetYear() {
84          YearMonth test = new YearMonth(1972, 6);
85          assertSame(test.getChronology().year(), test.year().getField());
86          assertEquals("year", test.year().getName());
87          assertEquals("Property[year]", test.year().toString());
88          assertSame(test, test.year().getReadablePartial());
89          assertSame(test, test.year().getYearMonth());
90          assertEquals(1972, test.year().get());
91          assertEquals("1972", test.year().getAsString());
92          assertEquals("1972", test.year().getAsText());
93          assertEquals("1972", test.year().getAsText(Locale.FRENCH));
94          assertEquals("1972", test.year().getAsShortText());
95          assertEquals("1972", test.year().getAsShortText(Locale.FRENCH));
96          assertEquals(test.getChronology().years(), test.year().getDurationField());
97          assertEquals(null, test.year().getRangeDurationField());
98          assertEquals(9, test.year().getMaximumTextLength(null));
99          assertEquals(9, test.year().getMaximumShortTextLength(null));
100     }
101 
102     public void testPropertyGetMaxMinValuesYear() {
103         YearMonth test = new YearMonth(1972, 6);
104         assertEquals(-292275054, test.year().getMinimumValue());
105         assertEquals(-292275054, test.year().getMinimumValueOverall());
106         assertEquals(292278993, test.year().getMaximumValue());
107         assertEquals(292278993, test.year().getMaximumValueOverall());
108     }
109 
110     public void testPropertyAddYear() {
111         YearMonth test = new YearMonth(1972, 6);
112         YearMonth copy = test.year().addToCopy(9);
113         check(test, 1972, 6);
114         check(copy, 1981, 6);
115         
116         copy = test.year().addToCopy(0);
117         check(copy, 1972, 6);
118         
119         copy = test.year().addToCopy(292277023 - 1972);
120         check(copy, 292277023, 6);
121         
122         try {
123             test.year().addToCopy(292278993 - 1972 + 1);
124             fail();
125         } catch (IllegalArgumentException ex) {}
126         check(test, 1972, 6);
127         
128         copy = test.year().addToCopy(-1972);
129         check(copy, 0, 6);
130         
131         copy = test.year().addToCopy(-1973);
132         check(copy, -1, 6);
133         
134         try {
135             test.year().addToCopy(-292275054 - 1972 - 1);
136             fail();
137         } catch (IllegalArgumentException ex) {}
138         check(test, 1972, 6);
139     }
140 
141     public void testPropertyAddWrapFieldYear() {
142         YearMonth test = new YearMonth(1972, 6);
143         YearMonth copy = test.year().addWrapFieldToCopy(9);
144         check(test, 1972, 6);
145         check(copy, 1981, 6);
146         
147         copy = test.year().addWrapFieldToCopy(0);
148         check(copy, 1972, 6);
149         
150         copy = test.year().addWrapFieldToCopy(292278993 - 1972 + 1);
151         check(copy, -292275054, 6);
152         
153         copy = test.year().addWrapFieldToCopy(-292275054 - 1972 - 1);
154         check(copy, 292278993, 6);
155     }
156 
157     public void testPropertySetYear() {
158         YearMonth test = new YearMonth(1972, 6);
159         YearMonth copy = test.year().setCopy(12);
160         check(test, 1972, 6);
161         check(copy, 12, 6);
162     }
163 
164     public void testPropertySetTextYear() {
165         YearMonth test = new YearMonth(1972, 6);
166         YearMonth copy = test.year().setCopy("12");
167         check(test, 1972, 6);
168         check(copy, 12, 6);
169     }
170 
171     public void testPropertyCompareToYear() {
172         YearMonth test1 = new YearMonth(TEST_TIME1);
173         YearMonth test2 = new YearMonth(TEST_TIME2);
174         assertEquals(true, test1.year().compareTo(test2) < 0);
175         assertEquals(true, test2.year().compareTo(test1) > 0);
176         assertEquals(true, test1.year().compareTo(test1) == 0);
177         try {
178             test1.year().compareTo((ReadablePartial) null);
179             fail();
180         } catch (IllegalArgumentException ex) {}
181         
182         DateTime dt1 = new DateTime(TEST_TIME1);
183         DateTime dt2 = new DateTime(TEST_TIME2);
184         assertEquals(true, test1.year().compareTo(dt2) < 0);
185         assertEquals(true, test2.year().compareTo(dt1) > 0);
186         assertEquals(true, test1.year().compareTo(dt1) == 0);
187         try {
188             test1.year().compareTo((ReadableInstant) null);
189             fail();
190         } catch (IllegalArgumentException ex) {}
191     }
192 
193     //-----------------------------------------------------------------------
194     public void testPropertyGetMonth() {
195         YearMonth test = new YearMonth(1972, 6);
196         assertSame(test.getChronology().monthOfYear(), test.monthOfYear().getField());
197         assertEquals("monthOfYear", test.monthOfYear().getName());
198         assertEquals("Property[monthOfYear]", test.monthOfYear().toString());
199         assertSame(test, test.monthOfYear().getReadablePartial());
200         assertSame(test, test.monthOfYear().getYearMonth());
201         assertEquals(6, test.monthOfYear().get());
202         assertEquals("6", test.monthOfYear().getAsString());
203         assertEquals("June", test.monthOfYear().getAsText());
204         assertEquals("juin", test.monthOfYear().getAsText(Locale.FRENCH));
205         assertEquals("Jun", test.monthOfYear().getAsShortText());
206         assertEquals("juin", test.monthOfYear().getAsShortText(Locale.FRENCH));
207         assertEquals(test.getChronology().months(), test.monthOfYear().getDurationField());
208         assertEquals(test.getChronology().years(), test.monthOfYear().getRangeDurationField());
209         assertEquals(9, test.monthOfYear().getMaximumTextLength(null));
210         assertEquals(3, test.monthOfYear().getMaximumShortTextLength(null));
211         test = new YearMonth(1972, 7);
212         assertEquals("juillet", test.monthOfYear().getAsText(Locale.FRENCH));
213         assertEquals("juil.", test.monthOfYear().getAsShortText(Locale.FRENCH));
214     }
215 
216     public void testPropertyGetMaxMinValuesMonth() {
217         YearMonth test = new YearMonth(1972, 6);
218         assertEquals(1, test.monthOfYear().getMinimumValue());
219         assertEquals(1, test.monthOfYear().getMinimumValueOverall());
220         assertEquals(12, test.monthOfYear().getMaximumValue());
221         assertEquals(12, test.monthOfYear().getMaximumValueOverall());
222     }
223 
224     public void testPropertyAddMonth() {
225         YearMonth test = new YearMonth(1972, 6);
226         YearMonth copy = test.monthOfYear().addToCopy(6);
227         check(test, 1972, 6);
228         check(copy, 1972, 12);
229         
230         copy = test.monthOfYear().addToCopy(7);
231         check(copy, 1973, 1);
232         
233         copy = test.monthOfYear().addToCopy(-5);
234         check(copy, 1972, 1);
235         
236         copy = test.monthOfYear().addToCopy(-6);
237         check(copy, 1971, 12);
238     }
239 
240     public void testPropertyAddWrapFieldMonth() {
241         YearMonth test = new YearMonth(1972, 6);
242         YearMonth copy = test.monthOfYear().addWrapFieldToCopy(4);
243         check(test, 1972, 6);
244         check(copy, 1972, 10);
245         
246         copy = test.monthOfYear().addWrapFieldToCopy(8);
247         check(copy, 1972, 2);
248         
249         copy = test.monthOfYear().addWrapFieldToCopy(-8);
250         check(copy, 1972, 10);
251     }
252 
253     public void testPropertySetMonth() {
254         YearMonth test = new YearMonth(1972, 6);
255         YearMonth copy = test.monthOfYear().setCopy(12);
256         check(test, 1972, 6);
257         check(copy, 1972, 12);
258         
259         try {
260             test.monthOfYear().setCopy(13);
261             fail();
262         } catch (IllegalArgumentException ex) {}
263         try {
264             test.monthOfYear().setCopy(0);
265             fail();
266         } catch (IllegalArgumentException ex) {}
267     }
268 
269     public void testPropertySetTextMonth() {
270         YearMonth test = new YearMonth(1972, 6);
271         YearMonth copy = test.monthOfYear().setCopy("12");
272         check(test, 1972, 6);
273         check(copy, 1972, 12);
274         
275         copy = test.monthOfYear().setCopy("December");
276         check(test, 1972, 6);
277         check(copy, 1972, 12);
278         
279         copy = test.monthOfYear().setCopy("Dec");
280         check(test, 1972, 6);
281         check(copy, 1972, 12);
282     }
283 
284     public void testPropertyCompareToMonth() {
285         YearMonth test1 = new YearMonth(TEST_TIME1);
286         YearMonth test2 = new YearMonth(TEST_TIME2);
287         assertEquals(true, test1.monthOfYear().compareTo(test2) < 0);
288         assertEquals(true, test2.monthOfYear().compareTo(test1) > 0);
289         assertEquals(true, test1.monthOfYear().compareTo(test1) == 0);
290         try {
291             test1.monthOfYear().compareTo((ReadablePartial) null);
292             fail();
293         } catch (IllegalArgumentException ex) {}
294         
295         DateTime dt1 = new DateTime(TEST_TIME1);
296         DateTime dt2 = new DateTime(TEST_TIME2);
297         assertEquals(true, test1.monthOfYear().compareTo(dt2) < 0);
298         assertEquals(true, test2.monthOfYear().compareTo(dt1) > 0);
299         assertEquals(true, test1.monthOfYear().compareTo(dt1) == 0);
300         try {
301             test1.monthOfYear().compareTo((ReadableInstant) null);
302             fail();
303         } catch (IllegalArgumentException ex) {}
304     }
305 
306     //-----------------------------------------------------------------------
307     public void testPropertyEquals() {
308         YearMonth test1 = new YearMonth(11, 11);
309         YearMonth test2 = new YearMonth(11, 12);
310         YearMonth test3 = new YearMonth(11, 11, CopticChronology.getInstanceUTC());
311         assertEquals(true, test1.monthOfYear().equals(test1.monthOfYear()));
312         assertEquals(false, test1.monthOfYear().equals(test1.year()));
313         assertEquals(false, test1.monthOfYear().equals(test2.monthOfYear()));
314         assertEquals(false, test1.monthOfYear().equals(test2.year()));
315         
316         assertEquals(false, test1.year().equals(test1.monthOfYear()));
317         assertEquals(true, test1.year().equals(test1.year()));
318         assertEquals(false, test1.year().equals(test2.monthOfYear()));
319         assertEquals(true, test1.year().equals(test2.year()));
320         
321         assertEquals(false, test1.monthOfYear().equals(null));
322         assertEquals(false, test1.monthOfYear().equals("any"));
323         
324         // chrono
325         assertEquals(false, test1.monthOfYear().equals(test3.monthOfYear()));
326     }
327 
328     public void testPropertyHashCode() {
329         YearMonth test1 = new YearMonth(2005, 11);
330         YearMonth test2 = new YearMonth(2005, 12);
331         assertEquals(true, test1.monthOfYear().hashCode() == test1.monthOfYear().hashCode());
332         assertEquals(false, test1.monthOfYear().hashCode() == test2.monthOfYear().hashCode());
333         assertEquals(true, test1.year().hashCode() == test1.year().hashCode());
334         assertEquals(true, test1.year().hashCode() == test2.year().hashCode());
335     }
336 
337     public void testPropertyEqualsHashCodeLenient() {
338         YearMonth test1 = new YearMonth(1970, 6, LenientChronology.getInstance(COPTIC_PARIS));
339         YearMonth test2 = new YearMonth(1970, 6, LenientChronology.getInstance(COPTIC_PARIS));
340         assertEquals(true, test1.monthOfYear().equals(test2.monthOfYear()));
341         assertEquals(true, test2.monthOfYear().equals(test1.monthOfYear()));
342         assertEquals(true, test1.monthOfYear().equals(test1.monthOfYear()));
343         assertEquals(true, test2.monthOfYear().equals(test2.monthOfYear()));
344         assertEquals(true, test1.monthOfYear().hashCode() == test2.monthOfYear().hashCode());
345         assertEquals(true, test1.monthOfYear().hashCode() == test1.monthOfYear().hashCode());
346         assertEquals(true, test2.monthOfYear().hashCode() == test2.monthOfYear().hashCode());
347     }
348 
349     public void testPropertyEqualsHashCodeStrict() {
350         YearMonth test1 = new YearMonth(1970, 6, StrictChronology.getInstance(COPTIC_PARIS));
351         YearMonth test2 = new YearMonth(1970, 6, StrictChronology.getInstance(COPTIC_PARIS));
352         assertEquals(true, test1.monthOfYear().equals(test2.monthOfYear()));
353         assertEquals(true, test2.monthOfYear().equals(test1.monthOfYear()));
354         assertEquals(true, test1.monthOfYear().equals(test1.monthOfYear()));
355         assertEquals(true, test2.monthOfYear().equals(test2.monthOfYear()));
356         assertEquals(true, test1.monthOfYear().hashCode() == test2.monthOfYear().hashCode());
357         assertEquals(true, test1.monthOfYear().hashCode() == test1.monthOfYear().hashCode());
358         assertEquals(true, test2.monthOfYear().hashCode() == test2.monthOfYear().hashCode());
359     }
360 
361     //-----------------------------------------------------------------------
362     private void check(YearMonth test, int year, int month) {
363         assertEquals(year, test.getYear());
364         assertEquals(month, test.getMonthOfYear());
365     }
366 }