Skip to content

Commit

Permalink
Use Java NIO to copy report resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Feb 12, 2015
1 parent 826c30f commit 5f4b87c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import net.thucydides.core.reports.ThucydidesReporter;
import net.thucydides.core.reports.templates.ReportTemplate;
import net.thucydides.core.reports.templates.TemplateManager;
import net.thucydides.core.reports.util.CopyDirectory;
import net.thucydides.core.util.EnvironmentVariables;
import org.apache.commons.io.FileUtils;
import org.joda.time.DateTime;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.Map;

/**
Expand Down Expand Up @@ -76,23 +80,33 @@ private void copyResources() throws IOException {
}

protected void copyTestResultsToOutputDirectory() throws IOException {
File testResultsSource = getSourceDirectoryOrDefault();
if ((!getOutputDirectory().getAbsolutePath().equals(testResultsSource.getAbsolutePath())) && testResultsSource.exists()) {
FileUtils.copyDirectory(testResultsSource, getOutputDirectory(), withXMLorHTMLorCSVFiles());
Path sourcePath = getSourceDirectoryOrDefault().toPath();
Path destinationPath = getOutputDirectory().toPath();
if (Files.exists(sourcePath) && !Files.isSameFile(sourcePath, destinationPath)) {
copyDirectoryContents(sourcePath, destinationPath);
}
}

private FileFilter withXMLorHTMLorCSVFiles() {
return new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endsWith(".xml")
|| file.getName().endsWith(".html")
|| file.getName().endsWith(".csv");
}
};
private void copyDirectoryContents(Path sourcePath, Path destinationPath) throws IOException {
Files.walkFileTree(sourcePath, EnumSet.of(FileVisitOption.FOLLOW_LINKS),
Integer.MAX_VALUE, new CopyDirectory(sourcePath, destinationPath));
// FileUtils.copyDirectory(sourcePath.toFile(), destinationPath.toFile(), withXMLorHTMLorCSVFiles());

}

// private FileFilter withXMLorHTMLorCSVFiles() {
// return new FileFilter() {
// @Override
// public boolean accept(File file) {
// return file.getName().endsWith(".xml")
// || file.getName().endsWith(".html")
// || file.getName().endsWith(".properties")
// || file.getName().endsWith(".json")
// || file.getName().endsWith(".csv");
// }
// };
// }

private File getSourceDirectoryOrDefault() {
String source = (getSourceDirectory() != null) ? getSourceDirectory().getAbsolutePath() : DEFAULT_SOURCE_DIR;
return new File(source);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.thucydides.core.reports.util;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class CopyDirectory extends SimpleFileVisitor<Path> {

private Path source;
private Path target;

public CopyDirectory(Path source, Path target) {
this.source = source;
this.target = target;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes)
throws IOException {
if (!Files.exists(target.resolve(source.relativize(file)))) {
Files.copy(file, target.resolve(source.relativize(file)));
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult preVisitDirectory(Path directory,
BasicFileAttributes attributes) throws IOException {
Path targetDirectory = target.resolve(source.relativize(directory));
try {
Files.copy(directory, targetDirectory);
} catch (FileAlreadyExistsException e) {
if (!Files.isDirectory(targetDirectory)) {
throw e;
}
}
return FileVisitResult.CONTINUE;
}
}

0 comments on commit 5f4b87c

Please sign in to comment.