View Javadoc

1   /*
2    *  Copyright 2001-2011 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 MonthDay. Based on {@link TestYearMonth_Propeties} 
29   */
30  public class TestMonthDay_Properties extends TestCase {
31  
32      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
33      private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS);
34  
35      private long TEST_TIME_NOW =
36              (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
37              
38      private long TEST_TIME1 =
39          (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
40          + 12L * DateTimeConstants.MILLIS_PER_HOUR
41          + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
42          
43      private long TEST_TIME2 =
44          (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
45          + 14L * DateTimeConstants.MILLIS_PER_HOUR
46          + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
47          
48      private DateTimeZone zone = null;
49      private Locale locale = 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(TestMonthDay_Properties.class);
57      }
58  
59      public TestMonthDay_Properties(String name) {
60          super(name);
61      }
62  
63      protected void setUp() throws Exception {
64          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
65          zone = DateTimeZone.getDefault();
66          locale = Locale.getDefault();
67          Locale.setDefault(Locale.UK);
68          DateTimeZone.setDefault(DateTimeZone.UTC);
69      }
70  
71      protected void tearDown() throws Exception {
72          DateTimeUtils.setCurrentMillisSystem();
73          DateTimeZone.setDefault(zone);
74          zone = null;
75          Locale.setDefault(locale);
76          locale = null;
77      }
78  
79      //-----------------------------------------------------------------------
80      public void testPropertyGetMonthOfYear() {
81          MonthDay test = new MonthDay(9, 6);
82          assertSame(test.getChronology().monthOfYear(), test.monthOfYear().getField());
83          assertEquals("monthOfYear", test.monthOfYear().getName());
84          assertEquals("Property[monthOfYear]", test.monthOfYear().toString());
85          assertSame(test, test.monthOfYear().getReadablePartial());
86          assertSame(test, test.monthOfYear().getMonthDay());
87          assertEquals(9, test.monthOfYear().get());
88          assertEquals("9", test.monthOfYear().getAsString());
89          assertEquals("September", test.monthOfYear().getAsText());
90          assertEquals("septembre", test.monthOfYear().getAsText(Locale.FRENCH));
91          assertEquals("Sep", test.monthOfYear().getAsShortText());
92          assertEquals("sept.", test.monthOfYear().getAsShortText(Locale.FRENCH));
93          assertEquals(test.getChronology().months(), test.monthOfYear().getDurationField());
94          // assertEquals(test.getChronology().days(), test.dayOfMonth().getRangeDurationField());
95          assertEquals(9, test.monthOfYear().getMaximumTextLength(null));
96          assertEquals(3, test.monthOfYear().getMaximumShortTextLength(null));
97      }
98  
99      public void testPropertyGetMaxMinValuesMonthOfYear() {
100         MonthDay test = new MonthDay(10, 6);
101         assertEquals(1, test.monthOfYear().getMinimumValue());
102         assertEquals(1, test.monthOfYear().getMinimumValueOverall());
103         assertEquals(12, test.monthOfYear().getMaximumValue());
104         assertEquals(12, test.monthOfYear().getMaximumValueOverall());
105     }
106 
107     public void testPropertyAddMonthOfYear() {
108         MonthDay test = new MonthDay(3, 6);
109         MonthDay copy = test.monthOfYear().addToCopy(9);
110         check(test, 3, 6);
111         check(copy, 12, 6);
112         
113         copy = test.monthOfYear().addToCopy(0);
114         check(copy, 3, 6);
115 
116         check(test, 3, 6);
117         
118         copy = test.monthOfYear().addToCopy(-3);
119         check(copy, 12, 6);
120         check(test, 3, 6);
121     }
122 
123     public void testPropertyAddWrapFieldMonthOfYear() {
124         MonthDay test = new MonthDay(5, 6);
125         MonthDay copy = test.monthOfYear().addWrapFieldToCopy(2);
126         check(test, 5, 6);
127         check(copy, 7, 6);
128         
129         copy = test.monthOfYear().addWrapFieldToCopy(2);
130         check(copy, 7, 6);
131         
132         copy = test.monthOfYear().addWrapFieldToCopy(292278993 - 4 + 1);
133         check(copy, 11, 6);
134         
135         copy = test.monthOfYear().addWrapFieldToCopy(-292275054 - 4 - 1);
136         check(copy, 6, 6);
137     }
138 
139     public void testPropertySetMonthOfYear() {
140         MonthDay test = new MonthDay(10, 6);
141         MonthDay copy = test.monthOfYear().setCopy(12);
142         check(test, 10, 6);
143         check(copy, 12, 6);
144     }
145 
146     public void testPropertySetTextMonthOfYear() {
147         MonthDay test = new MonthDay(10, 6);
148         MonthDay copy = test.monthOfYear().setCopy("12");
149         check(test, 10, 6);
150         check(copy, 12, 6);
151     }
152 
153     public void testPropertyCompareToMonthOfYear() {
154         MonthDay test1 = new MonthDay(TEST_TIME1);
155         MonthDay test2 = new MonthDay(TEST_TIME2);
156         assertEquals(true, test1.monthOfYear().compareTo(test2) < 0);
157         assertEquals(true, test2.monthOfYear().compareTo(test1) > 0);
158         assertEquals(true, test1.monthOfYear().compareTo(test1) == 0);
159         try {
160             test1.monthOfYear().compareTo((ReadablePartial) null);
161             fail();
162         } catch (IllegalArgumentException ex) {}
163         
164         DateTime dt1 = new DateTime(TEST_TIME1);
165         DateTime dt2 = new DateTime(TEST_TIME2);
166         assertEquals(true, test1.monthOfYear().compareTo(dt2) < 0);
167         assertEquals(true, test2.monthOfYear().compareTo(dt1) > 0);
168         assertEquals(true, test1.monthOfYear().compareTo(dt1) == 0);
169         try {
170             test1.monthOfYear().compareTo((ReadableInstant) null);
171             fail();
172         } catch (IllegalArgumentException ex) {}
173     }
174 
175     //-----------------------------------------------------------------------
176     public void testPropertyGetDayOfMonth() {
177         MonthDay test = new MonthDay(4, 6);
178         assertSame(test.getChronology().dayOfMonth(), test.dayOfMonth().getField());
179         assertEquals("dayOfMonth", test.dayOfMonth().getName());
180         assertEquals("Property[dayOfMonth]", test.dayOfMonth().toString());
181         assertSame(test, test.dayOfMonth().getReadablePartial());
182         assertSame(test, test.dayOfMonth().getMonthDay());
183         assertEquals(6, test.dayOfMonth().get());
184         assertEquals("6", test.dayOfMonth().getAsString());
185         assertEquals("6", test.dayOfMonth().getAsText());
186         assertEquals("6", test.dayOfMonth().getAsText(Locale.FRENCH));
187         assertEquals("6", test.dayOfMonth().getAsShortText());
188         assertEquals("6", test.dayOfMonth().getAsShortText(Locale.FRENCH));
189         assertEquals(test.getChronology().days(), test.dayOfMonth().getDurationField());
190         assertEquals(test.getChronology().months(), test.dayOfMonth().getRangeDurationField());
191         assertEquals(2, test.dayOfMonth().getMaximumTextLength(null));
192         assertEquals(2, test.dayOfMonth().getMaximumShortTextLength(null));
193         test = new MonthDay(4, 7);
194         assertEquals("7", test.dayOfMonth().getAsText(Locale.FRENCH));
195         assertEquals("7", test.dayOfMonth().getAsShortText(Locale.FRENCH));
196     }
197 
198     public void testPropertyGetMaxMinValuesDayOfMonth() {
199         MonthDay test = new MonthDay(4, 6);
200         assertEquals(1, test.dayOfMonth().getMinimumValue());
201         assertEquals(1, test.dayOfMonth().getMinimumValueOverall());
202         assertEquals(30, test.dayOfMonth().getMaximumValue());
203         assertEquals(31, test.dayOfMonth().getMaximumValueOverall());
204     }
205 
206     public void testPropertyAddDayOfMonth() {
207         MonthDay test = new MonthDay(4, 6);
208         MonthDay copy = test.dayOfMonth().addToCopy(6);
209         check(test, 4, 6);
210         check(copy, 4, 12);
211         
212         copy = test.dayOfMonth().addToCopy(7);
213         check(copy, 4, 13);
214         
215         copy = test.dayOfMonth().addToCopy(-5);
216         check(copy, 4, 1);
217         
218         copy = test.dayOfMonth().addToCopy(-6);
219         check(copy, 3, 31);
220     }
221 
222     public void testPropertyAddWrapFieldDayOfMonth() {
223         MonthDay test = new MonthDay(4, 6);
224         MonthDay copy = test.dayOfMonth().addWrapFieldToCopy(4);
225         check(test, 4, 6);
226         check(copy, 4, 10);
227         
228         copy = test.dayOfMonth().addWrapFieldToCopy(8);
229         check(copy, 4, 14);
230         
231         copy = test.dayOfMonth().addWrapFieldToCopy(-8);
232         check(copy, 4, 28);
233     }
234 
235     public void testPropertySetDayOfMonth() {
236         MonthDay test = new MonthDay(4, 6);
237         MonthDay copy = test.dayOfMonth().setCopy(12);
238         check(test, 4, 6);
239         check(copy, 4, 12);
240         
241         try {
242             test.dayOfMonth().setCopy(33);
243             fail();
244         } catch (IllegalArgumentException ex) {}
245         try {
246             test.dayOfMonth().setCopy(0);
247             fail();
248         } catch (IllegalArgumentException ex) {}
249     }
250 
251     public void testPropertySetTextDayOfMonth() {
252         MonthDay test = new MonthDay(4, 6);
253         MonthDay copy = test.dayOfMonth().setCopy("12");
254         check(test, 4, 6);
255         check(copy, 4, 12);
256         
257         copy = test.dayOfMonth().setCopy("2");
258         check(test, 4, 6);
259         check(copy, 4, 2);
260         
261         copy = test.dayOfMonth().setCopy("4");
262         check(test, 4, 6);
263         check(copy, 4, 4);
264     }
265 
266     public void testPropertyCompareToDayOfMonth() {
267         MonthDay test1 = new MonthDay(TEST_TIME1);
268         MonthDay test2 = new MonthDay(TEST_TIME2);
269         assertEquals(true, test1.dayOfMonth().compareTo(test2) < 0);
270         assertEquals(true, test2.dayOfMonth().compareTo(test1) > 0);
271         assertEquals(true, test1.dayOfMonth().compareTo(test1) == 0);
272         try {
273             test1.dayOfMonth().compareTo((ReadablePartial) null);
274             fail();
275         } catch (IllegalArgumentException ex) {}
276         
277         DateTime dt1 = new DateTime(TEST_TIME1);
278         DateTime dt2 = new DateTime(TEST_TIME2);
279         assertEquals(true, test1.dayOfMonth().compareTo(dt2) < 0);
280         assertEquals(true, test2.dayOfMonth().compareTo(dt1) > 0);
281         assertEquals(true, test1.dayOfMonth().compareTo(dt1) == 0);
282         try {
283             test1.dayOfMonth().compareTo((ReadableInstant) null);
284             fail();
285         } catch (IllegalArgumentException ex) {}
286     }
287 
288     //-----------------------------------------------------------------------
289     public void testPropertyEquals() {
290         MonthDay test1 = new MonthDay(11, 11);
291         MonthDay test2 = new MonthDay(11, 12);
292         MonthDay test3 = new MonthDay(11, 11, CopticChronology.getInstanceUTC());
293         assertEquals(true, test1.dayOfMonth().equals(test1.dayOfMonth()));
294         assertEquals(false, test1.dayOfMonth().equals(test1.monthOfYear()));
295         assertEquals(false, test1.dayOfMonth().equals(test2.dayOfMonth()));
296         assertEquals(false, test1.dayOfMonth().equals(test2.monthOfYear()));
297         
298         assertEquals(false, test1.monthOfYear().equals(test1.dayOfMonth()));
299         assertEquals(true, test1.monthOfYear().equals(test1.monthOfYear()));
300         assertEquals(false, test1.monthOfYear().equals(test2.dayOfMonth()));
301         assertEquals(true, test1.monthOfYear().equals(test2.monthOfYear()));
302         
303         assertEquals(false, test1.dayOfMonth().equals(null));
304         assertEquals(false, test1.dayOfMonth().equals("any"));
305         
306         // chrono
307         assertEquals(false, test1.dayOfMonth().equals(test3.dayOfMonth()));
308     }
309 
310     public void testPropertyHashCode() {
311         MonthDay test1 = new MonthDay(5, 11);
312         MonthDay test2 = new MonthDay(5, 12);
313         assertEquals(true, test1.dayOfMonth().hashCode() == test1.dayOfMonth().hashCode());
314         assertEquals(false, test1.dayOfMonth().hashCode() == test2.dayOfMonth().hashCode());
315         assertEquals(true, test1.monthOfYear().hashCode() == test1.monthOfYear().hashCode());
316         assertEquals(true, test1.monthOfYear().hashCode() == test2.monthOfYear().hashCode());
317     }
318 
319     public void testPropertyEqualsHashCodeLenient() {
320         MonthDay test1 = new MonthDay(5, 6, LenientChronology.getInstance(COPTIC_PARIS));
321         MonthDay test2 = new MonthDay(5, 6, LenientChronology.getInstance(COPTIC_PARIS));
322         assertEquals(true, test1.dayOfMonth().equals(test2.dayOfMonth()));
323         assertEquals(true, test2.dayOfMonth().equals(test1.dayOfMonth()));
324         assertEquals(true, test1.dayOfMonth().equals(test1.dayOfMonth()));
325         assertEquals(true, test2.dayOfMonth().equals(test2.dayOfMonth()));
326         assertEquals(true, test1.dayOfMonth().hashCode() == test2.dayOfMonth().hashCode());
327         assertEquals(true, test1.dayOfMonth().hashCode() == test1.dayOfMonth().hashCode());
328         assertEquals(true, test2.dayOfMonth().hashCode() == test2.dayOfMonth().hashCode());
329     }
330 
331     public void testPropertyEqualsHashCodeStrict() {
332         MonthDay test1 = new MonthDay(5, 6, StrictChronology.getInstance(COPTIC_PARIS));
333         MonthDay test2 = new MonthDay(5, 6, StrictChronology.getInstance(COPTIC_PARIS));
334         assertEquals(true, test1.dayOfMonth().equals(test2.dayOfMonth()));
335         assertEquals(true, test2.dayOfMonth().equals(test1.dayOfMonth()));
336         assertEquals(true, test1.dayOfMonth().equals(test1.dayOfMonth()));
337         assertEquals(true, test2.dayOfMonth().equals(test2.dayOfMonth()));
338         assertEquals(true, test1.dayOfMonth().hashCode() == test2.dayOfMonth().hashCode());
339         assertEquals(true, test1.dayOfMonth().hashCode() == test1.dayOfMonth().hashCode());
340         assertEquals(true, test2.dayOfMonth().hashCode() == test2.dayOfMonth().hashCode());
341     }
342 
343     //-----------------------------------------------------------------------
344     private void check(MonthDay test, int monthOfYear, int dayOfMonth) {
345         assertEquals(monthOfYear, test.getMonthOfYear());
346         assertEquals(dayOfMonth, test.getDayOfMonth());
347     }
348 }