Skip to content

Commit

Permalink
Ensure that HTML report resource files are only copied if they are no…
Browse files Browse the repository at this point in the history
…t already present.
  • Loading branch information
wakaleo committed Oct 22, 2015
1 parent f195e49 commit 496320a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import static net.thucydides.core.reports.html.HtmlResourceCopier.copyHtmlResourcesFrom;

/**
* An HTML report generates reports in a given directory and uses resources (images,...) from another.
Expand Down Expand Up @@ -66,21 +69,16 @@ protected EnvironmentVariables getEnvironmentVariables() {
return environmentVariables;
}

private boolean alreadyCopied = false;
private static AtomicBoolean alreadyCopied = new AtomicBoolean(false);

protected void copyResourcesToOutputDirectory() throws IOException {
if (!alreadyCopied) {
alreadyCopied = true;

if (!alreadyCopied.getAndSet(true)) {
updateResourceDirectoryFromSystemPropertyIfDefined();
copyResources();
copyHtmlResourcesFrom(getResourceDirectory()).to(getOutputDirectory());
}
}

private void copyResources() throws IOException {
HtmlResourceCopier copier = new HtmlResourceCopier(getResourceDirectory());
copier.copyHTMLResourcesTo(getOutputDirectory());
}

protected void copyTestResultsToOutputDirectory() throws IOException {
Path sourcePath = getSourceDirectoryOrDefault().toPath();
Path destinationPath = getOutputDirectory().toPath();
Expand All @@ -92,22 +90,8 @@ protected void copyTestResultsToOutputDirectory() throws IOException {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.regex.Pattern;

Expand All @@ -25,7 +27,11 @@ public HtmlResourceCopier(final String resourceDirectory) {
* src/main/resources/reports directory. When the jar is deployed, they will
* end up on the classpath.
*/
public void copyHTMLResourcesTo(final File targetDirectory) throws IOException {
public void to(final File targetDirectory) throws IOException {

if (resourceFilesAreAlreadyPresentIn(targetDirectory)) {
return;
}

Pattern resourcePattern = allFilesInDirectory(resourceDirectory);
FileResources fileResource = FileResources.from(resourceDirectory);
Expand All @@ -40,8 +46,22 @@ public void copyHTMLResourcesTo(final File targetDirectory) throws IOException {
targetDirectory);
}
}
recordResourceMarkerIn(targetDirectory);
}

private void recordResourceMarkerIn(File targetDirectory) throws IOException {
resourceMarkerIn(targetDirectory).toFile().createNewFile();
}

private boolean resourceFilesAreAlreadyPresentIn(File targetDirectory) {
return Files.exists(resourceMarkerIn(targetDirectory));
}

private Path resourceMarkerIn(File outputDirectory) {
return outputDirectory.toPath().resolve("serenity-resources");
}


private boolean fileResourceFromAJar(final String resourcePath) {
return (resourceIsFromAJar(resourcePath)
&& (thisIsNotTheRoot(resourcePath))
Expand Down Expand Up @@ -72,4 +92,8 @@ private Pattern allFilesInDirectory(final String directory) {
private boolean resourceIsFromAJar(final String resourcePath) {
return !resourcePath.startsWith("/");
}

public static HtmlResourceCopier copyHtmlResourcesFrom(String resourceDirectory) {
return new HtmlResourceCopier(resourceDirectory);
}
}

0 comments on commit 496320a

Please sign in to comment.