diff --git a/htmlSanityCheck-core/src/main/java/org/aim42/htmlsanitycheck/report/HtmlReporter.java b/htmlSanityCheck-core/src/main/java/org/aim42/htmlsanitycheck/report/HtmlReporter.java index 71bd3d3e..448eb507 100644 --- a/htmlSanityCheck-core/src/main/java/org/aim42/htmlsanitycheck/report/HtmlReporter.java +++ b/htmlSanityCheck-core/src/main/java/org/aim42/htmlsanitycheck/report/HtmlReporter.java @@ -22,10 +22,10 @@ public class HtmlReporter extends Reporter { private static final Logger log = LoggerFactory.getLogger(HtmlReporter.class); private static final String REPORT_FILENAME = "index.html"; - private String resultsOutputDir; + private final String resultsOutputDir; private FileWriter writer; - public void write(String content) { + protected void write(String content) { try { writer.write(content); } catch (IOException e) { @@ -41,7 +41,7 @@ public HtmlReporter(PerRunResults runResults, String outputDir) { @Override public void initReport() { // determine a path where we can write our output file... - File outputFile = createOutputFile(resultsOutputDir, REPORT_FILENAME); + File outputFile = createOutputFile(resultsOutputDir); // init the private writer object try { @@ -79,13 +79,19 @@ private void copyResourceFromJarToDirectory(String resourceName, File outputDire // unfortunately we currently are not able to use try-with-ressources yet. InputStream stream = null; try { - stream = resource.openStream(); + if (resource != null) { + stream = resource.openStream(); + } else { + throw new IOException("Resource not found: " + resourceName); + } Files.copy(stream, new File(outputDirectory, resourceName).toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (final IOException e) { throw new UncheckedIOException(e); } finally { try { - stream.close(); + if (stream != null) { + stream.close(); + } } catch (IOException e) { throw new UncheckedIOException(e); } @@ -346,13 +352,10 @@ protected void reportSingleCheckSummary(SingleCheckResults singleCheckResults) { @Override protected void reportSingleCheckDetails(SingleCheckResults checkResults) { - if (checkResults.getFindings().size() > 0) { + if (!checkResults.getFindings().isEmpty()) { write("\n \n"); } } @@ -361,11 +364,10 @@ protected void reportSingleCheckDetails(SingleCheckResults checkResults) { * tries to find a writable directory. First tries dirName, * if that does not work takes User.dir as second choice. * - * @param dirName : e.g. /Users/aim42/projects/htmlsc/build/report/htmlchecks - * @param fileName : default "index.html" + * @param dirName : e.g. /Users/aim42/projects/htmlsc/build/report/htmlchecks * @return complete path to a writable file that does not currently exist. */ - private File createOutputFile(String dirName, String fileName) { + private File createOutputFile(String dirName) { File outputFolder = new File(dirName); if (!outputFolder.isDirectory() || !outputFolder.canWrite()) { @@ -374,9 +376,7 @@ private File createOutputFile(String dirName, String fileName) { } // make sure we really have an existing file! - File outputFile = new File(outputFolder, fileName); - - return outputFile; + return new File(outputFolder, HtmlReporter.REPORT_FILENAME); } diff --git a/htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/report/HtmlReporterTest.groovy b/htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/report/HtmlReporterTest.groovy new file mode 100644 index 00000000..7fca33cb --- /dev/null +++ b/htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/report/HtmlReporterTest.groovy @@ -0,0 +1,68 @@ +package org.aim42.htmlsanitycheck.report + +import org.aim42.htmlsanitycheck.collect.PerRunResults +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +import java.nio.file.Files + +import static org.junit.Assert.assertTrue +import static org.junit.Assert.fail + +class HtmlReporterTest { + + private HtmlReporter htmlReporter + private PerRunResults runResults + + @Rule + public TemporaryFolder tempDir = new TemporaryFolder() + + @Before + void setUp() { + runResults = new PerRunResults() + htmlReporter = new HtmlReporter(runResults, tempDir.getRoot().getAbsolutePath()) + } + + @Test + void testInitReport() { + htmlReporter.initReport() + File reportFile = new File(tempDir.getRoot(), "index.html") + assertTrue("Report file should be created", reportFile.exists()) + } + + @Test + void testWrite() { + htmlReporter.initReport() + htmlReporter.write("Test content") + htmlReporter.closeReport() + File reportFile = new File(tempDir.getRoot(), "index.html") + try { + String content = new String(Files.readAllBytes(reportFile.toPath())) + assertTrue("Report file should contain written content", content.contains("Test content")) + } catch (IOException e) { + fail("Failed to read report file: ${e}") + } + } + + // Add more test methods for other methods in HtmlReporter +} + +/*====================================================================== + +Copyright Gernot Starke and aim42 contributors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an + "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + ======================================================================*/