001    /*
002     *  Copyright 2001-2011 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.time;
017    
018    import java.util.Locale;
019    
020    import junit.framework.TestCase;
021    import junit.framework.TestSuite;
022    
023    import org.joda.time.chrono.CopticChronology;
024    import org.joda.time.chrono.LenientChronology;
025    import org.joda.time.chrono.StrictChronology;
026    
027    /**
028     * This class is a Junit unit test for MonthDay. Based on {@link TestYearMonth_Propeties} 
029     */
030    public class TestMonthDay_Properties extends TestCase {
031    
032        private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
033        private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS);
034    
035        private long TEST_TIME_NOW =
036                (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
037                
038        private long TEST_TIME1 =
039            (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
040            + 12L * DateTimeConstants.MILLIS_PER_HOUR
041            + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
042            
043        private long TEST_TIME2 =
044            (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
045            + 14L * DateTimeConstants.MILLIS_PER_HOUR
046            + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
047            
048        private DateTimeZone zone = null;
049        private Locale locale = null;
050    
051        public static void main(String[] args) {
052            junit.textui.TestRunner.run(suite());
053        }
054    
055        public static TestSuite suite() {
056            return new TestSuite(TestMonthDay_Properties.class);
057        }
058    
059        public TestMonthDay_Properties(String name) {
060            super(name);
061        }
062    
063        protected void setUp() throws Exception {
064            DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
065            zone = DateTimeZone.getDefault();
066            locale = Locale.getDefault();
067            Locale.setDefault(Locale.UK);
068            DateTimeZone.setDefault(DateTimeZone.UTC);
069        }
070    
071        protected void tearDown() throws Exception {
072            DateTimeUtils.setCurrentMillisSystem();
073            DateTimeZone.setDefault(zone);
074            zone = null;
075            Locale.setDefault(locale);
076            locale = null;
077        }
078    
079        //-----------------------------------------------------------------------
080        public void testPropertyGetMonthOfYear() {
081            MonthDay test = new MonthDay(9, 6);
082            assertSame(test.getChronology().monthOfYear(), test.monthOfYear().getField());
083            assertEquals("monthOfYear", test.monthOfYear().getName());
084            assertEquals("Property[monthOfYear]", test.monthOfYear().toString());
085            assertSame(test, test.monthOfYear().getReadablePartial());
086            assertSame(test, test.monthOfYear().getMonthDay());
087            assertEquals(9, test.monthOfYear().get());
088            assertEquals("9", test.monthOfYear().getAsString());
089            assertEquals("September", test.monthOfYear().getAsText());
090            assertEquals("septembre", test.monthOfYear().getAsText(Locale.FRENCH));
091            assertEquals("Sep", test.monthOfYear().getAsShortText());
092            assertEquals("sept.", test.monthOfYear().getAsShortText(Locale.FRENCH));
093            assertEquals(test.getChronology().months(), test.monthOfYear().getDurationField());
094            // assertEquals(test.getChronology().days(), test.dayOfMonth().getRangeDurationField());
095            assertEquals(9, test.monthOfYear().getMaximumTextLength(null));
096            assertEquals(3, test.monthOfYear().getMaximumShortTextLength(null));
097        }
098    
099        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    }