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) 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 test.java.time.temporal;
  58 
  59 import static java.time.Month.AUGUST;
  60 import static java.time.Month.FEBRUARY;
  61 import static java.time.Month.JULY;
  62 import static java.time.Month.JUNE;
  63 import static java.time.Month.MARCH;
  64 import static java.time.Month.OCTOBER;
  65 import static java.time.Month.SEPTEMBER;
  66 import static java.time.temporal.ChronoUnit.DAYS;
  67 import static java.time.temporal.ChronoUnit.MONTHS;
  68 import static java.time.temporal.ChronoUnit.WEEKS;
  69 import static java.time.temporal.ChronoUnit.YEARS;
  70 import static org.testng.Assert.assertEquals;
  71 
  72 import java.time.LocalDate;
  73 import java.time.Month;
  74 import java.time.ZoneOffset;
  75 
  76 import org.testng.annotations.DataProvider;
  77 import org.testng.annotations.Test;
  78 
  79 /**
  80  * Test.
  81  */
  82 @Test
  83 public class TestChronoUnit {
  84 
  85     //-----------------------------------------------------------------------
  86     @DataProvider(name = "yearsBetween")
  87     Object[][] data_yearsBetween() {
  88         return new Object[][] {
  89             {date(1939, SEPTEMBER, 2), date(1939, SEPTEMBER, 1), 0},
  90             {date(1939, SEPTEMBER, 2), date(1939, SEPTEMBER, 2), 0},
  91             {date(1939, SEPTEMBER, 2), date(1939, SEPTEMBER, 3), 0},
  92 
  93             {date(1939, SEPTEMBER, 2), date(1940, SEPTEMBER, 1), 0},
  94             {date(1939, SEPTEMBER, 2), date(1940, SEPTEMBER, 2), 1},
  95             {date(1939, SEPTEMBER, 2), date(1940, SEPTEMBER, 3), 1},
  96 
  97             {date(1939, SEPTEMBER, 2), date(1938, SEPTEMBER, 1), -1},
  98             {date(1939, SEPTEMBER, 2), date(1938, SEPTEMBER, 2), -1},
  99             {date(1939, SEPTEMBER, 2), date(1938, SEPTEMBER, 3), 0},
 100 
 101             {date(1939, SEPTEMBER, 2), date(1945, SEPTEMBER, 3), 6},
 102             {date(1939, SEPTEMBER, 2), date(1945, OCTOBER, 3), 6},
 103             {date(1939, SEPTEMBER, 2), date(1945, AUGUST, 3), 5},
 104         };
 105     }
 106 
 107     @Test(dataProvider = "yearsBetween")
 108     public void test_yearsBetween(LocalDate start, LocalDate end, long expected) {
 109         assertEquals(YEARS.between(start, end).getAmount(), expected);
 110         assertEquals(YEARS.between(start, end).getUnit(), YEARS);
 111     }
 112 
 113     @Test(dataProvider = "yearsBetween")
 114     public void test_yearsBetweenReversed(LocalDate start, LocalDate end, long expected) {
 115         assertEquals(YEARS.between(end, start).getAmount(), -expected);
 116         assertEquals(YEARS.between(end, start).getUnit(), YEARS);
 117     }
 118 
 119     @Test(dataProvider = "yearsBetween")
 120     public void test_yearsBetween_LocalDateTimeSameTime(LocalDate start, LocalDate end, long expected) {
 121         assertEquals(YEARS.between(start.atTime(12, 30), end.atTime(12, 30)).getAmount(), expected);
 122     }
 123 
 124     @Test(dataProvider = "yearsBetween")
 125     public void test_yearsBetween_LocalDateTimeLaterTime(LocalDate start, LocalDate end, long expected) {
 126         assertEquals(YEARS.between(start.atTime(12, 30), end.atTime(12, 31)).getAmount(), expected);
 127     }
 128 
 129     @Test(dataProvider = "yearsBetween")
 130     public void test_yearsBetween_ZonedDateSameOffset(LocalDate start, LocalDate end, long expected) {
 131         assertEquals(YEARS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(2))).getAmount(), expected);
 132     }
 133 
 134     @Test(dataProvider = "yearsBetween")
 135     public void test_yearsBetween_ZonedDateLaterOffset(LocalDate start, LocalDate end, long expected) {
 136         // +01:00 is later than +02:00
 137         assertEquals(YEARS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(1))).getAmount(), expected);
 138     }
 139 
 140     //-----------------------------------------------------------------------
 141     @DataProvider(name = "monthsBetween")
 142     Object[][] data_monthsBetween() {
 143         return new Object[][] {
 144             {date(2012, JULY, 2), date(2012, JULY, 1), 0},
 145             {date(2012, JULY, 2), date(2012, JULY, 2), 0},
 146             {date(2012, JULY, 2), date(2012, JULY, 3), 0},
 147 
 148             {date(2012, JULY, 2), date(2012, AUGUST, 1), 0},
 149             {date(2012, JULY, 2), date(2012, AUGUST, 2), 1},
 150             {date(2012, JULY, 2), date(2012, AUGUST, 3), 1},
 151 
 152             {date(2012, JULY, 2), date(2012, SEPTEMBER, 1), 1},
 153             {date(2012, JULY, 2), date(2012, SEPTEMBER, 2), 2},
 154             {date(2012, JULY, 2), date(2012, SEPTEMBER, 3), 2},
 155 
 156             {date(2012, JULY, 2), date(2012, JUNE, 1), -1},
 157             {date(2012, JULY, 2), date(2012, JUNE, 2), -1},
 158             {date(2012, JULY, 2), date(2012, JUNE, 3), 0},
 159 
 160             {date(2012, FEBRUARY, 27), date(2012, MARCH, 26), 0},
 161             {date(2012, FEBRUARY, 27), date(2012, MARCH, 27), 1},
 162             {date(2012, FEBRUARY, 27), date(2012, MARCH, 28), 1},
 163 
 164             {date(2012, FEBRUARY, 28), date(2012, MARCH, 27), 0},
 165             {date(2012, FEBRUARY, 28), date(2012, MARCH, 28), 1},
 166             {date(2012, FEBRUARY, 28), date(2012, MARCH, 29), 1},
 167 
 168             {date(2012, FEBRUARY, 29), date(2012, MARCH, 28), 0},
 169             {date(2012, FEBRUARY, 29), date(2012, MARCH, 29), 1},
 170             {date(2012, FEBRUARY, 29), date(2012, MARCH, 30), 1},
 171         };
 172     }
 173 
 174     @Test(dataProvider = "monthsBetween")
 175     public void test_monthsBetween(LocalDate start, LocalDate end, long expected) {
 176         assertEquals(MONTHS.between(start, end).getAmount(), expected);
 177         assertEquals(MONTHS.between(start, end).getUnit(), MONTHS);
 178     }
 179 
 180     @Test(dataProvider = "monthsBetween")
 181     public void test_monthsBetweenReversed(LocalDate start, LocalDate end, long expected) {
 182         assertEquals(MONTHS.between(end, start).getAmount(), -expected);
 183         assertEquals(MONTHS.between(end, start).getUnit(), MONTHS);
 184     }
 185 
 186     @Test(dataProvider = "monthsBetween")
 187     public void test_monthsBetween_LocalDateTimeSameTime(LocalDate start, LocalDate end, long expected) {
 188         assertEquals(MONTHS.between(start.atTime(12, 30), end.atTime(12, 30)).getAmount(), expected);
 189     }
 190 
 191     @Test(dataProvider = "monthsBetween")
 192     public void test_monthsBetween_LocalDateTimeLaterTime(LocalDate start, LocalDate end, long expected) {
 193         assertEquals(MONTHS.between(start.atTime(12, 30), end.atTime(12, 31)).getAmount(), expected);
 194     }
 195 
 196     @Test(dataProvider = "monthsBetween")
 197     public void test_monthsBetween_ZonedDateSameOffset(LocalDate start, LocalDate end, long expected) {
 198         assertEquals(MONTHS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(2))).getAmount(), expected);
 199     }
 200 
 201     @Test(dataProvider = "monthsBetween")
 202     public void test_monthsBetween_ZonedDateLaterOffset(LocalDate start, LocalDate end, long expected) {
 203         // +01:00 is later than +02:00
 204         assertEquals(MONTHS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(1))).getAmount(), expected);
 205     }
 206 
 207     //-----------------------------------------------------------------------
 208     @DataProvider(name = "weeksBetween")
 209     Object[][] data_weeksBetween() {
 210         return new Object[][] {
 211             {date(2012, JULY, 2), date(2012, JUNE, 25), -1},
 212             {date(2012, JULY, 2), date(2012, JUNE, 26), 0},
 213             {date(2012, JULY, 2), date(2012, JULY, 2), 0},
 214             {date(2012, JULY, 2), date(2012, JULY, 8), 0},
 215             {date(2012, JULY, 2), date(2012, JULY, 9), 1},
 216 
 217             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 21), -1},
 218             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 22), 0},
 219             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 28), 0},
 220             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 29), 0},
 221             {date(2012, FEBRUARY, 28), date(2012, MARCH, 1), 0},
 222             {date(2012, FEBRUARY, 28), date(2012, MARCH, 5), 0},
 223             {date(2012, FEBRUARY, 28), date(2012, MARCH, 6), 1},
 224 
 225             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 22), -1},
 226             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 23), 0},
 227             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 28), 0},
 228             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 29), 0},
 229             {date(2012, FEBRUARY, 29), date(2012, MARCH, 1), 0},
 230             {date(2012, FEBRUARY, 29), date(2012, MARCH, 6), 0},
 231             {date(2012, FEBRUARY, 29), date(2012, MARCH, 7), 1},
 232         };
 233     }
 234 
 235     @Test(dataProvider = "weeksBetween")
 236     public void test_weeksBetween(LocalDate start, LocalDate end, long expected) {
 237         assertEquals(WEEKS.between(start, end).getAmount(), expected);
 238         assertEquals(WEEKS.between(start, end).getUnit(), WEEKS);
 239     }
 240 
 241     @Test(dataProvider = "weeksBetween")
 242     public void test_weeksBetweenReversed(LocalDate start, LocalDate end, long expected) {
 243         assertEquals(WEEKS.between(end, start).getAmount(), -expected);
 244         assertEquals(WEEKS.between(end, start).getUnit(), WEEKS);
 245     }
 246 
 247     //-----------------------------------------------------------------------
 248     @DataProvider(name = "daysBetween")
 249     Object[][] data_daysBetween() {
 250         return new Object[][] {
 251             {date(2012, JULY, 2), date(2012, JULY, 1), -1},
 252             {date(2012, JULY, 2), date(2012, JULY, 2), 0},
 253             {date(2012, JULY, 2), date(2012, JULY, 3), 1},
 254 
 255             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 27), -1},
 256             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 28), 0},
 257             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 29), 1},
 258             {date(2012, FEBRUARY, 28), date(2012, MARCH, 1), 2},
 259 
 260             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 27), -2},
 261             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 28), -1},
 262             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 29), 0},
 263             {date(2012, FEBRUARY, 29), date(2012, MARCH, 1), 1},
 264 
 265             {date(2012, MARCH, 1), date(2012, FEBRUARY, 27), -3},
 266             {date(2012, MARCH, 1), date(2012, FEBRUARY, 28), -2},
 267             {date(2012, MARCH, 1), date(2012, FEBRUARY, 29), -1},
 268             {date(2012, MARCH, 1), date(2012, MARCH, 1), 0},
 269             {date(2012, MARCH, 1), date(2012, MARCH, 2), 1},
 270 
 271             {date(2012, MARCH, 1), date(2013, FEBRUARY, 28), 364},
 272             {date(2012, MARCH, 1), date(2013, MARCH, 1), 365},
 273 
 274             {date(2011, MARCH, 1), date(2012, FEBRUARY, 28), 364},
 275             {date(2011, MARCH, 1), date(2012, FEBRUARY, 29), 365},
 276             {date(2011, MARCH, 1), date(2012, MARCH, 1), 366},
 277         };
 278     }
 279 
 280     @Test(dataProvider = "daysBetween")
 281     public void test_daysBetween(LocalDate start, LocalDate end, long expected) {
 282         assertEquals(DAYS.between(start, end).getAmount(), expected);
 283         assertEquals(DAYS.between(start, end).getUnit(), DAYS);
 284     }
 285 
 286     @Test(dataProvider = "daysBetween")
 287     public void test_daysBetweenReversed(LocalDate start, LocalDate end, long expected) {
 288         assertEquals(DAYS.between(end, start).getAmount(), -expected);
 289         assertEquals(DAYS.between(end, start).getUnit(), DAYS);
 290     }
 291 
 292     @Test(dataProvider = "daysBetween")
 293     public void test_daysBetween_LocalDateTimeSameTime(LocalDate start, LocalDate end, long expected) {
 294         assertEquals(DAYS.between(start.atTime(12, 30), end.atTime(12, 30)).getAmount(), expected);
 295     }
 296 
 297     @Test(dataProvider = "daysBetween")
 298     public void test_daysBetween_LocalDateTimeLaterTime(LocalDate start, LocalDate end, long expected) {
 299         assertEquals(DAYS.between(start.atTime(12, 30), end.atTime(12, 31)).getAmount(), expected);
 300     }
 301 
 302     @Test(dataProvider = "daysBetween")
 303     public void test_daysBetween_ZonedDateSameOffset(LocalDate start, LocalDate end, long expected) {
 304         assertEquals(DAYS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(2))).getAmount(), expected);
 305     }
 306 
 307     @Test(dataProvider = "daysBetween")
 308     public void test_daysBetween_ZonedDateLaterOffset(LocalDate start, LocalDate end, long expected) {
 309         // +01:00 is later than +02:00
 310         assertEquals(DAYS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(1))).getAmount(), expected);
 311     }
 312 
 313     //-----------------------------------------------------------------------
 314     private static LocalDate date(int year, Month month, int dom) {
 315         return LocalDate.of(year, month, dom);
 316     }
 317 
 318 }