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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * This file is available under and governed by the GNU General Public
  26  * License version 2 only, as published by the Free Software Foundation.
  27  * However, the following notice accompanied the original version of this
  28  * file:
  29  *
  30  * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
  31  *
  32  * All rights reserved.
  33  *
  34  * Redistribution and use in source and binary forms, with or without
  35  * modification, are permitted provided that the following conditions are met:
  36  *
  37  *  * Redistributions of source code must retain the above copyright notice,
  38  *    this list of conditions and the following disclaimer.
  39  *
  40  *  * Redistributions in binary form must reproduce the above copyright notice,
  41  *    this list of conditions and the following disclaimer in the documentation
  42  *    and/or other materials provided with the distribution.
  43  *
  44  *  * Neither the name of JSR-310 nor the names of its contributors
  45  *    may be used to endorse or promote products derived from this software
  46  *    without specific prior written permission.
  47  *
  48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59  */
  60 package test.java.time;
  61 
  62 import static java.time.temporal.ChronoField.YEAR;
  63 import static org.testng.Assert.assertEquals;
  64 import static org.testng.Assert.assertSame;
  65 import static org.testng.Assert.assertTrue;
  66 
  67 import java.time.LocalDate;
  68 import java.time.Month;
  69 import java.time.temporal.ChronoUnit;
  70 
  71 import org.testng.annotations.BeforeMethod;
  72 import org.testng.annotations.DataProvider;
  73 import org.testng.annotations.Test;
  74 
  75 /**
  76  * Test LocalDate.
  77  */
  78 @Test
  79 public class TestLocalDate extends AbstractTest {
  80 
  81     private LocalDate TEST_2007_07_15;
  82 
  83     @BeforeMethod
  84     public void setUp() {
  85         TEST_2007_07_15 = LocalDate.of(2007, 7, 15);
  86     }
  87 
  88     //-----------------------------------------------------------------------
  89     @Test
  90     public void test_immutable() {
  91         assertImmutable(LocalDate.class);
  92     }
  93 
  94     //-----------------------------------------------------------------------
  95     // Since plusDays/minusDays actually depends on MJDays, it cannot be used for testing
  96     private LocalDate next(LocalDate date) {
  97         int newDayOfMonth = date.getDayOfMonth() + 1;
  98         if (newDayOfMonth <= date.getMonth().length(isIsoLeap(date.getYear()))) {
  99             return date.withDayOfMonth(newDayOfMonth);
 100         }
 101         date = date.withDayOfMonth(1);
 102         if (date.getMonth() == Month.DECEMBER) {
 103             date = date.withYear(date.getYear() + 1);
 104         }
 105         return date.with(date.getMonth().plus(1));
 106     }
 107 
 108     private LocalDate previous(LocalDate date) {
 109         int newDayOfMonth = date.getDayOfMonth() - 1;
 110         if (newDayOfMonth > 0) {
 111             return date.withDayOfMonth(newDayOfMonth);
 112         }
 113         date = date.with(date.getMonth().minus(1));
 114         if (date.getMonth() == Month.DECEMBER) {
 115             date = date.withYear(date.getYear() - 1);
 116         }
 117         return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
 118     }
 119 
 120     @Test
 121     public void test_with_DateTimeField_long_noChange_same() {
 122         LocalDate t = TEST_2007_07_15.with(YEAR, 2007);
 123         assertSame(t, TEST_2007_07_15);
 124     }
 125 
 126     @Test
 127     public void test_withYear_int_noChange_same() {
 128         LocalDate t = TEST_2007_07_15.withYear(2007);
 129         assertSame(t, TEST_2007_07_15);
 130     }
 131 
 132     @Test
 133     public void test_withMonth_int_noChange_same() {
 134         LocalDate t = TEST_2007_07_15.withMonth(7);
 135         assertSame(t, TEST_2007_07_15);
 136     }
 137 
 138     @Test
 139     public void test_withDayOfMonth_noChange_same() {
 140         LocalDate t = TEST_2007_07_15.withDayOfMonth(15);
 141         assertSame(t, TEST_2007_07_15);
 142     }
 143 
 144     @Test
 145     public void test_withDayOfYear_noChange_same() {
 146         LocalDate t = TEST_2007_07_15.withDayOfYear(31 + 28 + 31 + 30 + 31 + 30 + 15);
 147         assertSame(t, TEST_2007_07_15);
 148     }
 149 
 150     @Test
 151     public void test_plus_Period_zero() {
 152         LocalDate t = TEST_2007_07_15.plus(MockSimplePeriod.ZERO_DAYS);
 153         assertSame(t, TEST_2007_07_15);
 154     }
 155 
 156     @Test
 157     public void test_plus_longPeriodUnit_zero() {
 158         LocalDate t = TEST_2007_07_15.plus(0, ChronoUnit.DAYS);
 159         assertSame(t, TEST_2007_07_15);
 160     }
 161 
 162     @Test
 163     public void test_plusYears_long_noChange_same() {
 164         LocalDate t = TEST_2007_07_15.plusYears(0);
 165         assertSame(t, TEST_2007_07_15);
 166     }
 167 
 168     @Test
 169     public void test_plusMonths_long_noChange_same() {
 170         LocalDate t = TEST_2007_07_15.plusMonths(0);
 171         assertSame(t, TEST_2007_07_15);
 172     }
 173 
 174     //-----------------------------------------------------------------------
 175     // plusWeeks()
 176     //-----------------------------------------------------------------------
 177     @DataProvider(name="samplePlusWeeksSymmetry")
 178     Object[][] provider_samplePlusWeeksSymmetry() {
 179         return new Object[][] {
 180             {LocalDate.of(-1, 1, 1)},
 181             {LocalDate.of(-1, 2, 28)},
 182             {LocalDate.of(-1, 3, 1)},
 183             {LocalDate.of(-1, 12, 31)},
 184             {LocalDate.of(0, 1, 1)},
 185             {LocalDate.of(0, 2, 28)},
 186             {LocalDate.of(0, 2, 29)},
 187             {LocalDate.of(0, 3, 1)},
 188             {LocalDate.of(0, 12, 31)},
 189             {LocalDate.of(2007, 1, 1)},
 190             {LocalDate.of(2007, 2, 28)},
 191             {LocalDate.of(2007, 3, 1)},
 192             {LocalDate.of(2007, 12, 31)},
 193             {LocalDate.of(2008, 1, 1)},
 194             {LocalDate.of(2008, 2, 28)},
 195             {LocalDate.of(2008, 2, 29)},
 196             {LocalDate.of(2008, 3, 1)},
 197             {LocalDate.of(2008, 12, 31)},
 198             {LocalDate.of(2099, 1, 1)},
 199             {LocalDate.of(2099, 2, 28)},
 200             {LocalDate.of(2099, 3, 1)},
 201             {LocalDate.of(2099, 12, 31)},
 202             {LocalDate.of(2100, 1, 1)},
 203             {LocalDate.of(2100, 2, 28)},
 204             {LocalDate.of(2100, 3, 1)},
 205             {LocalDate.of(2100, 12, 31)},
 206         };
 207     }
 208 
 209     @Test(dataProvider="samplePlusWeeksSymmetry")
 210     public void test_plusWeeks_symmetry(LocalDate reference) {
 211         for (int weeks = 0; weeks < 365 * 8; weeks++) {
 212             LocalDate t = reference.plusWeeks(weeks).plusWeeks(-weeks);
 213             assertEquals(t, reference);
 214 
 215             t = reference.plusWeeks(-weeks).plusWeeks(weeks);
 216             assertEquals(t, reference);
 217         }
 218     }
 219 
 220     @Test
 221     public void test_plusWeeks_noChange_same() {
 222         LocalDate t = TEST_2007_07_15.plusWeeks(0);
 223         assertSame(t, TEST_2007_07_15);
 224     }
 225 
 226     //-----------------------------------------------------------------------
 227     // plusDays()
 228     //-----------------------------------------------------------------------
 229     @DataProvider(name="samplePlusDaysSymmetry")
 230     Object[][] provider_samplePlusDaysSymmetry() {
 231         return new Object[][] {
 232             {LocalDate.of(-1, 1, 1)},
 233             {LocalDate.of(-1, 2, 28)},
 234             {LocalDate.of(-1, 3, 1)},
 235             {LocalDate.of(-1, 12, 31)},
 236             {LocalDate.of(0, 1, 1)},
 237             {LocalDate.of(0, 2, 28)},
 238             {LocalDate.of(0, 2, 29)},
 239             {LocalDate.of(0, 3, 1)},
 240             {LocalDate.of(0, 12, 31)},
 241             {LocalDate.of(2007, 1, 1)},
 242             {LocalDate.of(2007, 2, 28)},
 243             {LocalDate.of(2007, 3, 1)},
 244             {LocalDate.of(2007, 12, 31)},
 245             {LocalDate.of(2008, 1, 1)},
 246             {LocalDate.of(2008, 2, 28)},
 247             {LocalDate.of(2008, 2, 29)},
 248             {LocalDate.of(2008, 3, 1)},
 249             {LocalDate.of(2008, 12, 31)},
 250             {LocalDate.of(2099, 1, 1)},
 251             {LocalDate.of(2099, 2, 28)},
 252             {LocalDate.of(2099, 3, 1)},
 253             {LocalDate.of(2099, 12, 31)},
 254             {LocalDate.of(2100, 1, 1)},
 255             {LocalDate.of(2100, 2, 28)},
 256             {LocalDate.of(2100, 3, 1)},
 257             {LocalDate.of(2100, 12, 31)},
 258         };
 259     }
 260 
 261     @Test(dataProvider="samplePlusDaysSymmetry")
 262     public void test_plusDays_symmetry(LocalDate reference) {
 263         for (int days = 0; days < 365 * 8; days++) {
 264             LocalDate t = reference.plusDays(days).plusDays(-days);
 265             assertEquals(t, reference);
 266 
 267             t = reference.plusDays(-days).plusDays(days);
 268             assertEquals(t, reference);
 269         }
 270     }
 271 
 272     @Test
 273     public void test_plusDays_noChange_same() {
 274         LocalDate t = TEST_2007_07_15.plusDays(0);
 275         assertSame(t, TEST_2007_07_15);
 276     }
 277 
 278     @Test
 279     public void test_minus_Period_zero() {
 280         LocalDate t = TEST_2007_07_15.minus(MockSimplePeriod.ZERO_DAYS);
 281         assertSame(t, TEST_2007_07_15);
 282     }
 283 
 284     @Test
 285     public void test_minus_longPeriodUnit_zero() {
 286         LocalDate t = TEST_2007_07_15.minus(0, ChronoUnit.DAYS);
 287         assertSame(t, TEST_2007_07_15);
 288     }
 289 
 290     @Test
 291     public void test_minusYears_long_noChange_same() {
 292         LocalDate t = TEST_2007_07_15.minusYears(0);
 293         assertSame(t, TEST_2007_07_15);
 294     }
 295 
 296     @Test
 297     public void test_minusMonths_long_noChange_same() {
 298         LocalDate t = TEST_2007_07_15.minusMonths(0);
 299         assertSame(t, TEST_2007_07_15);
 300     }
 301 
 302     //-----------------------------------------------------------------------
 303     // minusWeeks()
 304     //-----------------------------------------------------------------------
 305     @DataProvider(name="sampleMinusWeeksSymmetry")
 306     Object[][] provider_sampleMinusWeeksSymmetry() {
 307         return new Object[][] {
 308             {LocalDate.of(-1, 1, 1)},
 309             {LocalDate.of(-1, 2, 28)},
 310             {LocalDate.of(-1, 3, 1)},
 311             {LocalDate.of(-1, 12, 31)},
 312             {LocalDate.of(0, 1, 1)},
 313             {LocalDate.of(0, 2, 28)},
 314             {LocalDate.of(0, 2, 29)},
 315             {LocalDate.of(0, 3, 1)},
 316             {LocalDate.of(0, 12, 31)},
 317             {LocalDate.of(2007, 1, 1)},
 318             {LocalDate.of(2007, 2, 28)},
 319             {LocalDate.of(2007, 3, 1)},
 320             {LocalDate.of(2007, 12, 31)},
 321             {LocalDate.of(2008, 1, 1)},
 322             {LocalDate.of(2008, 2, 28)},
 323             {LocalDate.of(2008, 2, 29)},
 324             {LocalDate.of(2008, 3, 1)},
 325             {LocalDate.of(2008, 12, 31)},
 326             {LocalDate.of(2099, 1, 1)},
 327             {LocalDate.of(2099, 2, 28)},
 328             {LocalDate.of(2099, 3, 1)},
 329             {LocalDate.of(2099, 12, 31)},
 330             {LocalDate.of(2100, 1, 1)},
 331             {LocalDate.of(2100, 2, 28)},
 332             {LocalDate.of(2100, 3, 1)},
 333             {LocalDate.of(2100, 12, 31)},
 334         };
 335     }
 336 
 337     @Test(dataProvider="sampleMinusWeeksSymmetry")
 338     public void test_minusWeeks_symmetry(LocalDate reference) {
 339         for (int weeks = 0; weeks < 365 * 8; weeks++) {
 340             LocalDate t = reference.minusWeeks(weeks).minusWeeks(-weeks);
 341             assertEquals(t, reference);
 342 
 343             t = reference.minusWeeks(-weeks).minusWeeks(weeks);
 344             assertEquals(t, reference);
 345         }
 346     }
 347 
 348     @Test
 349     public void test_minusWeeks_noChange_same() {
 350         LocalDate t = TEST_2007_07_15.minusWeeks(0);
 351         assertSame(t, TEST_2007_07_15);
 352     }
 353 
 354     //-----------------------------------------------------------------------
 355     // minusDays()
 356     //-----------------------------------------------------------------------
 357     @DataProvider(name="sampleMinusDaysSymmetry")
 358     Object[][] provider_sampleMinusDaysSymmetry() {
 359         return new Object[][] {
 360             {LocalDate.of(-1, 1, 1)},
 361             {LocalDate.of(-1, 2, 28)},
 362             {LocalDate.of(-1, 3, 1)},
 363             {LocalDate.of(-1, 12, 31)},
 364             {LocalDate.of(0, 1, 1)},
 365             {LocalDate.of(0, 2, 28)},
 366             {LocalDate.of(0, 2, 29)},
 367             {LocalDate.of(0, 3, 1)},
 368             {LocalDate.of(0, 12, 31)},
 369             {LocalDate.of(2007, 1, 1)},
 370             {LocalDate.of(2007, 2, 28)},
 371             {LocalDate.of(2007, 3, 1)},
 372             {LocalDate.of(2007, 12, 31)},
 373             {LocalDate.of(2008, 1, 1)},
 374             {LocalDate.of(2008, 2, 28)},
 375             {LocalDate.of(2008, 2, 29)},
 376             {LocalDate.of(2008, 3, 1)},
 377             {LocalDate.of(2008, 12, 31)},
 378             {LocalDate.of(2099, 1, 1)},
 379             {LocalDate.of(2099, 2, 28)},
 380             {LocalDate.of(2099, 3, 1)},
 381             {LocalDate.of(2099, 12, 31)},
 382             {LocalDate.of(2100, 1, 1)},
 383             {LocalDate.of(2100, 2, 28)},
 384             {LocalDate.of(2100, 3, 1)},
 385             {LocalDate.of(2100, 12, 31)},
 386         };
 387     }
 388 
 389     @Test(dataProvider="sampleMinusDaysSymmetry")
 390     public void test_minusDays_symmetry(LocalDate reference) {
 391         for (int days = 0; days < 365 * 8; days++) {
 392             LocalDate t = reference.minusDays(days).minusDays(-days);
 393             assertEquals(t, reference);
 394 
 395             t = reference.minusDays(-days).minusDays(days);
 396             assertEquals(t, reference);
 397         }
 398     }
 399 
 400     @Test
 401     public void test_minusDays_noChange_same() {
 402         LocalDate t = TEST_2007_07_15.minusDays(0);
 403         assertSame(t, TEST_2007_07_15);
 404     }
 405 
 406     @Test
 407     public void test_toEpochDay_fromMJDays_symmetry() {
 408         long date_0000_01_01 = -678941 - 40587;
 409 
 410         LocalDate test = LocalDate.of(0, 1, 1);
 411         for (long i = date_0000_01_01; i < 700000; i++) {
 412             assertEquals(LocalDate.ofEpochDay(test.toEpochDay()), test);
 413             test = next(test);
 414         }
 415         test = LocalDate.of(0, 1, 1);
 416         for (long i = date_0000_01_01; i > -2000000; i--) {
 417             assertEquals(LocalDate.ofEpochDay(test.toEpochDay()), test);
 418             test = previous(test);
 419         }
 420     }
 421 
 422     void doTest_comparisons_LocalDate(LocalDate... localDates) {
 423         for (int i = 0; i < localDates.length; i++) {
 424             LocalDate a = localDates[i];
 425             for (int j = 0; j < localDates.length; j++) {
 426                 LocalDate b = localDates[j];
 427                 if (i < j) {
 428                     assertTrue(a.compareTo(b) < 0, a + " <=> " + b);
 429                     assertEquals(a.isBefore(b), true, a + " <=> " + b);
 430                     assertEquals(a.isAfter(b), false, a + " <=> " + b);
 431                     assertEquals(a.equals(b), false, a + " <=> " + b);
 432                 } else if (i > j) {
 433                     assertTrue(a.compareTo(b) > 0, a + " <=> " + b);
 434                     assertEquals(a.isBefore(b), false, a + " <=> " + b);
 435                     assertEquals(a.isAfter(b), true, a + " <=> " + b);
 436                     assertEquals(a.equals(b), false, a + " <=> " + b);
 437                 } else {
 438                     assertEquals(a.compareTo(b), 0, a + " <=> " + b);
 439                     assertEquals(a.isBefore(b), false, a + " <=> " + b);
 440                     assertEquals(a.isAfter(b), false, a + " <=> " + b);
 441                     assertEquals(a.equals(b), true, a + " <=> " + b);
 442                 }
 443             }
 444         }
 445     }
 446 
 447 }