blob: 66cf21bfb35ee275a0b054d051f0d988f57071f6 [file] [log] [blame]
alanb383daf22009-02-15 12:25:54 +00001/*
ohairbf91ea12011-04-06 22:06:11 -07002 * Copyright (c) 2008, 2011, 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 {
alanbb75d5382011-01-28 09:28:43 +000034 Path dir = FileSystems.getDefault().getPath(where);
35 return Files.createTempDirectory(dir, "name");
alanb383daf22009-02-15 12:25:54 +000036 }
37
alanbb2d5e242010-01-18 15:21:34 +000038 static Path createTemporaryDirectory() throws IOException {
alanbb75d5382011-01-28 09:28:43 +000039 return Files.createTempDirectory("name");
alanbb2d5e242010-01-18 15:21:34 +000040 }
41
alanbf2687982010-10-03 19:39:25 +010042 static void removeAll(Path dir) throws IOException {
alanb383daf22009-02-15 12:25:54 +000043 Files.walkFileTree(dir, new FileVisitor<Path>() {
44 @Override
alanbf2687982010-10-03 19:39:25 +010045 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
alanb383daf22009-02-15 12:25:54 +000046 return FileVisitResult.CONTINUE;
47 }
48 @Override
49 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
50 try {
alanbb75d5382011-01-28 09:28:43 +000051 Files.delete(file);
alanb383daf22009-02-15 12:25:54 +000052 } 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 {
alanbb75d5382011-01-28 09:28:43 +000060 Files.delete(dir);
alanb383daf22009-02-15 12:25:54 +000061 } 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
alanb755cead2009-06-27 21:46:53 +010074 static void deleteUnchecked(Path file) {
alanb383daf22009-02-15 12:25:54 +000075 try {
alanbb75d5382011-01-28 09:28:43 +000076 Files.delete(file);
alanb383daf22009-02-15 12:25:54 +000077 } 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 */
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(".");
alanbb75d5382011-01-28 09:28:43 +000097 Files.createDirectory(dir);
alanb383daf22009-02-15 12:25:54 +000098 } while (dir.toString().length() < 2048);
99 return dir;
100 }
101
102 /**
103 * Returns true if symbolic links are supported
104 */
105 static boolean supportsLinks(Path dir) {
106 Path link = dir.resolve("testlink");
107 Path target = dir.resolve("testtarget");
108 try {
alanbb75d5382011-01-28 09:28:43 +0000109 Files.createSymbolicLink(link, target);
110 Files.delete(link);
alanb383daf22009-02-15 12:25:54 +0000111 return true;
112 } catch (UnsupportedOperationException x) {
113 return false;
114 } catch (IOException x) {
115 return false;
116 }
117 }
118}