8038330: tools/jar/JarEntryTime.java fails intermittently on checking extracted file last modified values are the current times
Reviewed-by: sherman, plevart
--- a/jdk/test/tools/jar/JarEntryTime.java Thu Mar 03 15:47:08 2016 -0800
+++ b/jdk/test/tools/jar/JarEntryTime.java Fri Mar 04 13:59:43 2016 +0800
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,8 @@
import java.io.File;
import java.io.PrintWriter;
import java.nio.file.attribute.FileTime;
+import java.util.Date;
+import java.util.TimeZone;
import sun.tools.jar.Main;
public class JarEntryTime {
@@ -39,6 +41,9 @@
// allow for e.g. rounding/truncation and networked/samba drives.
static final long PRECISION = 10000L;
+ static final TimeZone TZ = TimeZone.getDefault();
+ static final boolean DST = TZ.inDaylightTime(new Date());
+
static boolean cleanup(File dir) throws Throwable {
boolean rc = true;
File[] x = dir.listFiles();
@@ -75,11 +80,13 @@
File dirOuter = new File("outer");
File dirInner = new File(dirOuter, "inner");
File jarFile = new File("JarEntryTime.jar");
+ File testFile = new File("JarEntryTimeTest.txt");
// Remove any leftovers from prior run
cleanup(dirInner);
cleanup(dirOuter);
jarFile.delete();
+ testFile.delete();
/* Create a directory structure
* outer/
@@ -129,23 +136,39 @@
check(cleanup(dirInner));
check(cleanup(dirOuter));
+ try (PrintWriter pw = new PrintWriter(testFile)) {
+ pw.println("hello, world");
+ }
+ final long start = testFile.lastModified();
+
// Extract and check the last modified values are the current times.
// See sun.tools.jar.Main
extractJar(jarFile, true);
+
+ try (PrintWriter pw = new PrintWriter(testFile)) {
+ pw.println("hello, world");
+ }
+ final long end = testFile.lastModified();
+
check(dirOuter.exists());
check(dirInner.exists());
check(fileInner.exists());
- checkFileTime(dirOuter.lastModified(), now);
- checkFileTime(dirInner.lastModified(), now);
- checkFileTime(fileInner.lastModified(), now);
+ checkFileTime(start, dirOuter.lastModified(), end);
+ checkFileTime(start, dirInner.lastModified(), end);
+ checkFileTime(start, fileInner.lastModified(), end);
check(cleanup(dirInner));
check(cleanup(dirOuter));
check(jarFile.delete());
+ check(testFile.delete());
}
static void checkFileTime(long now, long original) {
+ if (isTimeSettingChanged()) {
+ return;
+ }
+
if (Math.abs(now - original) > PRECISION) {
System.out.format("Extracted to %s, expected to be close to %s%n",
FileTime.fromMillis(now), FileTime.fromMillis(original));
@@ -153,6 +176,27 @@
}
}
+ static void checkFileTime(long start, long now, long end) {
+ if (isTimeSettingChanged()) {
+ return;
+ }
+
+ if (now < start || now > end) {
+ System.out.format("Extracted to %s, "
+ + "expected to be after %s and before %s%n",
+ FileTime.fromMillis(now),
+ FileTime.fromMillis(start),
+ FileTime.fromMillis(end));
+ fail();
+ }
+ }
+
+ private static boolean isTimeSettingChanged() {
+ TimeZone currentTZ = TimeZone.getDefault();
+ boolean currentDST = currentTZ.inDaylightTime(new Date());
+ return (!currentTZ.equals(TZ) || currentDST != DST);
+ }
+
//--------------------- Infrastructure ---------------------------
static volatile int passed = 0, failed = 0;
static void pass() {passed++;}