< prev index next >

test/tools/jar/JarEntryTime.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -29,18 +29,23 @@
  */
 
 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 {
 
     // 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 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();
         if (x != null) {
             for (int i = 0; i < x.length; i++) {

@@ -73,14 +78,17 @@
     public static void realMain(String[] args) throws Throwable {
 
         File dirOuter = new File("outer");
         File dirInner = new File(dirOuter, "inner");
         File jarFile = new File("JarEntryTime.jar");
+        File dirTemp = new File("tempdir");
+        File fileTemp = new File(dirTemp, "tempfile.txt");
 
         // Remove any leftovers from prior run
         cleanup(dirInner);
         cleanup(dirOuter);
+        cleanup(dirTemp);
         jarFile.delete();
 
         /* Create a directory structure
          * outer/
          *     inner/

@@ -92,10 +100,11 @@
         check(dirInner.mkdir());
         File fileInner = new File(dirInner, "foo.txt");
         try (PrintWriter pw = new PrintWriter(fileInner)) {
             pw.println("hello, world");
         }
+        check(dirTemp.mkdir());
 
         // Get the "now" from the "last-modified-time" of the last file we
         // just created, instead of the "System.currentTimeMillis()", to
         // workaround the possible "time difference" due to nfs.
         final long now = fileInner.lastModified();

@@ -127,34 +136,63 @@
         checkFileTime(fileInner.lastModified(), earlier);
 
         check(cleanup(dirInner));
         check(cleanup(dirOuter));
 
+        try (PrintWriter pw = new PrintWriter(fileTemp)) {
+            pw.println("hello, world");
+        }
+        final long start = fileTemp.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(fileTemp)) {
+            pw.println("hello, world");
+        }
+        final long end = fileTemp.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(cleanup(dirTemp));
 
         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));
+        checkFileTime(now, now, original + PRECISION);
+    }
+
+    static void checkFileTime(long start, long now, long end) {
+        if (isTimeSettingChanged()) {
+            return;
+        }
+
+        if (!(Math.abs(now - start) >= 0L && Math.abs(end - now) >= 0L)) {
+            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++;}
     static void fail() {failed++; Thread.dumpStack();}
     static void fail(String msg) {System.out.println(msg); fail();}
< prev index next >