jdk/test/java/util/logging/HigherResolutionTimeStamps/XmlFormatterNanos.java
author dfuchs
Wed, 25 Feb 2015 18:41:07 +0100
changeset 29117 7956b5dc0eac
child 32649 2ee9017c7597
permissions -rw-r--r--
8072645: java.util.logging should use java.time to get more precise time stamps Summary: j.u.logging uses j.t.Instant to store LogRecord time stamps. XMLFormatter format is updated to allow for a new optional <nanos> element containing a nano second adjustment. SimpleFormatter passes a ZonedDateTime object to String.format. LogRecord getMillis/setMillis are deprecated, replaced by getInstant/setInstant. Reviewed-by: scolebourne, plevart, rriggs Contributed-by: daniel.fuchs@oracle.com, peter.levart@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
     1
/*
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
     4
 *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
     8
 *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    14
 *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    18
 *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    21
 * questions.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    22
 */
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    23
import java.io.ByteArrayInputStream;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    24
import java.io.ByteArrayOutputStream;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    25
import java.io.IOException;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    26
import java.time.Instant;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    27
import java.time.ZoneId;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    28
import java.time.ZonedDateTime;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    29
import java.time.format.DateTimeFormatter;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    30
import java.time.temporal.ChronoUnit;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    31
import java.util.Arrays;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    32
import java.util.Collections;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    33
import java.util.List;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    34
import java.util.Locale;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    35
import java.util.Objects;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    36
import java.util.Properties;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    37
import java.util.logging.Level;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    38
import java.util.logging.LogManager;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    39
import java.util.logging.LogRecord;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    40
import java.util.logging.XMLFormatter;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    41
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    42
/**
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    43
 * @test
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    44
 * @bug 8072645
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    45
 * @summary tests that XmlFormatter will print out dates with the new
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    46
 *                nanosecond precision.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    47
 * @run main/othervm XmlFormatterNanos
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    48
 * @author danielfuchs
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    49
 */
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    50
public class XmlFormatterNanos {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    51
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    52
    static final int MILLIS_IN_SECOND = 1000;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    53
    static final int NANOS_IN_MILLI = 1000_000;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    54
    static final int NANOS_IN_MICRO = 1000;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    55
    static final int NANOS_IN_SECOND = 1000_000_000;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    56
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    57
    static final boolean verbose = true;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    58
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    59
    static final class TestAssertException extends RuntimeException {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    60
        TestAssertException(String msg) { super(msg); }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    61
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    62
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    63
    private static void assertEquals(long expected, long received, String msg) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    64
        if (expected != received) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    65
            throw new TestAssertException("Unexpected result for " + msg
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    66
                    + ".\n\texpected: " + expected
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    67
                    +  "\n\tactual:   " + received);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    68
        } else if (verbose) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    69
            System.out.println("Got expected " + msg + ": " + received);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    70
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    71
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    72
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    73
    private static void assertEquals(Object expected, Object received, String msg) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    74
        if (!Objects.equals(expected, received)) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    75
            throw new TestAssertException("Unexpected result for " + msg
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    76
                    + ".\n\texpected: " + expected
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    77
                    +  "\n\tactual:   " + received);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    78
        } else if (verbose) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    79
            System.out.println("Got expected " + msg + ": " + received);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    80
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    81
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    82
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    83
    static class CustomXMLFormatter extends XMLFormatter {}
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    84
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    85
    static class Configuration {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    86
        final Properties propertyFile;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    87
        private Configuration(Properties properties) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    88
            propertyFile = properties;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    89
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    90
        public Configuration apply() {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    91
            try {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    92
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    93
                propertyFile.store(bytes, testName());
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    94
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes.toByteArray());
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    95
                LogManager.getLogManager().readConfiguration(bais);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    96
            } catch (IOException ex) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    97
                throw new RuntimeException(ex);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    98
            }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
    99
            return this;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   100
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   101
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   102
        public static String useInstantProperty(Class<?> type) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   103
            return type.getName()+".useInstant";
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   104
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   105
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   106
        public boolean useInstant(XMLFormatter formatter) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   107
            return Boolean.parseBoolean(propertyFile.getProperty(
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   108
                    formatter.getClass().getName()+".useInstant", "true"));
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   109
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   110
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   111
        public String testName() {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   112
            return propertyFile.getProperty("test.name", "????");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   113
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   114
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   115
        public static Configuration of(Properties props) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   116
            return new Configuration(props);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   117
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   118
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   119
        public static Configuration apply(Properties props) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   120
            return of(props).apply();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   121
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   122
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   123
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   124
    final static List<Properties> properties;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   125
    static {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   126
        Properties props1 = new Properties();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   127
        props1.setProperty("test.name", "with XML nano element (default)");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   128
        Properties props2 = new Properties();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   129
        props2.setProperty("test.name", "with XML nano element (standard=true, custom=false)");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   130
        props2.setProperty(Configuration.useInstantProperty(XMLFormatter.class), "true");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   131
        props2.setProperty(Configuration.useInstantProperty(CustomXMLFormatter.class), "false");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   132
        Properties props3 = new Properties();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   133
        props3.setProperty("test.name", "with XML nano element (standard=false, custom=true)");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   134
        props3.setProperty(Configuration.useInstantProperty(XMLFormatter.class), "false");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   135
        props3.setProperty(Configuration.useInstantProperty(CustomXMLFormatter.class), "true");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   136
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   137
        properties = Collections.unmodifiableList(Arrays.asList(
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   138
                    props1,
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   139
                    props2));
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   140
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   141
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   142
    public static void main(String[] args) throws Exception {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   143
        Locale.setDefault(Locale.ENGLISH);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   144
        properties.stream().forEach(XmlFormatterNanos::test);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   145
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   146
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   147
    static int getNanoAdjustment(LogRecord record) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   148
        return record.getInstant().getNano() % NANOS_IN_MILLI;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   149
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   150
    static void setNanoAdjustment(LogRecord record, int nanos) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   151
        record.setInstant(Instant.ofEpochSecond(record.getInstant().getEpochSecond(),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   152
                (record.getInstant().getNano() / NANOS_IN_MILLI) * NANOS_IN_MILLI + nanos));
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   153
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   154
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   155
    public static void test(Properties props) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   156
        Configuration conf = Configuration.apply(props);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   157
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   158
        LogRecord record = new LogRecord(Level.INFO, "Test Name: {0}");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   159
        record.setLoggerName("test");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   160
        record.setParameters(new Object[] {conf.testName()});
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   161
        int nanos = record.getInstant().getNano() % NANOS_IN_MILLI;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   162
        long millis = record.getMillis();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   163
        // make sure we don't have leading zeros when printing below
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   164
        // the second precision
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   165
        if (millis % MILLIS_IN_SECOND < 100) millis = millis + 100;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   166
        // make sure we some nanos - and make sure we don't have
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   167
        // trailing zeros
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   168
        if (nanos % 10 == 0) nanos = nanos + 7;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   169
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   170
        record.setMillis(millis);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   171
        setNanoAdjustment(record, nanos);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   172
        final Instant instant = record.getInstant();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   173
        if (nanos < 0) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   174
            throw new RuntimeException("Unexpected negative nano adjustment: "
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   175
                    + getNanoAdjustment(record));
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   176
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   177
        if (nanos >= NANOS_IN_MILLI) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   178
            throw new RuntimeException("Nano adjustment exceeds 1ms: "
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   179
                    + getNanoAdjustment(record));
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   180
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   181
        if (millis != record.getMillis()) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   182
            throw new RuntimeException("Unexpected millis: " + millis + " != "
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   183
                    + record.getMillis());
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   184
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   185
        if (millis != record.getInstant().toEpochMilli()) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   186
            throw new RuntimeException("Unexpected millis: "
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   187
                    + record.getInstant().toEpochMilli());
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   188
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   189
        long expectedNanos = (millis % MILLIS_IN_SECOND) * NANOS_IN_MILLI + nanos;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   190
        assertEquals(expectedNanos, instant.getNano(), "Instant.getNano()");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   191
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   192
        XMLFormatter formatter = new XMLFormatter();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   193
        testMatching(formatter, record, instant, expectedNanos, conf.useInstant(formatter));
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   194
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   195
        XMLFormatter custom = new CustomXMLFormatter();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   196
        testMatching(custom, record, instant, expectedNanos, conf.useInstant(custom));
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   197
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   198
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   199
    private static void testMatching(XMLFormatter formatter,
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   200
            LogRecord record,  Instant instant, long expectedNanos,
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   201
            boolean useInstant) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   202
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   203
        ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   204
        int zdtNanos = zdt.getNano();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   205
        assertEquals(expectedNanos, zdtNanos, "ZonedDateTime.getNano()");
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   206
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   207
        String str = formatter.format(record);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   208
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   209
        String match = "."+expectedNanos;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   210
        if (str.contains(match) != useInstant) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   211
            throw new RuntimeException(formatter.getClass().getSimpleName()
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   212
                    + ".format()"
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   213
                    + " string does not contain expected nanos: "
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   214
                    + "\n\texpected match for: '" + match + "'"
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   215
                    + "\n\tin: \n" + str);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   216
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   217
        System.out.println("Found expected match for '"+match+"' in \n"+str);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   218
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   219
        match = "<millis>"+instant.toEpochMilli()+"</millis>";
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   220
        if (!str.contains(match)) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   221
            throw new RuntimeException(formatter.getClass().getSimpleName()
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   222
                    + ".format()"
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   223
                    + " string does not contain expected millis: "
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   224
                    + "\n\texpected match for: '" + match + "'"
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   225
                    + "\n\tin: \n" + str);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   226
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   227
        System.out.println("Found expected match for '"+match+"' in \n"+str);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   228
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   229
        match = "<nanos>";
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   230
        if (str.contains(match) != useInstant) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   231
            throw new RuntimeException(formatter.getClass().getSimpleName()
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   232
                    + ".format()"
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   233
                    + " string "
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   234
                    + (useInstant
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   235
                            ? "does not contain expected nanos: "
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   236
                            : "contains unexpected nanos: ")
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   237
                    + "\n\t" + (useInstant ? "expected" : "unexpected")
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   238
                    + " match for: '" + match + "'"
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   239
                    + "\n\tin: \n" + str);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   240
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   241
        match = "<nanos>"+getNanoAdjustment(record)+"</nanos>";
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   242
        if (str.contains(match) != useInstant) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   243
            throw new RuntimeException(formatter.getClass().getSimpleName()
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   244
                    + ".format()"
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   245
                    + " string "
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   246
                    + (useInstant
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   247
                            ? "does not contain expected nanos: "
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   248
                            : "contains unexpected nanos: ")
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   249
                    + "\n\t" + (useInstant ? "expected" : "unexpected")
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   250
                    + " match for: '" + match + "'"
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   251
                    + "\n\tin: \n" + str);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   252
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   253
        if (useInstant) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   254
            System.out.println("Found expected match for '"+match+"' in \n"+str);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   255
        } else {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   256
            System.out.println("As expected '"+match+"' is not present in \n"+str);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   257
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   258
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   259
        match = useInstant ? DateTimeFormatter.ISO_INSTANT.format(instant)
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   260
                : zdt.truncatedTo(ChronoUnit.SECONDS)
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   261
                        .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   262
        match = "<date>"+match+"</date>";
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   263
        if (!str.contains(match)) {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   264
            throw new RuntimeException(formatter.getClass().getSimpleName()
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   265
                    + ".format()"
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   266
                    + " string does not contain expected date: "
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   267
                    + "\n\texpected match for: '" + match + "'"
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   268
                    + "\n\tin: \n" + str);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   269
        }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   270
        System.out.println("Found expected match for '"+match+"' in \n"+str);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   271
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   272
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   273
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
diff changeset
   274
}