• 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 
36 import org.testng.annotations.Test;
37 
38 /**
39  * Test Clock.
40  */
41 @Test
42 public class TestClock {
43 
44     static class MockInstantClock extends Clock {
45         final long millis;
46         final ZoneId zone;
MockInstantClock(long millis, ZoneId zone)47         MockInstantClock(long millis, ZoneId zone) {
48             this.millis = millis;
49             this.zone = zone;
50         }
51         @Override
instant()52         public Instant instant() {
53             return Instant.ofEpochMilli(millis);
54         }
55         @Override
getZone()56         public ZoneId getZone() {
57             return zone;
58         }
59         @Override
withZone(ZoneId timeZone)60         public Clock withZone(ZoneId timeZone) {
61             return new MockInstantClock(millis, timeZone);
62         }
63         @Override
equals(Object obj)64         public boolean equals(Object obj) {
65             return false;
66         }
67         @Override
hashCode()68         public int hashCode() {
69             return 0;
70         }
71         @Override
toString()72         public String toString() {
73             return "Mock";
74         }
75     }
76 
77     private static final Instant INSTANT = Instant.ofEpochSecond(1873687, 357000000);
78     private static final ZoneId ZONE = ZoneId.of("Europe/Paris");
79     private static final Clock MOCK_INSTANT = new MockInstantClock(INSTANT.toEpochMilli(), ZONE);
80 
81     //-----------------------------------------------------------------------
82     @Test
test_mockInstantClock_get()83     public void test_mockInstantClock_get() {
84         assertEquals(MOCK_INSTANT.instant(), INSTANT);
85         assertEquals(MOCK_INSTANT.millis(), INSTANT.toEpochMilli());
86         assertEquals(MOCK_INSTANT.getZone(), ZONE);
87     }
88 
89     @Test
test_mockInstantClock_withZone()90     public void test_mockInstantClock_withZone() {
91         ZoneId london = ZoneId.of("Europe/London");
92         Clock changed = MOCK_INSTANT.withZone(london);
93         assertEquals(MOCK_INSTANT.instant(), INSTANT);
94         assertEquals(MOCK_INSTANT.millis(), INSTANT.toEpochMilli());
95         assertEquals(changed.getZone(), london);
96     }
97 
98 }
99