jdk/test/java/nio/file/attribute/BasicFileAttributeView/Basic.java
changeset 2057 3acf8e5e2ca0
child 3065 452aaa2899fc
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 4313887
       
    26  * @summary Unit test for java.nio.file.attribute.BasicFileAttributeView
       
    27  * @library ../..
       
    28  */
       
    29 
       
    30 import java.nio.file.*;
       
    31 import java.nio.file.attribute.*;
       
    32 import java.util.*;
       
    33 import java.util.concurrent.TimeUnit;
       
    34 import java.io.*;
       
    35 
       
    36 public class Basic {
       
    37 
       
    38     static void check(boolean okay, String msg) {
       
    39         if (!okay)
       
    40             throw new RuntimeException(msg);
       
    41     }
       
    42 
       
    43     static void checkAttributesOfDirectory(Path dir)
       
    44         throws IOException
       
    45     {
       
    46         BasicFileAttributes attrs = Attributes.readBasicFileAttributes(dir);
       
    47         check(attrs.isDirectory(), "is a directory");
       
    48         check(!attrs.isRegularFile(), "is not a regular file");
       
    49         check(!attrs.isSymbolicLink(), "is not a link");
       
    50         check(!attrs.isOther(), "is not other");
       
    51         check(attrs.linkCount() >= 1, "should be at least 1");
       
    52 
       
    53         // last-modified-time should match java.io.File
       
    54         if (attrs.resolution() == TimeUnit.MILLISECONDS) {
       
    55             File f = new File(dir.toString());
       
    56             check(f.lastModified() == attrs.lastModifiedTime(),
       
    57                 "last-modified time should be the same");
       
    58         }
       
    59     }
       
    60 
       
    61     static void checkAttributesOfFile(Path dir, Path file)
       
    62         throws IOException
       
    63     {
       
    64         BasicFileAttributes attrs = Attributes.readBasicFileAttributes(file);
       
    65         check(attrs.isRegularFile(), "is a regular file");
       
    66         check(!attrs.isDirectory(), "is not a directory");
       
    67         check(!attrs.isSymbolicLink(), "is not a link");
       
    68         check(!attrs.isOther(), "is not other");
       
    69         check(attrs.linkCount() >= 1, "should be at least 1");
       
    70 
       
    71         // size and last-modified-time should match java.io.File
       
    72         File f = new File(file.toString());
       
    73         check(f.length() == attrs.size(), "size should be the same");
       
    74         if (attrs.resolution() == TimeUnit.MILLISECONDS) {
       
    75             check(f.lastModified() == attrs.lastModifiedTime(),
       
    76                 "last-modified time should be the same");
       
    77         }
       
    78 
       
    79         // copy last-modified time and file create time from directory to file,
       
    80         // re-read attribtues, and check they match
       
    81         BasicFileAttributeView view =
       
    82             file.getFileAttributeView(BasicFileAttributeView.class);
       
    83         BasicFileAttributes dirAttrs = Attributes.readBasicFileAttributes(dir);
       
    84         view.setTimes(dirAttrs.lastModifiedTime(), null, null, dirAttrs.resolution());
       
    85         if (dirAttrs.creationTime() != -1L) {
       
    86             view.setTimes(null, null, dirAttrs.creationTime(), dirAttrs.resolution());
       
    87         }
       
    88         attrs = view.readAttributes();
       
    89         check(attrs.lastModifiedTime() == dirAttrs.lastModifiedTime(),
       
    90             "last-modified time should be equal");
       
    91         if (dirAttrs.creationTime() != -1L) {
       
    92             check(attrs.creationTime() == dirAttrs.creationTime(),
       
    93                 "create time should be the same");
       
    94         }
       
    95 
       
    96         // security tests
       
    97         check (!(attrs instanceof PosixFileAttributes),
       
    98             "should not be able to cast to PosixFileAttributes");
       
    99     }
       
   100 
       
   101     static void checkAttributesOfLink(Path link)
       
   102         throws IOException
       
   103     {
       
   104         BasicFileAttributes attrs = Attributes
       
   105             .readBasicFileAttributes(link, LinkOption.NOFOLLOW_LINKS);
       
   106         check(attrs.isSymbolicLink(), "is a link");
       
   107         check(!attrs.isDirectory(), "is a directory");
       
   108         check(!attrs.isRegularFile(), "is not a regular file");
       
   109         check(!attrs.isOther(), "is not other");
       
   110         check(attrs.linkCount() >= 1, "should be at least 1");
       
   111     }
       
   112 
       
   113     static void attributeReadWriteTests(Path dir)
       
   114         throws IOException
       
   115     {
       
   116         // create file
       
   117         Path file = dir.resolve("foo");
       
   118         OutputStream out = file.newOutputStream();
       
   119         try {
       
   120             out.write("this is not an empty file".getBytes("UTF-8"));
       
   121         } finally {
       
   122             out.close();
       
   123         }
       
   124 
       
   125         // check attributes of directory and file
       
   126         checkAttributesOfDirectory(dir);
       
   127         checkAttributesOfFile(dir, file);
       
   128 
       
   129         // symbolic links may be supported
       
   130         Path link = dir.resolve("link");
       
   131         try {
       
   132             link.createSymbolicLink( file );
       
   133         } catch (UnsupportedOperationException x) {
       
   134             return;
       
   135         } catch (IOException x) {
       
   136             return;
       
   137         }
       
   138         checkAttributesOfLink(link);
       
   139     }
       
   140 
       
   141     public static void main(String[] args) throws IOException {
       
   142         // create temporary directory to run tests
       
   143         Path dir = TestUtil.createTemporaryDirectory();
       
   144         try {
       
   145             attributeReadWriteTests(dir);
       
   146         } finally {
       
   147             TestUtil.removeAll(dir);
       
   148         }
       
   149     }
       
   150 }