• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
33 
34 import static org.testng.Assert.assertEquals;
35 import static org.testng.Assert.assertSame;
36 
37 import java.io.IOException;
38 
39 import org.testng.annotations.Test;
40 
41 /**
42  * Test offset clock.
43  */
44 @Test
45 public class TestClock_Offset extends AbstractTest {
46 
47     private static final ZoneId MOSCOW = ZoneId.of("Europe/Moscow");
48     private static final ZoneId PARIS = ZoneId.of("Europe/Paris");
49     private static final Instant INSTANT = LocalDateTime.of(2008, 6, 30, 11, 30, 10, 500).atZone(ZoneOffset.ofHours(2)).toInstant();
50     private static final Duration OFFSET = Duration.ofSeconds(2);
51 
52     //-----------------------------------------------------------------------
test_isSerializable()53     public void test_isSerializable() throws IOException, ClassNotFoundException {
54         assertSerializable(Clock.offset(Clock.system(PARIS), OFFSET));
55     }
56 
57     //-----------------------------------------------------------------------
test_offset_ClockDuration()58     public void test_offset_ClockDuration() {
59         Clock test = Clock.offset(Clock.fixed(INSTANT, PARIS), OFFSET);
60         assertEquals(test.instant(), INSTANT.plus(OFFSET));
61         assertEquals(test.getZone(), PARIS);
62     }
63 
test_offset_ClockDuration_zeroDuration()64     public void test_offset_ClockDuration_zeroDuration() {
65         Clock underlying = Clock.system(PARIS);
66         Clock test = Clock.offset(underlying, Duration.ZERO);
67         assertSame(test, underlying);  // spec says same
68     }
69 
70     @Test(expectedExceptions = NullPointerException.class)
test_offset_ClockDuration_nullClock()71     public void test_offset_ClockDuration_nullClock() {
72         Clock.offset(null, Duration.ZERO);
73     }
74 
75     @Test(expectedExceptions = NullPointerException.class)
test_offset_ClockDuration_nullDuration()76     public void test_offset_ClockDuration_nullDuration() {
77         Clock.offset(Clock.systemUTC(), null);
78     }
79 
80     //-------------------------------------------------------------------------
test_withZone()81     public void test_withZone() {
82         Clock test = Clock.offset(Clock.system(PARIS), OFFSET);
83         Clock changed = test.withZone(MOSCOW);
84         assertEquals(test.getZone(), PARIS);
85         assertEquals(changed.getZone(), MOSCOW);
86     }
87 
test_withZone_same()88     public void test_withZone_same() {
89         Clock test = Clock.offset(Clock.system(PARIS), OFFSET);
90         Clock changed = test.withZone(PARIS);
91         assertSame(test, changed);
92     }
93 
94     @Test(expectedExceptions = NullPointerException.class)
test_withZone_null()95     public void test_withZone_null() {
96         Clock.offset(Clock.system(PARIS), OFFSET).withZone(null);
97     }
98 
99     //-----------------------------------------------------------------------
test_equals()100     public void test_equals() {
101         Clock a = Clock.offset(Clock.system(PARIS), OFFSET);
102         Clock b = Clock.offset(Clock.system(PARIS), OFFSET);
103         assertEquals(a.equals(a), true);
104         assertEquals(a.equals(b), true);
105         assertEquals(b.equals(a), true);
106         assertEquals(b.equals(b), true);
107 
108         Clock c = Clock.offset(Clock.system(MOSCOW), OFFSET);
109         assertEquals(a.equals(c), false);
110 
111         Clock d = Clock.offset(Clock.system(PARIS), OFFSET.minusNanos(1));
112         assertEquals(a.equals(d), false);
113 
114         assertEquals(a.equals(null), false);
115         assertEquals(a.equals("other type"), false);
116         assertEquals(a.equals(Clock.systemUTC()), false);
117     }
118 
test_hashCode()119     public void test_hashCode() {
120         Clock a = Clock.offset(Clock.system(PARIS), OFFSET);
121         Clock b = Clock.offset(Clock.system(PARIS), OFFSET);
122         assertEquals(a.hashCode(), a.hashCode());
123         assertEquals(a.hashCode(), b.hashCode());
124 
125         Clock c = Clock.offset(Clock.system(MOSCOW), OFFSET);
126         assertEquals(a.hashCode() == c.hashCode(), false);
127 
128         Clock d = Clock.offset(Clock.system(PARIS), OFFSET.minusNanos(1));
129         assertEquals(a.hashCode() == d.hashCode(), false);
130     }
131 
132     //-----------------------------------------------------------------------
test_toString()133     public void test_toString() {
134         Clock test = Clock.offset(Clock.systemUTC(), OFFSET);
135         assertEquals(test.toString(), "OffsetClock[SystemClock[Z],PT2S]");
136     }
137 
138 }
139