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