test/tools/jar/JarEntryTime.java

Print this page




  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  * @summary Check extracted files have date as per those in the .jar file
  28  */
  29 
  30 import java.io.File;
  31 import java.io.PrintWriter;
  32 import java.util.Date;
  33 import sun.tools.jar.Main;
  34 
  35 public class JarEntryTime {





  36     static boolean cleanup(File dir) throws Throwable {
  37         boolean rc = true;
  38         File[] x = dir.listFiles();
  39         if (x != null) {
  40             for (int i = 0; i < x.length; i++) {
  41                 rc &= x[i].delete();
  42             }
  43         }
  44         return rc & dir.delete();
  45     }
  46 
  47     static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {
  48         String javahome = System.getProperty("java.home");
  49         if (javahome.endsWith("jre")) {
  50             javahome = javahome.substring(0, javahome.length() - 4);
  51         }
  52         String jarcmd = javahome + File.separator + "bin" + File.separator + "jar";
  53         String[] args;
  54         if (useExtractionTime) {
  55             args = new String[] {


  71 
  72         File dirOuter = new File("outer");
  73         File dirInner = new File(dirOuter, "inner");
  74         File jarFile = new File("JarEntryTime.jar");
  75 
  76         // Remove any leftovers from prior run
  77         cleanup(dirInner);
  78         cleanup(dirOuter);
  79         jarFile.delete();
  80 
  81         /* Create a directory structure
  82          * outer/
  83          *     inner/
  84          *         foo.txt
  85          * Set the lastModified dates so that outer is created now, inner
  86          * yesterday, and foo.txt created "earlier".
  87          */
  88         check(dirOuter.mkdir());
  89         check(dirInner.mkdir());
  90         File fileInner = new File(dirInner, "foo.txt");
  91         PrintWriter pw = new PrintWriter(fileInner);
  92         pw.println("hello, world");
  93         pw.close();
  94 
  95         // Get the "now" from the "last-modified-time" of the last file we
  96         // just created, instead of the "System.currentTimeMillis()", to
  97         // workaround the possible "time difference" due to nfs.
  98         final long now = fileInner.lastModified();
  99         final long earlier = now - (60L * 60L * 6L * 1000L);
 100         final long yesterday = now - (60L * 60L * 24L * 1000L);
 101         // ZipEntry's mod date has 2 seconds precision: give extra time to
 102         // allow for e.g. rounding/truncation and networked/samba drives.
 103         final long PRECISION = 10000L;
 104 
 105         dirOuter.setLastModified(now);
 106         dirInner.setLastModified(yesterday);
 107         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         check(Math.abs(dirOuter.lastModified() - now) <= PRECISION);
 126         check(Math.abs(dirInner.lastModified() - yesterday) <= PRECISION);
 127         check(Math.abs(fileInner.lastModified() - earlier) <= PRECISION);
 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         check(Math.abs(dirOuter.lastModified() - now) <= PRECISION);
 139         check(Math.abs(dirInner.lastModified() - now) <= PRECISION);
 140         check(Math.abs(fileInner.lastModified() - now) <= PRECISION);
 141 
 142         check(cleanup(dirInner));
 143         check(cleanup(dirOuter));
 144 
 145         check(jarFile.delete());








 146     }
 147 
 148     //--------------------- Infrastructure ---------------------------
 149     static volatile int passed = 0, failed = 0;
 150     static void pass() {passed++;}
 151     static void fail() {failed++; Thread.dumpStack();}
 152     static void fail(String msg) {System.out.println(msg); fail();}
 153     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 154     static void check(boolean cond) {if (cond) pass(); else fail();}
 155     static void equal(Object x, Object y) {
 156         if (x == null ? y == null : x.equals(y)) pass();
 157         else fail(x + " not equal to " + y);}
 158     public static void main(String[] args) throws Throwable {
 159         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 160         System.out.println("\nPassed = " + passed + " failed = " + failed);
 161         if (failed > 0) throw new AssertionError("Some tests failed");}
 162 }


  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  * @summary Check extracted files have date as per those in the .jar file
  28  */
  29 
  30 import java.io.File;
  31 import java.io.PrintWriter;
  32 import java.nio.file.attribute.FileTime;
  33 import sun.tools.jar.Main;
  34 
  35 public class JarEntryTime {
  36 
  37     // ZipEntry's mod date has 2 seconds precision: give extra time to
  38     // allow for e.g. rounding/truncation and networked/samba drives.
  39     static final long PRECISION = 10000L;
  40 
  41     static boolean cleanup(File dir) throws Throwable {
  42         boolean rc = true;
  43         File[] x = dir.listFiles();
  44         if (x != null) {
  45             for (int i = 0; i < x.length; i++) {
  46                 rc &= x[i].delete();
  47             }
  48         }
  49         return rc & dir.delete();
  50     }
  51 
  52     static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {
  53         String javahome = System.getProperty("java.home");
  54         if (javahome.endsWith("jre")) {
  55             javahome = javahome.substring(0, javahome.length() - 4);
  56         }
  57         String jarcmd = javahome + File.separator + "bin" + File.separator + "jar";
  58         String[] args;
  59         if (useExtractionTime) {
  60             args = new String[] {


  76 
  77         File dirOuter = new File("outer");
  78         File dirInner = new File(dirOuter, "inner");
  79         File jarFile = new File("JarEntryTime.jar");
  80 
  81         // Remove any leftovers from prior run
  82         cleanup(dirInner);
  83         cleanup(dirOuter);
  84         jarFile.delete();
  85 
  86         /* Create a directory structure
  87          * outer/
  88          *     inner/
  89          *         foo.txt
  90          * Set the lastModified dates so that outer is created now, inner
  91          * yesterday, and foo.txt created "earlier".
  92          */
  93         check(dirOuter.mkdir());
  94         check(dirInner.mkdir());
  95         File fileInner = new File(dirInner, "foo.txt");
  96         try (PrintWriter pw = new PrintWriter(fileInner)) {
  97             pw.println("hello, world");
  98         }
  99 
 100         // Get the "now" from the "last-modified-time" of the last file we
 101         // just created, instead of the "System.currentTimeMillis()", to
 102         // workaround the possible "time difference" due to nfs.
 103         final long now = fileInner.lastModified();
 104         final long earlier = now - (60L * 60L * 6L * 1000L);
 105         final long yesterday = now - (60L * 60L * 24L * 1000L);



 106 
 107         check(dirOuter.setLastModified(now));
 108         check(dirInner.setLastModified(yesterday));
 109         check(fileInner.setLastModified(earlier));
 110 
 111         // Make a jar file from that directory structure
 112         Main jartool = new Main(System.out, System.err, "jar");
 113         check(jartool.run(new String[] {
 114                 "cf",
 115                 jarFile.getName(), dirOuter.getName() } ));
 116         check(jarFile.exists());
 117 
 118         check(cleanup(dirInner));
 119         check(cleanup(dirOuter));
 120 
 121         // Extract and check that the last modified values are those specified
 122         // in the archive
 123         extractJar(jarFile, false);
 124         check(dirOuter.exists());
 125         check(dirInner.exists());
 126         check(fileInner.exists());
 127         checkFileTime(dirOuter.lastModified(), now);
 128         checkFileTime(dirInner.lastModified(), yesterday);
 129         checkFileTime(fileInner.lastModified(), earlier);
 130 
 131         check(cleanup(dirInner));
 132         check(cleanup(dirOuter));
 133 
 134         // Extract and check the last modified values are the current times.
 135         // See sun.tools.jar.Main
 136         extractJar(jarFile, true);
 137         check(dirOuter.exists());
 138         check(dirInner.exists());
 139         check(fileInner.exists());
 140         checkFileTime(dirOuter.lastModified(), now);
 141         checkFileTime(dirInner.lastModified(), now);
 142         checkFileTime(fileInner.lastModified(), now);
 143 
 144         check(cleanup(dirInner));
 145         check(cleanup(dirOuter));
 146 
 147         check(jarFile.delete());
 148     }
 149 
 150     static void checkFileTime(long now, long original) {
 151         if (Math.abs(now - original) > PRECISION) {
 152             System.out.format("Extracted to %s, expected to be close to %s%n",
 153                 FileTime.fromMillis(now), FileTime.fromMillis(original));
 154             fail();
 155         }
 156     }
 157 
 158     //--------------------- Infrastructure ---------------------------
 159     static volatile int passed = 0, failed = 0;
 160     static void pass() {passed++;}
 161     static void fail() {failed++; Thread.dumpStack();}
 162     static void fail(String msg) {System.out.println(msg); fail();}
 163     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 164     static void check(boolean cond) {if (cond) pass(); else fail();}
 165     static void equal(Object x, Object y) {
 166         if (x == null ? y == null : x.equals(y)) pass();
 167         else fail(x + " not equal to " + y);}
 168     public static void main(String[] args) throws Throwable {
 169         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 170         System.out.println("\nPassed = " + passed + " failed = " + failed);
 171         if (failed > 0) throw new AssertionError("Some tests failed");}
 172 }