1 /*
   2  * Copyright (c) 2012, 2019, 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) 2008-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 org.testng.Assert.assertSame;
  63 import static org.testng.Assert.assertEquals;
  64 
  65 import java.time.Duration;
  66 import java.time.LocalDate;
  67 import java.time.LocalDateTime;
  68 import java.time.LocalTime;
  69 import java.time.OffsetDateTime;
  70 import java.time.ZoneOffset;
  71 
  72 import org.testng.annotations.BeforeMethod;
  73 import org.testng.annotations.DataProvider;
  74 import org.testng.annotations.Test;
  75 
  76 /**
  77  * Test OffsetDateTime.
  78  *
  79  * @bug 8211990
  80  */
  81 @Test
  82 public class TestOffsetDateTime extends AbstractTest {
  83 
  84     private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1);
  85     private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2);
  86     private OffsetDateTime TEST_2008_6_30_11_30_59_000000500;
  87 
  88     @BeforeMethod
  89     public void setUp() {
  90         TEST_2008_6_30_11_30_59_000000500 = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 500), OFFSET_PONE);
  91     }
  92 
  93     @Test
  94     public void test_immutable() {
  95         assertImmutable(OffsetDateTime.class);
  96     }
  97 
  98     //-----------------------------------------------------------------------
  99     // basics
 100     //-----------------------------------------------------------------------
 101     @DataProvider(name="sampleTimes")
 102     Object[][] provider_sampleTimes() {
 103         return new Object[][] {
 104             {2008, 6, 30, 11, 30, 20, 500, OFFSET_PONE},
 105             {2008, 6, 30, 11, 0, 0, 0, OFFSET_PONE},
 106             {2008, 6, 30, 23, 59, 59, 999999999, OFFSET_PONE},
 107             {-1, 1, 1, 0, 0, 0, 0, OFFSET_PONE},
 108         };
 109     }
 110 
 111     @Test(dataProvider="sampleTimes")
 112     public void test_get_same(int y, int o, int d, int h, int m, int s, int n, ZoneOffset offset) {
 113         LocalDate localDate = LocalDate.of(y, o, d);
 114         LocalTime localTime = LocalTime.of(h, m, s, n);
 115         LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
 116         OffsetDateTime a = OffsetDateTime.of(localDateTime, offset);
 117 
 118         assertSame(a.getOffset(), offset);
 119         assertSame(a.toLocalDate(), localDate);
 120         assertSame(a.toLocalTime(), localTime);
 121         assertSame(a.toLocalDateTime(), localDateTime);
 122     }
 123 
 124     //-----------------------------------------------------------------------
 125     // withOffsetSameLocal()
 126     //-----------------------------------------------------------------------
 127     @Test
 128     public void test_withOffsetSameLocal() {
 129         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 130         OffsetDateTime test = base.withOffsetSameLocal(OFFSET_PTWO);
 131         assertSame(test.toLocalDateTime(), base.toLocalDateTime());
 132         assertSame(test.getOffset(), OFFSET_PTWO);
 133     }
 134 
 135     @Test
 136     public void test_withOffsetSameLocal_noChange() {
 137         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 138         OffsetDateTime test = base.withOffsetSameLocal(OFFSET_PONE);
 139         assertSame(test, base);
 140     }
 141 
 142     @Test
 143     public void test_withOffsetSameInstant_noChange() {
 144         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 145         OffsetDateTime test = base.withOffsetSameInstant(OFFSET_PONE);
 146         assertSame(test, base);
 147     }
 148 
 149     @Test
 150     public void test_withYear_noChange() {
 151         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 152         OffsetDateTime test = base.withYear(2008);
 153         assertSame(test, base);
 154     }
 155 
 156     @Test
 157     public void test_withMonth_noChange() {
 158         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 159         OffsetDateTime test = base.withMonth(6);
 160         assertSame(test, base);
 161     }
 162 
 163     @Test
 164     public void test_withDayOfMonth_noChange() {
 165         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 166         OffsetDateTime test = base.withDayOfMonth(30);
 167         assertSame(test, base);
 168     }
 169 
 170     @Test
 171     public void test_withDayOfYear_noChange() {
 172         OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.withDayOfYear(31 + 29 + 31 + 30 + 31 + 30);
 173         assertSame(t, TEST_2008_6_30_11_30_59_000000500);
 174     }
 175 
 176     @Test
 177     public void test_withHour_noChange() {
 178         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 179         OffsetDateTime test = base.withHour(11);
 180         assertSame(test, base);
 181     }
 182 
 183     @Test
 184     public void test_withMinute_noChange() {
 185         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 186         OffsetDateTime test = base.withMinute(30);
 187         assertSame(test, base);
 188     }
 189 
 190     @Test
 191     public void test_withSecond_noChange() {
 192         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 193         OffsetDateTime test = base.withSecond(59);
 194         assertSame(test, base);
 195     }
 196 
 197     @Test
 198     public void test_withNanoOfSecond_noChange() {
 199         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 1), OFFSET_PONE);
 200         OffsetDateTime test = base.withNano(1);
 201         assertSame(test, base);
 202     }
 203 
 204     @Test
 205     public void test_plus_Period_zero() {
 206         OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.plus(MockSimplePeriod.ZERO_DAYS);
 207         assertSame(t, TEST_2008_6_30_11_30_59_000000500);
 208     }
 209 
 210     @Test
 211     public void test_plusYears_zero() {
 212         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 213         OffsetDateTime test = base.plusYears(0);
 214         assertSame(test, base);
 215     }
 216 
 217     @Test
 218     public void test_plusMonths_zero() {
 219         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 220         OffsetDateTime test = base.plusMonths(0);
 221         assertSame(test, base);
 222     }
 223 
 224     @Test
 225     public void test_plusWeeks_zero() {
 226         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 227         OffsetDateTime test = base.plusWeeks(0);
 228         assertSame(test, base);
 229     }
 230 
 231     @Test
 232     public void test_plusDays_zero() {
 233         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 234         OffsetDateTime test = base.plusDays(0);
 235         assertSame(test, base);
 236     }
 237 
 238     @Test
 239     public void test_plusHours_zero() {
 240         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 241         OffsetDateTime test = base.plusHours(0);
 242         assertSame(test, base);
 243     }
 244 
 245     @Test
 246     public void test_plusMinutes_zero() {
 247         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 248         OffsetDateTime test = base.plusMinutes(0);
 249         assertSame(test, base);
 250     }
 251 
 252     @Test
 253     public void test_plusSeconds_zero() {
 254         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 255         OffsetDateTime test = base.plusSeconds(0);
 256         assertSame(test, base);
 257     }
 258 
 259     @Test
 260     public void test_plusNanos_zero() {
 261         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 262         OffsetDateTime test = base.plusNanos(0);
 263     }
 264 
 265     @Test
 266     public void test_minus_Period_zero() {
 267         OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.minus(MockSimplePeriod.ZERO_DAYS);
 268         assertSame(t, TEST_2008_6_30_11_30_59_000000500);
 269     }
 270 
 271     @Test
 272     public void test_minusYears_zero() {
 273         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2007, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 274         OffsetDateTime test = base.minusYears(0);
 275         assertSame(test, base);
 276     }
 277 
 278     @Test
 279     public void test_minusMonths_zero() {
 280         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 281         OffsetDateTime test = base.minusMonths(0);
 282         assertSame(test, base);
 283     }
 284 
 285     @Test
 286     public void test_minusWeeks_zero() {
 287         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 288         OffsetDateTime test = base.minusWeeks(0);
 289         assertSame(test, base);
 290     }
 291 
 292     @Test
 293     public void test_minusDays_zero() {
 294         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 295         OffsetDateTime test = base.minusDays(0);
 296         assertSame(test, base);
 297     }
 298 
 299     @Test
 300     public void test_minusHours_zero() {
 301         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 302         OffsetDateTime test = base.minusHours(0);
 303         assertSame(test, base);
 304     }
 305 
 306     @Test
 307     public void test_minusMinutes_zero() {
 308         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 309         OffsetDateTime test = base.minusMinutes(0);
 310         assertSame(test, base);
 311     }
 312 
 313     @Test
 314     public void test_minusSeconds_zero() {
 315         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 316         OffsetDateTime test = base.minusSeconds(0);
 317         assertSame(test, base);
 318     }
 319 
 320     @Test
 321     public void test_minusNanos_zero() {
 322         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 323         OffsetDateTime test = base.minusNanos(0);
 324         assertSame(test, base);
 325     }
 326 
 327     @Test
 328     public void test_duration() {
 329         OffsetDateTime start = OffsetDateTime.MAX
 330                                 .withOffsetSameLocal(ZoneOffset.ofHours(-17));
 331         OffsetDateTime end = OffsetDateTime.MAX;
 332         assertEquals(Duration.between(start, end), Duration.ofHours(1));
 333     }
 334 }