40 * * Redistributions in binary form must reproduce the above copyright notice,
41 * this list of conditions and the following disclaimer in the documentation
42 * and/or other materials provided with the distribution.
43 *
44 * * Neither the name of JSR-310 nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60 package tck.java.time.temporal;
61
62 import static java.time.temporal.ChronoField.EPOCH_MONTH;
63 import static java.time.temporal.ChronoField.ERA;
64 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
65 import static java.time.temporal.ChronoField.YEAR;
66 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
67 import static org.testng.Assert.assertEquals;
68 import static org.testng.Assert.assertTrue;
69 import static org.testng.Assert.fail;
70
71 import java.io.ByteArrayOutputStream;
72 import java.io.DataOutputStream;
73 import java.io.IOException;
74 import java.util.ArrayList;
75 import java.util.Arrays;
76 import java.util.HashSet;
77 import java.util.List;
78 import java.util.Set;
79
80 import java.time.Clock;
81 import java.time.DateTimeException;
82 import java.time.Instant;
83 import java.time.LocalDate;
84 import java.time.LocalDateTime;
85 import java.time.LocalTime;
86 import java.time.Month;
87 import java.time.ZoneId;
88 import java.time.ZoneOffset;
89 import java.time.format.DateTimeFormatter;
90 import java.time.format.DateTimeFormatters;
91 import java.time.format.DateTimeParseException;
92 import java.time.temporal.ChronoField;
93 import java.time.temporal.JulianFields;
94 import java.time.temporal.TemporalAccessor;
95 import java.time.temporal.TemporalField;
96 import java.time.temporal.Year;
97 import java.time.temporal.YearMonth;
98
99 import org.testng.annotations.BeforeMethod;
100 import org.testng.annotations.DataProvider;
101 import org.testng.annotations.Test;
102 import tck.java.time.AbstractDateTimeTest;
103
104 /**
105 * Test YearMonth.
106 */
107 @Test
108 public class TCKYearMonth extends AbstractDateTimeTest {
109
110 private YearMonth TEST_2008_06;
111
112 @BeforeMethod(groups={"tck", "implementation"})
113 public void setUp() {
114 TEST_2008_06 = YearMonth.of(2008, 6);
115 }
116
117 //-----------------------------------------------------------------------
118 @Override
119 protected List<TemporalAccessor> samples() {
120 TemporalAccessor[] array = {TEST_2008_06, };
121 return Arrays.asList(array);
122 }
136 @Override
137 protected List<TemporalField> invalidFields() {
138 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
139 list.removeAll(validFields());
140 list.add(JulianFields.JULIAN_DAY);
141 list.add(JulianFields.MODIFIED_JULIAN_DAY);
142 list.add(JulianFields.RATA_DIE);
143 return list;
144 }
145
146 //-----------------------------------------------------------------------
147 @Test
148 public void test_serialization() throws IOException, ClassNotFoundException {
149 assertSerializable(TEST_2008_06);
150 }
151
152 @Test
153 public void test_serialization_format() throws Exception {
154 ByteArrayOutputStream baos = new ByteArrayOutputStream();
155 try (DataOutputStream dos = new DataOutputStream(baos) ) {
156 dos.writeByte(5);
157 dos.writeInt(2012);
158 dos.writeByte(9);
159 }
160 byte[] bytes = baos.toByteArray();
161 assertSerializedBySer(YearMonth.of(2012, 9), bytes);
162 }
163
164 //-----------------------------------------------------------------------
165 void check(YearMonth test, int y, int m) {
166 assertEquals(test.getYear(), y);
167 assertEquals(test.getMonth().getValue(), m);
168 }
169
170 //-----------------------------------------------------------------------
171 // now()
172 //-----------------------------------------------------------------------
173 @Test(groups={"tck"})
174 public void now() {
175 YearMonth expected = YearMonth.now(Clock.systemDefaultZone());
176 YearMonth test = YearMonth.now();
361 throw ex;
362 }
363 }
364
365 //-----------------------------------------------------------------------
366 @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
367 public void factory_parse_illegalValue_Month() {
368 YearMonth.parse("2008-13");
369 }
370
371 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
372 public void factory_parse_nullText() {
373 YearMonth.parse(null);
374 }
375
376 //-----------------------------------------------------------------------
377 // parse(DateTimeFormatter)
378 //-----------------------------------------------------------------------
379 @Test(groups={"tck"})
380 public void factory_parse_formatter() {
381 DateTimeFormatter f = DateTimeFormatters.pattern("y M");
382 YearMonth test = YearMonth.parse("2010 12", f);
383 assertEquals(test, YearMonth.of(2010, 12));
384 }
385
386 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
387 public void factory_parse_formatter_nullText() {
388 DateTimeFormatter f = DateTimeFormatters.pattern("y M");
389 YearMonth.parse((String) null, f);
390 }
391
392 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
393 public void factory_parse_formatter_nullFormatter() {
394 YearMonth.parse("ANY", null);
395 }
396
397 //-----------------------------------------------------------------------
398 // get(TemporalField)
399 //-----------------------------------------------------------------------
400 @Test
401 public void test_get_TemporalField() {
402 assertEquals(TEST_2008_06.get(ChronoField.YEAR), 2008);
403 assertEquals(TEST_2008_06.get(ChronoField.MONTH_OF_YEAR), 6);
404 assertEquals(TEST_2008_06.get(ChronoField.YEAR_OF_ERA), 2008);
405 assertEquals(TEST_2008_06.get(ChronoField.ERA), 1);
406 }
407
408 @Test
409 public void test_getLong_TemporalField() {
410 assertEquals(TEST_2008_06.getLong(ChronoField.YEAR), 2008);
411 assertEquals(TEST_2008_06.getLong(ChronoField.MONTH_OF_YEAR), 6);
412 assertEquals(TEST_2008_06.getLong(ChronoField.YEAR_OF_ERA), 2008);
413 assertEquals(TEST_2008_06.getLong(ChronoField.ERA), 1);
414 assertEquals(TEST_2008_06.getLong(ChronoField.EPOCH_MONTH), (2008 - 1970) * 12 + 6 - 1);
415 }
416
417 //-----------------------------------------------------------------------
418 // get*()
419 //-----------------------------------------------------------------------
420 @DataProvider(name="sampleDates")
421 Object[][] provider_sampleDates() {
422 return new Object[][] {
423 {2008, 1},
424 {2008, 2},
425 {-1, 3},
426 {0, 12},
427 };
428 }
429
430 //-----------------------------------------------------------------------
431 // with(Year)
432 //-----------------------------------------------------------------------
433 @Test(groups={"tck"})
434 public void test_with_Year() {
435 YearMonth test = YearMonth.of(2008, 6);
436 assertEquals(test.with(Year.of(2000)), YearMonth.of(2000, 6));
437 }
438
439 @Test(groups={"tck"})
440 public void test_with_Year_noChange_equal() {
441 YearMonth test = YearMonth.of(2008, 6);
442 assertEquals(test.with(Year.of(2008)), test);
443 }
444
445 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
446 public void test_with_Year_null() {
447 YearMonth test = YearMonth.of(2008, 6);
448 test.with((Year) null);
449 }
853 assertEquals(test.isValidDay(0), false);
854 assertEquals(test.isValidDay(29), false);
855 assertEquals(test.isValidDay(32), false);
856 }
857
858 @Test(groups={"tck"})
859 public void test_isValidDay_int_febLeap() {
860 YearMonth test = YearMonth.of(2008, 2);
861 assertEquals(test.isValidDay(1), true);
862 assertEquals(test.isValidDay(29), true);
863
864 assertEquals(test.isValidDay(-1), false);
865 assertEquals(test.isValidDay(0), false);
866 assertEquals(test.isValidDay(30), false);
867 assertEquals(test.isValidDay(32), false);
868 }
869
870 //-----------------------------------------------------------------------
871 // atDay(int)
872 //-----------------------------------------------------------------------
873 @Test(groups={"tck"})
874 public void test_atDay_int() {
875 YearMonth test = YearMonth.of(2008, 6);
876 assertEquals(test.atDay(30), LocalDate.of(2008, 6, 30));
877 }
878
879 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
880 public void test_atDay_int_invalidDay() {
881 YearMonth test = YearMonth.of(2008, 6);
882 test.atDay(31);
883 }
884
885 //-----------------------------------------------------------------------
886 // compareTo()
887 //-----------------------------------------------------------------------
888 @Test(groups={"tck"})
889 public void test_comparisons() {
890 doTest_comparisons_YearMonth(
891 YearMonth.of(-1, 1),
892 YearMonth.of(0, 1),
893 YearMonth.of(0, 12),
894 YearMonth.of(1, 1),
895 YearMonth.of(1, 2),
896 YearMonth.of(1, 12),
897 YearMonth.of(2008, 1),
898 YearMonth.of(2008, 6),
899 YearMonth.of(2008, 12)
900 );
901 }
902
1016 {2008, 1, "2008-01"},
1017 {2008, 12, "2008-12"},
1018 {7, 5, "0007-05"},
1019 {0, 5, "0000-05"},
1020 {-1, 1, "-0001-01"},
1021 };
1022 }
1023
1024 @Test(dataProvider="sampleToString", groups={"tck"})
1025 public void test_toString(int y, int m, String expected) {
1026 YearMonth test = YearMonth.of(y, m);
1027 String str = test.toString();
1028 assertEquals(str, expected);
1029 }
1030
1031 //-----------------------------------------------------------------------
1032 // toString(DateTimeFormatter)
1033 //-----------------------------------------------------------------------
1034 @Test(groups={"tck"})
1035 public void test_toString_formatter() {
1036 DateTimeFormatter f = DateTimeFormatters.pattern("y M");
1037 String t = YearMonth.of(2010, 12).toString(f);
1038 assertEquals(t, "2010 12");
1039 }
1040
1041 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
1042 public void test_toString_formatter_null() {
1043 YearMonth.of(2010, 12).toString(null);
1044 }
1045
1046 }
|
40 * * Redistributions in binary form must reproduce the above copyright notice,
41 * this list of conditions and the following disclaimer in the documentation
42 * and/or other materials provided with the distribution.
43 *
44 * * Neither the name of JSR-310 nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60 package tck.java.time;
61
62 import static java.time.temporal.ChronoField.EPOCH_MONTH;
63 import static java.time.temporal.ChronoField.ERA;
64 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
65 import static java.time.temporal.ChronoField.YEAR;
66 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
67 import static org.testng.Assert.assertEquals;
68 import static org.testng.Assert.assertTrue;
69 import static org.testng.Assert.fail;
70
71 import java.io.ByteArrayOutputStream;
72 import java.io.DataOutputStream;
73 import java.io.IOException;
74 import java.time.Clock;
75 import java.time.DateTimeException;
76 import java.time.Instant;
77 import java.time.LocalDate;
78 import java.time.LocalDateTime;
79 import java.time.LocalTime;
80 import java.time.Month;
81 import java.time.Year;
82 import java.time.YearMonth;
83 import java.time.ZoneId;
84 import java.time.ZoneOffset;
85 import java.time.chrono.IsoChronology;
86 import java.time.format.DateTimeFormatter;
87 import java.time.format.DateTimeParseException;
88 import java.time.temporal.ChronoField;
89 import java.time.temporal.ChronoUnit;
90 import java.time.temporal.JulianFields;
91 import java.time.temporal.Queries;
92 import java.time.temporal.TemporalAccessor;
93 import java.time.temporal.TemporalField;
94 import java.time.temporal.TemporalQuery;
95 import java.util.ArrayList;
96 import java.util.Arrays;
97 import java.util.HashSet;
98 import java.util.List;
99 import java.util.Set;
100
101 import org.testng.annotations.BeforeMethod;
102 import org.testng.annotations.DataProvider;
103 import org.testng.annotations.Test;
104
105 /**
106 * Test YearMonth.
107 */
108 @Test
109 public class TCKYearMonth extends AbstractDateTimeTest {
110
111 private YearMonth TEST_2008_06;
112
113 @BeforeMethod(groups={"tck", "implementation"})
114 public void setUp() {
115 TEST_2008_06 = YearMonth.of(2008, 6);
116 }
117
118 //-----------------------------------------------------------------------
119 @Override
120 protected List<TemporalAccessor> samples() {
121 TemporalAccessor[] array = {TEST_2008_06, };
122 return Arrays.asList(array);
123 }
137 @Override
138 protected List<TemporalField> invalidFields() {
139 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
140 list.removeAll(validFields());
141 list.add(JulianFields.JULIAN_DAY);
142 list.add(JulianFields.MODIFIED_JULIAN_DAY);
143 list.add(JulianFields.RATA_DIE);
144 return list;
145 }
146
147 //-----------------------------------------------------------------------
148 @Test
149 public void test_serialization() throws IOException, ClassNotFoundException {
150 assertSerializable(TEST_2008_06);
151 }
152
153 @Test
154 public void test_serialization_format() throws Exception {
155 ByteArrayOutputStream baos = new ByteArrayOutputStream();
156 try (DataOutputStream dos = new DataOutputStream(baos) ) {
157 dos.writeByte(12); // java.time.temporal.Ser.YEAR_MONTH_TYPE
158 dos.writeInt(2012);
159 dos.writeByte(9);
160 }
161 byte[] bytes = baos.toByteArray();
162 assertSerializedBySer(YearMonth.of(2012, 9), bytes);
163 }
164
165 //-----------------------------------------------------------------------
166 void check(YearMonth test, int y, int m) {
167 assertEquals(test.getYear(), y);
168 assertEquals(test.getMonth().getValue(), m);
169 }
170
171 //-----------------------------------------------------------------------
172 // now()
173 //-----------------------------------------------------------------------
174 @Test(groups={"tck"})
175 public void now() {
176 YearMonth expected = YearMonth.now(Clock.systemDefaultZone());
177 YearMonth test = YearMonth.now();
362 throw ex;
363 }
364 }
365
366 //-----------------------------------------------------------------------
367 @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
368 public void factory_parse_illegalValue_Month() {
369 YearMonth.parse("2008-13");
370 }
371
372 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
373 public void factory_parse_nullText() {
374 YearMonth.parse(null);
375 }
376
377 //-----------------------------------------------------------------------
378 // parse(DateTimeFormatter)
379 //-----------------------------------------------------------------------
380 @Test(groups={"tck"})
381 public void factory_parse_formatter() {
382 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M");
383 YearMonth test = YearMonth.parse("2010 12", f);
384 assertEquals(test, YearMonth.of(2010, 12));
385 }
386
387 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
388 public void factory_parse_formatter_nullText() {
389 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M");
390 YearMonth.parse((String) null, f);
391 }
392
393 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
394 public void factory_parse_formatter_nullFormatter() {
395 YearMonth.parse("ANY", null);
396 }
397
398 //-----------------------------------------------------------------------
399 // get(TemporalField)
400 //-----------------------------------------------------------------------
401 @Test
402 public void test_get_TemporalField() {
403 assertEquals(TEST_2008_06.get(ChronoField.YEAR), 2008);
404 assertEquals(TEST_2008_06.get(ChronoField.MONTH_OF_YEAR), 6);
405 assertEquals(TEST_2008_06.get(ChronoField.YEAR_OF_ERA), 2008);
406 assertEquals(TEST_2008_06.get(ChronoField.ERA), 1);
407 }
408
409 @Test
410 public void test_getLong_TemporalField() {
411 assertEquals(TEST_2008_06.getLong(ChronoField.YEAR), 2008);
412 assertEquals(TEST_2008_06.getLong(ChronoField.MONTH_OF_YEAR), 6);
413 assertEquals(TEST_2008_06.getLong(ChronoField.YEAR_OF_ERA), 2008);
414 assertEquals(TEST_2008_06.getLong(ChronoField.ERA), 1);
415 assertEquals(TEST_2008_06.getLong(ChronoField.EPOCH_MONTH), (2008 - 1970) * 12 + 6 - 1);
416 }
417
418 //-----------------------------------------------------------------------
419 // query(TemporalQuery)
420 //-----------------------------------------------------------------------
421 @DataProvider(name="query")
422 Object[][] data_query() {
423 return new Object[][] {
424 {TEST_2008_06, Queries.chronology(), IsoChronology.INSTANCE},
425 {TEST_2008_06, Queries.zoneId(), null},
426 {TEST_2008_06, Queries.precision(), ChronoUnit.MONTHS},
427 {TEST_2008_06, Queries.zone(), null},
428 {TEST_2008_06, Queries.offset(), null},
429 {TEST_2008_06, Queries.localDate(), null},
430 {TEST_2008_06, Queries.localTime(), null},
431 };
432 }
433
434 @Test(dataProvider="query")
435 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
436 assertEquals(temporal.query(query), expected);
437 }
438
439 @Test(dataProvider="query")
440 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
441 assertEquals(query.queryFrom(temporal), expected);
442 }
443
444 @Test(expectedExceptions=NullPointerException.class)
445 public void test_query_null() {
446 TEST_2008_06.query(null);
447 }
448
449 //-----------------------------------------------------------------------
450 // get*()
451 //-----------------------------------------------------------------------
452 @DataProvider(name="sampleDates")
453 Object[][] provider_sampleDates() {
454 return new Object[][] {
455 {2008, 1},
456 {2008, 2},
457 {-1, 3},
458 {0, 12},
459 };
460 }
461
462 @Test(dataProvider="sampleDates")
463 public void test_get(int y, int m) {
464 YearMonth a = YearMonth.of(y, m);
465 assertEquals(a.getYear(), y);
466 assertEquals(a.getMonth(), Month.of(m));
467 assertEquals(a.getMonthValue(), m);
468 }
469
470 //-----------------------------------------------------------------------
471 // with(Year)
472 //-----------------------------------------------------------------------
473 @Test(groups={"tck"})
474 public void test_with_Year() {
475 YearMonth test = YearMonth.of(2008, 6);
476 assertEquals(test.with(Year.of(2000)), YearMonth.of(2000, 6));
477 }
478
479 @Test(groups={"tck"})
480 public void test_with_Year_noChange_equal() {
481 YearMonth test = YearMonth.of(2008, 6);
482 assertEquals(test.with(Year.of(2008)), test);
483 }
484
485 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
486 public void test_with_Year_null() {
487 YearMonth test = YearMonth.of(2008, 6);
488 test.with((Year) null);
489 }
893 assertEquals(test.isValidDay(0), false);
894 assertEquals(test.isValidDay(29), false);
895 assertEquals(test.isValidDay(32), false);
896 }
897
898 @Test(groups={"tck"})
899 public void test_isValidDay_int_febLeap() {
900 YearMonth test = YearMonth.of(2008, 2);
901 assertEquals(test.isValidDay(1), true);
902 assertEquals(test.isValidDay(29), true);
903
904 assertEquals(test.isValidDay(-1), false);
905 assertEquals(test.isValidDay(0), false);
906 assertEquals(test.isValidDay(30), false);
907 assertEquals(test.isValidDay(32), false);
908 }
909
910 //-----------------------------------------------------------------------
911 // atDay(int)
912 //-----------------------------------------------------------------------
913 @DataProvider(name="atDay")
914 Object[][] data_atDay() {
915 return new Object[][] {
916 {YearMonth.of(2008, 6), 8, LocalDate.of(2008, 6, 8)},
917
918 {YearMonth.of(2008, 1), 31, LocalDate.of(2008, 1, 31)},
919 {YearMonth.of(2008, 2), 29, LocalDate.of(2008, 2, 29)},
920 {YearMonth.of(2008, 3), 31, LocalDate.of(2008, 3, 31)},
921 {YearMonth.of(2008, 4), 30, LocalDate.of(2008, 4, 30)},
922
923 {YearMonth.of(2009, 1), 32, null},
924 {YearMonth.of(2009, 1), 0, null},
925 {YearMonth.of(2009, 2), 29, null},
926 {YearMonth.of(2009, 2), 30, null},
927 {YearMonth.of(2009, 2), 31, null},
928 {YearMonth.of(2009, 4), 31, null},
929 };
930 }
931
932 @Test(dataProvider="atDay")
933 public void test_atDay(YearMonth test, int day, LocalDate expected) {
934 if (expected != null) {
935 assertEquals(test.atDay(day), expected);
936 } else {
937 try {
938 test.atDay(day);
939 fail();
940 } catch (DateTimeException ex) {
941 // expected
942 }
943 }
944 }
945
946 //-----------------------------------------------------------------------
947 // atEndOfMonth()
948 //-----------------------------------------------------------------------
949 @DataProvider(name="atEndOfMonth")
950 Object[][] data_atEndOfMonth() {
951 return new Object[][] {
952 {YearMonth.of(2008, 1), LocalDate.of(2008, 1, 31)},
953 {YearMonth.of(2008, 2), LocalDate.of(2008, 2, 29)},
954 {YearMonth.of(2008, 3), LocalDate.of(2008, 3, 31)},
955 {YearMonth.of(2008, 4), LocalDate.of(2008, 4, 30)},
956 {YearMonth.of(2008, 5), LocalDate.of(2008, 5, 31)},
957 {YearMonth.of(2008, 6), LocalDate.of(2008, 6, 30)},
958 {YearMonth.of(2008, 12), LocalDate.of(2008, 12, 31)},
959
960 {YearMonth.of(2009, 1), LocalDate.of(2009, 1, 31)},
961 {YearMonth.of(2009, 2), LocalDate.of(2009, 2, 28)},
962 {YearMonth.of(2009, 3), LocalDate.of(2009, 3, 31)},
963 {YearMonth.of(2009, 4), LocalDate.of(2009, 4, 30)},
964 {YearMonth.of(2009, 5), LocalDate.of(2009, 5, 31)},
965 {YearMonth.of(2009, 6), LocalDate.of(2009, 6, 30)},
966 {YearMonth.of(2009, 12), LocalDate.of(2009, 12, 31)},
967 };
968 }
969
970 @Test(dataProvider="atEndOfMonth")
971 public void test_atEndOfMonth(YearMonth test, LocalDate expected) {
972 assertEquals(test.atEndOfMonth(), expected);
973 }
974
975 //-----------------------------------------------------------------------
976 // compareTo()
977 //-----------------------------------------------------------------------
978 @Test(groups={"tck"})
979 public void test_comparisons() {
980 doTest_comparisons_YearMonth(
981 YearMonth.of(-1, 1),
982 YearMonth.of(0, 1),
983 YearMonth.of(0, 12),
984 YearMonth.of(1, 1),
985 YearMonth.of(1, 2),
986 YearMonth.of(1, 12),
987 YearMonth.of(2008, 1),
988 YearMonth.of(2008, 6),
989 YearMonth.of(2008, 12)
990 );
991 }
992
1106 {2008, 1, "2008-01"},
1107 {2008, 12, "2008-12"},
1108 {7, 5, "0007-05"},
1109 {0, 5, "0000-05"},
1110 {-1, 1, "-0001-01"},
1111 };
1112 }
1113
1114 @Test(dataProvider="sampleToString", groups={"tck"})
1115 public void test_toString(int y, int m, String expected) {
1116 YearMonth test = YearMonth.of(y, m);
1117 String str = test.toString();
1118 assertEquals(str, expected);
1119 }
1120
1121 //-----------------------------------------------------------------------
1122 // toString(DateTimeFormatter)
1123 //-----------------------------------------------------------------------
1124 @Test(groups={"tck"})
1125 public void test_toString_formatter() {
1126 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M");
1127 String t = YearMonth.of(2010, 12).toString(f);
1128 assertEquals(t, "2010 12");
1129 }
1130
1131 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
1132 public void test_toString_formatter_null() {
1133 YearMonth.of(2010, 12).toString(null);
1134 }
1135
1136 }
|