1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package libcore.java.time; 17 18 import org.junit.Test; 19 import java.time.DateTimeException; 20 import java.time.Month; 21 import java.time.Year; 22 import java.time.YearMonth; 23 import java.time.chrono.IsoEra; 24 import java.time.temporal.ChronoField; 25 import java.time.temporal.UnsupportedTemporalTypeException; 26 27 import static org.junit.Assert.assertEquals; 28 import static org.junit.Assert.assertSame; 29 import static org.junit.Assert.fail; 30 31 /** 32 * Additional tests for {@link YearMonth}. 33 * 34 * @see tck.java.time.TCKYearMonth 35 * @see test.java.time.TestYearMonth 36 */ 37 public class YearMonthTest { 38 39 @Test test_with_TemporalField_long()40 public void test_with_TemporalField_long() { 41 YearMonth ym = YearMonth.of(2000, Month.JANUARY); 42 // -1999 is actually 2000 BCE (and 0 is 1 BCE). 43 YearMonth bceYm = YearMonth.of(-1999, Month.JANUARY); 44 45 assertEquals(YearMonth.of(1000, Month.JANUARY), ym.with(ChronoField.YEAR, 1000)); 46 assertEquals(YearMonth.of(-1, Month.JANUARY), ym.with(ChronoField.YEAR, -1)); 47 assertEquals(YearMonth.of(2000, Month.FEBRUARY), ym.with(ChronoField.MONTH_OF_YEAR, 2)); 48 assertEquals(YearMonth.of(-1999, Month.DECEMBER), 49 bceYm.with(ChronoField.MONTH_OF_YEAR, 12)); 50 assertSame(ym, ym.with(ChronoField.ERA, IsoEra.CE.getValue())); 51 assertSame(bceYm, bceYm.with(ChronoField.ERA, IsoEra.BCE.getValue())); 52 53 assertEquals(bceYm, ym.with(ChronoField.ERA, IsoEra.BCE.getValue())); 54 assertEquals(ym, bceYm.with(ChronoField.ERA, IsoEra.CE.getValue())); 55 assertEquals(YearMonth.of(1, Month.JANUARY), ym.with(ChronoField.YEAR_OF_ERA, 1)); 56 // Proleptic year 0 is 1 BCE. 57 assertEquals(YearMonth.of(0, Month.JANUARY), bceYm.with(ChronoField.YEAR_OF_ERA, 1)); 58 assertEquals(YearMonth.of(0, Month.JANUARY), ym.with(ChronoField.PROLEPTIC_MONTH, 0)); 59 assertEquals(YearMonth.of(Year.MAX_VALUE, Month.DECEMBER), ym.with(ChronoField.PROLEPTIC_MONTH, Year.MAX_VALUE * 12L + 11)); 60 assertEquals(YearMonth.of(Year.MIN_VALUE, Month.JANUARY), ym.with(ChronoField.PROLEPTIC_MONTH, Year.MIN_VALUE * 12L)); 61 } 62 63 @Test test_with_TemporalField_long_invalidValue()64 public void test_with_TemporalField_long_invalidValue() { 65 Object[][] invalidValues = new Object[][] { 66 { ChronoField.YEAR_OF_ERA, 0 }, 67 { ChronoField.YEAR_OF_ERA, Year.MAX_VALUE + 1 }, 68 { ChronoField.YEAR, Year.MIN_VALUE - 1 }, 69 { ChronoField.YEAR, Year.MAX_VALUE + 1 }, 70 { ChronoField.ERA, -1 }, 71 { ChronoField.ERA, 2 }, 72 { ChronoField.MONTH_OF_YEAR, -1 }, 73 { ChronoField.MONTH_OF_YEAR, 0 }, 74 { ChronoField.MONTH_OF_YEAR, 13 }, 75 { ChronoField.PROLEPTIC_MONTH, Year.MAX_VALUE * 12L + 12 }, 76 { ChronoField.PROLEPTIC_MONTH, Year.MIN_VALUE * 12L - 1 }, 77 }; 78 79 YearMonth ym = YearMonth.of(2000, Month.JANUARY); 80 for (Object[] values : invalidValues) { 81 ChronoField field = (ChronoField) values[0]; 82 long value = ((Number) values[1]).longValue(); 83 try { 84 ym.with(field, value); 85 fail("ym.with(" + field + ", " + value + ") should have failed."); 86 } catch (DateTimeException expected) { 87 } 88 } 89 90 } 91 92 @Test test_with_TemporalField_long_invalidField()93 public void test_with_TemporalField_long_invalidField() { 94 ChronoField[] invalidFields = new ChronoField[] { 95 ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH, 96 ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR, 97 ChronoField.ALIGNED_WEEK_OF_MONTH, 98 ChronoField.ALIGNED_WEEK_OF_YEAR, 99 ChronoField.AMPM_OF_DAY, 100 ChronoField.CLOCK_HOUR_OF_AMPM, 101 ChronoField.CLOCK_HOUR_OF_DAY, 102 ChronoField.DAY_OF_MONTH, 103 ChronoField.DAY_OF_WEEK, 104 ChronoField.DAY_OF_YEAR, 105 ChronoField.EPOCH_DAY, 106 ChronoField.HOUR_OF_AMPM, 107 ChronoField.HOUR_OF_DAY, 108 ChronoField.INSTANT_SECONDS, 109 ChronoField.MICRO_OF_DAY, 110 ChronoField.MICRO_OF_SECOND, 111 ChronoField.MILLI_OF_DAY, 112 ChronoField.MILLI_OF_SECOND, 113 ChronoField.MINUTE_OF_DAY, 114 ChronoField.MINUTE_OF_HOUR, 115 ChronoField.NANO_OF_DAY, 116 ChronoField.NANO_OF_SECOND, 117 ChronoField.OFFSET_SECONDS, 118 ChronoField.SECOND_OF_DAY, 119 ChronoField.SECOND_OF_MINUTE, 120 }; 121 122 YearMonth ym = YearMonth.of(2000, Month.JANUARY); 123 for (ChronoField invalidField : invalidFields) { 124 // Get a valid value to ensure we fail to due invalid field, not due to invalid value. 125 long value = invalidField.range().getMinimum(); 126 try { 127 ym.with(invalidField, value); 128 fail("TemporalField.with() should not accept " + invalidField); 129 } catch (UnsupportedTemporalTypeException expected) { 130 } 131 } 132 133 } 134 } 135