jdk/test/java/time/test/java/time/TestClock_System.java
changeset 29108 57b3b196d3a6
parent 28765 8878e8455f2a
child 35613 4c29bcd3a107
equal deleted inserted replaced
29107:e9b83d4118d8 29108:57b3b196d3a6
    64 
    64 
    65 import java.lang.reflect.Field;
    65 import java.lang.reflect.Field;
    66 import java.time.Clock;
    66 import java.time.Clock;
    67 import java.time.Instant;
    67 import java.time.Instant;
    68 import java.time.ZoneId;
    68 import java.time.ZoneId;
       
    69 import java.time.ZoneOffset;
    69 
    70 
    70 import org.testng.annotations.Test;
    71 import org.testng.annotations.Test;
       
    72 import org.testng.annotations.DataProvider;
    71 
    73 
    72 /**
    74 /**
    73  * Test system clock.
    75  * Test system clock.
    74  */
    76  */
    75 @Test
    77 @Test
    76 public class TestClock_System {
    78 public class TestClock_System {
    77 
    79 
    78     private static final ZoneId PARIS = ZoneId.of("Europe/Paris");
    80     private static final ZoneId PARIS = ZoneId.of("Europe/Paris");
       
    81     private static final Clock systemUTC = Clock.systemUTC();
    79 
    82 
    80     public void test_withZone_same() {
    83     public void test_withZone_same() {
    81         Clock test = Clock.system(PARIS);
    84         Clock test = Clock.system(PARIS);
    82         Clock changed = test.withZone(PARIS);
    85         Clock changed = test.withZone(PARIS);
    83         assertSame(test, changed);
    86         assertSame(test, changed);
    85 
    88 
    86     //-----------------------------------------------------------------------
    89     //-----------------------------------------------------------------------
    87     public void test_toString() {
    90     public void test_toString() {
    88         Clock test = Clock.system(PARIS);
    91         Clock test = Clock.system(PARIS);
    89         assertEquals(test.toString(), "SystemClock[Europe/Paris]");
    92         assertEquals(test.toString(), "SystemClock[Europe/Paris]");
       
    93     }
       
    94 
       
    95     //-----------------------------------------------------------------------
       
    96     @DataProvider(name="sampleSystemUTC")
       
    97     Object[][] provider_sampleSystemUTC() {
       
    98         return new Object[][] {
       
    99             {"Clock.systemUTC()#1",  Clock.systemUTC()},
       
   100             {"Clock.systemUTC()#2",  Clock.systemUTC()},
       
   101             {"Clock.system(ZoneOffset.UTC)#1",  Clock.system(ZoneOffset.UTC)},
       
   102             {"Clock.system(ZoneOffset.UTC)#2",  Clock.system(ZoneOffset.UTC)}
       
   103         };
       
   104     }
       
   105 
       
   106     // Test for 8073394
       
   107     @Test(dataProvider="sampleSystemUTC")
       
   108     public void test_systemUTC(String s, Clock clock) {
       
   109         if (clock != systemUTC) {
       
   110             throw new RuntimeException("Unexpected clock instance for " + s + ": "
       
   111                 + "\n\texpected: " + toString(systemUTC)
       
   112                 + "\n\tactual:   " + toString(clock));
       
   113         }
       
   114     }
       
   115 
       
   116     private static String toString(Clock c) {
       
   117         return c == null ? null :
       
   118                c + " " + c.getClass().getName() + "@" + System.identityHashCode(c);
    90     }
   119     }
    91 
   120 
    92     //-----------------------------------------------------------------------
   121     //-----------------------------------------------------------------------
    93 
   122 
    94     private static String formatTime(String prefix, Instant time) {
   123     private static String formatTime(String prefix, Instant time) {