jdk/test/java/util/logging/XMLFormatterDate.java
changeset 38573 4964706e8a45
parent 21823 2100a5feec29
child 40684 2e37c119dc2a
--- a/jdk/test/java/util/logging/XMLFormatterDate.java	Thu May 26 17:19:13 2016 +0000
+++ b/jdk/test/java/util/logging/XMLFormatterDate.java	Thu May 26 19:55:00 2016 +0200
@@ -21,10 +21,18 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-import java.util.Calendar;
-import java.util.GregorianCalendar;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.time.Month;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
 import java.util.Locale;
+import java.util.Properties;
+import java.util.function.Supplier;
 import java.util.logging.Level;
+import java.util.logging.LogManager;
 import java.util.logging.LogRecord;
 import java.util.logging.XMLFormatter;
 
@@ -33,9 +41,25 @@
  * @bug 8028185
  * @summary XMLFormatter.format emits incorrect year (year + 1900)
  * @author dfuchs
+ * @run main/othervm XMLFormatterDate
  */
 public class XMLFormatterDate {
 
+    static final class TimeStamp {
+
+        final ZonedDateTime zdt;
+        TimeStamp(ZoneId zoneId) {
+            zdt = ZonedDateTime.now(zoneId);
+        }
+        int getYear() {
+            return zdt.getYear();
+        }
+        boolean isJanuaryFirst() {
+            return zdt.getMonth() == Month.JANUARY && zdt.getDayOfMonth() == 1;
+        }
+    }
+
+
     /**
      * Before the fix, JDK8 prints: {@code
      * <record>
@@ -64,39 +88,62 @@
         try {
             Locale.setDefault(Locale.ENGLISH);
 
-            final GregorianCalendar cal1 = new GregorianCalendar();
-            final int year1 = cal1.get(Calendar.YEAR);
+            // Test with default format: by default date is in UTC.
+            System.out.println("Testing with UTC");
+            test(() -> new TimeStamp(ZoneOffset.UTC));
 
-            LogRecord record = new LogRecord(Level.INFO, "test");
-            XMLFormatter formatter = new XMLFormatter();
-            final String formatted = formatter.format(record);
-            System.out.println(formatted);
-
-            final GregorianCalendar cal2 = new GregorianCalendar();
-            final int year2 = cal2.get(Calendar.YEAR);
-            if (year2 < 1900) {
-                throw new Error("Invalid system year: " + year2);
+            // Change LogManager configuration so that new
+            // XMLFormatter prints date in the pre Java 9 local zone format
+            try {
+                Properties props = new Properties();
+                props.setProperty("java.util.logging.XMLFormatter.useInstant", "false");
+                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                props.store(baos, "");
+                ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+                LogManager.getLogManager().updateConfiguration(bais, (k) -> (o,n) -> n!=null?n:o);
+            } catch (IOException io) {
+                throw new RuntimeException(io);
             }
 
-            StringBuilder buf2 = new StringBuilder()
-                    .append("<date>").append(year2).append("-");
-            if (!formatted.contains(buf2.toString())) {
-                StringBuilder buf1 = new StringBuilder()
-                        .append("<date>").append(year1).append("-");
-                if (formatted.contains(buf1)
-                        && year2 == year1 + 1
-                        && cal2.get(Calendar.MONTH) == Calendar.JANUARY
-                        && cal2.get(Calendar.DAY_OF_MONTH) == 1) {
-                    // Oh! The year just switched in the midst of the test...
-                    System.out.println("Happy new year!");
-                } else {
-                    throw new Error("Expected year " + year2
-                            + " not found in log:\n" + formatted);
-                }
-            }
+            // re test with the old format: date will be in the local time zone.
+            System.out.println("Testing with old format");
+            test(() -> new TimeStamp(ZoneId.systemDefault()));
+
         } finally {
             Locale.setDefault(locale);
         }
     }
 
+    static void test(Supplier<TimeStamp> timeStampSupplier) {
+
+        TimeStamp t1 = timeStampSupplier.get();
+        int year1 = t1.getYear();
+
+        LogRecord record = new LogRecord(Level.INFO, "test");
+        XMLFormatter formatter = new XMLFormatter();
+        final String formatted = formatter.format(record);
+        System.out.println(formatted);
+
+        final TimeStamp t2 = timeStampSupplier.get();
+        final int year2 = t2.getYear();
+        if (year2 < 1900) {
+            throw new Error("Invalid system year: " + year2);
+        }
+
+        final StringBuilder buf2 = new StringBuilder()
+                .append("<date>").append(year2).append("-");
+        if (!formatted.contains(buf2.toString())) {
+            StringBuilder buf1 = new StringBuilder()
+                    .append("<date>").append(year1).append("-");
+            if (formatted.contains(buf1) && year2 == year1 + 1
+                    && t2.isJanuaryFirst()) {
+                // Oh! The year just switched in the midst of the test...
+                System.out.println("Happy new year!");
+            } else {
+                throw new Error("Expected year " + year2
+                        + " not found in log:\n" + formatted);
+            }
+        }
+    }
+
 }