diff --git a/htmlSanityCheck-maven-plugin/src/main/java/org/aim42/htmlsanitycheck/maven/HtmlSanityCheckMojo.java b/htmlSanityCheck-maven-plugin/src/main/java/org/aim42/htmlsanitycheck/maven/HtmlSanityCheckMojo.java index 7d0309f4..67397620 100644 --- a/htmlSanityCheck-maven-plugin/src/main/java/org/aim42/htmlsanitycheck/maven/HtmlSanityCheckMojo.java +++ b/htmlSanityCheck-maven-plugin/src/main/java/org/aim42/htmlsanitycheck/maven/HtmlSanityCheckMojo.java @@ -186,8 +186,7 @@ public class HtmlSanityCheckMojo extends AbstractMojo { static PerRunResults performChekcs(Configuration myConfig) throws MojoExecutionException { try { AllChecksRunner allChecksRunner = new AllChecksRunner(myConfig); - PerRunResults allChecks = allChecksRunner.performAllChecks(); - return allChecks; + return allChecksRunner.performAllChecks(); } catch (IOException e) { throw new MojoExecutionException(e); } @@ -200,6 +199,11 @@ public void execute() throws MojoExecutionException { // Setup configuration Configuration myConfig = setupConfiguration(); + execute(myConfig); + + } + + void execute(Configuration myConfig) throws MojoExecutionException { // Check if configuration is valid try { myConfig.validate(); @@ -218,7 +222,6 @@ public void execute() throws MojoExecutionException { // Handle findings handleFindings(allChecks.nrOfFindingsOnAllPages(), myConfig); - } void handleFindings(int nrOfFindingsOnAllPages, Configuration config) throws MojoExecutionException { diff --git a/htmlSanityCheck-maven-plugin/src/test/java/org/aim42/htmlsanitycheck/maven/HtmlSanityCheckMojoTest.java b/htmlSanityCheck-maven-plugin/src/test/java/org/aim42/htmlsanitycheck/maven/HtmlSanityCheckMojoTest.java index fdc4227d..d1efb6f7 100644 --- a/htmlSanityCheck-maven-plugin/src/test/java/org/aim42/htmlsanitycheck/maven/HtmlSanityCheckMojoTest.java +++ b/htmlSanityCheck-maven-plugin/src/test/java/org/aim42/htmlsanitycheck/maven/HtmlSanityCheckMojoTest.java @@ -1,18 +1,26 @@ package org.aim42.htmlsanitycheck.maven; import org.aim42.htmlsanitycheck.Configuration; +import org.aim42.htmlsanitycheck.check.AllCheckers; import org.apache.maven.plugin.MojoExecutionException; import org.assertj.core.api.Assertions; import org.assertj.core.api.Assumptions; import org.junit.jupiter.api.Test; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.PrintStream; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashSet; +import java.util.Set; class HtmlSanityCheckMojoTest { + + final static String VALID_HTML = ""; + @Test void setupConfiguration() { HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo(); @@ -65,14 +73,30 @@ void createoutputDirs() throws IOException, MojoExecutionException { mojo.createoutputDirs(path.toFile(), "Fehlertext"); // Check - Assertions.assertThat(path.toFile().exists()).isTrue(); - Assertions.assertThat(path.toFile().canWrite()).isTrue(); - Assertions.assertThat(path.toFile().isDirectory()).isTrue(); + Assertions.assertThat(path.toFile()).exists(); + Assertions.assertThat(path.toFile()).canWrite(); + Assertions.assertThat(path.toFile()).isDirectory(); + + // Clean up + deleteDirectory(tempDir.toFile()); + } + + @Test + void createoutputDirsFail() throws IOException { + + // Set stage - Create a File, that is no dir to provoke an exception and create a mojo + Path tempDir = Files.createTempFile("MojoTest", ""); + + Assumptions.assumeThat(tempDir).isNotNull(); + + HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo(); + + // Check + Assertions.assertThatThrownBy(() -> mojo.createoutputDirs(tempDir.toFile(), "Fehlertext")) + .isInstanceOf(MojoExecutionException.class) + .hasMessageContaining("Fehlertext"); // Clean up - Files.deleteIfExists(path); - Files.deleteIfExists(tempDir.resolve("testdir/anotherTestdir")); - Files.deleteIfExists(tempDir.resolve("testdir")); Files.deleteIfExists(tempDir); } @@ -92,9 +116,50 @@ void handleFindings() throws IOException { .isInstanceOf(MojoExecutionException.class) .hasMessageContaining("2 error(s)"); - // Clea uo + // Clean up Files.deleteIfExists(tempDir); } + @Test + void execute() throws IOException, MojoExecutionException { + Path junitDir = Files.createTempDirectory("MojoJunit"); + Path resultDir = Files.createTempDirectory("MojoJunit"); + Path sourceDir = Files.createTempDirectory("MojoSource"); + sourceDir.toFile().deleteOnExit(); + File sourceFile = new File(sourceDir.toFile(), "test.html"); + Files.write(sourceFile.toPath(), VALID_HTML.getBytes(StandardCharsets.UTF_8)); + Set fileset = new HashSet<>(); + fileset.add(sourceFile); + + Configuration myConfig = Configuration.builder() + .checksToExecute(AllCheckers.CHECKER_CLASSES) + .junitResultsDir(junitDir.toFile()) + .checkingResultsDir(resultDir.toFile()) + .sourceDir(sourceDir.toFile()) + .sourceDocuments(fileset) + .build(); + HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo(); + + mojo.execute(myConfig); + + + // Clean up + deleteDirectory(junitDir.toFile()); + deleteDirectory(resultDir.toFile()); + } + + + // Helper functions + + void deleteDirectory(File directoryToBeDeleted) throws IOException { + File[] allContents = directoryToBeDeleted.listFiles(); + if (allContents != null) { + for (File file : allContents) { + deleteDirectory(file); + } + } + Files.deleteIfExists(directoryToBeDeleted.toPath()); + } + }