jdk/test/tools/jar/JarEntryTime.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 4225317
       
    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[] {
       
    56                 jarcmd,
       
    57                 "-J-Dsun.tools.jar.useExtractionTime=true",
       
    58                 "xf",
       
    59                 jarFile.getName() };
       
    60         } else {
       
    61             args = new String[] {
       
    62                 jarcmd,
       
    63                 "xf",
       
    64                 jarFile.getName() };
       
    65         }
       
    66         Process p = Runtime.getRuntime().exec(args);
       
    67         check(p != null && (p.waitFor() == 0));
       
    68     }
       
    69 
       
    70     public static void realMain(String[] args) throws Throwable {
       
    71         final long now = System.currentTimeMillis();
       
    72         final long earlier = now - (60L * 60L * 6L * 1000L);
       
    73         final long yesterday = now - (60L * 60L * 24L * 1000L);
       
    74 
       
    75         // ZipEntry's mod date has 2 seconds precision: give extra time to
       
    76         // allow for e.g. rounding/truncation and networked/samba drives.
       
    77         final long PRECISION = 10000L;
       
    78 
       
    79         File dirOuter = new File("outer");
       
    80         File dirInner = new File(dirOuter, "inner");
       
    81 
       
    82         File jarFile = new File("JarEntryTime.jar");
       
    83 
       
    84         // Remove any leftovers from prior run
       
    85         cleanup(dirInner);
       
    86         cleanup(dirOuter);
       
    87         jarFile.delete();
       
    88 
       
    89         /* Create a directory structure
       
    90          * outer/
       
    91          *     inner/
       
    92          *         foo.txt
       
    93          * Set the lastModified dates so that outer is created now, inner
       
    94          * yesterday, and foo.txt created "earlier".
       
    95          */
       
    96         check(dirOuter.mkdir());
       
    97         check(dirInner.mkdir());
       
    98         File fileInner = new File(dirInner, "foo.txt");
       
    99         PrintWriter pw = new PrintWriter(fileInner);
       
   100         pw.println("hello, world");
       
   101         pw.close();
       
   102         dirOuter.setLastModified(now);
       
   103         dirInner.setLastModified(yesterday);
       
   104         fileInner.setLastModified(earlier);
       
   105 
       
   106         // Make a jar file from that directory structure
       
   107         Main jartool = new Main(System.out, System.err, "jar");
       
   108         check(jartool.run(new String[] {
       
   109                 "cf",
       
   110                 jarFile.getName(), dirOuter.getName() } ));
       
   111         check(jarFile.exists());
       
   112 
       
   113         check(cleanup(dirInner));
       
   114         check(cleanup(dirOuter));
       
   115 
       
   116         // Extract and check that the last modified values are those specified
       
   117         // in the archive
       
   118         extractJar(jarFile, false);
       
   119         check(dirOuter.exists());
       
   120         check(dirInner.exists());
       
   121         check(fileInner.exists());
       
   122         check(Math.abs(dirOuter.lastModified() - now) <= PRECISION);
       
   123         check(Math.abs(dirInner.lastModified() - yesterday) <= PRECISION);
       
   124         check(Math.abs(fileInner.lastModified() - earlier) <= PRECISION);
       
   125 
       
   126         check(cleanup(dirInner));
       
   127         check(cleanup(dirOuter));
       
   128 
       
   129         // Extract and check the last modified values are the current times.
       
   130         // See sun.tools.jar.Main
       
   131         extractJar(jarFile, true);
       
   132         check(dirOuter.exists());
       
   133         check(dirInner.exists());
       
   134         check(fileInner.exists());
       
   135         check(Math.abs(dirOuter.lastModified() - now) <= PRECISION);
       
   136         check(Math.abs(dirInner.lastModified() - now) <= PRECISION);
       
   137         check(Math.abs(fileInner.lastModified() - now) <= PRECISION);
       
   138 
       
   139         check(cleanup(dirInner));
       
   140         check(cleanup(dirOuter));
       
   141 
       
   142         check(jarFile.delete());
       
   143     }
       
   144 
       
   145     //--------------------- Infrastructure ---------------------------
       
   146     static volatile int passed = 0, failed = 0;
       
   147     static void pass() {passed++;}
       
   148     static void fail() {failed++; Thread.dumpStack();}
       
   149     static void fail(String msg) {System.out.println(msg); fail();}
       
   150     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
       
   151     static void check(boolean cond) {if (cond) pass(); else fail();}
       
   152     static void equal(Object x, Object y) {
       
   153         if (x == null ? y == null : x.equals(y)) pass();
       
   154         else fail(x + " not equal to " + y);}
       
   155     public static void main(String[] args) throws Throwable {
       
   156         try {realMain(args);} catch (Throwable t) {unexpected(t);}
       
   157         System.out.println("\nPassed = " + passed + " failed = " + failed);
       
   158         if (failed > 0) throw new AssertionError("Some tests failed");}
       
   159 }