jdk/test/java/util/logging/HigherResolutionTimeStamps/SimpleFormatterNanos.java
changeset 29117 7956b5dc0eac
equal deleted inserted replaced
29116:9918719cfcc0 29117:7956b5dc0eac
       
     1 /*
       
     2  * Copyright (c) 2015, 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 import java.time.Instant;
       
    24 import java.time.ZoneId;
       
    25 import java.time.ZonedDateTime;
       
    26 import java.util.Locale;
       
    27 import java.util.logging.Level;
       
    28 import java.util.logging.LogRecord;
       
    29 import java.util.logging.SimpleFormatter;
       
    30 
       
    31 /**
       
    32  * @test
       
    33  * @bug 8072645
       
    34  * @summary tests that SimpleFormatter can print out dates with the new
       
    35  *                nanosecond precision.
       
    36  * @run main/othervm SimpleFormatterNanos
       
    37  * @author danielfuchs
       
    38  */
       
    39 public class SimpleFormatterNanos {
       
    40 
       
    41     static final int MILLIS_IN_SECOND = 1000;
       
    42     static final int NANOS_IN_MILLI = 1000_000;
       
    43     static final int NANOS_IN_MICRO = 1000;
       
    44     static final int NANOS_IN_SECOND = 1000_000_000;
       
    45 
       
    46     static final boolean verbose = true;
       
    47 
       
    48     static final class TestAssertException extends RuntimeException {
       
    49         TestAssertException(String msg) { super(msg); }
       
    50     }
       
    51 
       
    52     private static void assertEquals(long expected, long received, String msg) {
       
    53         if (expected != received) {
       
    54             throw new TestAssertException("Unexpected result for " + msg
       
    55                     + ".\n\texpected: " + expected
       
    56                     +  "\n\tactual:   " + received);
       
    57         } else if (verbose) {
       
    58             System.out.println("Got expected " + msg + ": " + received);
       
    59         }
       
    60     }
       
    61 
       
    62     static int getNanoAdjustment(LogRecord record) {
       
    63         return record.getInstant().getNano() % NANOS_IN_MILLI;
       
    64     }
       
    65     static void setNanoAdjustment(LogRecord record, int nanos) {
       
    66         record.setInstant(Instant.ofEpochSecond(record.getInstant().getEpochSecond(),
       
    67                 (record.getInstant().getNano() / NANOS_IN_MILLI) * NANOS_IN_MILLI + nanos));
       
    68     }
       
    69 
       
    70     public static void main(String[] args) throws Exception {
       
    71 
       
    72         Locale.setDefault(Locale.ENGLISH);
       
    73         LogRecord record = new LogRecord(Level.INFO, "Java Version: {0}");
       
    74         record.setLoggerName("test");
       
    75         record.setParameters(new Object[] {System.getProperty("java.version")});
       
    76         int nanos = getNanoAdjustment(record);
       
    77         long millis = record.getMillis();
       
    78         // make sure we don't have leading zeros when printing below
       
    79         // the second precision
       
    80         if (millis % MILLIS_IN_SECOND < 100) millis = millis + 100;
       
    81         // make sure we have some nanos
       
    82         if (nanos % NANOS_IN_MICRO == 0) nanos = nanos + 999;
       
    83         record.setMillis(millis);
       
    84         setNanoAdjustment(record, nanos);
       
    85         final Instant instant = record.getInstant();
       
    86         if (nanos < 0) {
       
    87             throw new RuntimeException("Unexpected negative nano adjustment: "
       
    88                     + getNanoAdjustment(record));
       
    89         }
       
    90         if (nanos >= NANOS_IN_MILLI) {
       
    91             throw new RuntimeException("Nano adjustment exceeds 1ms: "
       
    92                     + getNanoAdjustment(record));
       
    93         }
       
    94         if (millis != record.getMillis()) {
       
    95             throw new RuntimeException("Unexpected millis: " + millis + " != "
       
    96                     + record.getMillis());
       
    97         }
       
    98         if (millis != record.getInstant().toEpochMilli()) {
       
    99             throw new RuntimeException("Unexpected millis: "
       
   100                     + record.getInstant().toEpochMilli());
       
   101         }
       
   102         ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
       
   103         int zdtNanos = zdt.getNano();
       
   104         long expectedNanos = (millis % MILLIS_IN_SECOND) * NANOS_IN_MILLI + nanos;
       
   105         assertEquals(expectedNanos, instant.getNano(), "Instant.getNano()");
       
   106         assertEquals(expectedNanos, zdtNanos, "ZonedDateTime.getNano()");
       
   107         String match = "."+expectedNanos+" ";
       
   108 
       
   109         System.out.println("Testing with default format...");
       
   110 
       
   111         SimpleFormatter formatter = new SimpleFormatter();
       
   112         String str = formatter.format(record);
       
   113         if (str.contains(match)) {
       
   114             throw new RuntimeException("SimpleFormatter.format()"
       
   115                     + " string contains unexpected nanos: "
       
   116                     + "\n\tdid not expect match for: '" + match + "'"
       
   117                     + "\n\tin: " + str);
       
   118         }
       
   119 
       
   120         System.out.println("Nanos omitted as expected: no '"+match+"' in "+str);
       
   121 
       
   122 
       
   123         System.out.println("Changing format to print nanos...");
       
   124         System.setProperty("java.util.logging.SimpleFormatter.format",
       
   125                 "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS.%1$tN %1$Tp %2$s%n%4$s: %5$s%6$s%n");
       
   126 
       
   127         SimpleFormatter formatter2 = new SimpleFormatter();
       
   128         str = formatter2.format(record);
       
   129         if (!str.contains(match)) {
       
   130             throw new RuntimeException("SimpleFormatter.format()"
       
   131                     + " string does not contain expected nanos: "
       
   132                     + "\n\texpected match for: '" + match + "'"
       
   133                     + "\n\tin: " + str);
       
   134         }
       
   135         System.out.println("Found expected match for '"+match+"' in "+str);
       
   136 
       
   137 
       
   138         System.out.println("Changing format to omit nanos...");
       
   139         System.setProperty("java.util.logging.SimpleFormatter.format",
       
   140                 "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n");
       
   141 
       
   142         SimpleFormatter formatter3 = new SimpleFormatter();
       
   143         str = formatter3.format(record);
       
   144         String notMatch = match;
       
   145         if (str.contains(notMatch)) {
       
   146             throw new RuntimeException("SimpleFormatter.format()"
       
   147                     + " string contains unexpected nanos: "
       
   148                     + "\n\tdid not expect match for: '" + notMatch + "'"
       
   149                     + "\n\tin: " + str);
       
   150         }
       
   151         System.out.println("Nanos omitted as expected: no '"+notMatch+"' in "+str);
       
   152 
       
   153     }
       
   154 
       
   155 }