< prev index next >

test/tools/jar/JarEntryTime.java

Print this page


   1 /*
   2  * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4225317 6969651
  27  * @modules jdk.jartool/sun.tools.jar
  28  * @summary Check extracted files have date as per those in the .jar file
  29  */
  30 
  31 import java.io.File;
  32 import java.io.PrintWriter;
  33 import java.nio.file.attribute.FileTime;


  34 import sun.tools.jar.Main;
  35 
  36 public class JarEntryTime {
  37 
  38     // ZipEntry's mod date has 2 seconds precision: give extra time to
  39     // allow for e.g. rounding/truncation and networked/samba drives.
  40     static final long PRECISION = 10000L;
  41 



  42     static boolean cleanup(File dir) throws Throwable {
  43         boolean rc = true;
  44         File[] x = dir.listFiles();
  45         if (x != null) {
  46             for (int i = 0; i < x.length; i++) {
  47                 rc &= x[i].delete();
  48             }
  49         }
  50         return rc & dir.delete();
  51     }
  52 
  53     static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {
  54         String javahome = System.getProperty("java.home");
  55         String jarcmd = javahome + File.separator + "bin" + File.separator + "jar";
  56         String[] args;
  57         if (useExtractionTime) {
  58             args = new String[] {
  59                 jarcmd,
  60                 "-J-Dsun.tools.jar.useExtractionTime=true",
  61                 "xf",
  62                 jarFile.getName() };
  63         } else {
  64             args = new String[] {
  65                 jarcmd,
  66                 "xf",
  67                 jarFile.getName() };
  68         }
  69         Process p = Runtime.getRuntime().exec(args);
  70         check(p != null && (p.waitFor() == 0));
  71     }
  72 
  73     public static void realMain(String[] args) throws Throwable {
  74 
  75         File dirOuter = new File("outer");
  76         File dirInner = new File(dirOuter, "inner");
  77         File jarFile = new File("JarEntryTime.jar");


  78 
  79         // Remove any leftovers from prior run
  80         cleanup(dirInner);
  81         cleanup(dirOuter);

  82         jarFile.delete();
  83 
  84         /* Create a directory structure
  85          * outer/
  86          *     inner/
  87          *         foo.txt
  88          * Set the lastModified dates so that outer is created now, inner
  89          * yesterday, and foo.txt created "earlier".
  90          */
  91         check(dirOuter.mkdir());
  92         check(dirInner.mkdir());
  93         File fileInner = new File(dirInner, "foo.txt");
  94         try (PrintWriter pw = new PrintWriter(fileInner)) {
  95             pw.println("hello, world");
  96         }

  97 
  98         // Get the "now" from the "last-modified-time" of the last file we
  99         // just created, instead of the "System.currentTimeMillis()", to
 100         // workaround the possible "time difference" due to nfs.
 101         final long now = fileInner.lastModified();
 102         final long earlier = now - (60L * 60L * 6L * 1000L);
 103         final long yesterday = now - (60L * 60L * 24L * 1000L);
 104 
 105         check(dirOuter.setLastModified(now));
 106         check(dirInner.setLastModified(yesterday));
 107         check(fileInner.setLastModified(earlier));
 108 
 109         // Make a jar file from that directory structure
 110         Main jartool = new Main(System.out, System.err, "jar");
 111         check(jartool.run(new String[] {
 112                 "cf",
 113                 jarFile.getName(), dirOuter.getName() } ));
 114         check(jarFile.exists());
 115 
 116         check(cleanup(dirInner));
 117         check(cleanup(dirOuter));
 118 
 119         // Extract and check that the last modified values are those specified
 120         // in the archive
 121         extractJar(jarFile, false);
 122         check(dirOuter.exists());
 123         check(dirInner.exists());
 124         check(fileInner.exists());
 125         checkFileTime(dirOuter.lastModified(), now);
 126         checkFileTime(dirInner.lastModified(), yesterday);
 127         checkFileTime(fileInner.lastModified(), earlier);
 128 
 129         check(cleanup(dirInner));
 130         check(cleanup(dirOuter));
 131 





 132         // Extract and check the last modified values are the current times.
 133         // See sun.tools.jar.Main
 134         extractJar(jarFile, true);






 135         check(dirOuter.exists());
 136         check(dirInner.exists());
 137         check(fileInner.exists());
 138         checkFileTime(dirOuter.lastModified(), now);
 139         checkFileTime(dirInner.lastModified(), now);
 140         checkFileTime(fileInner.lastModified(), now);
 141 
 142         check(cleanup(dirInner));
 143         check(cleanup(dirOuter));

 144 
 145         check(jarFile.delete());
 146     }
 147 
 148     static void checkFileTime(long now, long original) {
 149         if (Math.abs(now - original) > PRECISION) {
 150             System.out.format("Extracted to %s, expected to be close to %s%n",
 151                 FileTime.fromMillis(now), FileTime.fromMillis(original));











 152             fail();
 153         }
 154     }
 155 






 156     //--------------------- Infrastructure ---------------------------
 157     static volatile int passed = 0, failed = 0;
 158     static void pass() {passed++;}
 159     static void fail() {failed++; Thread.dumpStack();}
 160     static void fail(String msg) {System.out.println(msg); fail();}
 161     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 162     static void check(boolean cond) {if (cond) pass(); else fail();}
 163     static void equal(Object x, Object y) {
 164         if (x == null ? y == null : x.equals(y)) pass();
 165         else fail(x + " not equal to " + y);}
 166     public static void main(String[] args) throws Throwable {
 167         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 168         System.out.println("\nPassed = " + passed + " failed = " + failed);
 169         if (failed > 0) throw new AssertionError("Some tests failed");}
 170 }
   1 /*
   2  * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4225317 6969651
  27  * @modules jdk.jartool/sun.tools.jar
  28  * @summary Check extracted files have date as per those in the .jar file
  29  */
  30 
  31 import java.io.File;
  32 import java.io.PrintWriter;
  33 import java.nio.file.attribute.FileTime;
  34 import java.util.Date;
  35 import java.util.TimeZone;
  36 import sun.tools.jar.Main;
  37 
  38 public class JarEntryTime {
  39 
  40     // ZipEntry's mod date has 2 seconds precision: give extra time to
  41     // allow for e.g. rounding/truncation and networked/samba drives.
  42     static final long PRECISION = 10000L;
  43 
  44     static final TimeZone TZ = TimeZone.getDefault();
  45     static final boolean DST = TZ.inDaylightTime(new Date());
  46 
  47     static boolean cleanup(File dir) throws Throwable {
  48         boolean rc = true;
  49         File[] x = dir.listFiles();
  50         if (x != null) {
  51             for (int i = 0; i < x.length; i++) {
  52                 rc &= x[i].delete();
  53             }
  54         }
  55         return rc & dir.delete();
  56     }
  57 
  58     static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {
  59         String javahome = System.getProperty("java.home");
  60         String jarcmd = javahome + File.separator + "bin" + File.separator + "jar";
  61         String[] args;
  62         if (useExtractionTime) {
  63             args = new String[] {
  64                 jarcmd,
  65                 "-J-Dsun.tools.jar.useExtractionTime=true",
  66                 "xf",
  67                 jarFile.getName() };
  68         } else {
  69             args = new String[] {
  70                 jarcmd,
  71                 "xf",
  72                 jarFile.getName() };
  73         }
  74         Process p = Runtime.getRuntime().exec(args);
  75         check(p != null && (p.waitFor() == 0));
  76     }
  77 
  78     public static void realMain(String[] args) throws Throwable {
  79 
  80         File dirOuter = new File("outer");
  81         File dirInner = new File(dirOuter, "inner");
  82         File jarFile = new File("JarEntryTime.jar");
  83         File dirTemp = new File("tempdir");
  84         File fileTemp = new File(dirTemp, "tempfile.txt");
  85 
  86         // Remove any leftovers from prior run
  87         cleanup(dirInner);
  88         cleanup(dirOuter);
  89         cleanup(dirTemp);
  90         jarFile.delete();
  91 
  92         /* Create a directory structure
  93          * outer/
  94          *     inner/
  95          *         foo.txt
  96          * Set the lastModified dates so that outer is created now, inner
  97          * yesterday, and foo.txt created "earlier".
  98          */
  99         check(dirOuter.mkdir());
 100         check(dirInner.mkdir());
 101         File fileInner = new File(dirInner, "foo.txt");
 102         try (PrintWriter pw = new PrintWriter(fileInner)) {
 103             pw.println("hello, world");
 104         }
 105         check(dirTemp.mkdir());
 106 
 107         // Get the "now" from the "last-modified-time" of the last file we
 108         // just created, instead of the "System.currentTimeMillis()", to
 109         // workaround the possible "time difference" due to nfs.
 110         final long now = fileInner.lastModified();
 111         final long earlier = now - (60L * 60L * 6L * 1000L);
 112         final long yesterday = now - (60L * 60L * 24L * 1000L);
 113 
 114         check(dirOuter.setLastModified(now));
 115         check(dirInner.setLastModified(yesterday));
 116         check(fileInner.setLastModified(earlier));
 117 
 118         // Make a jar file from that directory structure
 119         Main jartool = new Main(System.out, System.err, "jar");
 120         check(jartool.run(new String[] {
 121                 "cf",
 122                 jarFile.getName(), dirOuter.getName() } ));
 123         check(jarFile.exists());
 124 
 125         check(cleanup(dirInner));
 126         check(cleanup(dirOuter));
 127 
 128         // Extract and check that the last modified values are those specified
 129         // in the archive
 130         extractJar(jarFile, false);
 131         check(dirOuter.exists());
 132         check(dirInner.exists());
 133         check(fileInner.exists());
 134         checkFileTime(dirOuter.lastModified(), now);
 135         checkFileTime(dirInner.lastModified(), yesterday);
 136         checkFileTime(fileInner.lastModified(), earlier);
 137 
 138         check(cleanup(dirInner));
 139         check(cleanup(dirOuter));
 140 
 141         try (PrintWriter pw = new PrintWriter(fileTemp)) {
 142             pw.println("hello, world");
 143         }
 144         final long start = fileTemp.lastModified();
 145 
 146         // Extract and check the last modified values are the current times.
 147         // See sun.tools.jar.Main
 148         extractJar(jarFile, true);
 149 
 150         try (PrintWriter pw = new PrintWriter(fileTemp)) {
 151             pw.println("hello, world");
 152         }
 153         final long end = fileTemp.lastModified();
 154 
 155         check(dirOuter.exists());
 156         check(dirInner.exists());
 157         check(fileInner.exists());
 158         checkFileTime(start, dirOuter.lastModified(), end);
 159         checkFileTime(start, dirInner.lastModified(), end);
 160         checkFileTime(start, fileInner.lastModified(), end);
 161 
 162         check(cleanup(dirInner));
 163         check(cleanup(dirOuter));
 164         check(cleanup(dirTemp));
 165 
 166         check(jarFile.delete());
 167     }
 168 
 169     static void checkFileTime(long now, long original) {
 170         checkFileTime(now, now, original + PRECISION);
 171     }
 172 
 173     static void checkFileTime(long start, long now, long end) {
 174         if (isTimeSettingChanged()) {
 175             return;
 176         }
 177 
 178         if (!(Math.abs(now - start) >= 0L && Math.abs(end - now) >= 0L)) {
 179             System.out.format("Extracted to %s, "
 180                               + "expected to be after %s and before %s%n",
 181                               FileTime.fromMillis(now), 
 182                               FileTime.fromMillis(start),
 183                               FileTime.fromMillis(end));
 184             fail();
 185         }
 186     }
 187 
 188     private static boolean isTimeSettingChanged() {
 189         TimeZone currentTZ = TimeZone.getDefault();
 190         boolean currentDST = currentTZ.inDaylightTime(new Date());
 191         return (!currentTZ.equals(TZ) || currentDST != DST);
 192     }
 193 
 194     //--------------------- Infrastructure ---------------------------
 195     static volatile int passed = 0, failed = 0;
 196     static void pass() {passed++;}
 197     static void fail() {failed++; Thread.dumpStack();}
 198     static void fail(String msg) {System.out.println(msg); fail();}
 199     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 200     static void check(boolean cond) {if (cond) pass(); else fail();}
 201     static void equal(Object x, Object y) {
 202         if (x == null ? y == null : x.equals(y)) pass();
 203         else fail(x + " not equal to " + y);}
 204     public static void main(String[] args) throws Throwable {
 205         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 206         System.out.println("\nPassed = " + passed + " failed = " + failed);
 207         if (failed > 0) throw new AssertionError("Some tests failed");}
 208 }
< prev index next >