Path: blob/master/test/jdk/java/time/tck/java/time/chrono/TCKChronoLocalDate.java
41710 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos27*28* All rights reserved.29*30* Redistribution and use in source and binary forms, with or without31* modification, are permitted provided that the following conditions are met:32*33* * Redistributions of source code must retain the above copyright notice,34* this list of conditions and the following disclaimer.35*36* * Redistributions in binary form must reproduce the above copyright notice,37* this list of conditions and the following disclaimer in the documentation38* and/or other materials provided with the distribution.39*40* * Neither the name of JSR-310 nor the names of its contributors41* may be used to endorse or promote products derived from this software42* without specific prior written permission.43*44* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS45* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT46* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR47* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR48* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,49* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,50* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR51* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF52* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING53* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS54* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.55*/56package tck.java.time.chrono;5758import static org.testng.Assert.assertEquals;59import static org.testng.Assert.assertTrue;6061import java.io.ByteArrayInputStream;62import java.io.ByteArrayOutputStream;63import java.io.ObjectInputStream;64import java.io.ObjectOutputStream;65import java.time.DateTimeException;66import java.time.Duration;67import java.time.LocalDate;68import java.time.LocalTime;69import java.time.ZoneOffset;70import java.time.chrono.ChronoLocalDate;71import java.time.chrono.Chronology;72import java.time.chrono.HijrahChronology;73import java.time.chrono.IsoChronology;74import java.time.chrono.JapaneseChronology;75import java.time.chrono.MinguoChronology;76import java.time.chrono.ThaiBuddhistChronology;77import java.time.temporal.ChronoUnit;78import java.time.temporal.Temporal;79import java.time.temporal.TemporalAccessor;80import java.time.temporal.TemporalAdjuster;81import java.time.temporal.TemporalAmount;82import java.time.temporal.TemporalField;83import java.time.temporal.TemporalUnit;84import java.time.temporal.ValueRange;85import java.util.ArrayList;86import java.util.Collections;87import java.util.List;8889import org.testng.Assert;90import org.testng.annotations.DataProvider;91import org.testng.annotations.Test;9293/**94* Test assertions that must be true for all built-in chronologies.95*/96@Test97public class TCKChronoLocalDate {9899//-----------------------------------------------------------------------100// regular data factory for names and descriptions of available calendars101//-----------------------------------------------------------------------102@DataProvider(name = "calendars")103Chronology[][] data_of_calendars() {104return new Chronology[][]{105{HijrahChronology.INSTANCE},106{IsoChronology.INSTANCE},107{JapaneseChronology.INSTANCE},108{MinguoChronology.INSTANCE},109{ThaiBuddhistChronology.INSTANCE}};110}111112@Test(dataProvider="calendars")113public void test_badWithAdjusterChrono(Chronology chrono) {114LocalDate refDate = LocalDate.of(2013, 1, 1);115ChronoLocalDate date = chrono.date(refDate);116for (Chronology[] clist : data_of_calendars()) {117Chronology chrono2 = clist[0];118ChronoLocalDate date2 = chrono2.date(refDate);119TemporalAdjuster adjuster = new FixedAdjuster(date2);120if (chrono != chrono2) {121try {122date.with(adjuster);123Assert.fail("WithAdjuster should have thrown a ClassCastException");124} catch (ClassCastException cce) {125// Expected exception; not an error126}127} else {128// Same chronology,129ChronoLocalDate result = date.with(adjuster);130assertEquals(result, date2, "WithAdjuster failed to replace date");131}132}133}134135@Test(dataProvider="calendars")136public void test_badPlusAdjusterChrono(Chronology chrono) {137LocalDate refDate = LocalDate.of(2013, 1, 1);138ChronoLocalDate date = chrono.date(refDate);139for (Chronology[] clist : data_of_calendars()) {140Chronology chrono2 = clist[0];141ChronoLocalDate date2 = chrono2.date(refDate);142TemporalAmount adjuster = new FixedAdjuster(date2);143if (chrono != chrono2) {144try {145date.plus(adjuster);146Assert.fail("WithAdjuster should have thrown a ClassCastException");147} catch (ClassCastException cce) {148// Expected exception; not an error149}150} else {151// Same chronology,152ChronoLocalDate result = date.plus(adjuster);153assertEquals(result, date2, "WithAdjuster failed to replace date");154}155}156}157158@Test(dataProvider="calendars")159public void test_badMinusAdjusterChrono(Chronology chrono) {160LocalDate refDate = LocalDate.of(2013, 1, 1);161ChronoLocalDate date = chrono.date(refDate);162for (Chronology[] clist : data_of_calendars()) {163Chronology chrono2 = clist[0];164ChronoLocalDate date2 = chrono2.date(refDate);165TemporalAmount adjuster = new FixedAdjuster(date2);166if (chrono != chrono2) {167try {168date.minus(adjuster);169Assert.fail("WithAdjuster should have thrown a ClassCastException");170} catch (ClassCastException cce) {171// Expected exception; not an error172}173} else {174// Same chronology,175ChronoLocalDate result = date.minus(adjuster);176assertEquals(result, date2, "WithAdjuster failed to replace date");177}178}179}180181@Test(dataProvider="calendars")182public void test_badPlusTemporalUnitChrono(Chronology chrono) {183LocalDate refDate = LocalDate.of(2013, 1, 1);184ChronoLocalDate date = chrono.date(refDate);185for (Chronology[] clist : data_of_calendars()) {186Chronology chrono2 = clist[0];187ChronoLocalDate date2 = chrono2.date(refDate);188TemporalUnit adjuster = new FixedTemporalUnit(date2);189if (chrono != chrono2) {190try {191date.plus(1, adjuster);192Assert.fail("TemporalUnit.doAdd plus should have thrown a ClassCastException" + date.getClass()193+ ", can not be cast to " + date2.getClass());194} catch (ClassCastException cce) {195// Expected exception; not an error196}197} else {198// Same chronology,199ChronoLocalDate result = date.plus(1, adjuster);200assertEquals(result, date2, "WithAdjuster failed to replace date");201}202}203}204205@Test(dataProvider="calendars")206public void test_badMinusTemporalUnitChrono(Chronology chrono) {207LocalDate refDate = LocalDate.of(2013, 1, 1);208ChronoLocalDate date = chrono.date(refDate);209for (Chronology[] clist : data_of_calendars()) {210Chronology chrono2 = clist[0];211ChronoLocalDate date2 = chrono2.date(refDate);212TemporalUnit adjuster = new FixedTemporalUnit(date2);213if (chrono != chrono2) {214try {215date.minus(1, adjuster);216Assert.fail("TemporalUnit.doAdd minus should have thrown a ClassCastException" + date.getClass()217+ ", can not be cast to " + date2.getClass());218} catch (ClassCastException cce) {219// Expected exception; not an error220}221} else {222// Same chronology,223ChronoLocalDate result = date.minus(1, adjuster);224assertEquals(result, date2, "WithAdjuster failed to replace date");225}226}227}228229@Test(dataProvider="calendars")230public void test_badTemporalFieldChrono(Chronology chrono) {231LocalDate refDate = LocalDate.of(2013, 1, 1);232ChronoLocalDate date = chrono.date(refDate);233for (Chronology[] clist : data_of_calendars()) {234Chronology chrono2 = clist[0];235ChronoLocalDate date2 = chrono2.date(refDate);236TemporalField adjuster = new FixedTemporalField(date2);237if (chrono != chrono2) {238try {239date.with(adjuster, 1);240Assert.fail("TemporalField doSet should have thrown a ClassCastException" + date.getClass()241+ ", can not be cast to " + date2.getClass());242} catch (ClassCastException cce) {243// Expected exception; not an error244}245} else {246// Same chronology,247ChronoLocalDate result = date.with(adjuster, 1);248assertEquals(result, date2, "TemporalField doSet failed to replace date");249}250}251}252253//-----------------------------------------------------------------------254// isBefore, isAfter, isEqual, DATE_COMPARATOR255//-----------------------------------------------------------------------256@Test(dataProvider="calendars")257public void test_date_comparisons(Chronology chrono) {258List<ChronoLocalDate> dates = new ArrayList<>();259260ChronoLocalDate date = chrono.date(LocalDate.of(2013, 1, 1));261262// Insert dates in order, no duplicates263dates.add(date.minus(1, ChronoUnit.YEARS));264dates.add(date.minus(1, ChronoUnit.MONTHS));265dates.add(date.minus(1, ChronoUnit.WEEKS));266dates.add(date.minus(1, ChronoUnit.DAYS));267dates.add(date);268dates.add(date.plus(1, ChronoUnit.DAYS));269dates.add(date.plus(1, ChronoUnit.WEEKS));270dates.add(date.plus(1, ChronoUnit.MONTHS));271dates.add(date.plus(1, ChronoUnit.YEARS));272273// Check these dates against the corresponding dates for every calendar274for (Chronology[] clist : data_of_calendars()) {275List<ChronoLocalDate> otherDates = new ArrayList<>();276Chronology chrono2 = clist[0];277for (ChronoLocalDate d : dates) {278otherDates.add(chrono2.date(d));279}280281// Now compare the sequence of original dates with the sequence of converted dates282for (int i = 0; i < dates.size(); i++) {283ChronoLocalDate a = dates.get(i);284for (int j = 0; j < otherDates.size(); j++) {285ChronoLocalDate b = otherDates.get(j);286int cmp = ChronoLocalDate.timeLineOrder().compare(a, b);287if (i < j) {288assertTrue(cmp < 0, a + " compare " + b);289assertEquals(a.isBefore(b), true, a + " isBefore " + b);290assertEquals(a.isAfter(b), false, a + " isAfter " + b);291assertEquals(a.isEqual(b), false, a + " isEqual " + b);292} else if (i > j) {293assertTrue(cmp > 0, a + " compare " + b);294assertEquals(a.isBefore(b), false, a + " isBefore " + b);295assertEquals(a.isAfter(b), true, a + " isAfter " + b);296assertEquals(a.isEqual(b), false, a + " isEqual " + b);297} else {298assertTrue(cmp == 0, a + " compare " + b);299assertEquals(a.isBefore(b), false, a + " isBefore " + b);300assertEquals(a.isAfter(b), false, a + " isAfter " + b);301assertEquals(a.isEqual(b), true, a + " isEqual " + b);302}303}304}305}306}307308309//-----------------------------------------------------------------------310@Test(dataProvider="calendars")311public void test_from_TemporalAccessor(Chronology chrono) {312LocalDate refDate = LocalDate.of(2013, 1, 1);313ChronoLocalDate date = chrono.date(refDate);314ChronoLocalDate test1 = ChronoLocalDate.from(date);315assertEquals(test1, date);316ChronoLocalDate test2 = ChronoLocalDate.from(date.atTime(LocalTime.of(12, 30)));317assertEquals(test2, date);318ChronoLocalDate test3 = ChronoLocalDate.from(date.atTime(LocalTime.of(12, 30)).atZone(ZoneOffset.UTC));319assertEquals(test3, date);320}321322@Test(expectedExceptions = DateTimeException.class)323public void test_from_TemporalAccessor_timeOnly() {324ChronoLocalDate.from(LocalTime.of(12, 30));325}326327@Test(expectedExceptions = NullPointerException.class)328public void test_from_TemporalAccessor_null() {329ChronoLocalDate.from(null);330}331332//-----------------------------------------------------------------------333/**334* FixedAdjusted returns a fixed Temporal in all adjustments.335* Construct an adjuster with the Temporal that should be returned from adjust.336*/337static class FixedAdjuster implements TemporalAdjuster, TemporalAmount {338private Temporal datetime;339340FixedAdjuster(Temporal datetime) {341this.datetime = datetime;342}343344@Override345public Temporal adjustInto(Temporal ignore) {346return datetime;347}348349@Override350public Temporal addTo(Temporal ignore) {351return datetime;352}353354@Override355public Temporal subtractFrom(Temporal ignore) {356return datetime;357}358359@Override360public long get(TemporalUnit unit) {361throw new UnsupportedOperationException("Not supported yet.");362}363364@Override365public List<TemporalUnit> getUnits() {366throw new UnsupportedOperationException("Not supported yet.");367}368}369370/**371* FixedTemporalUnit returns a fixed Temporal in all adjustments.372* Construct an FixedTemporalUnit with the Temporal that should be returned from doAdd.373*/374static class FixedTemporalUnit implements TemporalUnit {375private Temporal temporal;376377FixedTemporalUnit(Temporal temporal) {378this.temporal = temporal;379}380381@Override382public Duration getDuration() {383throw new UnsupportedOperationException("Not supported yet.");384}385386@Override387public boolean isDurationEstimated() {388throw new UnsupportedOperationException("Not supported yet.");389}390391@Override392public boolean isDateBased() {393return false;394}395396@Override397public boolean isTimeBased() {398return false;399}400401@Override402public boolean isSupportedBy(Temporal temporal) {403throw new UnsupportedOperationException("Not supported yet.");404}405406@SuppressWarnings("unchecked")407@Override408public <R extends Temporal> R addTo(R temporal, long amount) {409return (R) this.temporal;410}411412@Override413public long between(Temporal temporal1, Temporal temporal2) {414throw new UnsupportedOperationException("Not supported yet.");415}416417@Override418public String toString() {419return "FixedTemporalUnit";420}421}422423/**424* FixedTemporalField returns a fixed Temporal in all adjustments.425* Construct an FixedTemporalField with the Temporal that should be returned from doSet.426*/427static class FixedTemporalField implements TemporalField {428private Temporal temporal;429FixedTemporalField(Temporal temporal) {430this.temporal = temporal;431}432433@Override434public TemporalUnit getBaseUnit() {435throw new UnsupportedOperationException("Not supported yet.");436}437438@Override439public TemporalUnit getRangeUnit() {440throw new UnsupportedOperationException("Not supported yet.");441}442443@Override444public ValueRange range() {445throw new UnsupportedOperationException("Not supported yet.");446}447448@Override449public boolean isDateBased() {450return false;451}452453@Override454public boolean isTimeBased() {455return false;456}457458@Override459public boolean isSupportedBy(TemporalAccessor temporal) {460throw new UnsupportedOperationException("Not supported yet.");461}462463@Override464public ValueRange rangeRefinedBy(TemporalAccessor temporal) {465throw new UnsupportedOperationException("Not supported yet.");466}467468@Override469public long getFrom(TemporalAccessor temporal) {470throw new UnsupportedOperationException("Not supported yet.");471}472473@SuppressWarnings("unchecked")474@Override475public <R extends Temporal> R adjustInto(R temporal, long newValue) {476return (R) this.temporal;477}478479@Override480public String toString() {481return "FixedTemporalField";482}483}484}485486487