|
| 1 | +package org.utplsql.maven.plugin; |
| 2 | + |
| 3 | +import org.apache.commons.io.FileUtils; |
| 4 | +import org.junit.jupiter.api.Assertions; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.util.stream.Collectors; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 13 | + |
| 14 | +public class ReportChecker { |
| 15 | + |
| 16 | + private ReportChecker() { |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Duration is set to 1 before comparing contents as it is always different. |
| 21 | + * Path separator is set to "/" to ensure windows / linux / mac compatibility. |
| 22 | + * \r and \n are removed to provide simpler comparison. |
| 23 | + * |
| 24 | + * @param testClass Class under test |
| 25 | + * @param testFolder Folder name |
| 26 | + * @param files Files to compare |
| 27 | + */ |
| 28 | + public static void checkReports(Class<?> testClass, String testFolder, String... files) throws IOException { |
| 29 | + String fullyQualifiedClassNameDirectory = testClass.getName().replace(".", "/"); |
| 30 | + |
| 31 | + for (String filename : files) { |
| 32 | + File expectedOutputFile = new File("target/maven-it/" + fullyQualifiedClassNameDirectory + "/" + testFolder + "/project/expected-output/utplsql", filename); |
| 33 | + File outputFile = new File("target/maven-it/" + fullyQualifiedClassNameDirectory + "/" + testFolder + "/project/target/utplsql", filename); |
| 34 | + assertTrue(outputFile.exists(), "The report " + outputFile.getPath() + " was not generated"); |
| 35 | + |
| 36 | + try (Stream<String> stream = Files.lines(outputFile.toPath())) { |
| 37 | + String outputContent = stream |
| 38 | + .filter(line -> !line.contains("<?xml")) |
| 39 | + .map(line -> line.replaceAll("(duration=\"[0-9.]*\")", "duration=\"1\"")) |
| 40 | + .map(line -> line.replaceAll("\\\\", "/")) |
| 41 | + .map(line -> line.replaceAll("\r", "").replaceAll("\n", "")) |
| 42 | + .collect(Collectors.joining("\n")); |
| 43 | + |
| 44 | + Assertions.assertEquals( |
| 45 | + FileUtils.readFileToString(expectedOutputFile, "utf-8") |
| 46 | + .replace("\r", "") |
| 47 | + .replace("\n", ""), |
| 48 | + outputContent.replace("\n", ""), "The files differ!"); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Check if a report file exits |
| 55 | + * |
| 56 | + * @param testClass Class under test |
| 57 | + * @param testFolder Folder name |
| 58 | + * @param filename File Name |
| 59 | + * @return true or false |
| 60 | + */ |
| 61 | + public static boolean reportExists(Class<?> testClass, String testFolder, String filename) { |
| 62 | + String fullyQualifiedClassNameDirectory = testClass.getName().replace(".", "/"); |
| 63 | + |
| 64 | + File outputFile = new File("target/maven-it/" + fullyQualifiedClassNameDirectory + "/" + testFolder + "/project/target/utplsql", filename); |
| 65 | + return outputFile.exists(); |
| 66 | + } |
| 67 | +} |
0 commit comments