Path: blob/master/test/jdk/java/io/File/SetLastModified.java
41149 views
/*1* Copyright (c) 1998, 2019, 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@requires os.maxMemory >= 16G26@summary Basic test for setLastModified method27*/2829import java.io.*;30import java.nio.ByteBuffer;31import java.nio.channels.FileChannel;323334public class SetLastModified {3536private static void ck(File f, long nt, long rt) throws Exception {37if (rt == nt) return;38if ((rt / 10 == nt / 10)39|| (rt / 100 == nt / 100)40|| (rt / 1000 == nt / 1000)41|| (rt / 10000 == (nt / 10000))) {42System.err.println(f + ": Time set to " + nt43+ ", rounded down by filesystem to " + rt);44return;45}46if ((rt / 10 == (nt + 5) / 10)47|| (rt / 100 == (nt + 50) / 100)48|| (rt / 1000 == (nt + 500) / 1000)49|| (rt / 10000 == ((nt + 5000) / 10000))) {50System.err.println(f + ": Time set to " + nt51+ ", rounded up by filesystem to " + rt);52return;53}54throw new Exception(f + ": Time set to " + nt55+ ", then read as " + rt);56}5758public static void main(String[] args) throws Exception {59File d = new File(System.getProperty("test.dir", "."));60File d2 = new File(d, "x.SetLastModified.dir");61File f = new File(d2, "x.SetLastModified");62long ot, t;6364/* New time: One week ago */65long nt = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7;6667if (f.exists()) f.delete();68if (d2.exists()) d2.delete();69if (!d2.mkdir()) {70throw new Exception("Can't create test directory " + d2);71}7273boolean threw = false;74try {75d2.setLastModified(-nt);76} catch (IllegalArgumentException x) {77threw = true;78}79if (!threw)80throw new Exception("setLastModified succeeded with a negative time");8182ot = d2.lastModified();83if (ot != 0) {84if (d2.setLastModified(nt)) {85ck(d2, nt, d2.lastModified());86d2.setLastModified(ot);87} else {88System.err.println("Warning: setLastModified on directories "89+ "not supported");90}91}9293if (f.exists()) {94if (!f.delete())95throw new Exception("Can't delete test file " + f);96}97if (f.setLastModified(nt))98throw new Exception("Succeeded on non-existent file: " + f);99100// set/check last modified on files of size 1, 1GB+1, 2GB+1, ..101// On Windows we only test with a tiny file as that platform doesn't102// support sparse files by default and so the test takes too long.103final long G = 1024L * 1024L * 1024L;104final long MAX_POSITION =105System.getProperty("os.name").startsWith("Windows") ? 0L : 3L*G;106long pos = 0L;107while (pos <= MAX_POSITION) {108try (FileChannel fc = new FileOutputStream(f).getChannel()) {109fc.position(pos).write(ByteBuffer.wrap("x".getBytes()));110}111ot = f.lastModified();112System.out.format("check with file size: %d\n", f.length());113if (!f.setLastModified(nt))114throw new Exception("setLastModified failed on file: " + f);115ck(f, nt, f.lastModified());116pos += G;117}118119if (!f.delete()) throw new Exception("Can't delete test file " + f);120if (!d2.delete()) throw new Exception("Can't delete test directory " + d2);121}122123}124125126