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