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