|
24 | 24 | import java.io.IOException; |
25 | 25 | import java.io.InputStream; |
26 | 26 | import java.nio.charset.StandardCharsets; |
27 | | -import java.nio.file.FileSystems; |
28 | | -import java.nio.file.Files; |
29 | | -import java.nio.file.StandardCopyOption; |
| 27 | +import java.nio.file.*; |
| 28 | +import java.nio.file.attribute.BasicFileAttributes; |
30 | 29 | import java.util.ArrayList; |
31 | 30 | import java.util.Arrays; |
32 | 31 | import java.util.Collections; |
@@ -157,6 +156,35 @@ public String readText(String fileName) { |
157 | 156 | return streamToText(stream); |
158 | 157 | } |
159 | 158 |
|
| 159 | + /** |
| 160 | + * Copies all files from the fixtureDirectoryPath into destinationDirectory. |
| 161 | + * |
| 162 | + * <p> |
| 163 | + * The files are copied from the test resources folder such as <code>src/main/test/resources</code>. |
| 164 | + * The current implementation does not support reading the directory content from the classpath (e.g. a different project) |
| 165 | + * </p> |
| 166 | + * @param fixtureDirectoryPath |
| 167 | + * @param destinationDirectory |
| 168 | + * @return |
| 169 | + */ |
| 170 | + public File copyTo(String fixtureDirectoryPath, File destinationDirectory) { |
| 171 | + File testingDirectory = new File(getTestResourcesLocation(), getFixtureLocation(fixtureDirectoryPath)); |
| 172 | + |
| 173 | + if (!testingDirectory.exists()) { |
| 174 | + if (testingDirectory.mkdirs()) { |
| 175 | + throw new IllegalArgumentException("The directory " + testingDirectory.getAbsolutePath() + " does not exist but it has been crated!" |
| 176 | + + "Please, pay attention that the folder must exists in the very same project as the test"); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + try { |
| 181 | + copyFolder(testingDirectory.toPath(), destinationDirectory.toPath()); |
| 182 | + return testingDirectory; |
| 183 | + } catch (IOException e) { |
| 184 | + throw new IllegalArgumentException("Failed to copy directory", e); |
| 185 | + } |
| 186 | + } |
| 187 | + |
160 | 188 | /** |
161 | 189 | * Saves the fixture to the appropriate location. |
162 | 190 | * |
@@ -193,6 +221,25 @@ private static String streamToText(InputStream stream) { |
193 | 221 | return ""; |
194 | 222 | } |
195 | 223 |
|
| 224 | + public static void copyFolder(Path source, Path target, CopyOption... options) throws IOException { |
| 225 | + Files.walkFileTree(source, new SimpleFileVisitor<Path>() { |
| 226 | + |
| 227 | + @Override |
| 228 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) |
| 229 | + throws IOException { |
| 230 | + Files.createDirectories(target.resolve(source.relativize(dir))); |
| 231 | + return FileVisitResult.CONTINUE; |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) |
| 236 | + throws IOException { |
| 237 | + Files.copy(file, target.resolve(source.relativize(file)), options); |
| 238 | + return FileVisitResult.CONTINUE; |
| 239 | + } |
| 240 | + }); |
| 241 | + } |
| 242 | + |
196 | 243 | private InputStream readStreamFromClasspath(String fileName) { |
197 | 244 | return clazz.getResourceAsStream(clazz.getSimpleName() + '/' + fileName); |
198 | 245 | } |
|
0 commit comments