001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2016 ForgeRock AS.
015 */
016
017package org.forgerock.util.test;
018
019import java.io.IOException;
020import java.nio.file.FileVisitResult;
021import java.nio.file.Files;
022import java.nio.file.Path;
023import java.nio.file.SimpleFileVisitor;
024import java.nio.file.attribute.BasicFileAttributes;
025
026/**
027 * Utilities for working with files within unit tests.
028 */
029public final class FileUtils {
030
031    private FileUtils() {
032        // hidden constructor
033    }
034
035    /**
036     * Recursively deletes one or more files and/or directories. If an {@link IOException} is thrown while traversing
037     * a path, the error message will be recorded and a new {@link IOException} thrown after traversing all other
038     * paths.
039     *
040     * @param paths Files and/or directories to delete
041     * @throws IOException failure to delete a path
042     */
043    public static void deleteRecursively(final Path... paths) throws IOException {
044        StringBuilder exceptionMessages = null;
045        for (final Path path : paths) {
046            try {
047                Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
048                    @Override
049                    public FileVisitResult visitFile(final Path file,
050                            final BasicFileAttributes attrs) throws IOException {
051                        Files.delete(file);
052                        return FileVisitResult.CONTINUE;
053                    }
054
055                    @Override
056                    public FileVisitResult postVisitDirectory(final Path dir,
057                            final IOException exc) throws IOException {
058                        Files.delete(dir);
059                        return FileVisitResult.CONTINUE;
060                    }
061                });
062            } catch (final IOException e) {
063                // store the message, so that we can continue deleting paths
064                if (exceptionMessages == null) {
065                    exceptionMessages = new StringBuilder();
066                    exceptionMessages.append(e.getMessage());
067                } else {
068                    exceptionMessages.append(", ").append(e.getMessage());
069                }
070            }
071        }
072        if (exceptionMessages != null) {
073            throw new IOException("Failed to delete one or more paths: " + exceptionMessages);
074        }
075    }
076
077}