blob: 972dc250d87d8aa4fcf0a5e8321893dca25d704c [file] [log] [blame]
alanb383daf22009-02-15 12:25:54 +00001/*
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
24import java.nio.file.*;
25import java.nio.file.attribute.BasicFileAttributes;
26import java.util.Random;
27import java.io.IOException;
28
29public class TestUtil {
30 private TestUtil() {
31 }
32
alanbb2d5e242010-01-18 15:21:34 +000033 static Path createTemporaryDirectory(String where) throws IOException {
34 Path top = FileSystems.getDefault().getPath(where);
alanb383daf22009-02-15 12:25:54 +000035 Random r = new Random();
alanb383daf22009-02-15 12:25:54 +000036 Path dir;
37 do {
alanbb2d5e242010-01-18 15:21:34 +000038 dir = top.resolve("name" + r.nextInt());
alanb383daf22009-02-15 12:25:54 +000039 } while (dir.exists());
40 return dir.createDirectory();
41 }
42
alanbb2d5e242010-01-18 15:21:34 +000043 static Path createTemporaryDirectory() throws IOException {
44 return createTemporaryDirectory(System.getProperty("java.io.tmpdir"));
45 }
46
alanb383daf22009-02-15 12:25:54 +000047 static void removeAll(Path dir) {
48 Files.walkFileTree(dir, new FileVisitor<Path>() {
49 @Override
50 public FileVisitResult preVisitDirectory(Path dir) {
51 return FileVisitResult.CONTINUE;
52 }
53 @Override
54 public FileVisitResult preVisitDirectoryFailed(Path dir, IOException exc) {
55 System.err.format("Error occured accessing directory %s\n", dir, exc);
56 return FileVisitResult.CONTINUE;
57 }
58 @Override
59 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
60 try {
alanb755cead2009-06-27 21:46:53 +010061 file.delete();
alanb383daf22009-02-15 12:25:54 +000062 } catch (IOException x) {
63 System.err.format("Unable to delete %s: %s\n", file, x);
64 }
65 return FileVisitResult.CONTINUE;
66 }
67 @Override
68 public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
69 try {
alanb755cead2009-06-27 21:46:53 +010070 dir.delete();
alanb383daf22009-02-15 12:25:54 +000071 } catch (IOException x) {
72 System.err.format("Unable to delete %s: %s\n", dir, x);
73 }
74 return FileVisitResult.CONTINUE;
75 }
76 @Override
77 public FileVisitResult visitFileFailed(Path file, IOException exc) {
78 System.err.format("Unable to visit %s: %s\n", file, exc);
79 return FileVisitResult.CONTINUE;
80 }
81 });
82 }
83
alanb755cead2009-06-27 21:46:53 +010084 static void deleteUnchecked(Path file) {
alanb383daf22009-02-15 12:25:54 +000085 try {
86 file.delete();
87 } catch (IOException exc) {
88 System.err.format("Unable to delete %s: %s\n", file, exc);
89 }
90 }
91
92 /**
93 * Creates a directory tree in the given directory so that the total
94 * size of the path is more than 2k in size. This is used for long
95 * path tests on Windows.
96 */
97 static Path createDirectoryWithLongPath(Path dir)
98 throws IOException
99 {
100 StringBuilder sb = new StringBuilder();
101 for (int i=0; i<240; i++) {
102 sb.append('A');
103 }
104 String name = sb.toString();
105 do {
106 dir = dir.resolve(name).resolve(".");
107 dir.createDirectory();
108 } while (dir.toString().length() < 2048);
109 return dir;
110 }
111
112 /**
113 * Returns true if symbolic links are supported
114 */
115 static boolean supportsLinks(Path dir) {
116 Path link = dir.resolve("testlink");
117 Path target = dir.resolve("testtarget");
118 try {
119 link.createSymbolicLink(target);
alanb755cead2009-06-27 21:46:53 +0100120 link.delete();
alanb383daf22009-02-15 12:25:54 +0000121 return true;
122 } catch (UnsupportedOperationException x) {
123 return false;
124 } catch (IOException x) {
125 return false;
126 }
127 }
128}