/* * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * This file is available under and governed by the GNU General Public * License version 2 only, as published by the Free Software Foundation. * However, the following notice accompanied the original version of this * file: * * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * * Neither the name of JSR-310 nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * 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;
@Test publicvoid now_ZoneId() {
ZoneId zone = ZoneId.of("UTC+01:02:03");
Year expected = Year.now(Clock.system(zone));
Year test = Year.now(zone); for (int i = 0; i < 100; i++) { if (expected.equals(test)) { return;
}
expected = Year.now(Clock.system(zone));
test = Year.now(zone);
}
assertEquals(test, expected);
}
//-----------------------------------------------------------------------
@Test publicvoid test_factory_int_singleton() { for (int i = -4; i <= 2104; i++) {
Year test = Year.of(i);
assertEquals(test.getValue(), i);
assertEquals(Year.of(i), test);
}
}
@Test(dataProvider="goodParseData") publicvoid factory_parse_success(String text, Year expected) {
Year year = Year.parse(text);
assertEquals(year, expected);
}
@Test(dataProvider="minus_long_TemporalUnit") publicvoid test_minus_long_TemporalUnit(Year base, long amount, TemporalUnit unit, Year expectedYear, Class<?> expectedEx) { if (expectedEx == null) {
assertEquals(base.minus(amount, unit), expectedYear);
} else { try {
Year result = base.minus(amount, unit);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
//----------------------------------------------------------------------- // adjustInto() //-----------------------------------------------------------------------
@Test publicvoid test_adjustDate() {
LocalDate base = LocalDate.of(2007, 2, 12); for (int i = -4; i <= 2104; i++) {
Temporal result = Year.of(i).adjustInto(base);
assertEquals(result, LocalDate.of(i, 2, 12));
}
}
@Test publicvoid test_adjustDate_resolve() {
Year test = Year.of(2011);
assertEquals(test.adjustInto(LocalDate.of(2012, 2, 29)), LocalDate.of(2011, 2, 28));
}
@Test(expectedExceptions=NullPointerException.class) publicvoid test_adjustDate_nullLocalDate() {
Year test = Year.of(1);
test.adjustInto((LocalDate) null);
}
//----------------------------------------------------------------------- // with(TemporalAdjuster) //-----------------------------------------------------------------------
@Test publicvoid test_with_TemporalAdjuster() {
Year base = Year.of(-10); for (int i = -4; i <= 2104; i++) {
Temporal result = base.with(Year.of(i));
assertEquals(result, Year.of(i));
}
}
@Test(expectedExceptions=DateTimeException.class) publicvoid test_with_BadTemporalAdjuster() {
Year test = Year.of(1);
test.with(LocalTime.of(18, 1, 2));
}
//----------------------------------------------------------------------- // with(TemporalField, long) //-----------------------------------------------------------------------
@Test publicvoid test_with() {
Year base = Year.of(5);
Year result = base.with(ChronoField.ERA, 0);
assertEquals(result, base.with(IsoEra.of(0)));
@Test(dataProvider="periodUntilUnit") publicvoid test_until_TemporalUnit(Year year1, Year year2, TemporalUnit unit, long expected) { long amount = year1.until(year2, unit);
assertEquals(amount, expected);
}
@Test(dataProvider="periodUntilUnit") publicvoid test_until_TemporalUnit_negated(Year year1, Year year2, TemporalUnit unit, long expected) { long amount = year2.until(year1, unit);
assertEquals(amount, -expected);
}
@Test(dataProvider="periodUntilUnit") publicvoid test_until_TemporalUnit_between(Year year1, Year year2, TemporalUnit unit, long expected) { long amount = unit.between(year1, year2);
assertEquals(amount, expected);
}
@Test publicvoid test_until_convertedType() {
Year start = Year.of(2010);
YearMonth end = start.plusYears(2).atMonth(Month.APRIL);
assertEquals(start.until(end, YEARS), 2);
}
@Test(expectedExceptions=NullPointerException.class) publicvoid test_atMonthDay_nullMonthDay() {
Year test = Year.of(2008);
test.atMonthDay((MonthDay) null);
}
//----------------------------------------------------------------------- // atDay(int) //-----------------------------------------------------------------------
@Test publicvoid test_atDay_notLeapYear() {
Year test = Year.of(2007);
LocalDate expected = LocalDate.of(2007, 1, 1); for (int i = 1; i <= 365; i++) {
assertEquals(test.atDay(i), expected);
expected = expected.plusDays(1);
}
}
@Test(expectedExceptions=DateTimeException.class) publicvoid test_atDay_notLeapYear_day366() {
Year test = Year.of(2007);
test.atDay(366);
}
@Test publicvoid test_atDay_leapYear() {
Year test = Year.of(2008);
LocalDate expected = LocalDate.of(2008, 1, 1); for (int i = 1; i <= 366; i++) {
assertEquals(test.atDay(i), expected);
expected = expected.plusDays(1);
}
}
@Test(expectedExceptions=DateTimeException.class) publicvoid test_atDay_day0() {
Year test = Year.of(2007);
test.atDay(0);
}
@Test(expectedExceptions=DateTimeException.class) publicvoid test_atDay_day367() {
Year test = Year.of(2007);
test.atDay(367);
}
//----------------------------------------------------------------------- // compareTo() //-----------------------------------------------------------------------
@Test publicvoid test_compareTo() { for (int i = -4; i <= 2104; i++) {
Year a = Year.of(i); for (int j = -4; j <= 2104; j++) {
Year b = Year.of(j); if (i < j) {
assertEquals(a.compareTo(b) < 0, true);
assertEquals(b.compareTo(a) > 0, true);
assertEquals(a.isAfter(b), false);
assertEquals(a.isBefore(b), true);
assertEquals(b.isAfter(a), true);
assertEquals(b.isBefore(a), false);
} elseif (i > j) {
assertEquals(a.compareTo(b) > 0, true);
assertEquals(b.compareTo(a) < 0, true);
assertEquals(a.isAfter(b), true);
assertEquals(a.isBefore(b), false);
assertEquals(b.isAfter(a), false);
assertEquals(b.isBefore(a), true);
} else {
assertEquals(a.compareTo(b), 0);
assertEquals(b.compareTo(a), 0);
assertEquals(a.isAfter(b), false);
assertEquals(a.isBefore(b), false);
assertEquals(b.isAfter(a), false);
assertEquals(b.isBefore(a), false);
}
}
}
}
@Test(expectedExceptions=NullPointerException.class) publicvoid test_compareTo_nullYear() {
Year doy = null;
Year test = Year.of(1);
test.compareTo(doy);
}
//----------------------------------------------------------------------- // equals() / hashCode() //-----------------------------------------------------------------------
@Test publicvoid test_equals() { for (int i = -4; i <= 2104; i++) {
Year a = Year.of(i); for (int j = -4; j <= 2104; j++) {
Year b = Year.of(j);
assertEquals(a.equals(b), i == j);
assertEquals(a.hashCode() == b.hashCode(), i == j);
}
}
}
@Test publicvoid test_equals_same() {
Year test = Year.of(2011);
assertEquals(test.equals(test), true);
}
@Test publicvoid test_equals_nullYear() {
Year doy = null;
Year test = Year.of(1);
assertEquals(test.equals(doy), false);
}
@Test publicvoid test_equals_incorrectType() {
Year test = Year.of(1);
assertEquals(test.equals("Incorrect type"), false);
}
//----------------------------------------------------------------------- // toString() //-----------------------------------------------------------------------
@Test publicvoid test_toString() { for (int i = -4; i <= 2104; i++) {
Year a = Year.of(i);
assertEquals(a.toString(), "" + i);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.