-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP 314 Increase test coverage on mojo
- Loading branch information
Thomas Ruhroth
committed
Dec 19, 2024
1 parent
490ede5
commit 1aa4133
Showing
3 changed files
with
132 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 87 additions & 2 deletions
89
...k-maven-plugin/src/test/java/org/aim42/htmlsanitycheck/maven/HtmlSanityCheckMojoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,100 @@ | ||
package org.aim42.htmlsanitycheck.maven; | ||
|
||
import org.aim42.htmlsanitycheck.Configuration; | ||
|
||
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.IOException; | ||
import java.io.PrintStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
class HtmlSanityCheckMojoTest { | ||
@Test | ||
void setupConfiguration() { | ||
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo(); | ||
Configuration config = mojo.setupConfiguration(); | ||
Assertions.assertThat(config).isNotNull(); | ||
Assertions.assertThat(config.getFailOnErrors()).isFalse(); | ||
} | ||
|
||
|
||
@Test | ||
void logBuildParameter() { | ||
|
||
// Write System.out and System.err to a stream. Keep the originals | ||
ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | ||
ByteArrayOutputStream errContent = new ByteArrayOutputStream(); | ||
PrintStream originalOut = System.out; | ||
PrintStream originalErr = System.err; | ||
System.setOut(new PrintStream(outContent)); | ||
System.setErr(new PrintStream(errContent)); | ||
|
||
// Run the code | ||
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo(); | ||
mojo.logBuildParameter(); | ||
|
||
// Reset System.out and System.err | ||
System.setOut(originalOut); | ||
System.setErr(originalErr); | ||
|
||
|
||
// Check Output | ||
Assertions.assertThat(errContent.toString()).isEmpty(); | ||
Assertions.assertThat(outContent.toString()) | ||
.contains("[info] Parameters given to sanityCheck plugin from Maven buildfile...") | ||
.contains("[info] Files to check : null"); | ||
} | ||
|
||
@Test | ||
void createoutputDirs() throws IOException, MojoExecutionException { | ||
|
||
// Set stage - Get a directory to safely work on and a mojo | ||
Path tempDir = Files.createTempDirectory("MojoTest"); | ||
|
||
Assumptions.assumeThat(tempDir).isNotNull(); | ||
|
||
Path path = tempDir.resolve("testdir/anotherTestdir/dir"); | ||
|
||
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo(); | ||
|
||
// The code to test | ||
mojo.createoutputDirs(path.toFile(), "Fehlertext"); | ||
|
||
// Check | ||
Assertions.assertThat(path.toFile().exists()).isTrue(); | ||
Assertions.assertThat(path.toFile().canWrite()).isTrue(); | ||
Assertions.assertThat(path.toFile().isDirectory()).isTrue(); | ||
|
||
// Clean up | ||
Files.deleteIfExists(path); | ||
Files.deleteIfExists(tempDir.resolve("testdir/anotherTestdir")); | ||
Files.deleteIfExists(tempDir.resolve("testdir")); | ||
Files.deleteIfExists(tempDir); | ||
} | ||
|
||
|
||
@Test | ||
void handleFindings() throws IOException { | ||
// Set stage - Get a directory to safely work on, a configuration and a mojo | ||
Path tempDir = Files.createTempDirectory("MojoTest"); | ||
Configuration config = Configuration.builder() | ||
.failOnErrors(true) | ||
.checkingResultsDir(tempDir.toFile()) | ||
.build(); | ||
HtmlSanityCheckMojo mojo = new HtmlSanityCheckMojo(); | ||
|
||
//Check | ||
Assertions.assertThatThrownBy(() -> mojo.handleFindings(2, config)) | ||
.isInstanceOf(MojoExecutionException.class) | ||
.hasMessageContaining("2 error(s)"); | ||
|
||
// Clea uo | ||
Files.deleteIfExists(tempDir); | ||
} | ||
} | ||
|
||
|
||
} |