test/jdk/java/util/logging/XMLFormatterDate.java
author mikael
Mon, 29 Jul 2019 09:59:04 -0700
changeset 57584 9d82a35b6ff7
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21823
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     1
/*
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     4
 *
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     8
 *
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    14
 *
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    18
 *
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    21
 * questions.
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    22
 */
40684
2e37c119dc2a 8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents: 38573
diff changeset
    23
38573
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    24
import java.io.ByteArrayInputStream;
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    25
import java.io.ByteArrayOutputStream;
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    26
import java.io.IOException;
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    27
import java.time.Month;
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    28
import java.time.ZoneId;
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    29
import java.time.ZoneOffset;
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    30
import java.time.ZonedDateTime;
21823
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    31
import java.util.Locale;
38573
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    32
import java.util.Properties;
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    33
import java.util.function.Supplier;
21823
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    34
import java.util.logging.Level;
38573
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    35
import java.util.logging.LogManager;
21823
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    36
import java.util.logging.LogRecord;
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    37
import java.util.logging.XMLFormatter;
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    38
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    39
/**
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    40
 * @test
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    41
 * @bug 8028185
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    42
 * @summary XMLFormatter.format emits incorrect year (year + 1900)
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    43
 * @author dfuchs
38573
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    44
 * @run main/othervm XMLFormatterDate
21823
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    45
 */
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    46
public class XMLFormatterDate {
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    47
38573
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    48
    static final class TimeStamp {
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    49
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    50
        final ZonedDateTime zdt;
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    51
        TimeStamp(ZoneId zoneId) {
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    52
            zdt = ZonedDateTime.now(zoneId);
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    53
        }
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    54
        int getYear() {
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    55
            return zdt.getYear();
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    56
        }
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    57
        boolean isJanuaryFirst() {
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    58
            return zdt.getMonth() == Month.JANUARY && zdt.getDayOfMonth() == 1;
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    59
        }
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    60
    }
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    61
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    62
21823
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    63
    /**
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    64
     * Before the fix, JDK8 prints: {@code
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    65
     * <record>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    66
     *   <date>3913-11-18T17:35:40</date>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    67
     *   <millis>1384792540403</millis>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    68
     *   <sequence>0</sequence>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    69
     *   <level>INFO</level>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    70
     *   <thread>1</thread>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    71
     *   <message>test</message>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    72
     * </record>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    73
     * }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    74
     * After the fix, it should print: {@code
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    75
     * <record>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    76
     *   <date>2013-11-18T17:35:40</date>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    77
     *   <millis>1384792696519</millis>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    78
     *   <sequence>0</sequence>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    79
     *   <level>INFO</level>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    80
     *   <thread>1</thread>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    81
     *   <message>test</message>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    82
     * </record>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    83
     * }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    84
     * @param args the command line arguments
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    85
     */
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    86
    public static void main(String[] args) {
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    87
        Locale locale = Locale.getDefault();
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    88
        try {
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    89
            Locale.setDefault(Locale.ENGLISH);
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    90
38573
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    91
            // Test with default format: by default date is in UTC.
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    92
            System.out.println("Testing with UTC");
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    93
            test(() -> new TimeStamp(ZoneOffset.UTC));
21823
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    94
38573
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    95
            // Change LogManager configuration so that new
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    96
            // XMLFormatter prints date in the pre Java 9 local zone format
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    97
            try {
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    98
                Properties props = new Properties();
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
    99
                props.setProperty("java.util.logging.XMLFormatter.useInstant", "false");
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   100
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   101
                props.store(baos, "");
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   102
                ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   103
                LogManager.getLogManager().updateConfiguration(bais, (k) -> (o,n) -> n!=null?n:o);
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   104
            } catch (IOException io) {
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   105
                throw new RuntimeException(io);
21823
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
   106
            }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
   107
38573
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   108
            // re test with the old format: date will be in the local time zone.
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   109
            System.out.println("Testing with old format");
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   110
            test(() -> new TimeStamp(ZoneId.systemDefault()));
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   111
21823
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
   112
        } finally {
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
   113
            Locale.setDefault(locale);
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
   114
        }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
   115
    }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
   116
38573
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   117
    static void test(Supplier<TimeStamp> timeStampSupplier) {
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   118
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   119
        TimeStamp t1 = timeStampSupplier.get();
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   120
        int year1 = t1.getYear();
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   121
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   122
        LogRecord record = new LogRecord(Level.INFO, "test");
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   123
        XMLFormatter formatter = new XMLFormatter();
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   124
        final String formatted = formatter.format(record);
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   125
        System.out.println(formatted);
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   126
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   127
        final TimeStamp t2 = timeStampSupplier.get();
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   128
        final int year2 = t2.getYear();
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   129
        if (year2 < 1900) {
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   130
            throw new Error("Invalid system year: " + year2);
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   131
        }
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   132
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   133
        final StringBuilder buf2 = new StringBuilder()
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   134
                .append("<date>").append(year2).append("-");
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   135
        if (!formatted.contains(buf2.toString())) {
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   136
            StringBuilder buf1 = new StringBuilder()
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   137
                    .append("<date>").append(year1).append("-");
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   138
            if (formatted.contains(buf1) && year2 == year1 + 1
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   139
                    && t2.isJanuaryFirst()) {
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   140
                // Oh! The year just switched in the midst of the test...
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   141
                System.out.println("Happy new year!");
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   142
            } else {
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   143
                throw new Error("Expected year " + year2
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   144
                        + " not found in log:\n" + formatted);
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   145
            }
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   146
        }
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   147
    }
4964706e8a45 8146389: java/util/logging/XMLFormatterDate.java fails during year switch
dfuchs
parents: 21823
diff changeset
   148
21823
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
   149
}