--- old/test/java/net/URLClassLoader/closetest/CloseTest.java Wed Nov 6 17:25:53 2013 +++ new/test/java/net/URLClassLoader/closetest/CloseTest.java Wed Nov 6 17:25:52 2013 @@ -25,7 +25,8 @@ * @test * @bug 4167874 * @library ../../../../com/sun/net/httpserver - * @build FileServerHandler + * @library /lib/testlibrary + * @build FileServerHandler jdk.testlibrary.FileUtils * @run shell build.sh * @run main/othervm CloseTest * @summary URL-downloaded jar files can consume all available file descriptors --- old/test/java/net/URLClassLoader/closetest/GetResourceAsStream.java Wed Nov 6 17:25:54 2013 +++ new/test/java/net/URLClassLoader/closetest/GetResourceAsStream.java Wed Nov 6 17:25:53 2013 @@ -24,6 +24,8 @@ /** * @test * @bug 6899919 + * @library /lib/testlibrary + * @build jdk.testlibrary.FileUtils * @run shell build2.sh * @run main/othervm GetResourceAsStream */ --- old/test/java/net/URLClassLoader/closetest/Common.java Wed Nov 6 17:25:55 2013 +++ new/test/java/net/URLClassLoader/closetest/Common.java Wed Nov 6 17:25:54 2013 @@ -23,6 +23,8 @@ import java.io.*; import java.net.*; +import java.nio.file.NoSuchFileException; +import jdk.testlibrary.FileUtils; public class Common { @@ -39,7 +41,9 @@ if (!src.isFile()) { throw new RuntimeException ("File not found: " + src.toString()); } - dst.delete(); + // delete dst, if it exists + try { FileUtils.delete(dst.toPath()); } + catch (NoSuchFileException x) {} dst.createNewFile(); FileInputStream i = new FileInputStream (src); FileOutputStream o = new FileOutputStream (dst); @@ -50,28 +54,19 @@ } i.close(); o.close(); - } catch (IOException e) { + } catch (IOException | InterruptedException e) { throw new RuntimeException (e); } } - static void rm_minus_rf (File path) { + static void rm_minus_rf (File path) throws IOException, InterruptedException { if (!path.exists()) { return; } if (path.isFile()) { - if (!path.delete()) { - throw new RuntimeException ("Could not delete " + path); - } + FileUtils.delete(path.toPath()); } else if (path.isDirectory ()) { - String[] names = path.list(); - File[] files = path.listFiles(); - for (int i=0; i MAX_RETRY_DELETE_TIMES) + throw ioe; + Thread.sleep(RETRY_DELETE_MILLIS); + } + } + } + + /** + * Deletes a directory and its subdirectories. + * + * @param dir the directory to delete + * + * @throws IOException + * If an I/O error occurs. Any such exceptions are caught + * internally. If only one is caught, then it is re-thrown. + * If more than one exception is caught, then the second and + * following exceptions are added as suppressed exceptions of the + * first one caught, which is then re-thrown. + */ + public static void deleteTree(Path dir) throws IOException { + IOException ioe = null; + final List excs = deleteTreeUnchecked(dir); + if (!excs.isEmpty()) { + ioe = excs.remove(0); + for (IOException x : excs) + ioe.addSuppressed(x); + } + if (ioe != null) + throw ioe; + } + + public static List deleteTreeUnchecked(Path dir) { + final List excs = new ArrayList<>(); + try { + java.nio.file.Files.walkFileTree(dir, new FileVisitor() { + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { + return FileVisitResult.CONTINUE; + } + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { + try { + deleteWithRetry(file); + } catch (IOException x) { + excs.add(x); + } catch (InterruptedException x) { + return FileVisitResult.TERMINATE; + } + return FileVisitResult.CONTINUE; + } + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) { + try { + deleteWithRetry(dir); + } catch (IOException x) { + excs.add(x); + } catch (InterruptedException x) { + return FileVisitResult.TERMINATE; + } + return FileVisitResult.CONTINUE; + } + @Override + public FileVisitResult visitFileFailed(Path file, IOException exc) { + excs.add(exc); + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException x) { + excs.add(x); + } + return excs; + } +} +