8146389: java/util/logging/XMLFormatterDate.java fails during year switch

Updates the test to match the changes in the XML content that were brought by JDK-8072645.

Reviewed-by: joehw
This commit is contained in:
Daniel Fuchs 2016-05-26 19:55:00 +02:00
parent e895469a87
commit c3435ed005

View File

@ -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 @@ import java.util.logging.XMLFormatter;
* @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 @@ public class XMLFormatterDate {
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);
}
}
}
}