Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/jar/JarEntryTime.java
47252 views
/*1* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4225317 696965126* @summary Check extracted files have date as per those in the .jar file27*/2829import java.io.File;30import java.io.PrintWriter;31import java.nio.file.attribute.FileTime;32import sun.tools.jar.Main;3334public class JarEntryTime {3536// ZipEntry's mod date has 2 seconds precision: give extra time to37// allow for e.g. rounding/truncation and networked/samba drives.38static final long PRECISION = 10000L;3940static boolean cleanup(File dir) throws Throwable {41boolean rc = true;42File[] x = dir.listFiles();43if (x != null) {44for (int i = 0; i < x.length; i++) {45rc &= x[i].delete();46}47}48return rc & dir.delete();49}5051static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {52String javahome = System.getProperty("java.home");53if (javahome.endsWith("jre")) {54javahome = javahome.substring(0, javahome.length() - 4);55}56String jarcmd = javahome + File.separator + "bin" + File.separator + "jar";57String[] args;58if (useExtractionTime) {59args = new String[] {60jarcmd,61"-J-Dsun.tools.jar.useExtractionTime=true",62"xf",63jarFile.getName() };64} else {65args = new String[] {66jarcmd,67"xf",68jarFile.getName() };69}70Process p = Runtime.getRuntime().exec(args);71check(p != null && (p.waitFor() == 0));72}7374public static void realMain(String[] args) throws Throwable {7576File dirOuter = new File("outer");77File dirInner = new File(dirOuter, "inner");78File jarFile = new File("JarEntryTime.jar");7980// Remove any leftovers from prior run81cleanup(dirInner);82cleanup(dirOuter);83jarFile.delete();8485/* Create a directory structure86* outer/87* inner/88* foo.txt89* Set the lastModified dates so that outer is created now, inner90* yesterday, and foo.txt created "earlier".91*/92check(dirOuter.mkdir());93check(dirInner.mkdir());94File fileInner = new File(dirInner, "foo.txt");95try (PrintWriter pw = new PrintWriter(fileInner)) {96pw.println("hello, world");97}9899// Get the "now" from the "last-modified-time" of the last file we100// just created, instead of the "System.currentTimeMillis()", to101// workaround the possible "time difference" due to nfs.102final long now = fileInner.lastModified();103final long earlier = now - (60L * 60L * 6L * 1000L);104final long yesterday = now - (60L * 60L * 24L * 1000L);105106check(dirOuter.setLastModified(now));107check(dirInner.setLastModified(yesterday));108check(fileInner.setLastModified(earlier));109110// Make a jar file from that directory structure111Main jartool = new Main(System.out, System.err, "jar");112check(jartool.run(new String[] {113"cf",114jarFile.getName(), dirOuter.getName() } ));115check(jarFile.exists());116117check(cleanup(dirInner));118check(cleanup(dirOuter));119120// Extract and check that the last modified values are those specified121// in the archive122extractJar(jarFile, false);123check(dirOuter.exists());124check(dirInner.exists());125check(fileInner.exists());126checkFileTime(dirOuter.lastModified(), now);127checkFileTime(dirInner.lastModified(), yesterday);128checkFileTime(fileInner.lastModified(), earlier);129130check(cleanup(dirInner));131check(cleanup(dirOuter));132133// Extract and check the last modified values are the current times.134// See sun.tools.jar.Main135extractJar(jarFile, true);136check(dirOuter.exists());137check(dirInner.exists());138check(fileInner.exists());139checkFileTime(dirOuter.lastModified(), now);140checkFileTime(dirInner.lastModified(), now);141checkFileTime(fileInner.lastModified(), now);142143check(cleanup(dirInner));144check(cleanup(dirOuter));145146check(jarFile.delete());147}148149static void checkFileTime(long now, long original) {150if (Math.abs(now - original) > PRECISION) {151System.out.format("Extracted to %s, expected to be close to %s%n",152FileTime.fromMillis(now), FileTime.fromMillis(original));153fail();154}155}156157//--------------------- Infrastructure ---------------------------158static volatile int passed = 0, failed = 0;159static void pass() {passed++;}160static void fail() {failed++; Thread.dumpStack();}161static void fail(String msg) {System.out.println(msg); fail();}162static void unexpected(Throwable t) {failed++; t.printStackTrace();}163static void check(boolean cond) {if (cond) pass(); else fail();}164static void equal(Object x, Object y) {165if (x == null ? y == null : x.equals(y)) pass();166else fail(x + " not equal to " + y);}167public static void main(String[] args) throws Throwable {168try {realMain(args);} catch (Throwable t) {unexpected(t);}169System.out.println("\nPassed = " + passed + " failed = " + failed);170if (failed > 0) throw new AssertionError("Some tests failed");}171}172173174