1 /* 2 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * * Neither the name of JSR-310 nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.threeten.bp.zone; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.threeten.bp.temporal.ChronoUnit.HOURS; 36 37 import java.io.IOException; 38 39 import org.testng.annotations.Test; 40 import org.threeten.bp.AbstractTest; 41 import org.threeten.bp.Duration; 42 import org.threeten.bp.LocalDateTime; 43 import org.threeten.bp.Year; 44 import org.threeten.bp.ZoneOffset; 45 46 /** 47 * Test ZoneOffsetTransition. 48 */ 49 @Test 50 public class TestZoneOffsetTransition extends AbstractTest { 51 52 private static final ZoneOffset OFFSET_0100 = ZoneOffset.ofHours(1); 53 private static final ZoneOffset OFFSET_0200 = ZoneOffset.ofHours(2); 54 private static final ZoneOffset OFFSET_0230 = ZoneOffset.ofHoursMinutes(2, 30); 55 private static final ZoneOffset OFFSET_0300 = ZoneOffset.ofHours(3); 56 private static final ZoneOffset OFFSET_0400 = ZoneOffset.ofHours(4); 57 58 //----------------------------------------------------------------------- 59 // factory 60 //----------------------------------------------------------------------- 61 @Test(expectedExceptions=NullPointerException.class) test_factory_nullTransition()62 public void test_factory_nullTransition() { 63 ZoneOffsetTransition.of(null, OFFSET_0100, OFFSET_0200); 64 } 65 66 @Test(expectedExceptions=NullPointerException.class) test_factory_nullOffsetBefore()67 public void test_factory_nullOffsetBefore() { 68 ZoneOffsetTransition.of(LocalDateTime.of(2010, 12, 3, 11, 30), null, OFFSET_0200); 69 } 70 71 @Test(expectedExceptions=NullPointerException.class) test_factory_nullOffsetAfter()72 public void test_factory_nullOffsetAfter() { 73 ZoneOffsetTransition.of(LocalDateTime.of(2010, 12, 3, 11, 30), OFFSET_0200, null); 74 } 75 76 @Test(expectedExceptions=IllegalArgumentException.class) test_factory_sameOffset()77 public void test_factory_sameOffset() { 78 ZoneOffsetTransition.of(LocalDateTime.of(2010, 12, 3, 11, 30), OFFSET_0200, OFFSET_0200); 79 } 80 81 @Test(expectedExceptions=IllegalArgumentException.class) test_factory_noNanos()82 public void test_factory_noNanos() { 83 ZoneOffsetTransition.of(LocalDateTime.of(2010, 12, 3, 11, 30, 0, 500), OFFSET_0200, OFFSET_0300); 84 } 85 86 //----------------------------------------------------------------------- 87 // getters 88 //----------------------------------------------------------------------- 89 @Test test_getters_gap()90 public void test_getters_gap() throws Exception { 91 LocalDateTime before = LocalDateTime.of(2010, 3, 31, 1, 0); 92 LocalDateTime after = LocalDateTime.of(2010, 3, 31, 2, 0); 93 ZoneOffsetTransition test = ZoneOffsetTransition.of(before, OFFSET_0200, OFFSET_0300); 94 assertEquals(test.isGap(), true); 95 assertEquals(test.isOverlap(), false); 96 assertEquals(test.getDateTimeBefore(), before); 97 assertEquals(test.getDateTimeAfter(), after); 98 assertEquals(test.getInstant(), before.toInstant(OFFSET_0200)); 99 assertEquals(test.getOffsetBefore(), OFFSET_0200); 100 assertEquals(test.getOffsetAfter(), OFFSET_0300); 101 assertEquals(test.getDuration(), Duration.of(1, HOURS)); 102 assertSerializable(test); 103 } 104 105 @Test test_getters_overlap()106 public void test_getters_overlap() throws Exception { 107 LocalDateTime before = LocalDateTime.of(2010, 10, 31, 1, 0); 108 LocalDateTime after = LocalDateTime.of(2010, 10, 31, 0, 0); 109 ZoneOffsetTransition test = ZoneOffsetTransition.of(before, OFFSET_0300, OFFSET_0200); 110 assertEquals(test.isGap(), false); 111 assertEquals(test.isOverlap(), true); 112 assertEquals(test.getDateTimeBefore(), before); 113 assertEquals(test.getDateTimeAfter(), after); 114 assertEquals(test.getInstant(), before.toInstant(OFFSET_0300)); 115 assertEquals(test.getOffsetBefore(), OFFSET_0300); 116 assertEquals(test.getOffsetAfter(), OFFSET_0200); 117 assertEquals(test.getDuration(), Duration.of(-1, HOURS)); 118 assertSerializable(test); 119 } 120 121 //----------------------------------------------------------------------- 122 @Test test_serialization_unusual1()123 public void test_serialization_unusual1() throws Exception { 124 LocalDateTime ldt = LocalDateTime.of(Year.MAX_VALUE, 12, 31, 1, 31, 53); 125 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, ZoneOffset.of("+02:04:56"), ZoneOffset.of("-10:02:34")); 126 assertSerializable(test); 127 } 128 129 @Test test_serialization_unusual2()130 public void test_serialization_unusual2() throws Exception { 131 LocalDateTime ldt = LocalDateTime.of(Year.MIN_VALUE, 1, 1, 12, 1, 3); 132 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, ZoneOffset.of("+02:04:56"), ZoneOffset.of("+10:02:34")); 133 assertSerializable(test); 134 } 135 136 @Test test_serialization_format()137 public void test_serialization_format() throws ClassNotFoundException, IOException { 138 LocalDateTime ldt = LocalDateTime.of(Year.MIN_VALUE, 1, 1, 12, 1, 3); 139 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, ZoneOffset.of("+02:04:56"), ZoneOffset.of("+10:02:34")); 140 assertEqualsSerialisedForm(test); 141 } 142 143 //----------------------------------------------------------------------- 144 // isValidOffset() 145 //----------------------------------------------------------------------- 146 @Test test_isValidOffset_gap()147 public void test_isValidOffset_gap() { 148 LocalDateTime ldt = LocalDateTime.of(2010, 3, 31, 1, 0); 149 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, OFFSET_0200, OFFSET_0300); 150 assertEquals(test.isValidOffset(OFFSET_0100), false); 151 assertEquals(test.isValidOffset(OFFSET_0200), false); 152 assertEquals(test.isValidOffset(OFFSET_0230), false); 153 assertEquals(test.isValidOffset(OFFSET_0300), false); 154 assertEquals(test.isValidOffset(OFFSET_0400), false); 155 } 156 157 @Test test_isValidOffset_overlap()158 public void test_isValidOffset_overlap() { 159 LocalDateTime ldt = LocalDateTime.of(2010, 10, 31, 1, 0); 160 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, OFFSET_0300, OFFSET_0200); 161 assertEquals(test.isValidOffset(OFFSET_0100), false); 162 assertEquals(test.isValidOffset(OFFSET_0200), true); 163 assertEquals(test.isValidOffset(OFFSET_0230), false); 164 assertEquals(test.isValidOffset(OFFSET_0300), true); 165 assertEquals(test.isValidOffset(OFFSET_0400), false); 166 } 167 168 //----------------------------------------------------------------------- 169 // compareTo() 170 //----------------------------------------------------------------------- 171 @Test test_compareTo()172 public void test_compareTo() { 173 ZoneOffsetTransition a = ZoneOffsetTransition.of( 174 LocalDateTime.ofEpochSecond(23875287L - 1, 0, OFFSET_0200), OFFSET_0200, OFFSET_0300); 175 ZoneOffsetTransition b = ZoneOffsetTransition.of( 176 LocalDateTime.ofEpochSecond(23875287L, 0, OFFSET_0300), OFFSET_0300, OFFSET_0200); 177 ZoneOffsetTransition c = ZoneOffsetTransition.of( 178 LocalDateTime.ofEpochSecond(23875287L + 1, 0, OFFSET_0100), OFFSET_0100, OFFSET_0400); 179 180 assertEquals(a.compareTo(a) == 0, true); 181 assertEquals(a.compareTo(b) < 0, true); 182 assertEquals(a.compareTo(c) < 0, true); 183 184 assertEquals(b.compareTo(a) > 0, true); 185 assertEquals(b.compareTo(b) == 0, true); 186 assertEquals(b.compareTo(c) < 0, true); 187 188 assertEquals(c.compareTo(a) > 0, true); 189 assertEquals(c.compareTo(b) > 0, true); 190 assertEquals(c.compareTo(c) == 0, true); 191 } 192 193 @Test test_compareTo_sameInstant()194 public void test_compareTo_sameInstant() { 195 ZoneOffsetTransition a = ZoneOffsetTransition.of( 196 LocalDateTime.ofEpochSecond(23875287L, 0, OFFSET_0200), OFFSET_0200, OFFSET_0300); 197 ZoneOffsetTransition b = ZoneOffsetTransition.of( 198 LocalDateTime.ofEpochSecond(23875287L, 0, OFFSET_0300), OFFSET_0300, OFFSET_0200); 199 ZoneOffsetTransition c = ZoneOffsetTransition.of( 200 LocalDateTime.ofEpochSecond(23875287L, 0, OFFSET_0100), OFFSET_0100, OFFSET_0400); 201 202 assertEquals(a.compareTo(a) == 0, true); 203 assertEquals(a.compareTo(b) == 0, true); 204 assertEquals(a.compareTo(c) == 0, true); 205 206 assertEquals(b.compareTo(a) == 0, true); 207 assertEquals(b.compareTo(b) == 0, true); 208 assertEquals(b.compareTo(c) == 0, true); 209 210 assertEquals(c.compareTo(a) == 0, true); 211 assertEquals(c.compareTo(b) == 0, true); 212 assertEquals(c.compareTo(c) == 0, true); 213 } 214 215 //----------------------------------------------------------------------- 216 // equals() 217 //----------------------------------------------------------------------- 218 @Test test_equals()219 public void test_equals() { 220 LocalDateTime ldtA = LocalDateTime.of(2010, 3, 31, 1, 0); 221 ZoneOffsetTransition a1 = ZoneOffsetTransition.of(ldtA, OFFSET_0200, OFFSET_0300); 222 ZoneOffsetTransition a2 = ZoneOffsetTransition.of(ldtA, OFFSET_0200, OFFSET_0300); 223 LocalDateTime ldtB = LocalDateTime.of(2010, 10, 31, 1, 0); 224 ZoneOffsetTransition b = ZoneOffsetTransition.of(ldtB, OFFSET_0300, OFFSET_0200); 225 226 assertEquals(a1.equals(a1), true); 227 assertEquals(a1.equals(a2), true); 228 assertEquals(a1.equals(b), false); 229 assertEquals(a2.equals(a1), true); 230 assertEquals(a2.equals(a2), true); 231 assertEquals(a2.equals(b), false); 232 assertEquals(b.equals(a1), false); 233 assertEquals(b.equals(a2), false); 234 assertEquals(b.equals(b), true); 235 236 assertEquals(a1.equals(""), false); 237 assertEquals(a1.equals(null), false); 238 } 239 240 //----------------------------------------------------------------------- 241 // hashCode() 242 //----------------------------------------------------------------------- 243 @Test test_hashCode_floatingWeek_gap_notEndOfDay()244 public void test_hashCode_floatingWeek_gap_notEndOfDay() { 245 LocalDateTime ldtA = LocalDateTime.of(2010, 3, 31, 1, 0); 246 ZoneOffsetTransition a1 = ZoneOffsetTransition.of(ldtA, OFFSET_0200, OFFSET_0300); 247 ZoneOffsetTransition a2 = ZoneOffsetTransition.of(ldtA, OFFSET_0200, OFFSET_0300); 248 LocalDateTime ldtB = LocalDateTime.of(2010, 10, 31, 1, 0); 249 ZoneOffsetTransition b = ZoneOffsetTransition.of(ldtB, OFFSET_0300, OFFSET_0200); 250 251 assertEquals(a1.hashCode(), a1.hashCode()); 252 assertEquals(a1.hashCode(), a2.hashCode()); 253 assertEquals(b.hashCode(), b.hashCode()); 254 } 255 256 //----------------------------------------------------------------------- 257 // toString() 258 //----------------------------------------------------------------------- 259 @Test test_toString_gap()260 public void test_toString_gap() { 261 LocalDateTime ldt = LocalDateTime.of(2010, 3, 31, 1, 0); 262 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, OFFSET_0200, OFFSET_0300); 263 assertEquals(test.toString(), "Transition[Gap at 2010-03-31T01:00+02:00 to +03:00]"); 264 } 265 266 @Test test_toString_overlap()267 public void test_toString_overlap() { 268 LocalDateTime ldt = LocalDateTime.of(2010, 10, 31, 1, 0); 269 ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, OFFSET_0300, OFFSET_0200); 270 assertEquals(test.toString(), "Transition[Overlap at 2010-10-31T01:00+03:00 to +02:00]"); 271 } 272 273 } 274