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