/* * Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * @test * @bug 4225317 6969651 8277422 * @modules jdk.jartool * @summary Check extracted files have date as per those in the .jar file
*/
publicclass JarEntryTime { staticfinal ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
.orElseThrow(() -> new RuntimeException("jar tool not found")
);
// ZipEntry's mod date has 2 seconds precision: give extra time to // allow for e.g. rounding/truncation and networked/samba drives. staticfinallong PRECISION = 10000L;
File dirOuter = new File("outer");
File dirInner = new File(dirOuter, "inner");
File jarFile = new File("JarEntryTime.jar");
File testFile = new File("JarEntryTimeTest.txt");
// Remove any leftovers from prior run
cleanup(dirInner);
cleanup(dirOuter);
jarFile.delete();
testFile.delete();
var date = new Date(); var defZone = ZoneId.systemDefault(); if (defZone.getRules().getTransition(
date.toInstant().atZone(defZone).toLocalDateTime()) != null) {
System.out.println("At the offset transition. JarEntryTime test skipped."); return;
}
/* Create a directory structure * outer/ * inner/ * foo.txt * Set the lastModified dates so that outer is created now, inner * yesterday, and foo.txt created "earlier".
*/
check(dirOuter.mkdir());
check(dirInner.mkdir());
File fileInner = new File(dirInner, "foo.txt"); try (PrintWriter pw = new PrintWriter(fileInner)) {
pw.println("hello, world");
}
// Get the "now" from the "last-modified-time" of the last file we // just created, instead of the "System.currentTimeMillis()", to // workaround the possible "time difference" due to nfs. finallong now = fileInner.lastModified(); finallong earlier = now - (60L * 60L * 6L * 1000L); finallong yesterday = now - (60L * 60L * 24L * 1000L);
// Make a jar file from that directory structure
check(JAR_TOOL.run(System.out, System.err, "cf", jarFile.getName(), dirOuter.getName()) == 0);
check(jarFile.exists());
// Extract and check that the last modified values are those specified // in the archive
extractJar(jarFile, false);
check(dirOuter.exists());
check(dirInner.exists());
check(fileInner.exists());
checkFileTime(dirOuter.lastModified(), now);
checkFileTime(dirInner.lastModified(), yesterday);
checkFileTime(fileInner.lastModified(), earlier);
staticvoid checkFileTime(long now, long original) { if (isTimeSettingChanged()) { return;
}
if (Math.abs(now - original) > PRECISION) {
System.out.format("Extracted to %s, expected to be close to %s%n",
FileTime.fromMillis(now), FileTime.fromMillis(original));
fail();
}
}
staticvoid checkFileTime(long start, long now, long end) { if (isTimeSettingChanged()) { return;
}
if (now < start || now > end) {
System.out.format("Extracted to %s, "
+ "expected to be after %s and before %s%n",
FileTime.fromMillis(now),
FileTime.fromMillis(start),
FileTime.fromMillis(end));
fail();
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.