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