1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.testutils;
17 
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.UncheckedIOException;
21 import java.nio.file.CopyOption;
22 import java.nio.file.FileVisitOption;
23 import java.nio.file.FileVisitResult;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.SimpleFileVisitor;
27 import java.nio.file.attribute.BasicFileAttributes;
28 import java.util.Comparator;
29 import java.util.stream.Stream;
30 import software.amazon.awssdk.utils.StringUtils;
31 
32 public final class FileUtils {
FileUtils()33     private FileUtils() {
34 
35     }
36 
cleanUpTestDirectory(Path directory)37     public static void cleanUpTestDirectory(Path directory) {
38         if (directory == null) {
39             return;
40         }
41 
42         try {
43             try (Stream<Path> paths = Files.walk(directory, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS)) {
44                 paths.sorted(Comparator.reverseOrder())
45                      .map(Path::toFile)
46                      .forEach(File::delete);
47             }
48 
49         } catch (IOException e) {
50             // ignore
51             e.printStackTrace();
52         }
53     }
54 
copyDirectory(Path source, Path destination, CopyOption... options)55     public static void copyDirectory(Path source, Path destination, CopyOption... options) {
56         try {
57             Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
58                 @Override
59                 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
60                     Files.createDirectories(mirror(dir));
61                     return FileVisitResult.CONTINUE;
62                 }
63 
64                 @Override
65                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
66                     Files.copy(file, mirror(file), options);
67                     return FileVisitResult.CONTINUE;
68                 }
69 
70                 private Path mirror(Path path) {
71                     Path relativePath = source.relativize(path);
72                     return destination.resolve(relativePath);
73                 }
74             });
75         } catch (IOException e) {
76             throw new UncheckedIOException(String.format("Failed to copy %s to %s", source, destination), e);
77         }
78     }
79 
80     /**
81      * Convert a given directory into a visual file tree. E.g., given an input structure of:
82      * <pre>
83      *     /tmp/testdir
84      *     /tmp/testdir/CHANGELOG.md
85      *     /tmp/testdir/README.md
86      *     /tmp/testdir/notes
87      *     /tmp/testdir/notes/2022
88      *     /tmp/testdir/notes/2022/1.txt
89      *     /tmp/testdir/notes/important.txt
90      *     /tmp/testdir/notes/2021
91      *     /tmp/testdir/notes/2021/2.txt
92      *     /tmp/testdir/notes/2021/1.txt
93      * </pre>
94      * Calling this method on {@code /tmp/testdir} will yield the following output:
95      * <pre>
96      *     - testdir
97      *         - CHANGELOG.md
98      *         - README.md
99      *         - notes
100      *             - 2022
101      *                 - 1.txt
102      *             - important.txt
103      *             - 2021
104      *                 - 2.txt
105      *                 - 1.txt
106      * </pre>
107      */
toFileTreeString(Path root)108     public static String toFileTreeString(Path root) {
109         int rootDepth = root.getNameCount();
110         String tab = StringUtils.repeat(" ", 4);
111         StringBuilder sb = new StringBuilder();
112         try (Stream<Path> files = Files.walk(root)) {
113             files.forEach(p -> {
114                 int indentLevel = p.getNameCount() - rootDepth;
115                 String line = String.format("%s- %s", StringUtils.repeat(tab, indentLevel), p.getFileName());
116                 sb.append(line);
117                 sb.append(System.lineSeparator());
118             });
119         } catch (IOException e) {
120             throw new UncheckedIOException(String.format("Failed to convert %s to file tree", root), e);
121         }
122         return sb.toString();
123     }
124 }
125