Skip to content

Commit 2d1b66c

Browse files
committed
#429 Add test coverage for Maven plugin
Increases test count from 10 to 18 tests to improve SonarCloud coverage for HtmlSanityCheckMojo. - Add tests for HTTP status code overrides (success/error/warning) - Add tests for findHtmlFiles() edge cases (null, non-existent, empty) - Verify non-HTML files are properly ignored - Refactor setField() helper to shared static method
1 parent 55fbf5d commit 2d1b66c

1 file changed

Lines changed: 155 additions & 13 deletions

File tree

htmlSanityCheck-maven-plugin/src/test/java/org/aim42/htmlsanitycheck/maven/HtmlSanityCheckMojoTest.java

Lines changed: 155 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,68 @@ void setupConfiguration() {
3636
Assertions.assertThat(config.getFailOnErrors()).isFalse();
3737
}
3838

39+
@Test
40+
void setupConfigurationWithHttpSuccessCodes() throws Exception {
41+
// Create mojo with custom HTTP success codes
42+
Set<Integer> customSuccessCodes = new HashSet<>();
43+
customSuccessCodes.add(299);
44+
45+
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo();
46+
setField(mojo, "httpSuccessCodes", customSuccessCodes);
47+
48+
Configuration config = mojo.setupConfiguration();
49+
50+
Assertions.assertThat(config).isNotNull();
51+
Assertions.assertThat(config.getHttpSuccessCodes()).contains(299);
52+
}
53+
54+
@Test
55+
void setupConfigurationWithHttpErrorCodes() throws Exception {
56+
// Create mojo with custom HTTP error codes
57+
Set<Integer> customErrorCodes = new HashSet<>();
58+
customErrorCodes.add(599);
59+
60+
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo();
61+
setField(mojo, "httpErrorCodes", customErrorCodes);
62+
63+
Configuration config = mojo.setupConfiguration();
64+
65+
Assertions.assertThat(config).isNotNull();
66+
Assertions.assertThat(config.getHttpErrorCodes()).contains(599);
67+
}
68+
69+
@Test
70+
void setupConfigurationWithHttpWarningCodes() throws Exception {
71+
// Create mojo with custom HTTP warning codes
72+
Set<Integer> customWarningCodes = new HashSet<>();
73+
customWarningCodes.add(199);
74+
75+
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo();
76+
setField(mojo, "httpWarningCodes", customWarningCodes);
77+
78+
Configuration config = mojo.setupConfiguration();
79+
80+
Assertions.assertThat(config).isNotNull();
81+
Assertions.assertThat(config.getHttpWarningCodes()).contains(199);
82+
}
83+
84+
@Test
85+
void setupConfigurationWithEmptyHttpStatusCodesShouldNotOverride() throws Exception {
86+
// Create mojo with empty HTTP status code sets (should not override defaults)
87+
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo();
88+
setField(mojo, "httpSuccessCodes", new HashSet<Integer>());
89+
setField(mojo, "httpErrorCodes", new HashSet<Integer>());
90+
setField(mojo, "httpWarningCodes", new HashSet<Integer>());
91+
92+
Configuration config = mojo.setupConfiguration();
93+
94+
// Verify that default codes are still present (not overridden by empty sets)
95+
Assertions.assertThat(config).isNotNull();
96+
Assertions.assertThat(config.getHttpSuccessCodes()).contains(200); // Default success code
97+
Assertions.assertThat(config.getHttpErrorCodes()).contains(404); // Default error code
98+
Assertions.assertThat(config.getHttpWarningCodes()).contains(301); // Default warning code (redirect)
99+
}
100+
39101

40102
@Test
41103
void logBuildParameter() {
@@ -226,6 +288,83 @@ void executeWithOnlySourceDir_ShouldSucceed() throws IOException, MojoExecutionE
226288
deleteDirectory(resultDir.toFile());
227289
}
228290

291+
@Test
292+
void findHtmlFilesWithNullDirectory() throws Exception {
293+
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo();
294+
setField(mojo, "sourceDir", null);
295+
setField(mojo, "sourceDocuments", null);
296+
297+
Configuration config = mojo.setupConfiguration();
298+
299+
// When both sourceDir and sourceDocuments are null, the config should have null sourceDocuments
300+
// (This will be caught by validation)
301+
Assertions.assertThat(config.getSourceDocuments()).isNull();
302+
}
303+
304+
@Test
305+
void findHtmlFilesWithNonExistentDirectory() throws Exception {
306+
Path nonExistentDir = java.nio.file.Paths.get("/tmp/this-directory-does-not-exist-" + System.currentTimeMillis());
307+
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo();
308+
setField(mojo, "sourceDir", nonExistentDir.toFile());
309+
setField(mojo, "sourceDocuments", null);
310+
311+
Configuration config = mojo.setupConfiguration();
312+
313+
// Should return empty set when directory doesn't exist
314+
Assertions.assertThat(config.getSourceDocuments()).isEmpty();
315+
}
316+
317+
@Test
318+
void findHtmlFilesWithEmptyDirectory() throws Exception {
319+
Path emptyDir = Files.createTempDirectory("MojoEmpty");
320+
321+
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo();
322+
setField(mojo, "sourceDir", emptyDir.toFile());
323+
setField(mojo, "sourceDocuments", null);
324+
325+
Configuration config = mojo.setupConfiguration();
326+
327+
// Should return empty set when directory is empty
328+
Assertions.assertThat(config.getSourceDocuments()).isEmpty();
329+
330+
// Clean up
331+
Files.deleteIfExists(emptyDir);
332+
}
333+
334+
@Test
335+
void findHtmlFilesIgnoresNonHtmlFiles() throws Exception {
336+
Path sourceDir = Files.createTempDirectory("MojoSource");
337+
338+
// Create various non-HTML files
339+
File txtFile = new File(sourceDir.toFile(), "readme.txt");
340+
Files.write(txtFile.toPath(), "Text content".getBytes(StandardCharsets.UTF_8));
341+
342+
File pdfFile = new File(sourceDir.toFile(), "document.pdf");
343+
Files.write(pdfFile.toPath(), "PDF content".getBytes(StandardCharsets.UTF_8));
344+
345+
File xmlFile = new File(sourceDir.toFile(), "config.xml");
346+
Files.write(xmlFile.toPath(), "<xml/>".getBytes(StandardCharsets.UTF_8));
347+
348+
// Create one HTML file
349+
File htmlFile = new File(sourceDir.toFile(), "page.html");
350+
Files.write(htmlFile.toPath(), VALID_HTML.getBytes(StandardCharsets.UTF_8));
351+
352+
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo();
353+
setField(mojo, "sourceDir", sourceDir.toFile());
354+
setField(mojo, "sourceDocuments", null);
355+
356+
Configuration config = mojo.setupConfiguration();
357+
358+
// Should only find the HTML file
359+
Assertions.assertThat(config.getSourceDocuments()).hasSize(1);
360+
Assertions.assertThat(config.getSourceDocuments())
361+
.extracting(File::getName)
362+
.containsExactly("page.html");
363+
364+
// Clean up
365+
deleteDirectory(sourceDir.toFile());
366+
}
367+
229368
@Test
230369
void executeWithOnlySourceDir_ShouldIncludeHtmFiles() throws IOException, MojoExecutionException {
231370
// Setup: Create temp directories
@@ -283,23 +422,16 @@ static class TestableHtmlSanityCheckMojo extends HtmlSanityCheckMojo {
283422
File checkingResultsDir, File junitResultsDir) {
284423
// Use reflection to set private fields
285424
try {
286-
setField(this, "sourceDir", sourceDir);
287-
setField(this, "sourceDocuments", sourceDocuments);
288-
setField(this, "checkingResultsDir", checkingResultsDir);
289-
setField(this, "junitResultsDir", junitResultsDir);
290-
setField(this, "checkerClasses", AllCheckers.CHECKER_CLASSES);
291-
setField(this, "excludes", new HashSet<String>());
425+
HtmlSanityCheckMojoTest.setField(this, "sourceDir", sourceDir);
426+
HtmlSanityCheckMojoTest.setField(this, "sourceDocuments", sourceDocuments);
427+
HtmlSanityCheckMojoTest.setField(this, "checkingResultsDir", checkingResultsDir);
428+
HtmlSanityCheckMojoTest.setField(this, "junitResultsDir", junitResultsDir);
429+
HtmlSanityCheckMojoTest.setField(this, "checkerClasses", AllCheckers.CHECKER_CLASSES);
430+
HtmlSanityCheckMojoTest.setField(this, "excludes", new HashSet<String>());
292431
} catch (Exception e) {
293432
throw new RuntimeException("Failed to set fields", e);
294433
}
295434
}
296-
297-
private void setField(Object target, String fieldName, Object value)
298-
throws NoSuchFieldException, IllegalAccessException {
299-
java.lang.reflect.Field field = HtmlSanityCheckMojo.class.getDeclaredField(fieldName);
300-
field.setAccessible(true);
301-
field.set(target, value);
302-
}
303435
}
304436

305437

@@ -315,5 +447,15 @@ void deleteDirectory(File directoryToBeDeleted) throws IOException {
315447
Files.deleteIfExists(directoryToBeDeleted.toPath());
316448
}
317449

450+
/**
451+
* Helper method to set private fields on HtmlSanityCheckMojo for testing
452+
*/
453+
private static void setField(Object target, String fieldName, Object value)
454+
throws NoSuchFieldException, IllegalAccessException {
455+
java.lang.reflect.Field field = HtmlSanityCheckMojo.class.getDeclaredField(fieldName);
456+
field.setAccessible(true);
457+
field.set(target, value);
458+
}
459+
318460

319461
}

0 commit comments

Comments
 (0)