Path: blob/master/test/jdk/java/nio/file/Files/SymlinkTime.java
41153 views
/*1* Copyright (c) 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 822079325* @summary Unit test for updating access and modification times of symlinks26* @requires (os.family == "linux" | os.family == "mac" | os.family == "windows")27* @library ..28* @build SymlinkTime29* @run main/othervm SymlinkTime30*/3132import java.io.IOException;33import java.nio.file.Files;34import java.nio.file.LinkOption;35import java.nio.file.Path;36import java.nio.file.attribute.BasicFileAttributes;37import java.nio.file.attribute.BasicFileAttributeView;38import java.nio.file.attribute.FileTime;3940public class SymlinkTime {41public static void main(String[] args) throws IOException {42Path dir = TestUtil.createTemporaryDirectory();43if (!TestUtil.supportsLinks(dir)) {44System.out.println("Links not supported: skipping test");45return;46}4748try {49// Create file and symbolic link to it50final Path file = dir.resolve("file");51final Path link = dir.resolve("link");52Files.createFile(file);53try {54// Delay creating the link to get different time attributes55Thread.currentThread().sleep(5000);56} catch (InterruptedException ignored) {57}58Files.createSymbolicLink(link, file);5960// Save file modification and access times61BasicFileAttributeView view = Files.getFileAttributeView(link,62BasicFileAttributeView.class);63BasicFileAttributes attr = view.readAttributes();64printTimes("Original file times", attr);65FileTime fileModTime = attr.lastModifiedTime();66FileTime fileAccTime = attr.lastAccessTime();6768// Read link modification and access times69view = Files.getFileAttributeView(link,70BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);71attr = view.readAttributes();72printTimes("Original link times", attr);7374// Set new base time and offset increment75long base = 1000000000000L; // 2001-09-09T01:46:40Z76long delta = 1000*60L;7778// Set new link modification and access times79FileTime linkModTime = FileTime.fromMillis(base + delta);80FileTime linkAccTime = FileTime.fromMillis(base + 2L*delta);81view.setTimes(linkModTime, linkAccTime, null);8283// Verify link modification and access times updated correctly84view = Files.getFileAttributeView(link,85BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);86attr = view.readAttributes();87printTimes("Updated link times", attr);88check("Link", attr, linkModTime, linkAccTime);8990// Verify file modification and access times unchanged91view = Files.getFileAttributeView(file,92BasicFileAttributeView.class);93attr = view.readAttributes();94printTimes("File times", attr);95check("File", attr, fileModTime, fileAccTime);96} finally {97TestUtil.removeAll(dir);98}99}100101private static void check(String pathType, BasicFileAttributes attr,102FileTime modTimeExpected, FileTime accTimeExpected) {103if (!attr.lastModifiedTime().equals(modTimeExpected) ||104!attr.lastAccessTime().equals(accTimeExpected)) {105String message = String.format(106"%s - modification time: expected %s, actual %s;%n" +107"access time: expected %s, actual %s.%n", pathType,108modTimeExpected, attr.lastModifiedTime(),109accTimeExpected, attr.lastAccessTime());110throw new RuntimeException(message);111}112}113114private static void printTimes(String label, BasicFileAttributes attr) {115System.out.format116("%s%ncreation: %s%nmodification: %s%naccess: %s%n%n",117label, attr.creationTime(), attr.lastModifiedTime(),118attr.lastAccessTime());119}120}121122123