alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 1 | /* |
ohair | bf91ea1 | 2011-04-06 22:06:11 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. |
alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 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 { |
| 30 | private TestUtil() { |
| 31 | } |
| 32 | |
alanb | b2d5e24 | 2010-01-18 15:21:34 +0000 | [diff] [blame] | 33 | static Path createTemporaryDirectory(String where) throws IOException { |
alanb | b75d538 | 2011-01-28 09:28:43 +0000 | [diff] [blame] | 34 | Path dir = FileSystems.getDefault().getPath(where); |
| 35 | return Files.createTempDirectory(dir, "name"); |
alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 36 | } |
| 37 | |
alanb | b2d5e24 | 2010-01-18 15:21:34 +0000 | [diff] [blame] | 38 | static Path createTemporaryDirectory() throws IOException { |
alanb | b75d538 | 2011-01-28 09:28:43 +0000 | [diff] [blame] | 39 | return Files.createTempDirectory("name"); |
alanb | b2d5e24 | 2010-01-18 15:21:34 +0000 | [diff] [blame] | 40 | } |
| 41 | |
alanb | f268798 | 2010-10-03 19:39:25 +0100 | [diff] [blame] | 42 | static void removeAll(Path dir) throws IOException { |
alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 43 | Files.walkFileTree(dir, new FileVisitor<Path>() { |
| 44 | @Override |
alanb | f268798 | 2010-10-03 19:39:25 +0100 | [diff] [blame] | 45 | public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { |
alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 46 | return FileVisitResult.CONTINUE; |
| 47 | } |
| 48 | @Override |
| 49 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| 50 | try { |
alanb | b75d538 | 2011-01-28 09:28:43 +0000 | [diff] [blame] | 51 | Files.delete(file); |
alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 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 { |
alanb | b75d538 | 2011-01-28 09:28:43 +0000 | [diff] [blame] | 60 | Files.delete(dir); |
alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 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 | |
alanb | 755cead | 2009-06-27 21:46:53 +0100 | [diff] [blame] | 74 | static void deleteUnchecked(Path file) { |
alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 75 | try { |
alanb | b75d538 | 2011-01-28 09:28:43 +0000 | [diff] [blame] | 76 | Files.delete(file); |
alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 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 | */ |
| 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("."); |
alanb | b75d538 | 2011-01-28 09:28:43 +0000 | [diff] [blame] | 97 | Files.createDirectory(dir); |
alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 98 | } 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 { |
alanb | b75d538 | 2011-01-28 09:28:43 +0000 | [diff] [blame] | 109 | Files.createSymbolicLink(link, target); |
| 110 | Files.delete(link); |
alanb | 383daf2 | 2009-02-15 12:25:54 +0000 | [diff] [blame] | 111 | return true; |
| 112 | } catch (UnsupportedOperationException x) { |
| 113 | return false; |
| 114 | } catch (IOException x) { |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | } |