Skip to content

Commit 63ebe57

Browse files
Merge pull request #468 from telekom/fix/file-report-handling
Migrate DefaultReport from File to Path
2 parents 99d58db + 0edd406 commit 63ebe57

File tree

8 files changed

+125
-90
lines changed

8 files changed

+125
-90
lines changed

core/src/main/java/eu/tsystems/mms/tic/testframework/report/DefaultReport.java

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,73 +24,79 @@
2424
import eu.tsystems.mms.tic.testframework.report.model.context.Screenshot;
2525
import eu.tsystems.mms.tic.testframework.report.model.context.Video;
2626
import eu.tsystems.mms.tic.testframework.utils.FileUtils;
27+
import org.apache.commons.io.file.PathUtils;
2728

2829
import java.io.File;
2930
import java.io.IOException;
3031
import java.lang.annotation.Annotation;
32+
import java.nio.file.Files;
33+
import java.nio.file.Path;
3134
import java.util.Arrays;
3235
import java.util.Optional;
3336
import java.util.concurrent.ConcurrentHashMap;
3437

3538
public class DefaultReport implements Report, Loggable {
3639

37-
private File currentReportDirectory;
40+
private Path currentReportDirectory;
3841
private final String baseDir = Properties.BASE_DIR.asString();
39-
private final File finalReportDirectory = new File(baseDir);
40-
private final File tempReportDirectory;
42+
private final Path finalReportPath = Path.of(baseDir);
43+
private final Path tempReportDirectory;
4144
private final ConcurrentHashMap<Class<? extends Annotation>, AnnotationConverter> annotationConverters = new ConcurrentHashMap<>();
4245

4346
public DefaultReport() {
4447
FileUtils fileUtils = new FileUtils();
45-
tempReportDirectory = fileUtils.createTempDir(baseDir);
46-
log().debug("Prepare report in " + tempReportDirectory.getAbsolutePath());
47-
48+
tempReportDirectory = fileUtils.createTempDir("test-report");
49+
log().debug("Prepare report in " + tempReportDirectory.toAbsolutePath());
4850
currentReportDirectory = tempReportDirectory;
4951
}
5052

51-
private File addFile(File sourceFile, File directory, FileMode fileMode) {
53+
private Path addFile(Path sourceFile, Path directory, FileMode fileMode) {
5254
try {
53-
switch (fileMode) {
54-
case COPY:
55-
FileUtils.copyFileToDirectory(sourceFile, directory, true);
56-
break;
57-
default:
58-
case MOVE:
59-
FileUtils.moveFileToDirectory(sourceFile, directory, true);
60-
break;
55+
PathUtils.copyFileToDirectory(sourceFile, directory);
56+
if (fileMode == FileMode.MOVE) {
57+
Files.delete(sourceFile);
6158
}
6259
} catch (IOException e) {
6360
log().error("Could not add file", e);
6461
}
65-
return new File(directory, sourceFile.getName());
62+
return directory.resolve(sourceFile.getFileName());
6663
}
6764

68-
public File finalizeReport() {
65+
public Path finalizeReport() {
6966
try {
70-
if (finalReportDirectory.exists()) {
71-
FileUtils.deleteDirectory(finalReportDirectory);
67+
if (Files.exists(finalReportPath)) {
68+
PathUtils.deleteDirectory(finalReportPath);
7269
}
7370

74-
if (tempReportDirectory.exists()) {
71+
if (Files.exists(tempReportDirectory)) {
7572
log().debug("Temporary directory is {}", tempReportDirectory);
76-
FileUtils.moveDirectory(tempReportDirectory, finalReportDirectory);
77-
currentReportDirectory = finalReportDirectory;
78-
log().info("Report written to " + finalReportDirectory.getAbsolutePath());
73+
Files.createDirectories(finalReportPath);
74+
PathUtils.copyDirectory(tempReportDirectory, finalReportPath);
75+
currentReportDirectory = finalReportPath;
76+
log().info("Report written to " + finalReportPath.toAbsolutePath());
77+
}
78+
79+
try {
80+
PathUtils.deleteDirectory(tempReportDirectory);
81+
} catch (IOException e) {
82+
log().warn("Could not delete temporary directory {}", tempReportDirectory);
7983
}
8084
} catch (IOException e) {
81-
throw new RuntimeException("Could not move report dir: " + e.getMessage(), e);
85+
throw new RuntimeException("Could not copy report dir: " + e.getMessage(), e);
8286
}
83-
return finalReportDirectory;
87+
return finalReportPath;
8488
}
8589

8690
private void addScreenshotFiles(Screenshot screenshot, FileMode fileMode) {
87-
File screenshotsDirectory = getReportDirectory(SCREENSHOTS_FOLDER_NAME);
91+
Path screenshotsDirectory = getReportDirectory(SCREENSHOTS_FOLDER_NAME);
8892
if (screenshot.getScreenshotFile() != null) {
89-
screenshot.setFile(addFile(screenshot.getScreenshotFile(), screenshotsDirectory, fileMode));
93+
Path path = addFile(screenshot.getScreenshotFile().toPath(), screenshotsDirectory, fileMode);
94+
screenshot.setFile(path.toFile());
9095
}
9196

9297
screenshot.getPageSourceFile().ifPresent(file -> {
93-
screenshot.setPageSourceFile(addFile(file, screenshotsDirectory, fileMode));
98+
Path path = addFile(file.toPath(), screenshotsDirectory, fileMode);
99+
screenshot.setPageSourceFile(path.toFile());
94100
});
95101
}
96102

@@ -109,8 +115,9 @@ public Screenshot provideScreenshot(File file, FileMode fileMode) {
109115

110116
@Override
111117
public Report addVideo(Video video, FileMode fileMode) {
112-
File videoDirectory = getReportDirectory(VIDEO_FOLDER_NAME);
113-
video.setFile(addFile(video.getVideoFile(), videoDirectory, fileMode));
118+
Path videoDirectory = getReportDirectory(VIDEO_FOLDER_NAME);
119+
Path path = addFile(video.getVideoFile().toPath(), videoDirectory, fileMode);
120+
video.setFile(path.toFile());
114121
return this;
115122
}
116123

@@ -124,23 +131,23 @@ public Video provideVideo(File file, FileMode fileMode) {
124131
/**
125132
* @return Final report directory defined by the user
126133
*/
127-
public File getReportDirectory() {
134+
public Path getReportDirectory() {
128135
return currentReportDirectory;
129136
}
130137

131138
/**
132139
* @return Final report directory defined by the user
133140
*/
134-
public File getFinalReportDirectory() {
135-
return finalReportDirectory;
141+
public Path getFinalReportDirectory() {
142+
return finalReportPath;
136143
}
137144

138145
@Override
139146
public String getRelativePath(File file) {
140147
String absFilePath = file.getAbsolutePath();
141148

142-
for (File dir : Arrays.asList(tempReportDirectory, finalReportDirectory)) {
143-
String absDirPath = dir.getAbsolutePath();
149+
for (Path path : Arrays.asList(tempReportDirectory, finalReportPath)) {
150+
String absDirPath = path.toAbsolutePath().toString();
144151
if (absFilePath.startsWith(absDirPath)) {
145152
return absFilePath.replace(absDirPath, "");
146153
}

core/src/main/java/eu/tsystems/mms/tic/testframework/report/Report.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
import eu.tsystems.mms.tic.testframework.common.IProperties;
2525
import eu.tsystems.mms.tic.testframework.report.model.context.Screenshot;
2626
import eu.tsystems.mms.tic.testframework.report.model.context.Video;
27+
import eu.tsystems.mms.tic.testframework.utils.FileUtils;
28+
2729
import java.io.File;
2830
import java.lang.annotation.Annotation;
31+
import java.nio.file.Files;
32+
import java.nio.file.Path;
2933
import java.util.Optional;
3034

3135
public interface Report {
@@ -83,32 +87,35 @@ enum Mode {
8387

8488
Report addVideo(Video video, FileMode fileMode);
8589
Video provideVideo(File file, FileMode fileMode);
86-
File finalizeReport();
90+
Path finalizeReport();
8791

88-
File getReportDirectory();
92+
Path getReportDirectory();
8993

9094
/**
9195
* @param childName Child directory or file name
9296
* @return Final report sub directory defined by the user
9397
*/
94-
default File getReportDirectory(String childName) {
95-
File dir = new File(getReportDirectory(), childName);
96-
if (!dir.exists()) {
97-
dir.mkdirs();
98+
default Path getReportDirectory(String childName) {
99+
Path path = getReportDirectory().resolve(childName);
100+
if (!Files.exists(path)) {
101+
new FileUtils().createDirectoriesSafely(path);
98102
}
99-
return dir;
103+
return path;
100104
}
101-
default File getReportFile(String filePath) {
102-
File file = new File(getReportDirectory(), filePath);
103-
File dir = file.getParentFile();
104-
if (!dir.exists()) {
105-
dir.mkdirs();
105+
106+
default Path getReportFile(String filePath) {
107+
Path path = getReportDirectory().resolve(Path.of(filePath));
108+
Path dir = path.getParent();
109+
if (!Files.exists(dir)) {
110+
new FileUtils().createDirectoriesSafely(dir);
106111
}
107-
return file;
112+
return path;
108113
}
109-
File getFinalReportDirectory();
110-
default File getFinalReportDirectory(String childName) {
111-
return new File(getFinalReportDirectory(), childName);
114+
115+
Path getFinalReportDirectory();
116+
117+
default Path getFinalReportDirectory(String childName) {
118+
return getFinalReportDirectory().resolve(Path.of(childName));
112119
}
113120

114121
/**

core/src/main/java/eu/tsystems/mms/tic/testframework/utils/FileUtils.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import eu.tsystems.mms.tic.testframework.exceptions.SystemException;
2727
import eu.tsystems.mms.tic.testframework.logging.Loggable;
2828
import org.apache.commons.io.FilenameUtils;
29+
2930
import java.io.BufferedReader;
3031
import java.io.File;
3132
import java.io.FileInputStream;
@@ -37,6 +38,9 @@
3738
import java.net.URISyntaxException;
3839
import java.net.URL;
3940
import java.net.URLDecoder;
41+
import java.nio.file.FileAlreadyExistsException;
42+
import java.nio.file.Files;
43+
import java.nio.file.Path;
4044
import java.util.Objects;
4145
import java.util.UUID;
4246

@@ -261,9 +265,26 @@ public File createTempFileName(String fileName) {
261265
(extension.length() > 0 ? "." + extension : ""));
262266
}
263267

264-
public File createTempDir(String dirName) {
265-
File dir = new File(System.getProperty("java.io.tmpdir") + "/" + dirName + "-" + UUID.randomUUID());
266-
dir.mkdirs();
267-
return dir;
268+
public Path createTempDir(String dirName) {
269+
try {
270+
// Path dir = Path.of(System.getProperty("java.io.tmpdir"), dirName + "-" + UUID.randomUUID());
271+
272+
return Files.createTempDirectory(dirName + "_");
273+
} catch (IOException e) {
274+
throw new SystemException("Cannot create temporary folder " + dirName, e);
275+
}
276+
}
277+
278+
public boolean createDirectoriesSafely(Path path) {
279+
try {
280+
Files.createDirectories(path);
281+
return true;
282+
} catch (FileAlreadyExistsException e) {
283+
log().error("Folder already exists: {}", path.toAbsolutePath());
284+
return false;
285+
} catch (IOException e) {
286+
log().error("Cannot create folder {}: {}", path.toAbsolutePath(), e.getMessage());
287+
return false;
288+
}
268289
}
269290
}

core/src/main/java/eu/tsystems/mms/tic/testframework/utils/PdfUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.io.FileNotFoundException;
3737
import java.io.IOException;
3838
import java.io.InputStream;
39+
import java.nio.file.Path;
3940
import java.util.ArrayList;
4041
import java.util.List;
4142

@@ -280,9 +281,8 @@ private static String getParsedDocumentPath(String fileName) {
280281
}
281282

282283
FileUtils utils = new FileUtils();
283-
File tempDir = utils.createTempDir(fileName);
284-
285-
return tempDir.getAbsolutePath() + File.separator + fileName;
284+
Path tempDir = utils.createTempDir(fileName);
285+
return tempDir.resolve(fileName).toAbsolutePath().toString();
286286
}
287287

288288
private static void closeDocument(PDDocument pdDoc) {

integration-tests/src/test/java/eu/tsystems/mms/tic/testframework/test/pagefactory/PageFactoryTest.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,33 @@
3636
import org.testng.annotations.DataProvider;
3737
import org.testng.annotations.Test;
3838

39-
import java.io.File;
39+
import java.io.IOException;
40+
import java.nio.file.Files;
41+
import java.nio.file.Path;
4042

4143
/**
4244
* Tests the responsive page factory for correct instantiated classes.
4345
*/
4446
public class PageFactoryTest extends AbstractTestSitesTest implements PageFactoryProvider, PropertyManagerProvider {
4547

4648
@Test
47-
public void testT08_CheckPage_ScreenshotOnLoad() {
49+
public void testT08_CheckPage_ScreenshotOnLoad() throws IOException {
4850

49-
final File reportScreenshotDirectory = Testerra.getInjector().getInstance(Report.class).getReportDirectory(Report.SCREENSHOTS_FOLDER_NAME);
51+
final Path reportScreenshotDirectory = Testerra.getInjector().getInstance(Report.class).getReportDirectory(Report.SCREENSHOTS_FOLDER_NAME);
5052
Assert.assertNotNull(reportScreenshotDirectory);
5153

5254
final WebDriver driver = getWebDriver();
5355

54-
final int fileCountBeforeAction = getNumFiles(reportScreenshotDirectory);
56+
final long fileCountBeforeAction = getNumFiles(reportScreenshotDirectory);
5557
PROPERTY_MANAGER.setTestLocalProperty(Testerra.Properties.SCREENSHOT_ON_PAGELOAD, false);
5658
PAGE_FACTORY.createPage(PageWithExistingElement.class, driver);
5759

58-
final int fileCountAfterCheckPageWithoutScreenshot = getNumFiles(reportScreenshotDirectory);
60+
final long fileCountAfterCheckPageWithoutScreenshot = getNumFiles(reportScreenshotDirectory);
5961
Assert.assertEquals(fileCountBeforeAction, fileCountAfterCheckPageWithoutScreenshot, "Record Screenshot count not altered.");
6062

6163
PROPERTY_MANAGER.setTestLocalProperty(Testerra.Properties.SCREENSHOT_ON_PAGELOAD, true);
6264
PAGE_FACTORY.createPage(PageWithExistingElement.class, driver);
63-
final int fileCountAfterCheckPageWithScreenshot = getNumFiles(reportScreenshotDirectory);
65+
final long fileCountAfterCheckPageWithScreenshot = getNumFiles(reportScreenshotDirectory);
6466

6567
Assert.assertNotEquals(fileCountAfterCheckPageWithoutScreenshot, fileCountAfterCheckPageWithScreenshot, "Record Screenshot count altered.");
6668
}
@@ -112,13 +114,8 @@ public void testT12_LoopDetectionTest_DataProvider_ParallelFalse(String loop) {
112114
}
113115
}
114116

115-
private int getNumFiles(File directory) {
116-
File[] files = directory.listFiles();
117-
if (files == null) {
118-
return 0;
119-
} else {
120-
return files.length;
121-
}
117+
private long getNumFiles(Path directory) throws IOException {
118+
return Files.list(directory).count();
122119
}
123120

124121
}

report-model/src/main/java/eu/tsystems/mms/tic/testframework/listeners/GenerateJUnitXML2ReportListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ protected void generateReport(ITestContext context) {
198198

199199
document.pop();
200200

201-
Utils.writeUtf8File(report.getReportDirectory(Report.XML_FOLDER_NAME).getAbsolutePath(), XML_RESULT_FILENAME, document.toXML());
201+
Utils.writeUtf8File(report.getReportDirectory(Report.XML_FOLDER_NAME).toAbsolutePath().toString(), XML_RESULT_FILENAME, document.toXML());
202202
}
203203

204204
static String formattedTime() {

report-ng/src/main/java/eu/tsystems/mms/tic/testframework/hook/ReportNgHook.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import eu.tsystems.mms.tic.testframework.listeners.GenerateReportNgModelListener;
3131
import eu.tsystems.mms.tic.testframework.report.Report;
3232

33+
import java.nio.file.Path;
34+
3335
public class ReportNgHook extends AbstractModule implements ModuleHook {
3436

3537
@Override
@@ -39,7 +41,7 @@ public void init() {
3941
Report report = Testerra.getInjector().getInstance(Report.class);
4042

4143
eventBus.register(new CopyReportAppListener(report.getReportDirectory()));
42-
eventBus.register(new GenerateReportNgModelListener(report.getReportDirectory("report-ng/model")));
44+
eventBus.register(new GenerateReportNgModelListener(report.getReportDirectory().resolve(Path.of("report-ng/model")).toFile()));
4345
}
4446

4547
@Override

0 commit comments

Comments
 (0)