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