Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/File/SetLastModified.java
38811 views
/*1* Copyright (c) 1998, 2017, 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/* @test24@bug 4091757 6652379 817780925@summary Basic test for setLastModified method26*/2728import java.io.*;29import java.nio.ByteBuffer;30import java.nio.channels.FileChannel;313233public class SetLastModified {3435private static void ck(File f, long nt, long rt) throws Exception {36if (rt == nt) return;37if ((rt / 10 == nt / 10)38|| (rt / 100 == nt / 100)39|| (rt / 1000 == nt / 1000)40|| (rt / 10000 == (nt / 10000))) {41System.err.println(f + ": Time set to " + nt42+ ", rounded down by filesystem to " + rt);43return;44}45if ((rt / 10 == (nt + 5) / 10)46|| (rt / 100 == (nt + 50) / 100)47|| (rt / 1000 == (nt + 500) / 1000)48|| (rt / 10000 == ((nt + 5000) / 10000))) {49System.err.println(f + ": Time set to " + nt50+ ", rounded up by filesystem to " + rt);51return;52}53throw new Exception(f + ": Time set to " + nt54+ ", then read as " + rt);55}5657public static void main(String[] args) throws Exception {58File d = new File(System.getProperty("test.dir", "."));59File d2 = new File(d, "x.SetLastModified.dir");60File f = new File(d2, "x.SetLastModified");61long ot, t;6263/* New time: One week ago */64long nt = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7;6566if (f.exists()) f.delete();67if (d2.exists()) d2.delete();68if (!d2.mkdir()) {69throw new Exception("Can't create test directory " + d2);70}7172boolean threw = false;73try {74d2.setLastModified(-nt);75} catch (IllegalArgumentException x) {76threw = true;77}78if (!threw)79throw new Exception("setLastModified succeeded with a negative time");8081ot = d2.lastModified();82if (ot != 0) {83if (d2.setLastModified(nt)) {84ck(d2, nt, d2.lastModified());85d2.setLastModified(ot);86} else {87System.err.println("Warning: setLastModified on directories "88+ "not supported");89}90}9192if (f.exists()) {93if (!f.delete())94throw new Exception("Can't delete test file " + f);95}96if (f.setLastModified(nt))97throw new Exception("Succeeded on non-existent file: " + f);9899// set/check last modified on files of size 1, 1GB+1, 2GB+1, ..100// On Windows we only test with a tiny file as that platform doesn't101// support sparse files by default and so the test takes too long.102final long G = 1024L * 1024L * 1024L;103final long MAX_POSITION =104System.getProperty("os.name").startsWith("Windows") ? 0L : 3L*G;105long pos = 0L;106while (pos <= MAX_POSITION) {107try (FileChannel fc = new FileOutputStream(f).getChannel()) {108fc.position(pos).write(ByteBuffer.wrap("x".getBytes()));109}110ot = f.lastModified();111System.out.format("check with file size: %d\n", f.length());112if (!f.setLastModified(nt))113throw new Exception("setLastModified failed on file: " + f);114ck(f, nt, f.lastModified());115pos += G;116}117118if (!f.delete()) throw new Exception("Can't delete test file " + f);119if (!d2.delete()) throw new Exception("Can't delete test directory " + d2);120}121122}123124125