xref: /aosp_15_r20/external/threetenbp/src/test/java/org/threeten/bp/temporal/TestYearMonth.java (revision 761b3f507e07ae42b4ad4333aa5dc559de53e1fb)
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp.temporal;
33 
34 import static org.testng.Assert.assertEquals;
35 import static org.testng.Assert.assertTrue;
36 import static org.testng.Assert.fail;
37 import static org.threeten.bp.temporal.ChronoField.ERA;
38 import static org.threeten.bp.temporal.ChronoField.MONTH_OF_YEAR;
39 import static org.threeten.bp.temporal.ChronoField.PROLEPTIC_MONTH;
40 import static org.threeten.bp.temporal.ChronoField.YEAR;
41 import static org.threeten.bp.temporal.ChronoField.YEAR_OF_ERA;
42 
43 import java.io.IOException;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.HashSet;
47 import java.util.List;
48 import java.util.Set;
49 
50 import org.testng.annotations.BeforeMethod;
51 import org.testng.annotations.DataProvider;
52 import org.testng.annotations.Test;
53 import org.threeten.bp.AbstractDateTimeTest;
54 import org.threeten.bp.Clock;
55 import org.threeten.bp.DateTimeException;
56 import org.threeten.bp.Instant;
57 import org.threeten.bp.LocalDate;
58 import org.threeten.bp.LocalDateTime;
59 import org.threeten.bp.LocalTime;
60 import org.threeten.bp.Month;
61 import org.threeten.bp.Year;
62 import org.threeten.bp.YearMonth;
63 import org.threeten.bp.ZoneId;
64 import org.threeten.bp.ZoneOffset;
65 import org.threeten.bp.chrono.IsoChronology;
66 import org.threeten.bp.format.DateTimeFormatter;
67 import org.threeten.bp.format.DateTimeParseException;
68 
69 /**
70  * Test YearMonth.
71  */
72 @Test
73 public class TestYearMonth extends AbstractDateTimeTest {
74 
75     private YearMonth TEST_2008_06;
76 
77     @BeforeMethod
setUp()78     public void setUp() {
79         TEST_2008_06 = YearMonth.of(2008, 6);
80     }
81 
82     //-----------------------------------------------------------------------
83     @Override
samples()84     protected List<TemporalAccessor> samples() {
85         TemporalAccessor[] array = {TEST_2008_06, };
86         return Arrays.asList(array);
87     }
88 
89     @Override
validFields()90     protected List<TemporalField> validFields() {
91         TemporalField[] array = {
92             MONTH_OF_YEAR,
93             PROLEPTIC_MONTH,
94             YEAR_OF_ERA,
95             YEAR,
96             ERA,
97         };
98         return Arrays.asList(array);
99     }
100 
101     @Override
invalidFields()102     protected List<TemporalField> invalidFields() {
103         List<TemporalField> list = new ArrayList<TemporalField>(Arrays.<TemporalField>asList(ChronoField.values()));
104         list.removeAll(validFields());
105         list.add(JulianFields.JULIAN_DAY);
106         list.add(JulianFields.MODIFIED_JULIAN_DAY);
107         list.add(JulianFields.RATA_DIE);
108         return list;
109     }
110 
111     //-----------------------------------------------------------------------
112     @Test
test_immutable()113     public void test_immutable() {
114         assertImmutable(YearMonth.class);
115     }
116 
117     @Test
test_serialization()118     public void test_serialization() throws IOException, ClassNotFoundException {
119         assertSerializable(TEST_2008_06);
120     }
121 
122     @Test
test_serialization_format()123     public void test_serialization_format() throws ClassNotFoundException, IOException {
124         assertEqualsSerialisedForm(YearMonth.of(2012, 9));
125     }
126 
127     //-----------------------------------------------------------------------
check(YearMonth test, int y, int m)128     void check(YearMonth test, int y, int m) {
129         assertEquals(test.getYear(), y);
130         assertEquals(test.getMonth().getValue(), m);
131     }
132 
133     //-----------------------------------------------------------------------
134     // now()
135     //-----------------------------------------------------------------------
136     @Test
now()137     public void now() {
138         YearMonth expected = YearMonth.now(Clock.systemDefaultZone());
139         YearMonth test = YearMonth.now();
140         for (int i = 0; i < 100; i++) {
141             if (expected.equals(test)) {
142                 return;
143             }
144             expected = YearMonth.now(Clock.systemDefaultZone());
145             test = YearMonth.now();
146         }
147         assertEquals(test, expected);
148     }
149 
150     //-----------------------------------------------------------------------
151     // now(ZoneId)
152     //-----------------------------------------------------------------------
153     @Test(expectedExceptions=NullPointerException.class)
now_ZoneId_nullZoneId()154     public void now_ZoneId_nullZoneId() {
155         YearMonth.now((ZoneId) null);
156     }
157 
158     @Test
now_ZoneId()159     public void now_ZoneId() {
160         ZoneId zone = ZoneId.of("UTC+01:02:03");
161         YearMonth expected = YearMonth.now(Clock.system(zone));
162         YearMonth test = YearMonth.now(zone);
163         for (int i = 0; i < 100; i++) {
164             if (expected.equals(test)) {
165                 return;
166             }
167             expected = YearMonth.now(Clock.system(zone));
168             test = YearMonth.now(zone);
169         }
170         assertEquals(test, expected);
171     }
172 
173     //-----------------------------------------------------------------------
174     // now(Clock)
175     //-----------------------------------------------------------------------
176     @Test
now_Clock()177     public void now_Clock() {
178         Instant instant = LocalDateTime.of(2010, 12, 31, 0, 0).toInstant(ZoneOffset.UTC);
179         Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
180         YearMonth test = YearMonth.now(clock);
181         assertEquals(test.getYear(), 2010);
182         assertEquals(test.getMonth(), Month.DECEMBER);
183     }
184 
185     @Test(expectedExceptions=NullPointerException.class)
now_Clock_nullClock()186     public void now_Clock_nullClock() {
187         YearMonth.now((Clock) null);
188     }
189 
190     //-----------------------------------------------------------------------
191     @Test
factory_intsMonth()192     public void factory_intsMonth() {
193         YearMonth test = YearMonth.of(2008, Month.FEBRUARY);
194         check(test, 2008, 2);
195     }
196 
197     @Test(expectedExceptions=DateTimeException.class)
test_factory_intsMonth_yearTooLow()198     public void test_factory_intsMonth_yearTooLow() {
199         YearMonth.of(Year.MIN_VALUE - 1, Month.JANUARY);
200     }
201 
202     @Test(expectedExceptions=DateTimeException.class)
test_factory_intsMonth_dayTooHigh()203     public void test_factory_intsMonth_dayTooHigh() {
204         YearMonth.of(Year.MAX_VALUE + 1, Month.JANUARY);
205     }
206 
207     @Test(expectedExceptions=NullPointerException.class)
factory_intsMonth_nullMonth()208     public void factory_intsMonth_nullMonth() {
209         YearMonth.of(2008, null);
210     }
211 
212     //-----------------------------------------------------------------------
213     @Test
factory_ints()214     public void factory_ints() {
215         YearMonth test = YearMonth.of(2008, 2);
216         check(test, 2008, 2);
217     }
218 
219     @Test(expectedExceptions=DateTimeException.class)
test_factory_ints_yearTooLow()220     public void test_factory_ints_yearTooLow() {
221         YearMonth.of(Year.MIN_VALUE - 1, 2);
222     }
223 
224     @Test(expectedExceptions=DateTimeException.class)
test_factory_ints_dayTooHigh()225     public void test_factory_ints_dayTooHigh() {
226         YearMonth.of(Year.MAX_VALUE + 1, 2);
227     }
228 
229     @Test(expectedExceptions=DateTimeException.class)
test_factory_ints_monthTooLow()230     public void test_factory_ints_monthTooLow() {
231         YearMonth.of(2008, 0);
232     }
233 
234     @Test(expectedExceptions=DateTimeException.class)
test_factory_ints_monthTooHigh()235     public void test_factory_ints_monthTooHigh() {
236         YearMonth.of(2008, 13);
237     }
238 
239     //-----------------------------------------------------------------------
240     @Test
test_factory_CalendricalObject()241     public void test_factory_CalendricalObject() {
242         assertEquals(YearMonth.from(LocalDate.of(2007, 7, 15)), YearMonth.of(2007, 7));
243     }
244 
245     @Test(expectedExceptions=DateTimeException.class)
test_factory_CalendricalObject_invalid_noDerive()246     public void test_factory_CalendricalObject_invalid_noDerive() {
247         YearMonth.from(LocalTime.of(12, 30));
248     }
249 
250     @Test(expectedExceptions=NullPointerException.class)
test_factory_CalendricalObject_null()251     public void test_factory_CalendricalObject_null() {
252         YearMonth.from((TemporalAccessor) null);
253     }
254 
255     //-----------------------------------------------------------------------
256     // parse()
257     //-----------------------------------------------------------------------
258     @DataProvider(name="goodParseData")
provider_goodParseData()259     Object[][] provider_goodParseData() {
260         return new Object[][] {
261                 {"0000-01", YearMonth.of(0, 1)},
262                 {"0000-12", YearMonth.of(0, 12)},
263                 {"9999-12", YearMonth.of(9999, 12)},
264                 {"2000-01", YearMonth.of(2000, 1)},
265                 {"2000-02", YearMonth.of(2000, 2)},
266                 {"2000-03", YearMonth.of(2000, 3)},
267                 {"2000-04", YearMonth.of(2000, 4)},
268                 {"2000-05", YearMonth.of(2000, 5)},
269                 {"2000-06", YearMonth.of(2000, 6)},
270                 {"2000-07", YearMonth.of(2000, 7)},
271                 {"2000-08", YearMonth.of(2000, 8)},
272                 {"2000-09", YearMonth.of(2000, 9)},
273                 {"2000-10", YearMonth.of(2000, 10)},
274                 {"2000-11", YearMonth.of(2000, 11)},
275                 {"2000-12", YearMonth.of(2000, 12)},
276 
277                 {"+12345678-03", YearMonth.of(12345678, 3)},
278                 {"+123456-03", YearMonth.of(123456, 3)},
279                 {"0000-03", YearMonth.of(0, 3)},
280                 {"-1234-03", YearMonth.of(-1234, 3)},
281                 {"-12345678-03", YearMonth.of(-12345678, 3)},
282 
283                 {"+" + Year.MAX_VALUE + "-03", YearMonth.of(Year.MAX_VALUE, 3)},
284                 {Year.MIN_VALUE + "-03", YearMonth.of(Year.MIN_VALUE, 3)},
285         };
286     }
287 
288     @Test(dataProvider="goodParseData")
factory_parse_success(String text, YearMonth expected)289     public void factory_parse_success(String text, YearMonth expected) {
290         YearMonth yearMonth = YearMonth.parse(text);
291         assertEquals(yearMonth, expected);
292     }
293 
294     //-----------------------------------------------------------------------
295     @DataProvider(name="badParseData")
provider_badParseData()296     Object[][] provider_badParseData() {
297         return new Object[][] {
298                 {"", 0},
299                 {"-00", 1},
300                 {"--01-0", 1},
301                 {"A01-3", 0},
302                 {"200-01", 0},
303                 {"2009/12", 4},
304 
305                 {"-0000-10", 0},
306                 {"-12345678901-10", 11},
307                 {"+1-10", 1},
308                 {"+12-10", 1},
309                 {"+123-10", 1},
310                 {"+1234-10", 0},
311                 {"12345-10", 0},
312                 {"+12345678901-10", 11},
313         };
314     }
315 
316     @Test(dataProvider="badParseData", expectedExceptions=DateTimeParseException.class)
factory_parse_fail(String text, int pos)317     public void factory_parse_fail(String text, int pos) {
318         try {
319             YearMonth.parse(text);
320             fail(String.format("Parse should have failed for %s at position %d", text, pos));
321         } catch (DateTimeParseException ex) {
322             assertEquals(ex.getParsedString(), text);
323             assertEquals(ex.getErrorIndex(), pos);
324             throw ex;
325         }
326     }
327 
328     //-----------------------------------------------------------------------
329     @Test(expectedExceptions=DateTimeParseException.class)
factory_parse_illegalValue_Month()330     public void factory_parse_illegalValue_Month() {
331         YearMonth.parse("2008-13");
332     }
333 
334     @Test(expectedExceptions=NullPointerException.class)
factory_parse_nullText()335     public void factory_parse_nullText() {
336         YearMonth.parse(null);
337     }
338 
339     //-----------------------------------------------------------------------
340     // parse(DateTimeFormatter)
341     //-----------------------------------------------------------------------
342     @Test
factory_parse_formatter()343     public void factory_parse_formatter() {
344         DateTimeFormatter f = DateTimeFormatter.ofPattern("u M");
345         YearMonth test = YearMonth.parse("2010 12", f);
346         assertEquals(test, YearMonth.of(2010, 12));
347     }
348 
349     @Test(expectedExceptions=NullPointerException.class)
factory_parse_formatter_nullText()350     public void factory_parse_formatter_nullText() {
351         DateTimeFormatter f = DateTimeFormatter.ofPattern("u M");
352         YearMonth.parse((String) null, f);
353     }
354 
355     @Test(expectedExceptions=NullPointerException.class)
factory_parse_formatter_nullFormatter()356     public void factory_parse_formatter_nullFormatter() {
357         YearMonth.parse("ANY", null);
358     }
359 
360     //-----------------------------------------------------------------------
361     // get(TemporalField)
362     //-----------------------------------------------------------------------
363     @Test
test_get_TemporalField()364     public void test_get_TemporalField() {
365         assertEquals(TEST_2008_06.get(YEAR), 2008);
366         assertEquals(TEST_2008_06.get(MONTH_OF_YEAR), 6);
367         assertEquals(TEST_2008_06.get(YEAR_OF_ERA), 2008);
368         assertEquals(TEST_2008_06.get(ERA), 1);
369     }
370 
371     @Test(expectedExceptions=DateTimeException.class)
test_get_TemporalField_tooBig()372     public void test_get_TemporalField_tooBig() {
373         TEST_2008_06.get(PROLEPTIC_MONTH);
374     }
375 
376     @Test(expectedExceptions=NullPointerException.class)
test_get_TemporalField_null()377     public void test_get_TemporalField_null() {
378         TEST_2008_06.get((TemporalField) null);
379     }
380 
381     @Test(expectedExceptions=DateTimeException.class)
test_get_TemporalField_invalidField()382     public void test_get_TemporalField_invalidField() {
383         TEST_2008_06.get(MockFieldNoValue.INSTANCE);
384     }
385 
386     @Test(expectedExceptions=DateTimeException.class)
test_get_TemporalField_timeField()387     public void test_get_TemporalField_timeField() {
388         TEST_2008_06.get(ChronoField.AMPM_OF_DAY);
389     }
390 
391     //-----------------------------------------------------------------------
392     // getLong(TemporalField)
393     //-----------------------------------------------------------------------
394     @Test
test_getLong_TemporalField()395     public void test_getLong_TemporalField() {
396         assertEquals(TEST_2008_06.getLong(YEAR), 2008);
397         assertEquals(TEST_2008_06.getLong(MONTH_OF_YEAR), 6);
398         assertEquals(TEST_2008_06.getLong(YEAR_OF_ERA), 2008);
399         assertEquals(TEST_2008_06.getLong(ERA), 1);
400         assertEquals(TEST_2008_06.getLong(PROLEPTIC_MONTH), 2008 * 12 + 6 - 1);
401     }
402 
403     @Test(expectedExceptions=NullPointerException.class)
test_getLong_TemporalField_null()404     public void test_getLong_TemporalField_null() {
405         TEST_2008_06.getLong((TemporalField) null);
406     }
407 
408     @Test(expectedExceptions=DateTimeException.class)
test_getLong_TemporalField_invalidField()409     public void test_getLong_TemporalField_invalidField() {
410         TEST_2008_06.getLong(MockFieldNoValue.INSTANCE);
411     }
412 
413     @Test(expectedExceptions=DateTimeException.class)
test_getLong_TemporalField_timeField()414     public void test_getLong_TemporalField_timeField() {
415         TEST_2008_06.getLong(ChronoField.AMPM_OF_DAY);
416     }
417 
418     //-----------------------------------------------------------------------
419     // get*()
420     //-----------------------------------------------------------------------
421     @DataProvider(name="sampleDates")
provider_sampleDates()422     Object[][] provider_sampleDates() {
423         return new Object[][] {
424             {2008, 1},
425             {2008, 2},
426             {-1, 3},
427             {0, 12},
428         };
429     }
430 
431     //-----------------------------------------------------------------------
432     // with(Year)
433     //-----------------------------------------------------------------------
434     @Test
test_with_Year()435     public void test_with_Year() {
436         YearMonth test = YearMonth.of(2008, 6);
437         assertEquals(test.with(Year.of(2000)), YearMonth.of(2000, 6));
438     }
439 
440     @Test
test_with_Year_noChange_equal()441     public void test_with_Year_noChange_equal() {
442         YearMonth test = YearMonth.of(2008, 6);
443         assertEquals(test.with(Year.of(2008)), test);
444     }
445 
446     @Test(expectedExceptions=NullPointerException.class)
test_with_Year_null()447     public void test_with_Year_null() {
448         YearMonth test = YearMonth.of(2008, 6);
449         test.with((Year) null);
450     }
451 
452     //-----------------------------------------------------------------------
453     // with(Month)
454     //-----------------------------------------------------------------------
455     @Test
test_with_Month()456     public void test_with_Month() {
457         YearMonth test = YearMonth.of(2008, 6);
458         assertEquals(test.with(Month.JANUARY), YearMonth.of(2008, 1));
459     }
460 
461     @Test
test_with_Month_noChange_equal()462     public void test_with_Month_noChange_equal() {
463         YearMonth test = YearMonth.of(2008, 6);
464         assertEquals(test.with(Month.JUNE), test);
465     }
466 
467     @Test(expectedExceptions=NullPointerException.class)
test_with_Month_null()468     public void test_with_Month_null() {
469         YearMonth test = YearMonth.of(2008, 6);
470         test.with((Month) null);
471     }
472 
473     //-----------------------------------------------------------------------
474     // withYear()
475     //-----------------------------------------------------------------------
476     @Test
test_withYear()477     public void test_withYear() {
478         YearMonth test = YearMonth.of(2008, 6);
479         assertEquals(test.withYear(1999), YearMonth.of(1999, 6));
480     }
481 
482     @Test
test_withYear_int_noChange_equal()483     public void test_withYear_int_noChange_equal() {
484         YearMonth test = YearMonth.of(2008, 6);
485         assertEquals(test.withYear(2008), test);
486     }
487 
488     @Test(expectedExceptions=DateTimeException.class)
test_withYear_tooLow()489     public void test_withYear_tooLow() {
490         YearMonth test = YearMonth.of(2008, 6);
491         test.withYear(Year.MIN_VALUE - 1);
492     }
493 
494     @Test(expectedExceptions=DateTimeException.class)
test_withYear_tooHigh()495     public void test_withYear_tooHigh() {
496         YearMonth test = YearMonth.of(2008, 6);
497         test.withYear(Year.MAX_VALUE + 1);
498     }
499 
500     //-----------------------------------------------------------------------
501     // withMonth()
502     //-----------------------------------------------------------------------
503     @Test
test_withMonth()504     public void test_withMonth() {
505         YearMonth test = YearMonth.of(2008, 6);
506         assertEquals(test.withMonth(1), YearMonth.of(2008, 1));
507     }
508 
509     @Test
test_withMonth_int_noChange_equal()510     public void test_withMonth_int_noChange_equal() {
511         YearMonth test = YearMonth.of(2008, 6);
512         assertEquals(test.withMonth(6), test);
513     }
514 
515     @Test(expectedExceptions=DateTimeException.class)
test_withMonth_tooLow()516     public void test_withMonth_tooLow() {
517         YearMonth test = YearMonth.of(2008, 6);
518         test.withMonth(0);
519     }
520 
521     @Test(expectedExceptions=DateTimeException.class)
test_withMonth_tooHigh()522     public void test_withMonth_tooHigh() {
523         YearMonth test = YearMonth.of(2008, 6);
524         test.withMonth(13);
525     }
526 
527     //-----------------------------------------------------------------------
528     // plusYears()
529     //-----------------------------------------------------------------------
530     @Test
test_plusYears_long()531     public void test_plusYears_long() {
532         YearMonth test = YearMonth.of(2008, 6);
533         assertEquals(test.plusYears(1), YearMonth.of(2009, 6));
534     }
535 
536     @Test
test_plusYears_long_noChange_equal()537     public void test_plusYears_long_noChange_equal() {
538         YearMonth test = YearMonth.of(2008, 6);
539         assertEquals(test.plusYears(0), test);
540     }
541 
542     @Test
test_plusYears_long_negative()543     public void test_plusYears_long_negative() {
544         YearMonth test = YearMonth.of(2008, 6);
545         assertEquals(test.plusYears(-1), YearMonth.of(2007, 6));
546     }
547 
548     @Test
test_plusYears_long_big()549     public void test_plusYears_long_big() {
550         YearMonth test = YearMonth.of(-40, 6);
551         assertEquals(test.plusYears(20L + Year.MAX_VALUE), YearMonth.of((int) (-40L + 20L + Year.MAX_VALUE), 6));
552     }
553 
554     @Test(expectedExceptions=DateTimeException.class)
test_plusYears_long_invalidTooLarge()555     public void test_plusYears_long_invalidTooLarge() {
556         YearMonth test = YearMonth.of(Year.MAX_VALUE, 6);
557         test.plusYears(1);
558     }
559 
560     @Test(expectedExceptions=DateTimeException.class)
test_plusYears_long_invalidTooLargeMaxAddMax()561     public void test_plusYears_long_invalidTooLargeMaxAddMax() {
562         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
563         test.plusYears(Long.MAX_VALUE);
564     }
565 
566     @Test(expectedExceptions=DateTimeException.class)
test_plusYears_long_invalidTooLargeMaxAddMin()567     public void test_plusYears_long_invalidTooLargeMaxAddMin() {
568         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
569         test.plusYears(Long.MIN_VALUE);
570     }
571 
572     @Test(expectedExceptions=DateTimeException.class)
test_plusYears_long_invalidTooSmall()573     public void test_plusYears_long_invalidTooSmall() {
574         YearMonth test = YearMonth.of(Year.MIN_VALUE, 6);
575         test.plusYears(-1);
576     }
577 
578     //-----------------------------------------------------------------------
579     // plusMonths()
580     //-----------------------------------------------------------------------
581     @Test
test_plusMonths_long()582     public void test_plusMonths_long() {
583         YearMonth test = YearMonth.of(2008, 6);
584         assertEquals(test.plusMonths(1), YearMonth.of(2008, 7));
585     }
586 
587     @Test
test_plusMonths_long_noChange_equal()588     public void test_plusMonths_long_noChange_equal() {
589         YearMonth test = YearMonth.of(2008, 6);
590         assertEquals(test.plusMonths(0), test);
591     }
592 
593     @Test
test_plusMonths_long_overYears()594     public void test_plusMonths_long_overYears() {
595         YearMonth test = YearMonth.of(2008, 6);
596         assertEquals(test.plusMonths(7), YearMonth.of(2009, 1));
597     }
598 
599     @Test
test_plusMonths_long_negative()600     public void test_plusMonths_long_negative() {
601         YearMonth test = YearMonth.of(2008, 6);
602         assertEquals(test.plusMonths(-1), YearMonth.of(2008, 5));
603     }
604 
605     @Test
test_plusMonths_long_negativeOverYear()606     public void test_plusMonths_long_negativeOverYear() {
607         YearMonth test = YearMonth.of(2008, 6);
608         assertEquals(test.plusMonths(-6), YearMonth.of(2007, 12));
609     }
610 
611     @Test
test_plusMonths_long_big()612     public void test_plusMonths_long_big() {
613         YearMonth test = YearMonth.of(-40, 6);
614         long months = 20L + Integer.MAX_VALUE;
615         assertEquals(test.plusMonths(months), YearMonth.of((int) (-40L + months / 12), 6 + (int) (months % 12)));
616     }
617 
618     @Test(expectedExceptions={DateTimeException.class})
test_plusMonths_long_invalidTooLarge()619     public void test_plusMonths_long_invalidTooLarge() {
620         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
621         test.plusMonths(1);
622     }
623 
624     @Test(expectedExceptions=DateTimeException.class)
test_plusMonths_long_invalidTooLargeMaxAddMax()625     public void test_plusMonths_long_invalidTooLargeMaxAddMax() {
626         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
627         test.plusMonths(Long.MAX_VALUE);
628     }
629 
630     @Test(expectedExceptions=DateTimeException.class)
test_plusMonths_long_invalidTooLargeMaxAddMin()631     public void test_plusMonths_long_invalidTooLargeMaxAddMin() {
632         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
633         test.plusMonths(Long.MIN_VALUE);
634     }
635 
636     @Test(expectedExceptions={DateTimeException.class})
test_plusMonths_long_invalidTooSmall()637     public void test_plusMonths_long_invalidTooSmall() {
638         YearMonth test = YearMonth.of(Year.MIN_VALUE, 1);
639         test.plusMonths(-1);
640     }
641 
642     //-----------------------------------------------------------------------
643     // minusYears()
644     //-----------------------------------------------------------------------
645     @Test
test_minusYears_long()646     public void test_minusYears_long() {
647         YearMonth test = YearMonth.of(2008, 6);
648         assertEquals(test.minusYears(1), YearMonth.of(2007, 6));
649     }
650 
651     @Test
test_minusYears_long_noChange_equal()652     public void test_minusYears_long_noChange_equal() {
653         YearMonth test = YearMonth.of(2008, 6);
654         assertEquals(test.minusYears(0), test);
655     }
656 
657     @Test
test_minusYears_long_negative()658     public void test_minusYears_long_negative() {
659         YearMonth test = YearMonth.of(2008, 6);
660         assertEquals(test.minusYears(-1), YearMonth.of(2009, 6));
661     }
662 
663     @Test
test_minusYears_long_big()664     public void test_minusYears_long_big() {
665         YearMonth test = YearMonth.of(40, 6);
666         assertEquals(test.minusYears(20L + Year.MAX_VALUE), YearMonth.of((int) (40L - 20L - Year.MAX_VALUE), 6));
667     }
668 
669     @Test(expectedExceptions=DateTimeException.class)
test_minusYears_long_invalidTooLarge()670     public void test_minusYears_long_invalidTooLarge() {
671         YearMonth test = YearMonth.of(Year.MAX_VALUE, 6);
672         test.minusYears(-1);
673     }
674 
675     @Test(expectedExceptions=DateTimeException.class)
test_minusYears_long_invalidTooLargeMaxSubtractMax()676     public void test_minusYears_long_invalidTooLargeMaxSubtractMax() {
677         YearMonth test = YearMonth.of(Year.MIN_VALUE, 12);
678         test.minusYears(Long.MAX_VALUE);
679     }
680 
681     @Test(expectedExceptions=DateTimeException.class)
test_minusYears_long_invalidTooLargeMaxSubtractMin()682     public void test_minusYears_long_invalidTooLargeMaxSubtractMin() {
683         YearMonth test = YearMonth.of(Year.MIN_VALUE, 12);
684         test.minusYears(Long.MIN_VALUE);
685     }
686 
687     @Test(expectedExceptions=DateTimeException.class)
test_minusYears_long_invalidTooSmall()688     public void test_minusYears_long_invalidTooSmall() {
689         YearMonth test = YearMonth.of(Year.MIN_VALUE, 6);
690         test.minusYears(1);
691     }
692 
693     //-----------------------------------------------------------------------
694     // minusMonths()
695     //-----------------------------------------------------------------------
696     @Test
test_minusMonths_long()697     public void test_minusMonths_long() {
698         YearMonth test = YearMonth.of(2008, 6);
699         assertEquals(test.minusMonths(1), YearMonth.of(2008, 5));
700     }
701 
702     @Test
test_minusMonths_long_noChange_equal()703     public void test_minusMonths_long_noChange_equal() {
704         YearMonth test = YearMonth.of(2008, 6);
705         assertEquals(test.minusMonths(0), test);
706     }
707 
708     @Test
test_minusMonths_long_overYears()709     public void test_minusMonths_long_overYears() {
710         YearMonth test = YearMonth.of(2008, 6);
711         assertEquals(test.minusMonths(6), YearMonth.of(2007, 12));
712     }
713 
714     @Test
test_minusMonths_long_negative()715     public void test_minusMonths_long_negative() {
716         YearMonth test = YearMonth.of(2008, 6);
717         assertEquals(test.minusMonths(-1), YearMonth.of(2008, 7));
718     }
719 
720     @Test
test_minusMonths_long_negativeOverYear()721     public void test_minusMonths_long_negativeOverYear() {
722         YearMonth test = YearMonth.of(2008, 6);
723         assertEquals(test.minusMonths(-7), YearMonth.of(2009, 1));
724     }
725 
726     @Test
test_minusMonths_long_big()727     public void test_minusMonths_long_big() {
728         YearMonth test = YearMonth.of(40, 6);
729         long months = 20L + Integer.MAX_VALUE;
730         assertEquals(test.minusMonths(months), YearMonth.of((int) (40L - months / 12), 6 - (int) (months % 12)));
731     }
732 
733     @Test(expectedExceptions={DateTimeException.class})
test_minusMonths_long_invalidTooLarge()734     public void test_minusMonths_long_invalidTooLarge() {
735         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
736         test.minusMonths(-1);
737     }
738 
739     @Test(expectedExceptions=DateTimeException.class)
test_minusMonths_long_invalidTooLargeMaxSubtractMax()740     public void test_minusMonths_long_invalidTooLargeMaxSubtractMax() {
741         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
742         test.minusMonths(Long.MAX_VALUE);
743     }
744 
745     @Test(expectedExceptions=DateTimeException.class)
test_minusMonths_long_invalidTooLargeMaxSubtractMin()746     public void test_minusMonths_long_invalidTooLargeMaxSubtractMin() {
747         YearMonth test = YearMonth.of(Year.MAX_VALUE, 12);
748         test.minusMonths(Long.MIN_VALUE);
749     }
750 
751     @Test(expectedExceptions={DateTimeException.class})
test_minusMonths_long_invalidTooSmall()752     public void test_minusMonths_long_invalidTooSmall() {
753         YearMonth test = YearMonth.of(Year.MIN_VALUE, 1);
754         test.minusMonths(1);
755     }
756 
757     //-----------------------------------------------------------------------
758     // doAdjustment()
759     //-----------------------------------------------------------------------
760     @Test
test_adjustDate()761     public void test_adjustDate() {
762         YearMonth test = YearMonth.of(2008, 6);
763         LocalDate date = LocalDate.of(2007, 1, 1);
764         assertEquals(test.adjustInto(date), LocalDate.of(2008, 6, 1));
765     }
766 
767     @Test
test_adjustDate_preserveDoM()768     public void test_adjustDate_preserveDoM() {
769         YearMonth test = YearMonth.of(2011, 3);
770         LocalDate date = LocalDate.of(2008, 2, 29);
771         assertEquals(test.adjustInto(date), LocalDate.of(2011, 3, 29));
772     }
773 
774     @Test
test_adjustDate_resolve()775     public void test_adjustDate_resolve() {
776         YearMonth test = YearMonth.of(2007, 2);
777         LocalDate date = LocalDate.of(2008, 3, 31);
778         assertEquals(test.adjustInto(date), LocalDate.of(2007, 2, 28));
779     }
780 
781     @Test
test_adjustDate_equal()782     public void test_adjustDate_equal() {
783         YearMonth test = YearMonth.of(2008, 6);
784         LocalDate date = LocalDate.of(2008, 6, 30);
785         assertEquals(test.adjustInto(date), date);
786     }
787 
788     @Test(expectedExceptions=NullPointerException.class)
test_adjustDate_null()789     public void test_adjustDate_null() {
790         TEST_2008_06.adjustInto((LocalDate) null);
791     }
792 
793     //-----------------------------------------------------------------------
794     // isLeapYear()
795     //-----------------------------------------------------------------------
796     @Test
test_isLeapYear()797     public void test_isLeapYear() {
798         assertEquals(YearMonth.of(2007, 6).isLeapYear(), false);
799         assertEquals(YearMonth.of(2008, 6).isLeapYear(), true);
800     }
801 
802     //-----------------------------------------------------------------------
803     // lengthOfMonth()
804     //-----------------------------------------------------------------------
805     @Test
test_lengthOfMonth_june()806     public void test_lengthOfMonth_june() {
807         YearMonth test = YearMonth.of(2007, 6);
808         assertEquals(test.lengthOfMonth(), 30);
809     }
810 
811     @Test
test_lengthOfMonth_febNonLeap()812     public void test_lengthOfMonth_febNonLeap() {
813         YearMonth test = YearMonth.of(2007, 2);
814         assertEquals(test.lengthOfMonth(), 28);
815     }
816 
817     @Test
test_lengthOfMonth_febLeap()818     public void test_lengthOfMonth_febLeap() {
819         YearMonth test = YearMonth.of(2008, 2);
820         assertEquals(test.lengthOfMonth(), 29);
821     }
822 
823     //-----------------------------------------------------------------------
824     // lengthOfYear()
825     //-----------------------------------------------------------------------
826     @Test
test_lengthOfYear()827     public void test_lengthOfYear() {
828         assertEquals(YearMonth.of(2007, 6).lengthOfYear(), 365);
829         assertEquals(YearMonth.of(2008, 6).lengthOfYear(), 366);
830     }
831 
832     //-----------------------------------------------------------------------
833     // isValidDay(int)
834     //-----------------------------------------------------------------------
835     @Test
test_isValidDay_int_june()836     public void test_isValidDay_int_june() {
837         YearMonth test = YearMonth.of(2007, 6);
838         assertEquals(test.isValidDay(1), true);
839         assertEquals(test.isValidDay(30), true);
840 
841         assertEquals(test.isValidDay(-1), false);
842         assertEquals(test.isValidDay(0), false);
843         assertEquals(test.isValidDay(31), false);
844         assertEquals(test.isValidDay(32), false);
845     }
846 
847     @Test
test_isValidDay_int_febNonLeap()848     public void test_isValidDay_int_febNonLeap() {
849         YearMonth test = YearMonth.of(2007, 2);
850         assertEquals(test.isValidDay(1), true);
851         assertEquals(test.isValidDay(28), true);
852 
853         assertEquals(test.isValidDay(-1), false);
854         assertEquals(test.isValidDay(0), false);
855         assertEquals(test.isValidDay(29), false);
856         assertEquals(test.isValidDay(32), false);
857     }
858 
859     @Test
test_isValidDay_int_febLeap()860     public void test_isValidDay_int_febLeap() {
861         YearMonth test = YearMonth.of(2008, 2);
862         assertEquals(test.isValidDay(1), true);
863         assertEquals(test.isValidDay(29), true);
864 
865         assertEquals(test.isValidDay(-1), false);
866         assertEquals(test.isValidDay(0), false);
867         assertEquals(test.isValidDay(30), false);
868         assertEquals(test.isValidDay(32), false);
869     }
870 
871     //-----------------------------------------------------------------------
872     // atDay(int)
873     //-----------------------------------------------------------------------
874     @Test
test_atDay_int()875     public void test_atDay_int() {
876         YearMonth test = YearMonth.of(2008, 6);
877         assertEquals(test.atDay(30), LocalDate.of(2008, 6, 30));
878     }
879 
880     @Test(expectedExceptions=DateTimeException.class)
test_atDay_int_invalidDay()881     public void test_atDay_int_invalidDay() {
882         YearMonth test = YearMonth.of(2008, 6);
883         test.atDay(31);
884     }
885 
886     //-----------------------------------------------------------------------
887     // query(TemporalQuery)
888     //-----------------------------------------------------------------------
889     @Test
test_query()890     public void test_query() {
891         assertEquals(TEST_2008_06.query(TemporalQueries.chronology()), IsoChronology.INSTANCE);
892         assertEquals(TEST_2008_06.query(TemporalQueries.localDate()), null);
893         assertEquals(TEST_2008_06.query(TemporalQueries.localTime()), null);
894         assertEquals(TEST_2008_06.query(TemporalQueries.offset()), null);
895         assertEquals(TEST_2008_06.query(TemporalQueries.precision()), ChronoUnit.MONTHS);
896         assertEquals(TEST_2008_06.query(TemporalQueries.zone()), null);
897         assertEquals(TEST_2008_06.query(TemporalQueries.zoneId()), null);
898     }
899 
900     @Test(expectedExceptions=NullPointerException.class)
test_query_null()901     public void test_query_null() {
902         TEST_2008_06.query(null);
903     }
904 
905     //-----------------------------------------------------------------------
906     // compareTo()
907     //-----------------------------------------------------------------------
908     @Test
test_comparisons()909     public void test_comparisons() {
910         doTest_comparisons_YearMonth(
911             YearMonth.of(-1, 1),
912             YearMonth.of(0, 1),
913             YearMonth.of(0, 12),
914             YearMonth.of(1, 1),
915             YearMonth.of(1, 2),
916             YearMonth.of(1, 12),
917             YearMonth.of(2008, 1),
918             YearMonth.of(2008, 6),
919             YearMonth.of(2008, 12)
920         );
921     }
922 
doTest_comparisons_YearMonth(YearMonth... localDates)923     void doTest_comparisons_YearMonth(YearMonth... localDates) {
924         for (int i = 0; i < localDates.length; i++) {
925             YearMonth a = localDates[i];
926             for (int j = 0; j < localDates.length; j++) {
927                 YearMonth b = localDates[j];
928                 if (i < j) {
929                     assertTrue(a.compareTo(b) < 0, a + " <=> " + b);
930                     assertEquals(a.isBefore(b), true, a + " <=> " + b);
931                     assertEquals(a.isAfter(b), false, a + " <=> " + b);
932                     assertEquals(a.equals(b), false, a + " <=> " + b);
933                 } else if (i > j) {
934                     assertTrue(a.compareTo(b) > 0, a + " <=> " + b);
935                     assertEquals(a.isBefore(b), false, a + " <=> " + b);
936                     assertEquals(a.isAfter(b), true, a + " <=> " + b);
937                     assertEquals(a.equals(b), false, a + " <=> " + b);
938                 } else {
939                     assertEquals(a.compareTo(b), 0, a + " <=> " + b);
940                     assertEquals(a.isBefore(b), false, a + " <=> " + b);
941                     assertEquals(a.isAfter(b), false, a + " <=> " + b);
942                     assertEquals(a.equals(b), true, a + " <=> " + b);
943                 }
944             }
945         }
946     }
947 
948     @Test(expectedExceptions=NullPointerException.class)
test_compareTo_ObjectNull()949     public void test_compareTo_ObjectNull() {
950         TEST_2008_06.compareTo(null);
951     }
952 
953     @Test(expectedExceptions=NullPointerException.class)
test_isBefore_ObjectNull()954     public void test_isBefore_ObjectNull() {
955         TEST_2008_06.isBefore(null);
956     }
957 
958     @Test(expectedExceptions=NullPointerException.class)
test_isAfter_ObjectNull()959     public void test_isAfter_ObjectNull() {
960         TEST_2008_06.isAfter(null);
961     }
962 
963     //-----------------------------------------------------------------------
964     // equals()
965     //-----------------------------------------------------------------------
966     @Test
test_equals()967     public void test_equals() {
968         YearMonth a = YearMonth.of(2008, 6);
969         YearMonth b = YearMonth.of(2008, 6);
970         YearMonth c = YearMonth.of(2007, 6);
971         YearMonth d = YearMonth.of(2008, 5);
972 
973         assertEquals(a.equals(a), true);
974         assertEquals(a.equals(b), true);
975         assertEquals(a.equals(c), false);
976         assertEquals(a.equals(d), false);
977 
978         assertEquals(b.equals(a), true);
979         assertEquals(b.equals(b), true);
980         assertEquals(b.equals(c), false);
981         assertEquals(b.equals(d), false);
982 
983         assertEquals(c.equals(a), false);
984         assertEquals(c.equals(b), false);
985         assertEquals(c.equals(c), true);
986         assertEquals(c.equals(d), false);
987 
988         assertEquals(d.equals(a), false);
989         assertEquals(d.equals(b), false);
990         assertEquals(d.equals(c), false);
991         assertEquals(d.equals(d), true);
992     }
993 
994     @Test
test_equals_itself_true()995     public void test_equals_itself_true() {
996         assertEquals(TEST_2008_06.equals(TEST_2008_06), true);
997     }
998 
999     @Test
test_equals_string_false()1000     public void test_equals_string_false() {
1001         assertEquals(TEST_2008_06.equals("2007-07-15"), false);
1002     }
1003 
1004     @Test
test_equals_null_false()1005     public void test_equals_null_false() {
1006         assertEquals(TEST_2008_06.equals(null), false);
1007     }
1008 
1009     //-----------------------------------------------------------------------
1010     // hashCode()
1011     //-----------------------------------------------------------------------
1012     @Test(dataProvider="sampleDates")
test_hashCode(int y, int m)1013     public void test_hashCode(int y, int m) {
1014         YearMonth a = YearMonth.of(y, m);
1015         assertEquals(a.hashCode(), a.hashCode());
1016         YearMonth b = YearMonth.of(y, m);
1017         assertEquals(a.hashCode(), b.hashCode());
1018     }
1019 
1020     @Test
test_hashCode_unique()1021     public void test_hashCode_unique() {
1022         Set<Integer> uniques = new HashSet<Integer>(201 * 12);
1023         for (int i = 1900; i <= 2100; i++) {
1024             for (int j = 1; j <= 12; j++) {
1025                 assertTrue(uniques.add(YearMonth.of(i, j).hashCode()));
1026             }
1027         }
1028     }
1029 
1030     //-----------------------------------------------------------------------
1031     // toString()
1032     //-----------------------------------------------------------------------
1033     @DataProvider(name="sampleToString")
provider_sampleToString()1034     Object[][] provider_sampleToString() {
1035         return new Object[][] {
1036             {2008, 1, "2008-01"},
1037             {2008, 12, "2008-12"},
1038             {7, 5, "0007-05"},
1039             {0, 5, "0000-05"},
1040             {-1, 1, "-0001-01"},
1041         };
1042     }
1043 
1044     @Test(dataProvider="sampleToString")
test_toString(int y, int m, String expected)1045     public void test_toString(int y, int m, String expected) {
1046         YearMonth test = YearMonth.of(y, m);
1047         String str = test.toString();
1048         assertEquals(str, expected);
1049     }
1050 
1051     //-----------------------------------------------------------------------
1052     // format(DateTimeFormatter)
1053     //-----------------------------------------------------------------------
1054     @Test
test_format_formatter()1055     public void test_format_formatter() {
1056         DateTimeFormatter f = DateTimeFormatter.ofPattern("y M");
1057         String t = YearMonth.of(2010, 12).format(f);
1058         assertEquals(t, "2010 12");
1059     }
1060 
1061     @Test(expectedExceptions=NullPointerException.class)
test_format_formatter_null()1062     public void test_format_formatter_null() {
1063         YearMonth.of(2010, 12).format(null);
1064     }
1065 
1066 }
1067