jdk/test/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java
changeset 17154 e8991539c4d7
equal deleted inserted replaced
16929:c984ae5655cb 17154:e8991539c4d7
       
     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 
       
    31 import java.nio.file.Path;
       
    32 import java.nio.file.Files;
       
    33 import java.nio.file.attribute.*;
       
    34 import java.time.Instant;
       
    35 import java.io.IOException;
       
    36 
       
    37 public class CreationTime {
       
    38 
       
    39     private static final java.io.PrintStream err = System.err;
       
    40 
       
    41     /**
       
    42      * Reads the creationTime attribute
       
    43      */
       
    44     private static FileTime creationTime(Path file) throws IOException {
       
    45         return Files.readAttributes(file, BasicFileAttributes.class).creationTime();
       
    46     }
       
    47 
       
    48     /**
       
    49      * Sets the creationTime attribute
       
    50      */
       
    51     private static void setCreationTime(Path file, FileTime time) throws IOException {
       
    52         BasicFileAttributeView view =
       
    53             Files.getFileAttributeView(file, BasicFileAttributeView.class);
       
    54         view.setTimes(null, null, time);
       
    55     }
       
    56 
       
    57     static void test(Path top) throws IOException {
       
    58         Path file = Files.createFile(top.resolve("foo"));
       
    59 
       
    60         /**
       
    61          * Check that creationTime reported
       
    62          */
       
    63         FileTime creationTime = creationTime(file);
       
    64         Instant now = Instant.now();
       
    65         if (Math.abs(creationTime.toMillis()-now.toEpochMilli()) > 10000L) {
       
    66             err.println("File creation time reported as: " + creationTime);
       
    67             throw new RuntimeException("Expected to be close to: " + now);
       
    68         }
       
    69 
       
    70         /**
       
    71          * Is the creationTime attribute supported here?
       
    72          */
       
    73         boolean supportsCreationTimeRead = false;
       
    74         boolean supportsCreationTimeWrite = false;
       
    75         String os = System.getProperty("os.name");
       
    76         if (os.contains("OS X") && Files.getFileStore(file).type().equals("hfs")) {
       
    77             supportsCreationTimeRead = true;
       
    78         } else if (os.startsWith("Windows")) {
       
    79             String type = Files.getFileStore(file).type();
       
    80             if (type.equals("NTFS") || type.equals("FAT")) {
       
    81                 supportsCreationTimeRead = true;
       
    82                 supportsCreationTimeWrite = true;
       
    83             }
       
    84         }
       
    85 
       
    86         /**
       
    87          * If the creation-time attribute is supported then change the file's
       
    88          * last modified and check that it doesn't change the creation-time.
       
    89          */
       
    90         if (supportsCreationTimeRead) {
       
    91             // change modified time by +1 hour
       
    92             Instant plusHour = Instant.now().plusSeconds(60L * 60L);
       
    93             Files.setLastModifiedTime(file, FileTime.from(plusHour));
       
    94             FileTime current = creationTime(file);
       
    95             if (!current.equals(creationTime))
       
    96                 throw new RuntimeException("Creation time should not have changed");
       
    97         }
       
    98 
       
    99         /**
       
   100          * If the creation-time attribute is supported and can be changed then
       
   101          * check that the change is effective.
       
   102          */
       
   103         if (supportsCreationTimeWrite) {
       
   104             // change creation time by -1 hour
       
   105             Instant minusHour = Instant.now().minusSeconds(60L * 60L);
       
   106             creationTime = FileTime.from(minusHour);
       
   107             setCreationTime(file, creationTime);
       
   108             FileTime current = creationTime(file);
       
   109             if (Math.abs(creationTime.toMillis()-current.toMillis()) > 1000L)
       
   110                 throw new RuntimeException("Creation time not changed");
       
   111         }
       
   112     }
       
   113 
       
   114     public static void main(String[] args) throws IOException {
       
   115         // create temporary directory to run tests
       
   116         Path dir = TestUtil.createTemporaryDirectory();
       
   117         try {
       
   118             test(dir);
       
   119         } finally {
       
   120             TestUtil.removeAll(dir);
       
   121         }
       
   122     }
       
   123 }