1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 8011536 26 * @summary Basic test for creationTime attribute on platforms/file systems 27 * that support it. 28 * @library ../.. 29 */ 30 // Android-changed: Adapted from 31 // jdk/test/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java 32 // Android-changed: Added package & Test import 33 package test.java.nio.file.attribute; 34 import org.testng.annotations.Test; 35 import test.java.nio.file.TestUtil; 36 37 import java.nio.file.Path; 38 import java.nio.file.Files; 39 import java.nio.file.attribute.*; 40 import java.time.Instant; 41 import java.io.IOException; 42 43 // Android-changed: Renamed from "CreationTime" 44 public class BasicFileAttributeViewCreationTimeTest { 45 46 private static final java.io.PrintStream err = System.err; 47 48 /** 49 * Reads the creationTime attribute 50 */ 51 private static FileTime creationTime(Path file) throws IOException { 52 return Files.readAttributes(file, BasicFileAttributes.class).creationTime(); 53 } 54 55 /** 56 * Sets the creationTime attribute 57 */ 58 private static void setCreationTime(Path file, FileTime time) throws IOException { 59 BasicFileAttributeView view = 60 Files.getFileAttributeView(file, BasicFileAttributeView.class); 61 view.setTimes(null, null, time); 62 } 63 64 static void test(Path top) throws IOException { 65 Path file = Files.createFile(top.resolve("foo")); 66 67 /** 68 * Check that creationTime reported 69 */ 70 FileTime creationTime = creationTime(file); 71 Instant now = Instant.now(); 72 if (Math.abs(creationTime.toMillis()-now.toEpochMilli()) > 10000L) { 73 err.println("File creation time reported as: " + creationTime); 74 throw new RuntimeException("Expected to be close to: " + now); 75 } 76 77 /** 78 * Is the creationTime attribute supported here? 79 */ 80 boolean supportsCreationTimeRead = false; 81 boolean supportsCreationTimeWrite = false; 82 String os = System.getProperty("os.name"); 83 // Android-changed: This test is never run on Mac OS or windows hosts. 84 // 85 // if (os.contains("OS X") && Files.getFileStore(file).type().equals("hfs")) { 86 // supportsCreationTimeRead = true; 87 // } else if (os.startsWith("Windows")) { 88 // String type = Files.getFileStore(file).type(); 89 // if (type.equals("NTFS") || type.equals("FAT")) { 90 // supportsCreationTimeRead = true; 91 // supportsCreationTimeWrite = true; 92 // } 93 // } 94 95 /** 96 * If the creation-time attribute is supported then change the file's 97 * last modified and check that it doesn't change the creation-time. 98 */ 99 if (supportsCreationTimeRead) { 100 // change modified time by +1 hour 101 Instant plusHour = Instant.now().plusSeconds(60L * 60L); 102 Files.setLastModifiedTime(file, FileTime.from(plusHour)); 103 FileTime current = creationTime(file); 104 if (!current.equals(creationTime)) 105 throw new RuntimeException("Creation time should not have changed"); 106 } 107 108 /** 109 * If the creation-time attribute is supported and can be changed then 110 * check that the change is effective. 111 */ 112 if (supportsCreationTimeWrite) { 113 // change creation time by -1 hour 114 Instant minusHour = Instant.now().minusSeconds(60L * 60L); 115 creationTime = FileTime.from(minusHour); 116 setCreationTime(file, creationTime); 117 FileTime current = creationTime(file); 118 if (Math.abs(creationTime.toMillis()-current.toMillis()) > 1000L) 119 throw new RuntimeException("Creation time not changed"); 120 } 121 } 122 123 // Android-changed: Removed args & added @Test 124 @Test 125 public static void main() throws IOException { 126 // create temporary directory to run tests 127 Path dir = TestUtil.createTemporaryDirectory(); 128 try { 129 test(dir); 130 } finally { 131 TestUtil.removeAll(dir); 132 } 133 } 134 } 135