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.Duration;
  67 import java.time.LocalDate;
  68 import java.time.chrono.HijrahChronology;
  69 import java.time.chrono.JapaneseChronology;
  70 import java.time.chrono.MinguoChronology;
  71 import java.time.chrono.ThaiBuddhistChronology;
  72 import java.time.chrono.Chronology;
  73 import java.time.chrono.ChronoLocalDate;
  74 import java.time.chrono.IsoChronology;
  75 import java.time.temporal.ChronoUnit;
  76 import java.time.temporal.Temporal;
  77 import java.time.temporal.TemporalAccessor;
  78 import java.time.temporal.TemporalAdjuster;
  79 import java.time.temporal.TemporalAmount;
  80 import java.time.temporal.TemporalField;
  81 import java.time.temporal.TemporalUnit;
  82 import java.time.temporal.ValueRange;
  83 import java.util.ArrayList;
  84 import java.util.List;
  85 
  86 import org.testng.Assert;
  87 import org.testng.annotations.DataProvider;
  88 import org.testng.annotations.Test;
  89 
  90 /**
  91  * Test assertions that must be true for all built-in chronologies.
  92  */
  93 @Test
  94 public class TestChronoLocalDate {
  95 
  96     //-----------------------------------------------------------------------
  97     // regular data factory for names and descriptions of available calendars
  98     //-----------------------------------------------------------------------
  99     @DataProvider(name = "calendars")
 100     Chronology[][] data_of_calendars() {
 101         return new Chronology[][]{
 102                     {HijrahChronology.INSTANCE},
 103                     {IsoChronology.INSTANCE},
 104                     {JapaneseChronology.INSTANCE},
 105                     {MinguoChronology.INSTANCE},
 106                     {ThaiBuddhistChronology.INSTANCE}};
 107     }
 108 
 109     @Test(groups={"tck"}, dataProvider="calendars")
 110     public void test_badWithAdjusterChrono(Chronology chrono) {
 111         LocalDate refDate = LocalDate.of(1900, 1, 1);
 112         ChronoLocalDate<?> date = chrono.date(refDate);
 113         for (Chronology[] clist : data_of_calendars()) {
 114             Chronology chrono2 = clist[0];
 115             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 116             TemporalAdjuster adjuster = new FixedAdjuster(date2);
 117             if (chrono != chrono2) {
 118                 try {
 119                     date.with(adjuster);
 120                     Assert.fail("WithAdjuster should have thrown a ClassCastException");
 121                 } catch (ClassCastException cce) {
 122                     // Expected exception; not an error
 123                 }
 124             } else {
 125                 // Same chronology,
 126                 ChronoLocalDate<?> result = date.with(adjuster);
 127                 assertEquals(result, date2, "WithAdjuster failed to replace date");
 128             }
 129         }
 130     }
 131 
 132     @Test(groups={"tck"}, dataProvider="calendars")
 133     public void test_badPlusAdjusterChrono(Chronology chrono) {
 134         LocalDate refDate = LocalDate.of(1900, 1, 1);
 135         ChronoLocalDate<?> date = chrono.date(refDate);
 136         for (Chronology[] clist : data_of_calendars()) {
 137             Chronology chrono2 = clist[0];
 138             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 139             TemporalAmount adjuster = new FixedAdjuster(date2);
 140             if (chrono != chrono2) {
 141                 try {
 142                     date.plus(adjuster);
 143                     Assert.fail("WithAdjuster should have thrown a ClassCastException");
 144                 } catch (ClassCastException cce) {
 145                     // Expected exception; not an error
 146                 }
 147             } else {
 148                 // Same chronology,
 149                 ChronoLocalDate<?> result = date.plus(adjuster);
 150                 assertEquals(result, date2, "WithAdjuster failed to replace date");
 151             }
 152         }
 153     }
 154 
 155     @Test(groups={"tck"}, dataProvider="calendars")
 156     public void test_badMinusAdjusterChrono(Chronology chrono) {
 157         LocalDate refDate = LocalDate.of(1900, 1, 1);
 158         ChronoLocalDate<?> date = chrono.date(refDate);
 159         for (Chronology[] clist : data_of_calendars()) {
 160             Chronology chrono2 = clist[0];
 161             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 162             TemporalAmount adjuster = new FixedAdjuster(date2);
 163             if (chrono != chrono2) {
 164                 try {
 165                     date.minus(adjuster);
 166                     Assert.fail("WithAdjuster should have thrown a ClassCastException");
 167                 } catch (ClassCastException cce) {
 168                     // Expected exception; not an error
 169                 }
 170             } else {
 171                 // Same chronology,
 172                 ChronoLocalDate<?> result = date.minus(adjuster);
 173                 assertEquals(result, date2, "WithAdjuster failed to replace date");
 174             }
 175         }
 176     }
 177 
 178     @Test(groups={"tck"}, dataProvider="calendars")
 179     public void test_badPlusTemporalUnitChrono(Chronology chrono) {
 180         LocalDate refDate = LocalDate.of(1900, 1, 1);
 181         ChronoLocalDate<?> date = chrono.date(refDate);
 182         for (Chronology[] clist : data_of_calendars()) {
 183             Chronology chrono2 = clist[0];
 184             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 185             TemporalUnit adjuster = new FixedTemporalUnit(date2);
 186             if (chrono != chrono2) {
 187                 try {
 188                     date.plus(1, adjuster);
 189                     Assert.fail("TemporalUnit.doAdd plus should have thrown a ClassCastException" + date.getClass()
 190                             + ", can not be cast to " + date2.getClass());
 191                 } catch (ClassCastException cce) {
 192                     // Expected exception; not an error
 193                 }
 194             } else {
 195                 // Same chronology,
 196                 ChronoLocalDate<?> result = date.plus(1, adjuster);
 197                 assertEquals(result, date2, "WithAdjuster failed to replace date");
 198             }
 199         }
 200     }
 201 
 202     @Test(groups={"tck"}, dataProvider="calendars")
 203     public void test_badMinusTemporalUnitChrono(Chronology chrono) {
 204         LocalDate refDate = LocalDate.of(1900, 1, 1);
 205         ChronoLocalDate<?> date = chrono.date(refDate);
 206         for (Chronology[] clist : data_of_calendars()) {
 207             Chronology chrono2 = clist[0];
 208             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 209             TemporalUnit adjuster = new FixedTemporalUnit(date2);
 210             if (chrono != chrono2) {
 211                 try {
 212                     date.minus(1, adjuster);
 213                     Assert.fail("TemporalUnit.doAdd minus should have thrown a ClassCastException" + date.getClass()
 214                             + ", can not be cast to " + date2.getClass());
 215                 } catch (ClassCastException cce) {
 216                     // Expected exception; not an error
 217                 }
 218             } else {
 219                 // Same chronology,
 220                 ChronoLocalDate<?> result = date.minus(1, adjuster);
 221                 assertEquals(result, date2, "WithAdjuster failed to replace date");
 222             }
 223         }
 224     }
 225 
 226     @Test(groups={"tck"}, dataProvider="calendars")
 227     public void test_badTemporalFieldChrono(Chronology chrono) {
 228         LocalDate refDate = LocalDate.of(1900, 1, 1);
 229         ChronoLocalDate<?> date = chrono.date(refDate);
 230         for (Chronology[] clist : data_of_calendars()) {
 231             Chronology chrono2 = clist[0];
 232             ChronoLocalDate<?> date2 = chrono2.date(refDate);
 233             TemporalField adjuster = new FixedTemporalField(date2);
 234             if (chrono != chrono2) {
 235                 try {
 236                     date.with(adjuster, 1);
 237                     Assert.fail("TemporalField doSet should have thrown a ClassCastException" + date.getClass()
 238                             + ", can not be cast to " + date2.getClass());
 239                 } catch (ClassCastException cce) {
 240                     // Expected exception; not an error
 241                 }
 242             } else {
 243                 // Same chronology,
 244                 ChronoLocalDate<?> result = date.with(adjuster, 1);
 245                 assertEquals(result, date2, "TemporalField doSet failed to replace date");
 246             }
 247         }
 248     }
 249 
 250     //-----------------------------------------------------------------------
 251     // isBefore, isAfter, isEqual, DATE_COMPARATOR
 252     //-----------------------------------------------------------------------
 253     @Test(groups={"tck"}, dataProvider="calendars")
 254     public void test_date_comparisons(Chronology chrono) {
 255         List<ChronoLocalDate> dates = new ArrayList<>();
 256 
 257         ChronoLocalDate<?> date = chrono.date(LocalDate.of(1900, 1, 1));
 258 
 259         // Insert dates in order, no duplicates
 260         dates.add(date.minus(1000, ChronoUnit.YEARS));
 261         dates.add(date.minus(100, ChronoUnit.YEARS));
 262         dates.add(date.minus(10, ChronoUnit.YEARS));
 263         dates.add(date.minus(1, ChronoUnit.YEARS));
 264         dates.add(date.minus(1, ChronoUnit.MONTHS));
 265         dates.add(date.minus(1, ChronoUnit.WEEKS));
 266         dates.add(date.minus(1, ChronoUnit.DAYS));
 267         dates.add(date);
 268         dates.add(date.plus(1, ChronoUnit.DAYS));
 269         dates.add(date.plus(1, ChronoUnit.WEEKS));
 270         dates.add(date.plus(1, ChronoUnit.MONTHS));
 271         dates.add(date.plus(1, ChronoUnit.YEARS));
 272         dates.add(date.plus(10, ChronoUnit.YEARS));
 273         dates.add(date.plus(100, ChronoUnit.YEARS));
 274         dates.add(date.plus(1000, ChronoUnit.YEARS));
 275 
 276         // Check these dates against the corresponding dates for every calendar
 277         for (Chronology[] clist : data_of_calendars()) {
 278             List<ChronoLocalDate<?>> otherDates = new ArrayList<>();
 279             Chronology chrono2 = clist[0];
 280             for (ChronoLocalDate<?> d : dates) {
 281                 otherDates.add(chrono2.date(d));
 282             }
 283 
 284             // Now compare  the sequence of original dates with the sequence of converted dates
 285             for (int i = 0; i < dates.size(); i++) {
 286                 ChronoLocalDate<?> a = dates.get(i);
 287                 for (int j = 0; j < otherDates.size(); j++) {
 288                     ChronoLocalDate<?> b = otherDates.get(j);
 289                     int cmp = ChronoLocalDate.DATE_COMPARATOR.compare(a, b);
 290                     if (i < j) {
 291                         assertTrue(cmp < 0, a + " compare " + b);
 292                         assertEquals(a.isBefore(b), true, a + " isBefore " + b);
 293                         assertEquals(a.isAfter(b), false, a + " isAfter " + b);
 294                         assertEquals(a.isEqual(b), false, a + " isEqual " + b);
 295                     } else if (i > j) {
 296                         assertTrue(cmp > 0, a + " compare " + b);
 297                         assertEquals(a.isBefore(b), false, a + " isBefore " + b);
 298                         assertEquals(a.isAfter(b), true, a + " isAfter " + b);
 299                         assertEquals(a.isEqual(b), false, a + " isEqual " + b);
 300                     } else {
 301                         assertTrue(cmp == 0, a + " compare " + b);
 302                         assertEquals(a.isBefore(b), false, a + " isBefore " + b);
 303                         assertEquals(a.isAfter(b), false, a + " isAfter " + b);
 304                         assertEquals(a.isEqual(b), true, a + " isEqual " + b);
 305                     }
 306                 }
 307             }
 308         }
 309     }
 310 
 311     //-----------------------------------------------------------------------
 312     // Test Serialization of Calendars
 313     //-----------------------------------------------------------------------
 314     @Test( groups={"tck"}, dataProvider="calendars")
 315     public void test_ChronoSerialization(Chronology chrono) throws Exception {
 316         LocalDate ref = LocalDate.of(1900, 1, 5);
 317         ChronoLocalDate<?> orginal = chrono.date(ref);
 318         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 319         ObjectOutputStream out = new ObjectOutputStream(baos);
 320         out.writeObject(orginal);
 321         out.close();
 322         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
 323         ObjectInputStream in = new ObjectInputStream(bais);
 324         @SuppressWarnings("unchecked")
 325         ChronoLocalDate<?> ser = (ChronoLocalDate<?>) in.readObject();
 326         assertEquals(ser, orginal, "deserialized date is wrong");
 327     }
 328 
 329     /**
 330      * FixedAdjusted returns a fixed Temporal in all adjustments.
 331      * Construct an adjuster with the Temporal that should be returned from adjust.
 332      */
 333     static class FixedAdjuster implements TemporalAdjuster, TemporalAmount {
 334         private Temporal datetime;
 335 
 336         FixedAdjuster(Temporal datetime) {
 337             this.datetime = datetime;
 338         }
 339 
 340         @Override
 341         public Temporal adjustInto(Temporal ignore) {
 342             return datetime;
 343         }
 344 
 345         @Override
 346         public Temporal addTo(Temporal ignore) {
 347             return datetime;
 348         }
 349 
 350         @Override
 351         public Temporal subtractFrom(Temporal ignore) {
 352             return datetime;
 353         }
 354 
 355         @Override
 356         public long get(TemporalUnit unit) {
 357             throw new UnsupportedOperationException("Not supported yet.");
 358         }
 359 
 360         @Override
 361         public List<TemporalUnit> getUnits() {
 362             throw new UnsupportedOperationException("Not supported yet.");
 363         }
 364     }
 365 
 366     /**
 367      * FixedTemporalUnit returns a fixed Temporal in all adjustments.
 368      * Construct an FixedTemporalUnit with the Temporal that should be returned from doAdd.
 369      */
 370     static class FixedTemporalUnit implements TemporalUnit {
 371         private Temporal temporal;
 372 
 373         FixedTemporalUnit(Temporal temporal) {
 374             this.temporal = temporal;
 375         }
 376 
 377         @Override
 378         public String getName() {
 379             return "FixedTemporalUnit";
 380         }
 381 
 382         @Override
 383         public Duration getDuration() {
 384             throw new UnsupportedOperationException("Not supported yet.");
 385         }
 386 
 387         @Override
 388         public boolean isDurationEstimated() {
 389             throw new UnsupportedOperationException("Not supported yet.");
 390         }
 391 
 392         @Override
 393         public boolean isSupportedBy(Temporal temporal) {
 394             throw new UnsupportedOperationException("Not supported yet.");
 395         }
 396 
 397         @SuppressWarnings("unchecked")
 398         @Override
 399         public <R extends Temporal> R addTo(R temporal, long amount) {
 400             return (R) this.temporal;
 401         }
 402 
 403         @Override
 404         public long between(Temporal temporal1, Temporal temporal2) {
 405             throw new UnsupportedOperationException("Not supported yet.");
 406         }
 407     }
 408 
 409     /**
 410      * FixedTemporalField returns a fixed Temporal in all adjustments.
 411      * Construct an FixedTemporalField with the Temporal that should be returned from doSet.
 412      */
 413     static class FixedTemporalField implements TemporalField {
 414         private Temporal temporal;
 415         FixedTemporalField(Temporal temporal) {
 416             this.temporal = temporal;
 417         }
 418 
 419         @Override
 420         public String getName() {
 421             return "FixedTemporalField";
 422         }
 423 
 424         @Override
 425         public TemporalUnit getBaseUnit() {
 426             throw new UnsupportedOperationException("Not supported yet.");
 427         }
 428 
 429         @Override
 430         public TemporalUnit getRangeUnit() {
 431             throw new UnsupportedOperationException("Not supported yet.");
 432         }
 433 
 434         @Override
 435         public ValueRange range() {
 436             throw new UnsupportedOperationException("Not supported yet.");
 437         }
 438 
 439         @Override
 440         public boolean isSupportedBy(TemporalAccessor temporal) {
 441             throw new UnsupportedOperationException("Not supported yet.");
 442         }
 443 
 444         @Override
 445         public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
 446             throw new UnsupportedOperationException("Not supported yet.");
 447         }
 448 
 449         @Override
 450         public long getFrom(TemporalAccessor temporal) {
 451             throw new UnsupportedOperationException("Not supported yet.");
 452         }
 453 
 454         @SuppressWarnings("unchecked")
 455         @Override
 456         public <R extends Temporal> R adjustInto(R temporal, long newValue) {
 457             return (R) this.temporal;
 458         }
 459     }
 460 }