提交 6bb7a48a 编写于 作者: A alanb

8028589: Instrument tools/jar/JarEntryTime.java to make it easier to diagnose failures

Reviewed-by: chegar
上级 37b4280b
...@@ -29,10 +29,15 @@ ...@@ -29,10 +29,15 @@
import java.io.File; import java.io.File;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Date; import java.nio.file.attribute.FileTime;
import sun.tools.jar.Main; import sun.tools.jar.Main;
public class JarEntryTime { public class JarEntryTime {
// ZipEntry's mod date has 2 seconds precision: give extra time to
// allow for e.g. rounding/truncation and networked/samba drives.
static final long PRECISION = 10000L;
static boolean cleanup(File dir) throws Throwable { static boolean cleanup(File dir) throws Throwable {
boolean rc = true; boolean rc = true;
File[] x = dir.listFiles(); File[] x = dir.listFiles();
...@@ -88,9 +93,9 @@ public class JarEntryTime { ...@@ -88,9 +93,9 @@ public class JarEntryTime {
check(dirOuter.mkdir()); check(dirOuter.mkdir());
check(dirInner.mkdir()); check(dirInner.mkdir());
File fileInner = new File(dirInner, "foo.txt"); File fileInner = new File(dirInner, "foo.txt");
PrintWriter pw = new PrintWriter(fileInner); try (PrintWriter pw = new PrintWriter(fileInner)) {
pw.println("hello, world"); pw.println("hello, world");
pw.close(); }
// Get the "now" from the "last-modified-time" of the last file we // Get the "now" from the "last-modified-time" of the last file we
// just created, instead of the "System.currentTimeMillis()", to // just created, instead of the "System.currentTimeMillis()", to
...@@ -98,13 +103,10 @@ public class JarEntryTime { ...@@ -98,13 +103,10 @@ public class JarEntryTime {
final long now = fileInner.lastModified(); final long now = fileInner.lastModified();
final long earlier = now - (60L * 60L * 6L * 1000L); final long earlier = now - (60L * 60L * 6L * 1000L);
final long yesterday = now - (60L * 60L * 24L * 1000L); final long yesterday = now - (60L * 60L * 24L * 1000L);
// ZipEntry's mod date has 2 seconds precision: give extra time to
// allow for e.g. rounding/truncation and networked/samba drives.
final long PRECISION = 10000L;
dirOuter.setLastModified(now); check(dirOuter.setLastModified(now));
dirInner.setLastModified(yesterday); check(dirInner.setLastModified(yesterday));
fileInner.setLastModified(earlier); check(fileInner.setLastModified(earlier));
// Make a jar file from that directory structure // Make a jar file from that directory structure
Main jartool = new Main(System.out, System.err, "jar"); Main jartool = new Main(System.out, System.err, "jar");
...@@ -122,9 +124,9 @@ public class JarEntryTime { ...@@ -122,9 +124,9 @@ public class JarEntryTime {
check(dirOuter.exists()); check(dirOuter.exists());
check(dirInner.exists()); check(dirInner.exists());
check(fileInner.exists()); check(fileInner.exists());
check(Math.abs(dirOuter.lastModified() - now) <= PRECISION); checkFileTime(dirOuter.lastModified(), now);
check(Math.abs(dirInner.lastModified() - yesterday) <= PRECISION); checkFileTime(dirInner.lastModified(), yesterday);
check(Math.abs(fileInner.lastModified() - earlier) <= PRECISION); checkFileTime(fileInner.lastModified(), earlier);
check(cleanup(dirInner)); check(cleanup(dirInner));
check(cleanup(dirOuter)); check(cleanup(dirOuter));
...@@ -135,9 +137,9 @@ public class JarEntryTime { ...@@ -135,9 +137,9 @@ public class JarEntryTime {
check(dirOuter.exists()); check(dirOuter.exists());
check(dirInner.exists()); check(dirInner.exists());
check(fileInner.exists()); check(fileInner.exists());
check(Math.abs(dirOuter.lastModified() - now) <= PRECISION); checkFileTime(dirOuter.lastModified(), now);
check(Math.abs(dirInner.lastModified() - now) <= PRECISION); checkFileTime(dirInner.lastModified(), now);
check(Math.abs(fileInner.lastModified() - now) <= PRECISION); checkFileTime(fileInner.lastModified(), now);
check(cleanup(dirInner)); check(cleanup(dirInner));
check(cleanup(dirOuter)); check(cleanup(dirOuter));
...@@ -145,6 +147,14 @@ public class JarEntryTime { ...@@ -145,6 +147,14 @@ public class JarEntryTime {
check(jarFile.delete()); check(jarFile.delete());
} }
static void checkFileTime(long now, long original) {
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));
fail();
}
}
//--------------------- Infrastructure --------------------------- //--------------------- Infrastructure ---------------------------
static volatile int passed = 0, failed = 0; static volatile int passed = 0, failed = 0;
static void pass() {passed++;} static void pass() {passed++;}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册