blob: b0fdd2a1f9d2f9d90beef6e436e30bffe0250c6a [file] [log] [blame]
alanb383daf22009-02-15 12:25:54 +00001/*
ohairf5857212010-12-28 15:53:50 -08002 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
alanb383daf22009-02-15 12:25:54 +00003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
alanb383daf22009-02-15 12:25:54 +000022 */
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
alanbf2687982010-10-03 19:39:25 +010047 static void removeAll(Path dir) throws IOException {
alanb383daf22009-02-15 12:25:54 +000048 Files.walkFileTree(dir, new FileVisitor<Path>() {
49 @Override
alanbf2687982010-10-03 19:39:25 +010050 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
alanb383daf22009-02-15 12:25:54 +000051 return FileVisitResult.CONTINUE;
52 }
53 @Override
54 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
55 try {
alanb755cead2009-06-27 21:46:53 +010056 file.delete();
alanb383daf22009-02-15 12:25:54 +000057 } 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 {
alanb755cead2009-06-27 21:46:53 +010065 dir.delete();
alanb383daf22009-02-15 12:25:54 +000066 } 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
alanb755cead2009-06-27 21:46:53 +010079 static void deleteUnchecked(Path file) {
alanb383daf22009-02-15 12:25:54 +000080 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);
alanb755cead2009-06-27 21:46:53 +0100115 link.delete();
alanb383daf22009-02-15 12:25:54 +0000116 return true;
117 } catch (UnsupportedOperationException x) {
118 return false;
119 } catch (IOException x) {
120 return false;
121 }
122 }
123}