< prev index next >

test/jdk/java/nio/file/Files/CopyAndMove.java

Print this page




  71             } finally {
  72                 TestUtil.removeAll(dir2);
  73             }
  74 
  75             // Target is location associated with custom provider
  76             Path dir3 = PassThroughFileSystem.create().getPath(dir1.toString());
  77             testPosixAttributes = getFileStore(dir1).supportsFileAttributeView("posix") &&
  78                                   getFileStore(dir3).supportsFileAttributeView("posix");
  79             testCopyFileToFile(dir1, dir3, false);
  80             testMove(dir1, dir3, false);
  81 
  82             // Test copy(InputStream,Path) and copy(Path,OutputStream)
  83             testCopyInputStreamToFile();
  84             testCopyFileToOuputStream();
  85 
  86         } finally {
  87             TestUtil.removeAll(dir1);
  88         }
  89     }
  90 












  91     static void checkBasicAttributes(BasicFileAttributes attrs1,
  92                                      BasicFileAttributes attrs2)
  93     {
  94         // check file type
  95         assertTrue(attrs1.isRegularFile() == attrs2.isRegularFile());
  96         assertTrue(attrs1.isDirectory() == attrs2.isDirectory());
  97         assertTrue(attrs1.isSymbolicLink() == attrs2.isSymbolicLink());
  98         assertTrue(attrs1.isOther() == attrs2.isOther());
  99 
 100         // check last modified time if not a symbolic link
 101         if (!attrs1.isSymbolicLink()) {
 102             long time1 = attrs1.lastModifiedTime().to(TimeUnit.SECONDS);
 103             long time2 = attrs2.lastModifiedTime().to(TimeUnit.SECONDS);
 104 
 105             if (time1 != time2) {
 106                 System.err.format("File time for %s is %s\n", attrs1.fileKey(), attrs1.lastModifiedTime());
 107                 System.err.format("File time for %s is %s\n", attrs2.fileKey(), attrs2.lastModifiedTime());
 108                 assertTrue(false);
 109             }
 110         }
 111 
 112         // check size
 113         if (attrs1.isRegularFile())
 114             assertTrue(attrs1.size() == attrs2.size());
 115     }
 116 
 117     static void checkPosixAttributes(PosixFileAttributes attrs1,
 118                                      PosixFileAttributes attrs2)
 119     {

 120         assertTrue(attrs1.permissions().equals(attrs2.permissions()));
 121         assertTrue(attrs1.owner().equals(attrs2.owner()));
 122         assertTrue(attrs1.group().equals(attrs2.group()));













 123     }
 124 
 125     static void checkDosAttributes(DosFileAttributes attrs1,
 126                                    DosFileAttributes attrs2)
 127     {

 128         assertTrue(attrs1.isReadOnly() == attrs2.isReadOnly());
 129         assertTrue(attrs1.isHidden() == attrs2.isHidden());
 130         assertTrue(attrs1.isSystem() == attrs2.isSystem());












 131     }
 132 
 133     static void checkUserDefinedFileAttributes(Map<String,ByteBuffer> attrs1,
 134                                      Map<String,ByteBuffer> attrs2)
 135     {
 136         assert attrs1.size() == attrs2.size();
 137         for (String name: attrs1.keySet()) {
 138             ByteBuffer bb1 = attrs1.get(name);
 139             ByteBuffer bb2 = attrs2.get(name);
 140             assertTrue(bb2 != null);
 141             assertTrue(bb1.equals(bb2));
 142         }
 143     }
 144 
 145     static Map<String,ByteBuffer> readUserDefinedFileAttributes(Path file)
 146         throws IOException
 147     {
 148         UserDefinedFileAttributeView view =
 149             getFileAttributeView(file, UserDefinedFileAttributeView.class);
 150         Map<String,ByteBuffer> result = new HashMap<>();


 236                     readAttributes(target, DosFileAttributes.class, NOFOLLOW_LINKS);
 237                 checkDosAttributes(dosAttributes, attrs);
 238             }
 239 
 240             // verify named attributes
 241             if (namedAttributes != null &&
 242                 getFileStore(target).supportsFileAttributeView("xattr"))
 243             {
 244                 checkUserDefinedFileAttributes(namedAttributes,
 245                                                readUserDefinedFileAttributes(target));
 246             }
 247         }
 248     }
 249 
 250     /**
 251      * Tests all possible ways to invoke move
 252      */
 253     static void testMove(Path dir1, Path dir2, boolean supportsLinks)
 254         throws IOException
 255     {


 256         Path source, target, entry;
 257 
 258         boolean sameDevice = getFileStore(dir1).equals(getFileStore(dir2));
 259 
 260         // -- regular file --
 261 
 262         /**
 263          * Test: move regular file, target does not exist
 264          */
 265         source = createSourceFile(dir1);
 266         target = getTargetFile(dir2);
 267         moveAndVerify(source, target);
 268         delete(target);
 269 
 270         /**
 271          * Test: move regular file, target exists
 272          */
 273         source = createSourceFile(dir1);
 274         target = getTargetFile(dir2);
 275         createFile(target);


 671                 }
 672 
 673                 // check named attributes are copied
 674                 if (followLinks &&
 675                     getFileStore(source).supportsFileAttributeView("xattr") &&
 676                     getFileStore(target).supportsFileAttributeView("xattr"))
 677                 {
 678                     checkUserDefinedFileAttributes(readUserDefinedFileAttributes(source),
 679                                                    readUserDefinedFileAttributes(target));
 680                 }
 681             }
 682         }
 683     }
 684 
 685     /**
 686      * Tests all possible ways to invoke copy to copy a file to a file
 687      */
 688     static void testCopyFileToFile(Path dir1, Path dir2, boolean supportsLinks)
 689         throws IOException
 690     {


 691         Path source, target, link, entry;
 692 
 693         // -- regular file --
 694 
 695         /**
 696          * Test: move regular file, target does not exist
 697          */
 698         source = createSourceFile(dir1);
 699         target = getTargetFile(dir2);
 700         copyAndVerify(source, target);
 701         delete(source);
 702         delete(target);
 703 
 704         /**
 705          * Test: copy regular file, target exists
 706          */
 707         source = createSourceFile(dir1);
 708         target = getTargetFile(dir2);
 709         createFile(target);
 710         try {




  71             } finally {
  72                 TestUtil.removeAll(dir2);
  73             }
  74 
  75             // Target is location associated with custom provider
  76             Path dir3 = PassThroughFileSystem.create().getPath(dir1.toString());
  77             testPosixAttributes = getFileStore(dir1).supportsFileAttributeView("posix") &&
  78                                   getFileStore(dir3).supportsFileAttributeView("posix");
  79             testCopyFileToFile(dir1, dir3, false);
  80             testMove(dir1, dir3, false);
  81 
  82             // Test copy(InputStream,Path) and copy(Path,OutputStream)
  83             testCopyInputStreamToFile();
  84             testCopyFileToOuputStream();
  85 
  86         } finally {
  87             TestUtil.removeAll(dir1);
  88         }
  89     }
  90 
  91     static void printDirInfo(String name, Path dir) throws IOException {
  92         System.err.format("%s: %s (%s)%n", name, dir,
  93             Files.getFileStore(dir).type());
  94     }
  95 
  96     static void printDirInfo(String label, Path dir1, Path dir2)
  97         throws IOException {
  98         System.err.format("--- %s ---%n", label);
  99         printDirInfo("dir1", dir1);
 100         printDirInfo("dir2", dir2);
 101     }
 102 
 103     static void checkBasicAttributes(BasicFileAttributes attrs1,
 104                                      BasicFileAttributes attrs2)
 105     {
 106         // check file type
 107         assertTrue(attrs1.isRegularFile() == attrs2.isRegularFile());
 108         assertTrue(attrs1.isDirectory() == attrs2.isDirectory());
 109         assertTrue(attrs1.isSymbolicLink() == attrs2.isSymbolicLink());
 110         assertTrue(attrs1.isOther() == attrs2.isOther());
 111 
 112         // check last modified time if not a symbolic link
 113         if (!attrs1.isSymbolicLink()) {
 114             long time1 = attrs1.lastModifiedTime().to(TimeUnit.SECONDS);
 115             long time2 = attrs2.lastModifiedTime().to(TimeUnit.SECONDS);
 116 
 117             if (time1 != time2) {
 118                 System.err.format("File time for %s is %s\n", attrs1.fileKey(), attrs1.lastModifiedTime());
 119                 System.err.format("File time for %s is %s\n", attrs2.fileKey(), attrs2.lastModifiedTime());
 120                 assertTrue(false);
 121             }
 122         }
 123 
 124         // check size
 125         if (attrs1.isRegularFile())
 126             assertTrue(attrs1.size() == attrs2.size());
 127     }
 128 
 129     static void checkPosixAttributes(PosixFileAttributes attrs1,
 130                                      PosixFileAttributes attrs2)
 131     {
 132         try {
 133             assertTrue(attrs1.permissions().equals(attrs2.permissions()));
 134             assertTrue(attrs1.owner().equals(attrs2.owner()));
 135             assertTrue(attrs1.group().equals(attrs2.group()));
 136         } catch (Exception e) {
 137             if (!attrs1.permissions().equals(attrs2.permissions()))
 138                 System.err.format("permissions%n1 (%d): %s%n2 (%d): %s%n%n",
 139                     attrs1.permissions().size(), attrs1.permissions(),
 140                     attrs2.permissions().size(), attrs2.permissions());
 141             if (!attrs1.owner().equals(attrs2.owner()))
 142                 System.err.format("owner%n1: %s%n2: %s%n%n",
 143                     attrs1.owner(), attrs2.owner());
 144             if (!attrs1.group().equals(attrs2.group()))
 145                 System.err.format("group%n1: %s%n2: %s%n%n",
 146                     attrs1.group(), attrs2.group());
 147             throw e;
 148         }
 149     }
 150 
 151     static void checkDosAttributes(DosFileAttributes attrs1,
 152                                    DosFileAttributes attrs2)
 153     {
 154         try {
 155             assertTrue(attrs1.isReadOnly() == attrs2.isReadOnly());
 156             assertTrue(attrs1.isHidden() == attrs2.isHidden());
 157             assertTrue(attrs1.isSystem() == attrs2.isSystem());
 158         } catch (Exception e) {
 159             if(attrs1.isReadOnly() != attrs2.isReadOnly())
 160                 System.err.format("isReadOnly%n1: %s%n2: %s%n%n",
 161                     attrs1.isReadOnly(), attrs2.isReadOnly());
 162             if(attrs1.isHidden() != attrs2.isHidden())
 163                 System.err.format("isHidden%n1: %s%n2: %s%n%n",
 164                     attrs1.isHidden(), attrs2.isHidden());
 165             if(attrs1.isSystem() != attrs2.isSystem())
 166                 System.err.format("isSystem%n1: %s%n2: %s%n%n",
 167                     attrs1.isSystem(), attrs2.isSystem());
 168             throw e;
 169         }
 170     }
 171 
 172     static void checkUserDefinedFileAttributes(Map<String,ByteBuffer> attrs1,
 173                                      Map<String,ByteBuffer> attrs2)
 174     {
 175         assert attrs1.size() == attrs2.size();
 176         for (String name: attrs1.keySet()) {
 177             ByteBuffer bb1 = attrs1.get(name);
 178             ByteBuffer bb2 = attrs2.get(name);
 179             assertTrue(bb2 != null);
 180             assertTrue(bb1.equals(bb2));
 181         }
 182     }
 183 
 184     static Map<String,ByteBuffer> readUserDefinedFileAttributes(Path file)
 185         throws IOException
 186     {
 187         UserDefinedFileAttributeView view =
 188             getFileAttributeView(file, UserDefinedFileAttributeView.class);
 189         Map<String,ByteBuffer> result = new HashMap<>();


 275                     readAttributes(target, DosFileAttributes.class, NOFOLLOW_LINKS);
 276                 checkDosAttributes(dosAttributes, attrs);
 277             }
 278 
 279             // verify named attributes
 280             if (namedAttributes != null &&
 281                 getFileStore(target).supportsFileAttributeView("xattr"))
 282             {
 283                 checkUserDefinedFileAttributes(namedAttributes,
 284                                                readUserDefinedFileAttributes(target));
 285             }
 286         }
 287     }
 288 
 289     /**
 290      * Tests all possible ways to invoke move
 291      */
 292     static void testMove(Path dir1, Path dir2, boolean supportsLinks)
 293         throws IOException
 294     {
 295         printDirInfo("testMove", dir1, dir2);
 296 
 297         Path source, target, entry;
 298 
 299         boolean sameDevice = getFileStore(dir1).equals(getFileStore(dir2));
 300 
 301         // -- regular file --
 302 
 303         /**
 304          * Test: move regular file, target does not exist
 305          */
 306         source = createSourceFile(dir1);
 307         target = getTargetFile(dir2);
 308         moveAndVerify(source, target);
 309         delete(target);
 310 
 311         /**
 312          * Test: move regular file, target exists
 313          */
 314         source = createSourceFile(dir1);
 315         target = getTargetFile(dir2);
 316         createFile(target);


 712                 }
 713 
 714                 // check named attributes are copied
 715                 if (followLinks &&
 716                     getFileStore(source).supportsFileAttributeView("xattr") &&
 717                     getFileStore(target).supportsFileAttributeView("xattr"))
 718                 {
 719                     checkUserDefinedFileAttributes(readUserDefinedFileAttributes(source),
 720                                                    readUserDefinedFileAttributes(target));
 721                 }
 722             }
 723         }
 724     }
 725 
 726     /**
 727      * Tests all possible ways to invoke copy to copy a file to a file
 728      */
 729     static void testCopyFileToFile(Path dir1, Path dir2, boolean supportsLinks)
 730         throws IOException
 731     {
 732         printDirInfo("testCopyFileToFile", dir1, dir2);
 733 
 734         Path source, target, link, entry;
 735 
 736         // -- regular file --
 737 
 738         /**
 739          * Test: move regular file, target does not exist
 740          */
 741         source = createSourceFile(dir1);
 742         target = getTargetFile(dir2);
 743         copyAndVerify(source, target);
 744         delete(source);
 745         delete(target);
 746 
 747         /**
 748          * Test: copy regular file, target exists
 749          */
 750         source = createSourceFile(dir1);
 751         target = getTargetFile(dir2);
 752         createFile(target);
 753         try {


< prev index next >