Skip to content

Commit

Permalink
#312 WIP Start unit test for HtmlReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ascheman committed Mar 28, 2024
1 parent 8654b38 commit f02c48f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 <ul>\n");
checkResults.getFindings().forEach(finding -> {

write(String.format(" <li> %s </li>\n", finding.toString()));
});
checkResults.getFindings().forEach(finding -> write(String.format(" <li> %s </li>\n", finding.toString())));
write(" </ul>\n");
}
}
Expand All @@ -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()) {
Expand All @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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.
======================================================================*/

0 comments on commit f02c48f

Please sign in to comment.