0N/A/*
3261N/A * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
2662N/A * @bug 4225317 6969651
0N/A * @summary Check extracted files have date as per those in the .jar file
0N/A */
0N/A
0N/Aimport java.io.File;
0N/Aimport java.io.PrintWriter;
0N/Aimport java.util.Date;
0N/Aimport sun.tools.jar.Main;
0N/A
0N/Apublic class JarEntryTime {
0N/A static boolean cleanup(File dir) throws Throwable {
0N/A boolean rc = true;
0N/A File[] x = dir.listFiles();
0N/A if (x != null) {
0N/A for (int i = 0; i < x.length; i++) {
0N/A rc &= x[i].delete();
0N/A }
0N/A }
0N/A return rc & dir.delete();
0N/A }
0N/A
0N/A static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {
0N/A String javahome = System.getProperty("java.home");
0N/A if (javahome.endsWith("jre")) {
0N/A javahome = javahome.substring(0, javahome.length() - 4);
0N/A }
0N/A String jarcmd = javahome + File.separator + "bin" + File.separator + "jar";
0N/A String[] args;
0N/A if (useExtractionTime) {
0N/A args = new String[] {
0N/A jarcmd,
0N/A "-J-Dsun.tools.jar.useExtractionTime=true",
0N/A "xf",
0N/A jarFile.getName() };
0N/A } else {
0N/A args = new String[] {
0N/A jarcmd,
0N/A "xf",
0N/A jarFile.getName() };
0N/A }
0N/A Process p = Runtime.getRuntime().exec(args);
0N/A check(p != null && (p.waitFor() == 0));
0N/A }
0N/A
0N/A public static void realMain(String[] args) throws Throwable {
0N/A
0N/A File dirOuter = new File("outer");
0N/A File dirInner = new File(dirOuter, "inner");
0N/A File jarFile = new File("JarEntryTime.jar");
0N/A
0N/A // Remove any leftovers from prior run
0N/A cleanup(dirInner);
0N/A cleanup(dirOuter);
0N/A jarFile.delete();
0N/A
0N/A /* Create a directory structure
0N/A * outer/
0N/A * inner/
0N/A * foo.txt
0N/A * Set the lastModified dates so that outer is created now, inner
0N/A * yesterday, and foo.txt created "earlier".
0N/A */
0N/A check(dirOuter.mkdir());
0N/A check(dirInner.mkdir());
0N/A File fileInner = new File(dirInner, "foo.txt");
0N/A PrintWriter pw = new PrintWriter(fileInner);
0N/A pw.println("hello, world");
0N/A pw.close();
2662N/A
2662N/A // Get the "now" from the "last-modified-time" of the last file we
2662N/A // just created, instead of the "System.currentTimeMillis()", to
2662N/A // workaround the possible "time difference" due to nfs.
2662N/A final long now = fileInner.lastModified();
2662N/A final long earlier = now - (60L * 60L * 6L * 1000L);
2662N/A final long yesterday = now - (60L * 60L * 24L * 1000L);
2662N/A // ZipEntry's mod date has 2 seconds precision: give extra time to
2662N/A // allow for e.g. rounding/truncation and networked/samba drives.
2662N/A final long PRECISION = 10000L;
2662N/A
0N/A dirOuter.setLastModified(now);
0N/A dirInner.setLastModified(yesterday);
0N/A fileInner.setLastModified(earlier);
0N/A
0N/A // Make a jar file from that directory structure
0N/A Main jartool = new Main(System.out, System.err, "jar");
0N/A check(jartool.run(new String[] {
0N/A "cf",
0N/A jarFile.getName(), dirOuter.getName() } ));
0N/A check(jarFile.exists());
0N/A
0N/A check(cleanup(dirInner));
0N/A check(cleanup(dirOuter));
0N/A
0N/A // Extract and check that the last modified values are those specified
0N/A // in the archive
0N/A extractJar(jarFile, false);
0N/A check(dirOuter.exists());
0N/A check(dirInner.exists());
0N/A check(fileInner.exists());
0N/A check(Math.abs(dirOuter.lastModified() - now) <= PRECISION);
0N/A check(Math.abs(dirInner.lastModified() - yesterday) <= PRECISION);
0N/A check(Math.abs(fileInner.lastModified() - earlier) <= PRECISION);
0N/A
0N/A check(cleanup(dirInner));
0N/A check(cleanup(dirOuter));
0N/A
0N/A // Extract and check the last modified values are the current times.
0N/A // See sun.tools.jar.Main
0N/A extractJar(jarFile, true);
0N/A check(dirOuter.exists());
0N/A check(dirInner.exists());
0N/A check(fileInner.exists());
0N/A check(Math.abs(dirOuter.lastModified() - now) <= PRECISION);
0N/A check(Math.abs(dirInner.lastModified() - now) <= PRECISION);
0N/A check(Math.abs(fileInner.lastModified() - now) <= PRECISION);
0N/A
0N/A check(cleanup(dirInner));
0N/A check(cleanup(dirOuter));
0N/A
0N/A check(jarFile.delete());
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
0N/A static volatile int passed = 0, failed = 0;
0N/A static void pass() {passed++;}
0N/A static void fail() {failed++; Thread.dumpStack();}
0N/A static void fail(String msg) {System.out.println(msg); fail();}
0N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
0N/A static void check(boolean cond) {if (cond) pass(); else fail();}
0N/A static void equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) pass();
0N/A else fail(x + " not equal to " + y);}
0N/A public static void main(String[] args) throws Throwable {
0N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
0N/A System.out.println("\nPassed = " + passed + " failed = " + failed);
0N/A if (failed > 0) throw new AssertionError("Some tests failed");}
0N/A}