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 fixed clock. 43 */ 44 @Test 45 public class TestClock_Fixed 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 51 //----------------------------------------------------------------------- test_isSerializable()52 public void test_isSerializable() throws IOException, ClassNotFoundException { 53 assertSerializable(Clock.fixed(INSTANT, ZoneOffset.UTC)); 54 assertSerializable(Clock.fixed(INSTANT, PARIS)); 55 } 56 57 //------------------------------------------------------------------------- test_fixed_InstantZoneId()58 public void test_fixed_InstantZoneId() { 59 Clock test = Clock.fixed(INSTANT, PARIS); 60 assertEquals(test.instant(), INSTANT); 61 assertEquals(test.getZone(), PARIS); 62 } 63 64 @Test(expectedExceptions = NullPointerException.class) test_fixed_InstantZoneId_nullInstant()65 public void test_fixed_InstantZoneId_nullInstant() { 66 Clock.fixed(null, PARIS); 67 } 68 69 @Test(expectedExceptions = NullPointerException.class) test_fixed_InstantZoneId_nullZoneId()70 public void test_fixed_InstantZoneId_nullZoneId() { 71 Clock.fixed(INSTANT, null); 72 } 73 74 //------------------------------------------------------------------------- test_withZone()75 public void test_withZone() { 76 Clock test = Clock.fixed(INSTANT, PARIS); 77 Clock changed = test.withZone(MOSCOW); 78 assertEquals(test.getZone(), PARIS); 79 assertEquals(changed.getZone(), MOSCOW); 80 } 81 test_withZone_same()82 public void test_withZone_same() { 83 Clock test = Clock.fixed(INSTANT, PARIS); 84 Clock changed = test.withZone(PARIS); 85 assertSame(test, changed); 86 } 87 88 @Test(expectedExceptions = NullPointerException.class) test_withZone_null()89 public void test_withZone_null() { 90 Clock.fixed(INSTANT, PARIS).withZone(null); 91 } 92 93 //----------------------------------------------------------------------- test_equals()94 public void test_equals() { 95 Clock a = Clock.fixed(INSTANT, ZoneOffset.UTC); 96 Clock b = Clock.fixed(INSTANT, ZoneOffset.UTC); 97 assertEquals(a.equals(a), true); 98 assertEquals(a.equals(b), true); 99 assertEquals(b.equals(a), true); 100 assertEquals(b.equals(b), true); 101 102 Clock c = Clock.fixed(INSTANT, PARIS); 103 assertEquals(a.equals(c), false); 104 105 Clock d = Clock.fixed(INSTANT.minusNanos(1), ZoneOffset.UTC); 106 assertEquals(a.equals(d), false); 107 108 assertEquals(a.equals(null), false); 109 assertEquals(a.equals("other type"), false); 110 assertEquals(a.equals(Clock.systemUTC()), false); 111 } 112 test_hashCode()113 public void test_hashCode() { 114 Clock a = Clock.fixed(INSTANT, ZoneOffset.UTC); 115 Clock b = Clock.fixed(INSTANT, ZoneOffset.UTC); 116 assertEquals(a.hashCode(), a.hashCode()); 117 assertEquals(a.hashCode(), b.hashCode()); 118 119 Clock c = Clock.fixed(INSTANT, PARIS); 120 assertEquals(a.hashCode() == c.hashCode(), false); 121 122 Clock d = Clock.fixed(INSTANT.minusNanos(1), ZoneOffset.UTC); 123 assertEquals(a.hashCode() == d.hashCode(), false); 124 } 125 126 //----------------------------------------------------------------------- test_toString()127 public void test_toString() { 128 Clock test = Clock.fixed(INSTANT, PARIS); 129 assertEquals(test.toString(), "FixedClock[2008-06-30T09:30:10.000000500Z,Europe/Paris]"); 130 } 131 132 } 133