jdk/test/java/nio/file/TestUtil.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 import java.nio.file.*;
       
    25 import java.nio.file.attribute.BasicFileAttributes;
       
    26 import java.util.Random;
       
    27 import java.io.IOException;
       
    28 
       
    29 public class TestUtil {
       
    30     private TestUtil() {
       
    31     }
       
    32 
       
    33     public static Path createTemporaryDirectory() throws IOException {
       
    34         Path tmpdir = Paths.get(System.getProperty("java.io.tmpdir"));
       
    35         Random r = new Random();
       
    36 
       
    37         Path dir;
       
    38         do {
       
    39             dir = tmpdir.resolve("name" + r.nextInt());
       
    40         } while (dir.exists());
       
    41         return dir.createDirectory();
       
    42     }
       
    43 
       
    44     static void removeAll(Path dir) {
       
    45         Files.walkFileTree(dir, new FileVisitor<Path>() {
       
    46             @Override
       
    47             public FileVisitResult preVisitDirectory(Path dir) {
       
    48                 return FileVisitResult.CONTINUE;
       
    49             }
       
    50             @Override
       
    51             public FileVisitResult preVisitDirectoryFailed(Path dir, IOException exc) {
       
    52                 System.err.format("Error occured accessing directory %s\n", dir, exc);
       
    53                 return FileVisitResult.CONTINUE;
       
    54             }
       
    55             @Override
       
    56             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
       
    57                 try {
       
    58                     file.delete(false);
       
    59                 } catch (IOException x) {
       
    60                     System.err.format("Unable to delete %s: %s\n", file, x);
       
    61                 }
       
    62                 return FileVisitResult.CONTINUE;
       
    63             }
       
    64             @Override
       
    65             public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
       
    66                 try {
       
    67                     dir.delete(false);
       
    68                 } catch (IOException x) {
       
    69                     System.err.format("Unable to delete %s: %s\n", dir, x);
       
    70                 }
       
    71                 return FileVisitResult.CONTINUE;
       
    72             }
       
    73             @Override
       
    74             public FileVisitResult visitFileFailed(Path file, IOException exc) {
       
    75                 System.err.format("Unable to visit %s: %s\n", file, exc);
       
    76                 return FileVisitResult.CONTINUE;
       
    77             }
       
    78         });
       
    79     }
       
    80 
       
    81     static void deleteUnchecked(FileRef file) {
       
    82         try {
       
    83             file.delete();
       
    84         } catch (IOException exc) {
       
    85             System.err.format("Unable to delete %s: %s\n", file, exc);
       
    86         }
       
    87     }
       
    88 
       
    89     /**
       
    90      * Creates a directory tree in the given directory so that the total
       
    91      * size of the path is more than 2k in size. This is used for long
       
    92      * path tests on Windows.
       
    93      */
       
    94     static Path createDirectoryWithLongPath(Path dir)
       
    95         throws IOException
       
    96     {
       
    97         StringBuilder sb = new StringBuilder();
       
    98         for (int i=0; i<240; i++) {
       
    99             sb.append('A');
       
   100         }
       
   101         String name = sb.toString();
       
   102         do {
       
   103             dir = dir.resolve(name).resolve(".");
       
   104             dir.createDirectory();
       
   105         } while (dir.toString().length() < 2048);
       
   106         return dir;
       
   107     }
       
   108 
       
   109     /**
       
   110      * Returns true if symbolic links are supported
       
   111      */
       
   112     static boolean supportsLinks(Path dir) {
       
   113         Path link = dir.resolve("testlink");
       
   114         Path target = dir.resolve("testtarget");
       
   115         try {
       
   116             link.createSymbolicLink(target);
       
   117             target.delete(false);
       
   118             return true;
       
   119         } catch (UnsupportedOperationException x) {
       
   120             return false;
       
   121         } catch (IOException x) {
       
   122             return false;
       
   123         }
       
   124     }
       
   125 }