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) 2010-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 tck.java.time.zone; 61 62 import static java.time.temporal.ChronoUnit.HOURS; 63 import static org.testng.Assert.assertEquals; 64 65 import tck.java.time.AbstractTCKTest; 66 import java.time.Duration; 67 import java.time.LocalDateTime; 68 import java.time.ZoneOffset; 69 import java.time.Year; 70 import java.time.zone.ZoneOffsetTransition; 71 import org.testng.annotations.Test; 72 73 /** 74 * Test ZoneOffsetTransition. 75 */ 76 @Test 77 public class TCKZoneOffsetTransition extends AbstractTCKTest { 78 79 private static final ZoneOffset OFFSET_0100 = ZoneOffset.ofHours(1); 80 private static final ZoneOffset OFFSET_0200 = ZoneOffset.ofHours(2); 81 private static final ZoneOffset OFFSET_0230 = ZoneOffset.ofHoursMinutes(2, 30); 82 private static final ZoneOffset OFFSET_0300 = ZoneOffset.ofHours(3); 83 private static final ZoneOffset OFFSET_0400 = ZoneOffset.ofHours(4); 84 85 //----------------------------------------------------------------------- 86 // factory 87 //----------------------------------------------------------------------- 88 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 89 public void test_factory_nullTransition() { 90 ZoneOffsetTransition.of(null, OFFSET_0100, OFFSET_0200); 91 } 92 93 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 94 public void test_factory_nullOffsetBefore() { 95 ZoneOffsetTransition.of(LocalDateTime.of(2010, 12, 3, 11, 30), null, OFFSET_0200); 96 } 97 98 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 99 public void test_factory_nullOffsetAfter() { 100 ZoneOffsetTransition.of(LocalDateTime.of(2010, 12, 3, 11, 30), OFFSET_0200, null); 101 } 102 103 @Test(expectedExceptions=IllegalArgumentException.class, groups={"tck"}) 104 public void test_factory_sameOffset() { 105 ZoneOffsetTransition.of(LocalDateTime.of(2010, 12, 3, 11, 30), OFFSET_0200, OFFSET_0200); 106 } 107 108 @Test(expectedExceptions=IllegalArgumentException.class, groups={"tck"}) 109 public void test_factory_noNanos() { 110 ZoneOffsetTransition.of(LocalDateTime.of(2010, 12, 3, 11, 30, 0, 500), OFFSET_0200, OFFSET_0300); 111 } 112 113 //----------------------------------------------------------------------- 114 // getters 115 //----------------------------------------------------------------------- 116 @Test(groups={"tck"}) 117 public void test_getters_gap() throws Exception { 118 LocalDateTime before = LocalDateTime.of(2010, 3, 31, 1, 0); 119 LocalDateTime after = LocalDateTime.of(2010, 3, 31, 2, 0); 120 ZoneOffsetTransition test = ZoneOffsetTransition.of(before, OFFSET_0200, OFFSET_0300); 121 assertEquals(test.isGap(), true); 122 assertEquals(test.isOverlap(), false); 123 assertEquals(test.getDateTimeBefore(), before); 124 assertEquals(test.getDateTimeAfter(), after); 125 assertEquals(test.getInstant(), before.toInstant(OFFSET_0200)); 126 assertEquals(test.getOffsetBefore(), OFFSET_0200); 127 assertEquals(test.getOffsetAfter(), OFFSET_0300); 128 assertEquals(test.getDuration(), Duration.of(1, HOURS)); 129 assertSerializable(test); 130 } 131 132 @Test(groups={"tck"}) 133 public void test_getters_overlap() throws Exception { 134 LocalDateTime before = LocalDateTime.of(2010, 10, 31, 1, 0); 135 LocalDateTime after = LocalDateTime.of(2010, 10, 31, 0, 0); 136 ZoneOffsetTransition test = ZoneOffsetTransition.of(before, OFFSET_0300, OFFSET_0200); 137 assertEquals(test.isGap(), false); 138 assertEquals(test.isOverlap(), true); 139 assertEquals(test.getDateTimeBefore(), before); 140 assertEquals(test.getDateTimeAfter(), after); 141 assertEquals(test.getInstant(), before.toInstant(OFFSET_0300)); 142 assertEquals(test.getOffsetBefore(), OFFSET_0300); 143 assertEquals(test.getOffsetAfter(), OFFSET_0200); 144 assertEquals(test.getDuration(), Duration.of(-1, HOURS)); 145 assertSerializable(test); 146 } 147 148 //----------------------------------------------------------------------- 149 @Test 150 public void test_serialization_unusual1() throws Exception { 151 LocalDateTime ldt = LocalDateTime.of(Year.MAX_VALUE, 12, 31, 1, 31, 53); 152 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, ZoneOffset.of("+02:04:56"), ZoneOffset.of("-10:02:34")); 153 assertSerializable(test); 154 } 155 156 @Test 157 public void test_serialization_unusual2() throws Exception { 158 LocalDateTime ldt = LocalDateTime.of(Year.MIN_VALUE, 1, 1, 12, 1, 3); 159 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, ZoneOffset.of("+02:04:56"), ZoneOffset.of("+10:02:34")); 160 assertSerializable(test); 161 } 162 163 //----------------------------------------------------------------------- 164 // isValidOffset() 165 //----------------------------------------------------------------------- 166 @Test(groups={"tck"}) 167 public void test_isValidOffset_gap() { 168 LocalDateTime ldt = LocalDateTime.of(2010, 3, 31, 1, 0); 169 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, OFFSET_0200, OFFSET_0300); 170 assertEquals(test.isValidOffset(OFFSET_0100), false); 171 assertEquals(test.isValidOffset(OFFSET_0200), false); 172 assertEquals(test.isValidOffset(OFFSET_0230), false); 173 assertEquals(test.isValidOffset(OFFSET_0300), false); 174 assertEquals(test.isValidOffset(OFFSET_0400), false); 175 } 176 177 @Test(groups={"tck"}) 178 public void test_isValidOffset_overlap() { 179 LocalDateTime ldt = LocalDateTime.of(2010, 10, 31, 1, 0); 180 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, OFFSET_0300, OFFSET_0200); 181 assertEquals(test.isValidOffset(OFFSET_0100), false); 182 assertEquals(test.isValidOffset(OFFSET_0200), true); 183 assertEquals(test.isValidOffset(OFFSET_0230), false); 184 assertEquals(test.isValidOffset(OFFSET_0300), true); 185 assertEquals(test.isValidOffset(OFFSET_0400), false); 186 } 187 188 //----------------------------------------------------------------------- 189 // compareTo() 190 //----------------------------------------------------------------------- 191 @Test(groups={"tck"}) 192 public void test_compareTo() { 193 ZoneOffsetTransition a = ZoneOffsetTransition.of( 194 LocalDateTime.ofEpochSecond(23875287L - 1, 0, OFFSET_0200), OFFSET_0200, OFFSET_0300); 195 ZoneOffsetTransition b = ZoneOffsetTransition.of( 196 LocalDateTime.ofEpochSecond(23875287L, 0, OFFSET_0300), OFFSET_0300, OFFSET_0200); 197 ZoneOffsetTransition c = ZoneOffsetTransition.of( 198 LocalDateTime.ofEpochSecond(23875287L + 1, 0, OFFSET_0100), OFFSET_0100,OFFSET_0400); 199 200 assertEquals(a.compareTo(a) == 0, true); 201 assertEquals(a.compareTo(b) < 0, true); 202 assertEquals(a.compareTo(c) < 0, true); 203 204 assertEquals(b.compareTo(a) > 0, true); 205 assertEquals(b.compareTo(b) == 0, true); 206 assertEquals(b.compareTo(c) < 0, true); 207 208 assertEquals(c.compareTo(a) > 0, true); 209 assertEquals(c.compareTo(b) > 0, true); 210 assertEquals(c.compareTo(c) == 0, true); 211 } 212 213 @Test(groups={"tck"}) 214 public void test_compareTo_sameInstant() { 215 ZoneOffsetTransition a = ZoneOffsetTransition.of( 216 LocalDateTime.ofEpochSecond(23875287L, 0, OFFSET_0200), OFFSET_0200, OFFSET_0300); 217 ZoneOffsetTransition b = ZoneOffsetTransition.of( 218 LocalDateTime.ofEpochSecond(23875287L, 0, OFFSET_0300), OFFSET_0300, OFFSET_0200); 219 ZoneOffsetTransition c = ZoneOffsetTransition.of( 220 LocalDateTime.ofEpochSecond(23875287L, 0, OFFSET_0100), OFFSET_0100, OFFSET_0400); 221 222 assertEquals(a.compareTo(a) == 0, true); 223 assertEquals(a.compareTo(b) == 0, true); 224 assertEquals(a.compareTo(c) == 0, true); 225 226 assertEquals(b.compareTo(a) == 0, true); 227 assertEquals(b.compareTo(b) == 0, true); 228 assertEquals(b.compareTo(c) == 0, true); 229 230 assertEquals(c.compareTo(a) == 0, true); 231 assertEquals(c.compareTo(b) == 0, true); 232 assertEquals(c.compareTo(c) == 0, true); 233 } 234 235 //----------------------------------------------------------------------- 236 // equals() 237 //----------------------------------------------------------------------- 238 @Test(groups={"tck"}) 239 public void test_equals() { 240 LocalDateTime ldtA = LocalDateTime.of(2010, 3, 31, 1, 0); 241 ZoneOffsetTransition a1 = ZoneOffsetTransition.of(ldtA, OFFSET_0200, OFFSET_0300); 242 ZoneOffsetTransition a2 = ZoneOffsetTransition.of(ldtA, OFFSET_0200, OFFSET_0300); 243 LocalDateTime ldtB = LocalDateTime.of(2010, 10, 31, 1, 0); 244 ZoneOffsetTransition b = ZoneOffsetTransition.of(ldtB, OFFSET_0300, OFFSET_0200); 245 246 assertEquals(a1.equals(a1), true); 247 assertEquals(a1.equals(a2), true); 248 assertEquals(a1.equals(b), false); 249 assertEquals(a2.equals(a1), true); 250 assertEquals(a2.equals(a2), true); 251 assertEquals(a2.equals(b), false); 252 assertEquals(b.equals(a1), false); 253 assertEquals(b.equals(a2), false); 254 assertEquals(b.equals(b), true); 255 256 assertEquals(a1.equals(""), false); 257 assertEquals(a1.equals(null), false); 258 } 259 260 //----------------------------------------------------------------------- 261 // hashCode() 262 //----------------------------------------------------------------------- 263 @Test(groups={"tck"}) 264 public void test_hashCode_floatingWeek_gap_notEndOfDay() { 265 LocalDateTime ldtA = LocalDateTime.of(2010, 3, 31, 1, 0); 266 ZoneOffsetTransition a1 = ZoneOffsetTransition.of(ldtA, OFFSET_0200, OFFSET_0300); 267 ZoneOffsetTransition a2 = ZoneOffsetTransition.of(ldtA, OFFSET_0200, OFFSET_0300); 268 LocalDateTime ldtB = LocalDateTime.of(2010, 10, 31, 1, 0); 269 ZoneOffsetTransition b = ZoneOffsetTransition.of(ldtB, OFFSET_0300, OFFSET_0200); 270 271 assertEquals(a1.hashCode(), a1.hashCode()); 272 assertEquals(a1.hashCode(), a2.hashCode()); 273 assertEquals(b.hashCode(), b.hashCode()); 274 } 275 276 //----------------------------------------------------------------------- 277 // toString() 278 //----------------------------------------------------------------------- 279 @Test(groups={"tck"}) 280 public void test_toString_gap() { 281 LocalDateTime ldt = LocalDateTime.of(2010, 3, 31, 1, 0); 282 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, OFFSET_0200, OFFSET_0300); 283 assertEquals(test.toString(), "Transition[Gap at 2010-03-31T01:00+02:00 to +03:00]"); 284 } 285 286 @Test(groups={"tck"}) 287 public void test_toString_overlap() { 288 LocalDateTime ldt = LocalDateTime.of(2010, 10, 31, 1, 0); 289 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, OFFSET_0300, OFFSET_0200); 290 assertEquals(test.toString(), "Transition[Overlap at 2010-10-31T01:00+03:00 to +02:00]"); 291 } 292 293 }