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