Skip to content

Commit 3fad99e

Browse files
aschemanclaude
andcommitted
#405 Improve test coverage for JUnitXmlReporter
Add tests to improve code coverage for exception handling and both FLAT and HIERARCHICAL output modes. - testFlatModeCreatesEncodedFilename: Tests explicit FLAT mode - testFlatModeIsDefaultWhenNotSpecified: Tests default behavior - testHierarchicalModeFailsWhenCannotCreateDirectory: Tests exception when directory creation fails in HIERARCHICAL mode - Fix testInitReportWithNonWritableDirectory to properly test exception when output path cannot be created These tests address Sonar coverage gaps in: - Line 64: initReport() exception handling - Line 165: getHierarchicalOutputFile() directory creation error - getFlatOutputFile() method coverage Coverage improvements ensure both output modes and exception paths are properly tested. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d076086 commit 3fad99e

1 file changed

Lines changed: 84 additions & 8 deletions

File tree

htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/report/JUnitXmlReporterTest.groovy

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ class JUnitXmlReporterTest {
5454
}
5555

5656
@Test(expected = RuntimeException.class)
57-
void testInitReportWithNonWritableDirectory() throws IOException {
58-
// Create a temporary directory
59-
File tempDir = tempFolder.newFolder()
57+
void testInitReportWithNonWritableDirectory() {
58+
// Create a path that cannot be created (using a non-existent parent and restricted path)
59+
File nonExistentPath = new File("/nonexistent/path/that/cannot/be/created")
6060

61-
// Make the directory non-writable
62-
assertTrue("Could not make temp directory non-writable", tempDir.setWritable(false))
63-
64-
// Create a new JUnitXmlReporter with the non-writable directory
61+
// Try to create a JUnitXmlReporter with a path that cannot be created
6562
PerRunResults runResults = new PerRunResults()
66-
new JUnitXmlReporter(runResults, tempDir.getAbsolutePath()).initReport()
63+
JUnitXmlReporter reporter = new JUnitXmlReporter(runResults, nonExistentPath.getAbsolutePath())
64+
65+
// This should throw RuntimeException because the path cannot be created
66+
reporter.initReport()
6767
}
6868

6969
@Test
@@ -232,8 +232,84 @@ class JUnitXmlReporterTest {
232232
return null
233233
}
234234

235+
// Tests for FLAT output style (default, backwards compatible)
236+
237+
@Test
238+
void testFlatModeCreatesEncodedFilename() {
239+
// Given: a page with a nested path
240+
SinglePageResults pageWithPath = new SinglePageResults(
241+
"about.html",
242+
"docs/guide/about.html",
243+
"About Page",
244+
1000,
245+
new ArrayList<>())
246+
PerRunResults runResults = new PerRunResults()
247+
runResults.addPageResults(pageWithPath)
248+
249+
// When: we generate the report in FLAT mode (explicit)
250+
new JUnitXmlReporter(runResults, outputPath.absolutePath, Configuration.JunitOutputStyle.FLAT)
251+
.reportPageSummary(pageWithPath)
252+
253+
// Then: the file should be created in the root with encoded path
254+
File[] files = outputPath.listFiles()
255+
assertEquals("Should have exactly one file in root", 1, files.length)
256+
assertTrue("Filename should contain encoded path",
257+
files[0].name.contains("docs") && files[0].name.contains("guide"))
258+
assertTrue("Filename should start with TEST-unit-html-", files[0].name.startsWith("TEST-unit-html-"))
259+
260+
def testsuite = new XmlSlurper().parse(files[0])
261+
assertEquals("docs/guide/about.html", testsuite.@name.text())
262+
}
263+
264+
@Test
265+
void testFlatModeIsDefaultWhenNotSpecified() {
266+
// Given: a page with a nested path
267+
SinglePageResults pageWithPath = new SinglePageResults(
268+
"about.html",
269+
"docs/guide/about.html",
270+
"About Page",
271+
1000,
272+
new ArrayList<>())
273+
PerRunResults runResults = new PerRunResults()
274+
runResults.addPageResults(pageWithPath)
275+
276+
// When: we generate the report WITHOUT specifying mode (should default to FLAT)
277+
new JUnitXmlReporter(runResults, outputPath.absolutePath)
278+
.reportPageSummary(pageWithPath)
279+
280+
// Then: the file should be created in the root with encoded path (FLAT behavior)
281+
File[] files = outputPath.listFiles()
282+
assertEquals("Should have exactly one file in root", 1, files.length)
283+
assertTrue("Filename should contain encoded path",
284+
files[0].name.contains("docs") && files[0].name.contains("guide"))
285+
286+
def testsuite = new XmlSlurper().parse(files[0])
287+
assertEquals("docs/guide/about.html", testsuite.@name.text())
288+
}
289+
235290
// Tests for hierarchical directory structure (issue #405)
236291

292+
@Test(expected = RuntimeException.class)
293+
void testHierarchicalModeFailsWhenCannotCreateDirectory() {
294+
// Given: an output path that's a file (not a directory)
295+
File tempFile = File.createTempFile("test", ".txt")
296+
tempFile.deleteOnExit()
297+
298+
SinglePageResults pageWithPath = new SinglePageResults(
299+
"about.html",
300+
"docs/guide/about.html",
301+
"About Page",
302+
1000,
303+
new ArrayList<>())
304+
PerRunResults runResults = new PerRunResults()
305+
runResults.addPageResults(pageWithPath)
306+
307+
// When: we try to generate a report in HIERARCHICAL mode with a file as output path
308+
// Then: it should throw RuntimeException because it cannot create subdirectories
309+
new JUnitXmlReporter(runResults, tempFile.getAbsolutePath(), Configuration.JunitOutputStyle.HIERARCHICAL)
310+
.reportPageSummary(pageWithPath)
311+
}
312+
237313
@Test
238314
void testSimpleFilenameCreatesFileInRootDirectory() {
239315
// Given: a page with a simple filename (no directory path)

0 commit comments

Comments
 (0)