Skip to content

Commit

Permalink
WIP 314 Increase test coverage on mojo Part II
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Ruhroth committed Dec 19, 2024
1 parent 1aa4133 commit 07b22c7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
Expand All @@ -218,7 +222,6 @@ public void execute() throws MojoExecutionException {

// Handle findings
handleFindings(allChecks.nrOfFindingsOnAllPages(), myConfig);

}

void handleFindings(int nrOfFindingsOnAllPages, Configuration config) throws MojoExecutionException {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"><html><head></head><body></body><html>";

@Test
void setupConfiguration() {
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo();
Expand Down Expand Up @@ -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);
}

Expand All @@ -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<File> 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());
}


}

0 comments on commit 07b22c7

Please sign in to comment.