jdk/test/tools/jar/JarEntryTime.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6308 7d860a571385
child 21811 4810bf82fa69
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6308
diff changeset
     2
 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
6308
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
    26
 * @bug 4225317 6969651
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check extracted files have date as per those in the .jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.PrintWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.tools.jar.Main;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class JarEntryTime {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static boolean cleanup(File dir) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        boolean rc = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        File[] x = dir.listFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            for (int i = 0; i < x.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                rc &= x[i].delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        return rc & dir.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        String javahome = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (javahome.endsWith("jre")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            javahome = javahome.substring(0, javahome.length() - 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        String jarcmd = javahome + File.separator + "bin" + File.separator + "jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        String[] args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (useExtractionTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            args = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                jarcmd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                "-J-Dsun.tools.jar.useExtractionTime=true",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                "xf",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                jarFile.getName() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            args = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                jarcmd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                "xf",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                jarFile.getName() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        Process p = Runtime.getRuntime().exec(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        check(p != null && (p.waitFor() == 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        File dirOuter = new File("outer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        File dirInner = new File(dirOuter, "inner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        File jarFile = new File("JarEntryTime.jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // Remove any leftovers from prior run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        cleanup(dirInner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        cleanup(dirOuter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        jarFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        /* Create a directory structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         * outer/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
         *     inner/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         *         foo.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         * Set the lastModified dates so that outer is created now, inner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * yesterday, and foo.txt created "earlier".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        check(dirOuter.mkdir());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        check(dirInner.mkdir());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        File fileInner = new File(dirInner, "foo.txt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        PrintWriter pw = new PrintWriter(fileInner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        pw.println("hello, world");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        pw.close();
6308
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
    94
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
    95
        // Get the "now" from the "last-modified-time" of the last file we
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
    96
        // just created, instead of the "System.currentTimeMillis()", to
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
    97
        // workaround the possible "time difference" due to nfs.
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
    98
        final long now = fileInner.lastModified();
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
    99
        final long earlier = now - (60L * 60L * 6L * 1000L);
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
   100
        final long yesterday = now - (60L * 60L * 24L * 1000L);
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
   101
        // ZipEntry's mod date has 2 seconds precision: give extra time to
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
   102
        // allow for e.g. rounding/truncation and networked/samba drives.
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
   103
        final long PRECISION = 10000L;
7d860a571385 6969651: TEST_BUG: tools/jar/JarEntryTime.java failed on JDK7 when run on NFS
sherman
parents: 5506
diff changeset
   104
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        dirOuter.setLastModified(now);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        dirInner.setLastModified(yesterday);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        fileInner.setLastModified(earlier);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Make a jar file from that directory structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Main jartool = new Main(System.out, System.err, "jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        check(jartool.run(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                "cf",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                jarFile.getName(), dirOuter.getName() } ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        check(jarFile.exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        check(cleanup(dirInner));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        check(cleanup(dirOuter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // Extract and check that the last modified values are those specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // in the archive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        extractJar(jarFile, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        check(dirOuter.exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        check(dirInner.exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        check(fileInner.exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        check(Math.abs(dirOuter.lastModified() - now) <= PRECISION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        check(Math.abs(dirInner.lastModified() - yesterday) <= PRECISION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        check(Math.abs(fileInner.lastModified() - earlier) <= PRECISION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        check(cleanup(dirInner));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        check(cleanup(dirOuter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // Extract and check the last modified values are the current times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // See sun.tools.jar.Main
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        extractJar(jarFile, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        check(dirOuter.exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        check(dirInner.exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        check(fileInner.exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        check(Math.abs(dirOuter.lastModified() - now) <= PRECISION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        check(Math.abs(dirInner.lastModified() - now) <= PRECISION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        check(Math.abs(fileInner.lastModified() - now) <= PRECISION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        check(cleanup(dirInner));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        check(cleanup(dirOuter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        check(jarFile.delete());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        System.out.println("\nPassed = " + passed + " failed = " + failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}