Path: blob/master/test/jdk/java/nio/file/TestUtil.java
41149 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.file.*;24import java.nio.file.attribute.BasicFileAttributes;25import java.util.Random;26import java.io.IOException;2728public class TestUtil {29private TestUtil() {30}3132static Path createTemporaryDirectory(String where) throws IOException {33Path dir = FileSystems.getDefault().getPath(where);34return Files.createTempDirectory(dir, "name");35}3637static Path createTemporaryDirectory() throws IOException {38return Files.createTempDirectory("name");39}4041static void removeAll(Path dir) throws IOException {42Files.walkFileTree(dir, new FileVisitor<Path>() {43@Override44public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {45return FileVisitResult.CONTINUE;46}47@Override48public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {49try {50Files.delete(file);51} catch (IOException x) {52System.err.format("Unable to delete %s: %s\n", file, x);53}54return FileVisitResult.CONTINUE;55}56@Override57public FileVisitResult postVisitDirectory(Path dir, IOException exc) {58try {59Files.delete(dir);60} catch (IOException x) {61System.err.format("Unable to delete %s: %s\n", dir, x);62}63return FileVisitResult.CONTINUE;64}65@Override66public FileVisitResult visitFileFailed(Path file, IOException exc) {67System.err.format("Unable to visit %s: %s\n", file, exc);68return FileVisitResult.CONTINUE;69}70});71}7273static void deleteUnchecked(Path file) {74try {75Files.delete(file);76} catch (IOException exc) {77System.err.format("Unable to delete %s: %s\n", file, exc);78}79}8081/**82* Creates a directory tree in the given directory so that the total83* size of the path is more than 2k in size. This is used for long84* path tests on Windows.85*/86static Path createDirectoryWithLongPath(Path dir)87throws IOException88{89StringBuilder sb = new StringBuilder();90for (int i=0; i<240; i++) {91sb.append('A');92}93String name = sb.toString();94do {95dir = dir.resolve(name).resolve(".");96Files.createDirectory(dir);97} while (dir.toString().length() < 2048);98return dir;99}100101/**102* Returns true if symbolic links are supported103*/104static boolean supportsLinks(Path dir) {105Path link = dir.resolve("testlink");106Path target = dir.resolve("testtarget");107try {108Files.createSymbolicLink(link, target);109Files.delete(link);110return true;111} catch (UnsupportedOperationException x) {112return false;113} catch (IOException x) {114return false;115}116}117}118119120