1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
  28  *
  29  * All rights reserved.
  30  *
  31  * Redistribution and use in source and binary forms, with or without
  32  * modification, are permitted provided that the following conditions are met:
  33  *
  34  *  * Redistributions of source code must retain the above copyright notice,
  35  *    this list of conditions and the following disclaimer.
  36  *
  37  *  * Redistributions in binary form must reproduce the above copyright notice,
  38  *    this list of conditions and the following disclaimer in the documentation
  39  *    and/or other materials provided with the distribution.
  40  *
  41  *  * Neither the name of JSR-310 nor the names of its contributors
  42  *    may be used to endorse or promote products derived from this software
  43  *    without specific prior written permission.
  44  *
  45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package tck.java.time.chrono;
  58 
  59 import static org.testng.Assert.assertEquals;
  60 import static org.testng.Assert.assertTrue;
  61 
  62 import java.io.ByteArrayInputStream;
  63 import java.io.ByteArrayOutputStream;
  64 import java.io.ObjectInputStream;
  65 import java.io.ObjectOutputStream;
  66 import java.time.DateTimeException;
  67 import java.time.Duration;
  68 import java.time.LocalDate;
  69 import java.time.LocalTime;
  70 import java.time.ZoneOffset;
  71 import java.time.chrono.ChronoLocalDate;
  72 import java.time.chrono.Chronology;
  73 import java.time.chrono.HijrahChronology;
  74 import java.time.chrono.IsoChronology;
  75 import java.time.chrono.JapaneseChronology;
  76 import java.time.chrono.MinguoChronology;
  77 import java.time.chrono.ThaiBuddhistChronology;
  78 import java.time.temporal.ChronoUnit;
  79 import java.time.temporal.Temporal;
  80 import java.time.temporal.TemporalAccessor;
  81 import java.time.temporal.TemporalAdjuster;
  82 import java.time.temporal.TemporalAmount;
  83 import java.time.temporal.TemporalField;
  84 import java.time.temporal.TemporalUnit;
  85 import java.time.temporal.ValueRange;
  86 import java.util.ArrayList;
  87 import java.util.Collections;
  88 import java.util.List;
  89 
  90 import org.testng.Assert;
  91 import org.testng.annotations.DataProvider;
  92 import org.testng.annotations.Test;
  93 
  94 /**
  95  * Test assertions that must be true for all built-in chronologies.
  96  */
  97 @Test
  98 public class TCKChronoLocalDate {
  99 
 100     //-----------------------------------------------------------------------
 101     // regular data factory for names and descriptions of available calendars
 102     //-----------------------------------------------------------------------
 103     @DataProvider(name = "calendars")
 104     Chronology[][] data_of_calendars() {
 105         return new Chronology[][]{
 106                     {HijrahChronology.INSTANCE},
 107                     {IsoChronology.INSTANCE},
 108                     {JapaneseChronology.INSTANCE},
 109                     {MinguoChronology.INSTANCE},
 110                     {ThaiBuddhistChronology.INSTANCE}};
 111     }
 112 
 113     @Test(dataProvider="calendars")
 114     public void test_badWithAdjusterChrono(Chronology chrono) {
 115         LocalDate refDate = LocalDate.of(2013, 1, 1);
 116         ChronoLocalDate<?> date = chrono.date(refDate);
 117         for (Chronology[] clist : data_of_calendars()) {
 118             Chronology chrono2 = clist[0];
 119             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 120             TemporalAdjuster adjuster = new FixedAdjuster(date2);
 121             if (chrono != chrono2) {
 122                 try {
 123                     date.with(adjuster);
 124                     Assert.fail("WithAdjuster should have thrown a ClassCastException");
 125                 } catch (ClassCastException cce) {
 126                     // Expected exception; not an error
 127                 }
 128             } else {
 129                 // Same chronology,
 130                 ChronoLocalDate<?> result = date.with(adjuster);
 131                 assertEquals(result, date2, "WithAdjuster failed to replace date");
 132             }
 133         }
 134     }
 135 
 136     @Test(dataProvider="calendars")
 137     public void test_badPlusAdjusterChrono(Chronology chrono) {
 138         LocalDate refDate = LocalDate.of(2013, 1, 1);
 139         ChronoLocalDate<?> date = chrono.date(refDate);
 140         for (Chronology[] clist : data_of_calendars()) {
 141             Chronology chrono2 = clist[0];
 142             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 143             TemporalAmount adjuster = new FixedAdjuster(date2);
 144             if (chrono != chrono2) {
 145                 try {
 146                     date.plus(adjuster);
 147                     Assert.fail("WithAdjuster should have thrown a ClassCastException");
 148                 } catch (ClassCastException cce) {
 149                     // Expected exception; not an error
 150                 }
 151             } else {
 152                 // Same chronology,
 153                 ChronoLocalDate<?> result = date.plus(adjuster);
 154                 assertEquals(result, date2, "WithAdjuster failed to replace date");
 155             }
 156         }
 157     }
 158 
 159     @Test(dataProvider="calendars")
 160     public void test_badMinusAdjusterChrono(Chronology chrono) {
 161         LocalDate refDate = LocalDate.of(2013, 1, 1);
 162         ChronoLocalDate<?> date = chrono.date(refDate);
 163         for (Chronology[] clist : data_of_calendars()) {
 164             Chronology chrono2 = clist[0];
 165             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 166             TemporalAmount adjuster = new FixedAdjuster(date2);
 167             if (chrono != chrono2) {
 168                 try {
 169                     date.minus(adjuster);
 170                     Assert.fail("WithAdjuster should have thrown a ClassCastException");
 171                 } catch (ClassCastException cce) {
 172                     // Expected exception; not an error
 173                 }
 174             } else {
 175                 // Same chronology,
 176                 ChronoLocalDate<?> result = date.minus(adjuster);
 177                 assertEquals(result, date2, "WithAdjuster failed to replace date");
 178             }
 179         }
 180     }
 181 
 182     @Test(dataProvider="calendars")
 183     public void test_badPlusTemporalUnitChrono(Chronology chrono) {
 184         LocalDate refDate = LocalDate.of(2013, 1, 1);
 185         ChronoLocalDate<?> date = chrono.date(refDate);
 186         for (Chronology[] clist : data_of_calendars()) {
 187             Chronology chrono2 = clist[0];
 188             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 189             TemporalUnit adjuster = new FixedTemporalUnit(date2);
 190             if (chrono != chrono2) {
 191                 try {
 192                     date.plus(1, adjuster);
 193                     Assert.fail("TemporalUnit.doAdd plus should have thrown a ClassCastException" + date.getClass()
 194                             + ", can not be cast to " + date2.getClass());
 195                 } catch (ClassCastException cce) {
 196                     // Expected exception; not an error
 197                 }
 198             } else {
 199                 // Same chronology,
 200                 ChronoLocalDate<?> result = date.plus(1, adjuster);
 201                 assertEquals(result, date2, "WithAdjuster failed to replace date");
 202             }
 203         }
 204     }
 205 
 206     @Test(dataProvider="calendars")
 207     public void test_badMinusTemporalUnitChrono(Chronology chrono) {
 208         LocalDate refDate = LocalDate.of(2013, 1, 1);
 209         ChronoLocalDate<?> date = chrono.date(refDate);
 210         for (Chronology[] clist : data_of_calendars()) {
 211             Chronology chrono2 = clist[0];
 212             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 213             TemporalUnit adjuster = new FixedTemporalUnit(date2);
 214             if (chrono != chrono2) {
 215                 try {
 216                     date.minus(1, adjuster);
 217                     Assert.fail("TemporalUnit.doAdd minus should have thrown a ClassCastException" + date.getClass()
 218                             + ", can not be cast to " + date2.getClass());
 219                 } catch (ClassCastException cce) {
 220                     // Expected exception; not an error
 221                 }
 222             } else {
 223                 // Same chronology,
 224                 ChronoLocalDate<?> result = date.minus(1, adjuster);
 225                 assertEquals(result, date2, "WithAdjuster failed to replace date");
 226             }
 227         }
 228     }
 229 
 230     @Test(dataProvider="calendars")
 231     public void test_badTemporalFieldChrono(Chronology chrono) {
 232         LocalDate refDate = LocalDate.of(2013, 1, 1);
 233         ChronoLocalDate<?> date = chrono.date(refDate);
 234         for (Chronology[] clist : data_of_calendars()) {
 235             Chronology chrono2 = clist[0];
 236             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 237             TemporalField adjuster = new FixedTemporalField(date2);
 238             if (chrono != chrono2) {
 239                 try {
 240                     date.with(adjuster, 1);
 241                     Assert.fail("TemporalField doSet should have thrown a ClassCastException" + date.getClass()
 242                             + ", can not be cast to " + date2.getClass());
 243                 } catch (ClassCastException cce) {
 244                     // Expected exception; not an error
 245                 }
 246             } else {
 247                 // Same chronology,
 248                 ChronoLocalDate<?> result = date.with(adjuster, 1);
 249                 assertEquals(result, date2, "TemporalField doSet failed to replace date");
 250             }
 251         }
 252     }
 253 
 254     //-----------------------------------------------------------------------
 255     // isBefore, isAfter, isEqual, DATE_COMPARATOR
 256     //-----------------------------------------------------------------------
 257     @Test(dataProvider="calendars")
 258     public void test_date_comparisons(Chronology chrono) {
 259         List<ChronoLocalDate> dates = new ArrayList<>();
 260 
 261         ChronoLocalDate<?> date = chrono.date(LocalDate.of(2013, 1, 1));
 262 
 263         // Insert dates in order, no duplicates
 264         dates.add(date.minus(1, ChronoUnit.YEARS));
 265         dates.add(date.minus(1, ChronoUnit.MONTHS));
 266         dates.add(date.minus(1, ChronoUnit.WEEKS));
 267         dates.add(date.minus(1, ChronoUnit.DAYS));
 268         dates.add(date);
 269         dates.add(date.plus(1, ChronoUnit.DAYS));
 270         dates.add(date.plus(1, ChronoUnit.WEEKS));
 271         dates.add(date.plus(1, ChronoUnit.MONTHS));
 272         dates.add(date.plus(1, ChronoUnit.YEARS));
 273 
 274         // Check these dates against the corresponding dates for every calendar
 275         for (Chronology[] clist : data_of_calendars()) {
 276             List<ChronoLocalDate<?>> otherDates = new ArrayList<>();
 277             Chronology chrono2 = clist[0];
 278             for (ChronoLocalDate<?> d : dates) {
 279                 otherDates.add(chrono2.date(d));
 280             }
 281 
 282             // Now compare  the sequence of original dates with the sequence of converted dates
 283             for (int i = 0; i < dates.size(); i++) {
 284                 ChronoLocalDate<?> a = dates.get(i);
 285                 for (int j = 0; j < otherDates.size(); j++) {
 286                     ChronoLocalDate<?> b = otherDates.get(j);
 287                     int cmp = ChronoLocalDate.timeLineOrder().compare(a, b);
 288                     if (i < j) {
 289                         assertTrue(cmp < 0, a + " compare " + b);
 290                         assertEquals(a.isBefore(b), true, a + " isBefore " + b);
 291                         assertEquals(a.isAfter(b), false, a + " isAfter " + b);
 292                         assertEquals(a.isEqual(b), false, a + " isEqual " + b);
 293                     } else if (i > j) {
 294                         assertTrue(cmp > 0, a + " compare " + b);
 295                         assertEquals(a.isBefore(b), false, a + " isBefore " + b);
 296                         assertEquals(a.isAfter(b), true, a + " isAfter " + b);
 297                         assertEquals(a.isEqual(b), false, a + " isEqual " + b);
 298                     } else {
 299                         assertTrue(cmp == 0, a + " compare " + b);
 300                         assertEquals(a.isBefore(b), false, a + " isBefore " + b);
 301                         assertEquals(a.isAfter(b), false, a + " isAfter " + b);
 302                         assertEquals(a.isEqual(b), true, a + " isEqual " + b);
 303                     }
 304                 }
 305             }
 306         }
 307     }
 308 
 309     //-----------------------------------------------------------------------
 310     // Test Serialization of Calendars
 311     //-----------------------------------------------------------------------
 312     @Test( dataProvider="calendars")
 313     public void test_ChronoSerialization(Chronology chrono) throws Exception {
 314         LocalDate ref = LocalDate.of(2013, 1, 5);
 315         ChronoLocalDate<?> orginal = chrono.date(ref);
 316         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 317         ObjectOutputStream out = new ObjectOutputStream(baos);
 318         out.writeObject(orginal);
 319         out.close();
 320         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
 321         ObjectInputStream in = new ObjectInputStream(bais);
 322         @SuppressWarnings("unchecked")
 323         ChronoLocalDate<?> ser = (ChronoLocalDate<?>) in.readObject();
 324         assertEquals(ser, orginal, "deserialized date is wrong");
 325     }
 326 
 327     //-----------------------------------------------------------------------
 328     @Test(dataProvider="calendars")
 329     public void test_from_TemporalAccessor(Chronology chrono) {
 330         LocalDate refDate = LocalDate.of(2013, 1, 1);
 331         ChronoLocalDate<?> date = chrono.date(refDate);
 332         ChronoLocalDate<?> test1 = ChronoLocalDate.from(date);
 333         assertEquals(test1, date);
 334         ChronoLocalDate<?> test2 = ChronoLocalDate.from(date.atTime(LocalTime.of(12, 30)));
 335         assertEquals(test2, date);
 336         ChronoLocalDate<?> test3 = ChronoLocalDate.from(date.atTime(LocalTime.of(12, 30)).atZone(ZoneOffset.UTC));
 337         assertEquals(test3, date);
 338     }
 339 
 340     @Test(expectedExceptions = DateTimeException.class)
 341     public void test_from_TemporalAccessor_timeOnly() {
 342         ChronoLocalDate.from(LocalTime.of(12, 30));
 343     }
 344 
 345     @Test(expectedExceptions = NullPointerException.class)
 346     public void test_from_TemporalAccessor_null() {
 347         ChronoLocalDate.from(null);
 348     }
 349 
 350     //-----------------------------------------------------------------------
 351     /**
 352      * FixedAdjusted returns a fixed Temporal in all adjustments.
 353      * Construct an adjuster with the Temporal that should be returned from adjust.
 354      */
 355     static class FixedAdjuster implements TemporalAdjuster, TemporalAmount {
 356         private Temporal datetime;
 357 
 358         FixedAdjuster(Temporal datetime) {
 359             this.datetime = datetime;
 360         }
 361 
 362         @Override
 363         public Temporal adjustInto(Temporal ignore) {
 364             return datetime;
 365         }
 366 
 367         @Override
 368         public Temporal addTo(Temporal ignore) {
 369             return datetime;
 370         }
 371 
 372         @Override
 373         public Temporal subtractFrom(Temporal ignore) {
 374             return datetime;
 375         }
 376 
 377         @Override
 378         public long get(TemporalUnit unit) {
 379             throw new UnsupportedOperationException("Not supported yet.");
 380         }
 381 
 382         @Override
 383         public List<TemporalUnit> getUnits() {
 384             throw new UnsupportedOperationException("Not supported yet.");
 385         }
 386     }
 387 
 388     /**
 389      * FixedTemporalUnit returns a fixed Temporal in all adjustments.
 390      * Construct an FixedTemporalUnit with the Temporal that should be returned from doAdd.
 391      */
 392     static class FixedTemporalUnit implements TemporalUnit {
 393         private Temporal temporal;
 394 
 395         FixedTemporalUnit(Temporal temporal) {
 396             this.temporal = temporal;
 397         }
 398 
 399         @Override
 400         public String getName() {
 401             return "FixedTemporalUnit";
 402         }
 403 
 404         @Override
 405         public Duration getDuration() {
 406             throw new UnsupportedOperationException("Not supported yet.");
 407         }
 408 
 409         @Override
 410         public boolean isDurationEstimated() {
 411             throw new UnsupportedOperationException("Not supported yet.");
 412         }
 413 
 414         @Override
 415         public boolean isSupportedBy(Temporal temporal) {
 416             throw new UnsupportedOperationException("Not supported yet.");
 417         }
 418 
 419         @SuppressWarnings("unchecked")
 420         @Override
 421         public <R extends Temporal> R addTo(R temporal, long amount) {
 422             return (R) this.temporal;
 423         }
 424 
 425         @Override
 426         public long between(Temporal temporal1, Temporal temporal2) {
 427             throw new UnsupportedOperationException("Not supported yet.");
 428         }
 429     }
 430 
 431     /**
 432      * FixedTemporalField returns a fixed Temporal in all adjustments.
 433      * Construct an FixedTemporalField with the Temporal that should be returned from doSet.
 434      */
 435     static class FixedTemporalField implements TemporalField {
 436         private Temporal temporal;
 437         FixedTemporalField(Temporal temporal) {
 438             this.temporal = temporal;
 439         }
 440 
 441         @Override
 442         public String getName() {
 443             return "FixedTemporalField";
 444         }
 445 
 446         @Override
 447         public TemporalUnit getBaseUnit() {
 448             throw new UnsupportedOperationException("Not supported yet.");
 449         }
 450 
 451         @Override
 452         public TemporalUnit getRangeUnit() {
 453             throw new UnsupportedOperationException("Not supported yet.");
 454         }
 455 
 456         @Override
 457         public ValueRange range() {
 458             throw new UnsupportedOperationException("Not supported yet.");
 459         }
 460 
 461         @Override
 462         public boolean isSupportedBy(TemporalAccessor temporal) {
 463             throw new UnsupportedOperationException("Not supported yet.");
 464         }
 465 
 466         @Override
 467         public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
 468             throw new UnsupportedOperationException("Not supported yet.");
 469         }
 470 
 471         @Override
 472         public long getFrom(TemporalAccessor temporal) {
 473             throw new UnsupportedOperationException("Not supported yet.");
 474         }
 475 
 476         @SuppressWarnings("unchecked")
 477         @Override
 478         public <R extends Temporal> R adjustInto(R temporal, long newValue) {
 479             return (R) this.temporal;
 480         }
 481     }
 482 }