test/java/time/tck/java/time/TCKYear.java

Print this page

        

*** 55,104 **** * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ! package tck.java.time.temporal; import static java.time.temporal.ChronoField.ERA; import static java.time.temporal.ChronoField.YEAR; import static java.time.temporal.ChronoField.YEAR_OF_ERA; import static org.testng.Assert.assertEquals; import static org.testng.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; - import java.io.IOException; - import java.util.ArrayList; - import java.util.Arrays; - import java.util.List; - import java.time.Clock; import java.time.DateTimeException; import java.time.Instant; import java.time.LocalDate; import java.time.LocalTime; import java.time.Month; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; - import java.time.format.DateTimeFormatters; import java.time.format.DateTimeParseException; import java.time.temporal.ChronoField; import java.time.temporal.JulianFields; ! import java.time.temporal.MonthDay; ! import java.time.temporal.OffsetDateTime; import java.time.temporal.Temporal; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; ! import java.time.temporal.Year; ! import java.time.temporal.YearMonth; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; - import tck.java.time.AbstractDateTimeTest; /** * Test Year. */ @Test --- 55,104 ---- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ! package tck.java.time; import static java.time.temporal.ChronoField.ERA; import static java.time.temporal.ChronoField.YEAR; import static java.time.temporal.ChronoField.YEAR_OF_ERA; import static org.testng.Assert.assertEquals; import static org.testng.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.time.Clock; import java.time.DateTimeException; import java.time.Instant; import java.time.LocalDate; import java.time.LocalTime; import java.time.Month; + import java.time.MonthDay; + import java.time.OffsetDateTime; + import java.time.Year; + import java.time.YearMonth; import java.time.ZoneId; import java.time.ZoneOffset; + import java.time.chrono.IsoChronology; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.temporal.ChronoField; + import java.time.temporal.ChronoUnit; import java.time.temporal.JulianFields; ! import java.time.temporal.Queries; import java.time.temporal.Temporal; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; ! import java.time.temporal.TemporalQuery; ! import java.util.ArrayList; ! import java.util.Arrays; ! import java.util.List; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; /** * Test Year. */ @Test
*** 147,157 **** @Test public void test_serialization_format() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (DataOutputStream dos = new DataOutputStream(baos) ) { ! dos.writeByte(4); dos.writeInt(2012); } byte[] bytes = baos.toByteArray(); assertSerializedBySer(Year.of(2012), bytes); } --- 147,157 ---- @Test public void test_serialization_format() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (DataOutputStream dos = new DataOutputStream(baos) ) { ! dos.writeByte(11); // java.time.temporal.Ser.YEAR_TYPE dos.writeInt(2012); } byte[] bytes = baos.toByteArray(); assertSerializedBySer(Year.of(2012), bytes); }
*** 315,332 **** //----------------------------------------------------------------------- // parse(DateTimeFormatter) //----------------------------------------------------------------------- @Test(groups={"tck"}) public void factory_parse_formatter() { ! DateTimeFormatter f = DateTimeFormatters.pattern("y"); Year test = Year.parse("2010", f); assertEquals(test, Year.of(2010)); } @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) public void factory_parse_formatter_nullText() { ! DateTimeFormatter f = DateTimeFormatters.pattern("y"); Year.parse((String) null, f); } @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) public void factory_parse_formatter_nullFormatter() { --- 315,332 ---- //----------------------------------------------------------------------- // parse(DateTimeFormatter) //----------------------------------------------------------------------- @Test(groups={"tck"}) public void factory_parse_formatter() { ! DateTimeFormatter f = DateTimeFormatter.ofPattern("y"); Year test = Year.parse("2010", f); assertEquals(test, Year.of(2010)); } @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) public void factory_parse_formatter_nullText() { ! DateTimeFormatter f = DateTimeFormatter.ofPattern("y"); Year.parse((String) null, f); } @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) public void factory_parse_formatter_nullFormatter() {
*** 349,358 **** --- 349,389 ---- assertEquals(TEST_2008.getLong(ChronoField.YEAR_OF_ERA), 2008); assertEquals(TEST_2008.getLong(ChronoField.ERA), 1); } //----------------------------------------------------------------------- + // query(TemporalQuery) + //----------------------------------------------------------------------- + @DataProvider(name="query") + Object[][] data_query() { + return new Object[][] { + {TEST_2008, Queries.chronology(), IsoChronology.INSTANCE}, + {TEST_2008, Queries.zoneId(), null}, + {TEST_2008, Queries.precision(), ChronoUnit.YEARS}, + {TEST_2008, Queries.zone(), null}, + {TEST_2008, Queries.offset(), null}, + {TEST_2008, Queries.localDate(), null}, + {TEST_2008, Queries.localTime(), null}, + }; + } + + @Test(dataProvider="query") + public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { + assertEquals(temporal.query(query), expected); + } + + @Test(dataProvider="query") + public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { + assertEquals(query.queryFrom(temporal), expected); + } + + @Test(expectedExceptions=NullPointerException.class) + public void test_query_null() { + TEST_2008.query(null); + } + + //----------------------------------------------------------------------- // isLeap() //----------------------------------------------------------------------- @Test(groups={"tck"}) public void test_isLeap() { assertEquals(Year.of(1999).isLeap(), false);
*** 553,640 **** assertEquals(Year.of(400).length(), 366); assertEquals(Year.of(500).length(), 365); } //----------------------------------------------------------------------- ! // isValidMonthDay(Month) //----------------------------------------------------------------------- ! @Test(groups={"tck"}) ! public void test_isValidMonthDay_june() { ! Year test = Year.of(2007); ! MonthDay monthDay = MonthDay.of(6, 30); ! assertEquals(test.isValidMonthDay(monthDay), true); ! } ! ! @Test(groups={"tck"}) ! public void test_isValidMonthDay_febNonLeap() { ! Year test = Year.of(2007); ! MonthDay monthDay = MonthDay.of(2, 29); ! assertEquals(test.isValidMonthDay(monthDay), false); } ! @Test(groups={"tck"}) ! public void test_isValidMonthDay_febLeap() { ! Year test = Year.of(2008); ! MonthDay monthDay = MonthDay.of(2, 29); ! assertEquals(test.isValidMonthDay(monthDay), true); ! } ! ! @Test(groups={"tck"}) ! public void test_isValidMonthDay_null() { ! Year test = Year.of(2008); ! assertEquals(test.isValidMonthDay(null), false); } //----------------------------------------------------------------------- // atMonth(Month) //----------------------------------------------------------------------- ! @Test(groups={"tck"}) public void test_atMonth() { Year test = Year.of(2008); assertEquals(test.atMonth(Month.JUNE), YearMonth.of(2008, 6)); } ! @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) public void test_atMonth_nullMonth() { Year test = Year.of(2008); test.atMonth((Month) null); } //----------------------------------------------------------------------- // atMonth(int) //----------------------------------------------------------------------- ! @Test(groups={"tck"}) public void test_atMonth_int() { Year test = Year.of(2008); assertEquals(test.atMonth(6), YearMonth.of(2008, 6)); } ! @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) public void test_atMonth_int_invalidMonth() { Year test = Year.of(2008); test.atMonth(13); } //----------------------------------------------------------------------- // atMonthDay(Month) //----------------------------------------------------------------------- ! @Test(groups={"tck"}) ! public void test_atMonthDay() { ! Year test = Year.of(2008); ! assertEquals(test.atMonthDay(MonthDay.of(6, 30)), LocalDate.of(2008, 6, 30)); } ! @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) ! public void test_atMonthDay_nullMonthDay() { ! Year test = Year.of(2008); ! test.atMonthDay((MonthDay) null); } ! @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) ! public void test_atMonthDay_invalidMonthDay() { Year test = Year.of(2008); ! test.atMonthDay(MonthDay.of(6, 31)); } //----------------------------------------------------------------------- // atDay(int) //----------------------------------------------------------------------- --- 584,663 ---- assertEquals(Year.of(400).length(), 366); assertEquals(Year.of(500).length(), 365); } //----------------------------------------------------------------------- ! // isValidMonthDay(MonthDay) //----------------------------------------------------------------------- ! @DataProvider(name="isValidMonthDay") ! Object[][] data_isValidMonthDay() { ! return new Object[][] { ! {Year.of(2007), MonthDay.of(6, 30), true}, ! {Year.of(2008), MonthDay.of(2, 28), true}, ! {Year.of(2008), MonthDay.of(2, 29), true}, ! {Year.of(2009), MonthDay.of(2, 28), true}, ! {Year.of(2009), MonthDay.of(2, 29), false}, ! {Year.of(2009), null, false}, ! }; } ! @Test(dataProvider="isValidMonthDay") ! public void test_isValidMonthDay(Year year, MonthDay monthDay, boolean expected) { ! assertEquals(year.isValidMonthDay(monthDay), expected); } //----------------------------------------------------------------------- // atMonth(Month) //----------------------------------------------------------------------- ! @Test public void test_atMonth() { Year test = Year.of(2008); assertEquals(test.atMonth(Month.JUNE), YearMonth.of(2008, 6)); } ! @Test(expectedExceptions=NullPointerException.class) public void test_atMonth_nullMonth() { Year test = Year.of(2008); test.atMonth((Month) null); } //----------------------------------------------------------------------- // atMonth(int) //----------------------------------------------------------------------- ! @Test public void test_atMonth_int() { Year test = Year.of(2008); assertEquals(test.atMonth(6), YearMonth.of(2008, 6)); } ! @Test(expectedExceptions=DateTimeException.class) public void test_atMonth_int_invalidMonth() { Year test = Year.of(2008); test.atMonth(13); } //----------------------------------------------------------------------- // atMonthDay(Month) //----------------------------------------------------------------------- ! @DataProvider(name="atMonthDay") ! Object[][] data_atMonthDay() { ! return new Object[][] { ! {Year.of(2008), MonthDay.of(6, 30), LocalDate.of(2008, 6, 30)}, ! {Year.of(2008), MonthDay.of(2, 29), LocalDate.of(2008, 2, 29)}, ! {Year.of(2009), MonthDay.of(2, 29), LocalDate.of(2009, 2, 28)}, ! }; } ! @Test(dataProvider="atMonthDay") ! public void test_atMonthDay(Year year, MonthDay monthDay, LocalDate expected) { ! assertEquals(year.atMonthDay(monthDay), expected); } ! @Test(expectedExceptions=NullPointerException.class) ! public void test_atMonthDay_nullMonthDay() { Year test = Year.of(2008); ! test.atMonthDay((MonthDay) null); } //----------------------------------------------------------------------- // atDay(int) //-----------------------------------------------------------------------
*** 766,776 **** //----------------------------------------------------------------------- // toString(DateTimeFormatter) //----------------------------------------------------------------------- @Test(groups={"tck"}) public void test_toString_formatter() { ! DateTimeFormatter f = DateTimeFormatters.pattern("y"); String t = Year.of(2010).toString(f); assertEquals(t, "2010"); } @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) --- 789,799 ---- //----------------------------------------------------------------------- // toString(DateTimeFormatter) //----------------------------------------------------------------------- @Test(groups={"tck"}) public void test_toString_formatter() { ! DateTimeFormatter f = DateTimeFormatter.ofPattern("y"); String t = Year.of(2010).toString(f); assertEquals(t, "2010"); } @Test(expectedExceptions=NullPointerException.class, groups={"tck"})