jdk/test/java/util/logging/XMLFormatterDate.java
author malenkov
Fri, 16 May 2014 15:51:57 +0400
changeset 25088 8d4b058368f0
parent 21823 2100a5feec29
child 38573 4964706e8a45
permissions -rw-r--r--
8043152: KSS: javax.swing.plaf.synth.SynthContext Reviewed-by: alexsch, serb
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
/*
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     3
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     5
 *
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     8
 * published by the Free Software Foundation.
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
     9
 *
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    10
 * 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
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    13
 * 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
    14
 * accompanied this code).
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    15
 *
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    19
 *
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    22
 * questions.
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    23
 */
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    24
import java.util.Calendar;
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    25
import java.util.GregorianCalendar;
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    26
import java.util.Locale;
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    27
import java.util.logging.Level;
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    28
import java.util.logging.LogRecord;
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    29
import java.util.logging.XMLFormatter;
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    30
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    31
/**
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    32
 * @test
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    33
 * @bug 8028185
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    34
 * @summary XMLFormatter.format emits incorrect year (year + 1900)
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    35
 * @author dfuchs
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    36
 */
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    37
public class XMLFormatterDate {
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
     * Before the fix, JDK8 prints: {@code
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    41
     * <record>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    42
     *   <date>3913-11-18T17:35:40</date>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    43
     *   <millis>1384792540403</millis>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    44
     *   <sequence>0</sequence>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    45
     *   <level>INFO</level>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    46
     *   <thread>1</thread>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    47
     *   <message>test</message>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    48
     * </record>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    49
     * }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    50
     * After the fix, it should print: {@code
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    51
     * <record>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    52
     *   <date>2013-11-18T17:35:40</date>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    53
     *   <millis>1384792696519</millis>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    54
     *   <sequence>0</sequence>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    55
     *   <level>INFO</level>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    56
     *   <thread>1</thread>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    57
     *   <message>test</message>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    58
     * </record>
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    59
     * }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    60
     * @param args the command line arguments
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    61
     */
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    62
    public static void main(String[] args) {
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    63
        Locale locale = Locale.getDefault();
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    64
        try {
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    65
            Locale.setDefault(Locale.ENGLISH);
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    66
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    67
            final GregorianCalendar cal1 = new GregorianCalendar();
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    68
            final int year1 = cal1.get(Calendar.YEAR);
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    69
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    70
            LogRecord record = new LogRecord(Level.INFO, "test");
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    71
            XMLFormatter formatter = new XMLFormatter();
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    72
            final String formatted = formatter.format(record);
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    73
            System.out.println(formatted);
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    74
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    75
            final GregorianCalendar cal2 = new GregorianCalendar();
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    76
            final int year2 = cal2.get(Calendar.YEAR);
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    77
            if (year2 < 1900) {
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    78
                throw new Error("Invalid system year: " + year2);
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    79
            }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    80
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    81
            StringBuilder buf2 = new StringBuilder()
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    82
                    .append("<date>").append(year2).append("-");
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    83
            if (!formatted.contains(buf2.toString())) {
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    84
                StringBuilder buf1 = new StringBuilder()
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    85
                        .append("<date>").append(year1).append("-");
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    86
                if (formatted.contains(buf1)
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    87
                        && year2 == year1 + 1
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    88
                        && cal2.get(Calendar.MONTH) == Calendar.JANUARY
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    89
                        && cal2.get(Calendar.DAY_OF_MONTH) == 1) {
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    90
                    // Oh! The year just switched in the midst of the test...
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    91
                    System.out.println("Happy new year!");
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    92
                } else {
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    93
                    throw new Error("Expected year " + year2
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    94
                            + " not found in log:\n" + formatted);
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    95
                }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    96
            }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    97
        } finally {
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    98
            Locale.setDefault(locale);
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
    99
        }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
   100
    }
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
   101
2100a5feec29 8028185: XMLFormatter.format emits incorrect year
dfuchs
parents:
diff changeset
   102
}