001 /*
002 * Copyright 2001-2010 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 YearMonth.
029 *
030 * @author Stephen Colebourne
031 */
032 public class TestYearMonth_Properties extends TestCase {
033
034 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
035 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS);
036
037 private long TEST_TIME_NOW =
038 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
039
040 private long TEST_TIME1 =
041 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
042 + 12L * DateTimeConstants.MILLIS_PER_HOUR
043 + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
044
045 private long TEST_TIME2 =
046 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
047 + 14L * DateTimeConstants.MILLIS_PER_HOUR
048 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
049
050 private DateTimeZone zone = null;
051
052 private Locale systemDefaultLocale = null;
053
054 public static void main(String[] args) {
055 junit.textui.TestRunner.run(suite());
056 }
057
058 public static TestSuite suite() {
059 return new TestSuite(TestYearMonth_Properties.class);
060 }
061
062 public TestYearMonth_Properties(String name) {
063 super(name);
064 }
065
066 protected void setUp() throws Exception {
067 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
068 zone = DateTimeZone.getDefault();
069 DateTimeZone.setDefault(DateTimeZone.UTC);
070 systemDefaultLocale = Locale.getDefault();
071 Locale.setDefault(Locale.ENGLISH);
072 }
073
074 protected void tearDown() throws Exception {
075 DateTimeUtils.setCurrentMillisSystem();
076 DateTimeZone.setDefault(zone);
077 zone = null;
078 Locale.setDefault(systemDefaultLocale);
079 systemDefaultLocale = null;
080 }
081
082 //-----------------------------------------------------------------------
083 public void testPropertyGetYear() {
084 YearMonth test = new YearMonth(1972, 6);
085 assertSame(test.getChronology().year(), test.year().getField());
086 assertEquals("year", test.year().getName());
087 assertEquals("Property[year]", test.year().toString());
088 assertSame(test, test.year().getReadablePartial());
089 assertSame(test, test.year().getYearMonth());
090 assertEquals(1972, test.year().get());
091 assertEquals("1972", test.year().getAsString());
092 assertEquals("1972", test.year().getAsText());
093 assertEquals("1972", test.year().getAsText(Locale.FRENCH));
094 assertEquals("1972", test.year().getAsShortText());
095 assertEquals("1972", test.year().getAsShortText(Locale.FRENCH));
096 assertEquals(test.getChronology().years(), test.year().getDurationField());
097 assertEquals(null, test.year().getRangeDurationField());
098 assertEquals(9, test.year().getMaximumTextLength(null));
099 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 }