jdk/test/java/nio/file/Files/CopyAndMove.java
changeset 14011 3d24d5dcf602
parent 13667 a7901d690bde
child 16048 e8891a5d639f
equal deleted inserted replaced
14010:24f9f29d5532 14011:3d24d5dcf602
     1 /*
     1 /*
     2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 4313887 6838333 6917021 7006126
    25  * @bug 4313887 6838333 6917021 7006126 6950237
    26  * @summary Unit test for java.nio.file.Files copy and move methods
    26  * @summary Unit test for java.nio.file.Files copy and move methods
    27  * @library ..
    27  * @library ..
    28  * @build CopyAndMove PassThroughFileSystem
    28  * @build CopyAndMove PassThroughFileSystem
    29  * @run main/othervm CopyAndMove
    29  * @run main/othervm CopyAndMove
    30  */
    30  */
    39 import java.util.*;
    39 import java.util.*;
    40 
    40 
    41 public class CopyAndMove {
    41 public class CopyAndMove {
    42     static final Random rand = new Random();
    42     static final Random rand = new Random();
    43     static boolean heads() { return rand.nextBoolean(); }
    43     static boolean heads() { return rand.nextBoolean(); }
       
    44     private static boolean testPosixAttributes = false;
    44 
    45 
    45     public static void main(String[] args) throws Exception {
    46     public static void main(String[] args) throws Exception {
    46         Path dir1 = TestUtil.createTemporaryDirectory();
    47         Path dir1 = TestUtil.createTemporaryDirectory();
    47         try {
    48         try {
    48 
    49 
    49             // Same directory
    50             // Same directory
       
    51             testPosixAttributes = getFileStore(dir1).supportsFileAttributeView("posix");
    50             testCopyFileToFile(dir1, dir1, TestUtil.supportsLinks(dir1));
    52             testCopyFileToFile(dir1, dir1, TestUtil.supportsLinks(dir1));
    51             testMove(dir1, dir1, TestUtil.supportsLinks(dir1));
    53             testMove(dir1, dir1, TestUtil.supportsLinks(dir1));
    52 
    54 
    53             // Different directories. Use test.dir if possible as it might be
    55             // Different directories. Use test.dir if possible as it might be
    54             // a different volume/file system and so improve test coverage.
    56             // a different volume/file system and so improve test coverage.
    55             String testDir = System.getProperty("test.dir", ".");
    57             String testDir = System.getProperty("test.dir", ".");
    56             Path dir2 = TestUtil.createTemporaryDirectory(testDir);
    58             Path dir2 = TestUtil.createTemporaryDirectory(testDir);
    57             try {
    59             try {
    58                 boolean testSymbolicLinks =
    60                 boolean testSymbolicLinks =
    59                     TestUtil.supportsLinks(dir1) && TestUtil.supportsLinks(dir2);
    61                     TestUtil.supportsLinks(dir1) && TestUtil.supportsLinks(dir2);
       
    62                 testPosixAttributes = getFileStore(dir1).supportsFileAttributeView("posix") &&
       
    63                                       getFileStore(dir2).supportsFileAttributeView("posix");
    60                 testCopyFileToFile(dir1, dir2, testSymbolicLinks);
    64                 testCopyFileToFile(dir1, dir2, testSymbolicLinks);
    61                 testMove(dir1, dir2, testSymbolicLinks);
    65                 testMove(dir1, dir2, testSymbolicLinks);
    62             } finally {
    66             } finally {
    63                 TestUtil.removeAll(dir2);
    67                 TestUtil.removeAll(dir2);
    64             }
    68             }
    65 
    69 
    66             // Target is location associated with custom provider
    70             // Target is location associated with custom provider
    67             Path dir3 = PassThroughFileSystem.create().getPath(dir1.toString());
    71             Path dir3 = PassThroughFileSystem.create().getPath(dir1.toString());
       
    72             testPosixAttributes = getFileStore(dir1).supportsFileAttributeView("posix") &&
       
    73                                   getFileStore(dir3).supportsFileAttributeView("posix");
    68             testCopyFileToFile(dir1, dir3, false);
    74             testCopyFileToFile(dir1, dir3, false);
    69             testMove(dir1, dir3, false);
    75             testMove(dir1, dir3, false);
    70 
    76 
    71             // Test copy(InputStream,Path) and copy(Path,OutputStream)
    77             // Test copy(InputStream,Path) and copy(Path,OutputStream)
    72             testCopyInputStreamToFile();
    78             testCopyInputStreamToFile();
    88 
    94 
    89         // check last modified time if not a symbolic link
    95         // check last modified time if not a symbolic link
    90         if (!attrs1.isSymbolicLink()) {
    96         if (!attrs1.isSymbolicLink()) {
    91             long time1 = attrs1.lastModifiedTime().toMillis();
    97             long time1 = attrs1.lastModifiedTime().toMillis();
    92             long time2 = attrs2.lastModifiedTime().toMillis();
    98             long time2 = attrs2.lastModifiedTime().toMillis();
    93             assertTrue(time1 == time2);
    99 
       
   100             if (time1 != time2) {
       
   101                 System.err.format("File time for %s is %s\n", attrs1.fileKey(), attrs1.lastModifiedTime());
       
   102                 System.err.format("File time for %s is %s\n", attrs2.fileKey(), attrs2.lastModifiedTime());
       
   103                 assertTrue(false);
       
   104             }
    94         }
   105         }
    95 
   106 
    96         // check size
   107         // check size
    97         if (attrs1.isRegularFile())
   108         if (attrs1.isRegularFile())
    98             assertTrue(attrs1.size() == attrs2.size());
   109             assertTrue(attrs1.size() == attrs2.size());
   205 
   216 
   206         // verify other attributes when same provider
   217         // verify other attributes when same provider
   207         if (source.getFileSystem().provider() == target.getFileSystem().provider()) {
   218         if (source.getFileSystem().provider() == target.getFileSystem().provider()) {
   208 
   219 
   209             // verify POSIX attributes
   220             // verify POSIX attributes
   210             if (posixAttributes != null && !basicAttributes.isSymbolicLink()) {
   221             if (posixAttributes != null &&
       
   222                 !basicAttributes.isSymbolicLink() &&
       
   223                 testPosixAttributes)
       
   224             {
   211                 checkPosixAttributes(posixAttributes,
   225                 checkPosixAttributes(posixAttributes,
   212                     readAttributes(target, PosixFileAttributes.class, NOFOLLOW_LINKS));
   226                     readAttributes(target, PosixFileAttributes.class, NOFOLLOW_LINKS));
   213             }
   227             }
   214 
   228 
   215             // verify DOS attributes
   229             // verify DOS attributes
   634             // verify other attributes when same provider
   648             // verify other attributes when same provider
   635             if (source.getFileSystem().provider() == target.getFileSystem().provider()) {
   649             if (source.getFileSystem().provider() == target.getFileSystem().provider()) {
   636 
   650 
   637                 // check POSIX attributes are copied
   651                 // check POSIX attributes are copied
   638                 String os = System.getProperty("os.name");
   652                 String os = System.getProperty("os.name");
   639                 if (os.equals("SunOS") || os.equals("Linux")) {
   653                 if ((os.equals("SunOS") || os.equals("Linux")) &&
       
   654                     testPosixAttributes)
       
   655                 {
   640                     checkPosixAttributes(
   656                     checkPosixAttributes(
   641                         readAttributes(source, PosixFileAttributes.class, linkOptions),
   657                         readAttributes(source, PosixFileAttributes.class, linkOptions),
   642                         readAttributes(target, PosixFileAttributes.class, linkOptions));
   658                         readAttributes(target, PosixFileAttributes.class, linkOptions));
   643                 }
   659                 }
   644 
   660