Path: blob/master/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java
41155 views
/*1* Copyright (c) 2013, 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 801153625* @summary Basic test for creationTime attribute on platforms/file systems26* that support it.27* @library ../..28*/2930import java.nio.file.Path;31import java.nio.file.Files;32import java.nio.file.attribute.*;33import java.time.Instant;34import java.io.IOException;3536public class CreationTime {3738private static final java.io.PrintStream err = System.err;3940/**41* Reads the creationTime attribute42*/43private static FileTime creationTime(Path file) throws IOException {44return Files.readAttributes(file, BasicFileAttributes.class).creationTime();45}4647/**48* Sets the creationTime attribute49*/50private static void setCreationTime(Path file, FileTime time) throws IOException {51BasicFileAttributeView view =52Files.getFileAttributeView(file, BasicFileAttributeView.class);53view.setTimes(null, null, time);54}5556static void test(Path top) throws IOException {57Path file = Files.createFile(top.resolve("foo"));5859/**60* Check that creationTime reported61*/62FileTime creationTime = creationTime(file);63Instant now = Instant.now();64if (Math.abs(creationTime.toMillis()-now.toEpochMilli()) > 10000L) {65err.println("File creation time reported as: " + creationTime);66throw new RuntimeException("Expected to be close to: " + now);67}6869/**70* Is the creationTime attribute supported here?71*/72boolean supportsCreationTimeRead = false;73boolean supportsCreationTimeWrite = false;74String os = System.getProperty("os.name");75if (os.contains("OS X") && Files.getFileStore(file).type().equals("hfs")) {76supportsCreationTimeRead = true;77} else if (os.startsWith("Windows")) {78String type = Files.getFileStore(file).type();79if (type.equals("NTFS") || type.equals("FAT")) {80supportsCreationTimeRead = true;81supportsCreationTimeWrite = true;82}83}8485/**86* If the creation-time attribute is supported then change the file's87* last modified and check that it doesn't change the creation-time.88*/89if (supportsCreationTimeRead) {90// change modified time by +1 hour91Instant plusHour = Instant.now().plusSeconds(60L * 60L);92Files.setLastModifiedTime(file, FileTime.from(plusHour));93FileTime current = creationTime(file);94if (!current.equals(creationTime))95throw new RuntimeException("Creation time should not have changed");96}9798/**99* If the creation-time attribute is supported and can be changed then100* check that the change is effective.101*/102if (supportsCreationTimeWrite) {103// change creation time by -1 hour104Instant minusHour = Instant.now().minusSeconds(60L * 60L);105creationTime = FileTime.from(minusHour);106setCreationTime(file, creationTime);107FileTime current = creationTime(file);108if (Math.abs(creationTime.toMillis()-current.toMillis()) > 1000L)109throw new RuntimeException("Creation time not changed");110}111}112113public static void main(String[] args) throws IOException {114// create temporary directory to run tests115Path dir = TestUtil.createTemporaryDirectory();116try {117test(dir);118} finally {119TestUtil.removeAll(dir);120}121}122}123124125