Skip to content

Commit

Permalink
Migrate CopyReportAppListener from File to Path
Browse files Browse the repository at this point in the history
  • Loading branch information
martingrossmann committed Jan 29, 2025
1 parent 85925dc commit 8adcf29
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,16 @@ public DefaultReport() {
currentReportDirectory = tempReportDirectory;
}

// TODO: Migrate to Path
private File addFile(File sourceFile, File directory, FileMode fileMode) {
private Path addFile(Path sourceFile, Path directory, FileMode fileMode) {
try {
switch (fileMode) {
case COPY:
FileUtils.copyFileToDirectory(sourceFile, directory, true);
break;
default:
case MOVE:
FileUtils.moveFileToDirectory(sourceFile, directory, true);
break;
PathUtils.copyFileToDirectory(sourceFile, directory);
if (fileMode == FileMode.MOVE) {
Files.delete(sourceFile);
}
} catch (IOException e) {
log().error("Could not add file", e);
}
return new File(directory, sourceFile.getName());
return directory.resolve(sourceFile.getFileName());
}

public Path finalizeReport() {
Expand Down Expand Up @@ -95,11 +89,13 @@ public Path finalizeReport() {
private void addScreenshotFiles(Screenshot screenshot, FileMode fileMode) {
Path screenshotsDirectory = getReportDirectory(SCREENSHOTS_FOLDER_NAME);
if (screenshot.getScreenshotFile() != null) {
screenshot.setFile(addFile(screenshot.getScreenshotFile(), screenshotsDirectory.toFile(), fileMode));
Path path = addFile(screenshot.getScreenshotFile().toPath(), screenshotsDirectory, fileMode);
screenshot.setFile(path.toFile());
}

screenshot.getPageSourceFile().ifPresent(file -> {
screenshot.setPageSourceFile(addFile(file, screenshotsDirectory.toFile(), fileMode));
Path path = addFile(file.toPath(), screenshotsDirectory, fileMode);
screenshot.setPageSourceFile(path.toFile());
});
}

Expand All @@ -119,7 +115,8 @@ public Screenshot provideScreenshot(File file, FileMode fileMode) {
@Override
public Report addVideo(Video video, FileMode fileMode) {
Path videoDirectory = getReportDirectory(VIDEO_FOLDER_NAME);
video.setFile(addFile(video.getVideoFile(), videoDirectory.toFile(), fileMode));
Path path = addFile(video.getVideoFile().toPath(), videoDirectory, fileMode);
video.setFile(path.toFile());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void init() {

Report report = Testerra.getInjector().getInstance(Report.class);

eventBus.register(new CopyReportAppListener(report.getReportDirectory().toFile()));
eventBus.register(new CopyReportAppListener(report.getReportDirectory()));
eventBus.register(new GenerateReportNgModelListener(report.getReportDirectory().resolve(Path.of("report-ng/model")).toFile()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import com.google.common.eventbus.Subscribe;
import eu.tsystems.mms.tic.testframework.events.FinalizeExecutionEvent;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import java.net.URL;
import org.apache.commons.lang3.StringUtils;
import java.io.File;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
Expand All @@ -40,15 +40,13 @@
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;

public class CopyReportAppListener implements FinalizeExecutionEvent.Listener, Loggable {

private File targetDir;
private final Path targetPath;

// TODO: Migrate to Path
public CopyReportAppListener(File targetDir) {
this.targetDir = targetDir;
public CopyReportAppListener(Path targetPath) {
this.targetPath = targetPath;
}

@Subscribe
Expand All @@ -61,8 +59,9 @@ public void onFinalizeExecution(FinalizeExecutionEvent event) {
if (resourceStream == null) {
throw new Exception("Could not open stream: " + stringRepresentationOfResourcePath);
}
final File targetFile = new File(this.targetDir, resourcePath.toString());
FileUtils.copyInputStreamToFile(resourceStream, targetFile);
final Path finalPath = targetPath.resolve(resourcePath.toString());
Files.createDirectories(finalPath.getParent());
Files.copy(resourceStream, finalPath);
}
}
} catch (Exception e) {
Expand All @@ -73,19 +72,20 @@ public void onFinalizeExecution(FinalizeExecutionEvent event) {
/**
* This methods tries to read all files from a resource directory.
* When this methods runs inside a JAR, it reads the directory listing from the JAR's resource folder:
* application.jar#/folder/...
* application.jar#/folder/...
* and maps them to relative resource paths:
* /folder/file1.jpg
* /folder/file2.js
* ...
* /folder/file1.jpg
* /folder/file2.js
* ...
* Otherwise, it reads the directory listing from the local resource folder:
* ../build/resources/main/folder/...
* ../build/resources/main/folder/...
* and maps the files to relative resource paths:
* /folder/file1.jpg
* /folder/file2.js
* ...
*
* /folder/file1.jpg
* /folder/file2.js
* ...
* <p>
* In both cases, you need to read the actual resource by {@link ClassLoader#getResource(String)}.
*
* @see {https://mkyong.com/java/java-read-a-file-from-resources-folder/}
*/
private Stream<Path> getPathsFromResource(String folder) throws URISyntaxException, IOException {
Expand Down

0 comments on commit 8adcf29

Please sign in to comment.