< prev index next >

test/tools/jar/JarEntryTime.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2006, 2014, 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. --- 1,7 ---- /* ! * Copyright (c) 2006, 2016, 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.
*** 29,46 **** --- 29,51 ---- */ import java.io.File; import java.io.PrintWriter; import java.nio.file.attribute.FileTime; + import java.util.Date; + import java.util.TimeZone; import sun.tools.jar.Main; public class JarEntryTime { // ZipEntry's mod date has 2 seconds precision: give extra time to // allow for e.g. rounding/truncation and networked/samba drives. static final long PRECISION = 10000L; + static final TimeZone TZ = TimeZone.getDefault(); + static final boolean DST = TZ.inDaylightTime(new Date()); + static boolean cleanup(File dir) throws Throwable { boolean rc = true; File[] x = dir.listFiles(); if (x != null) { for (int i = 0; i < x.length; i++) {
*** 73,87 **** --- 78,94 ---- public static void realMain(String[] args) throws Throwable { 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(); /* Create a directory structure * outer/ * inner/ * foo.txt
*** 127,160 **** checkFileTime(fileInner.lastModified(), earlier); check(cleanup(dirInner)); check(cleanup(dirOuter)); // Extract and check the last modified values are the current times. // See sun.tools.jar.Main extractJar(jarFile, true); check(dirOuter.exists()); check(dirInner.exists()); check(fileInner.exists()); ! checkFileTime(dirOuter.lastModified(), now); ! checkFileTime(dirInner.lastModified(), now); ! checkFileTime(fileInner.lastModified(), now); check(cleanup(dirInner)); check(cleanup(dirOuter)); check(jarFile.delete()); } static void checkFileTime(long now, long original) { 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(); } } //--------------------- Infrastructure --------------------------- static volatile int passed = 0, failed = 0; static void pass() {passed++;} static void fail() {failed++; Thread.dumpStack();} static void fail(String msg) {System.out.println(msg); fail();} --- 134,204 ---- checkFileTime(fileInner.lastModified(), earlier); check(cleanup(dirInner)); check(cleanup(dirOuter)); + try (PrintWriter pw = new PrintWriter(testFile)) { + pw.println("hello, world"); + } + final long start = testFile.lastModified(); + // Extract and check the last modified values are the current times. // See sun.tools.jar.Main extractJar(jarFile, true); + + try (PrintWriter pw = new PrintWriter(testFile)) { + pw.println("hello, world"); + } + final long end = testFile.lastModified(); + check(dirOuter.exists()); check(dirInner.exists()); check(fileInner.exists()); ! checkFileTime(start, dirOuter.lastModified(), end); ! checkFileTime(start, dirInner.lastModified(), end); ! checkFileTime(start, fileInner.lastModified(), end); check(cleanup(dirInner)); check(cleanup(dirOuter)); check(jarFile.delete()); + check(testFile.delete()); } static void 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(); } } + static void checkFileTime(long start, long now, long end) { + if (isTimeSettingChanged()) { + return; + } + + if (!(start <= now && 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(); + } + } + + private static boolean isTimeSettingChanged() { + TimeZone currentTZ = TimeZone.getDefault(); + boolean currentDST = currentTZ.inDaylightTime(new Date()); + return (!currentTZ.equals(TZ) || currentDST != DST); + } + //--------------------- Infrastructure --------------------------- static volatile int passed = 0, failed = 0; static void pass() {passed++;} static void fail() {failed++; Thread.dumpStack();} static void fail(String msg) {System.out.println(msg); fail();}
< prev index next >