-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract MojoMocker + reduce TestUtils methods
- Loading branch information
1 parent
118f74c
commit d3fa92f
Showing
17 changed files
with
206 additions
and
110 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
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
5 changes: 4 additions & 1 deletion
5
asciidoctor-maven-plugin/src/test/java/org/asciidoctor/maven/AsciidoctorAsserter.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
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
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
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
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
46 changes: 46 additions & 0 deletions
46
asciidoctor-maven-plugin/src/test/java/org/asciidoctor/maven/test/MojoMocker.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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.asciidoctor.maven.test; | ||
|
||
import lombok.SneakyThrows; | ||
import org.apache.maven.plugin.logging.SystemStreamLog; | ||
import org.apache.maven.project.MavenProject; | ||
import org.asciidoctor.maven.AsciidoctorMojo; | ||
import org.asciidoctor.maven.log.LogHandler; | ||
import org.mockito.Mockito; | ||
|
||
import java.io.File; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import static org.codehaus.plexus.util.ReflectionUtils.setVariableValueInObject; | ||
import static org.mockito.Mockito.when; | ||
|
||
class MojoMocker { | ||
|
||
private static final ParametersInitializer parametersInitializer = new ParametersInitializer(); | ||
|
||
@SneakyThrows | ||
@SuppressWarnings("unchecked") | ||
<T> T mock(Class<T> clazz, Map<String, String> mavenProperties, LogHandler logHandler) { | ||
|
||
final AsciidoctorMojo mojo = (AsciidoctorMojo) clazz.getConstructor(new Class[]{}).newInstance(); | ||
|
||
parametersInitializer.initialize(mojo); | ||
setVariableValueInObject(mojo, "log", new SystemStreamLog()); | ||
setVariableValueInObject(mojo, "project", mockMavenProject(mavenProperties)); | ||
if (logHandler != null) | ||
setVariableValueInObject(mojo, "logHandler", logHandler); | ||
|
||
return (T) mojo; | ||
} | ||
|
||
private MavenProject mockMavenProject(Map<String, String> mavenProperties) { | ||
final MavenProject mavenProject = Mockito.mock(MavenProject.class); | ||
when(mavenProject.getBasedir()).thenReturn(new File(".")); | ||
if (mavenProperties != null) { | ||
final Properties properties = new Properties(); | ||
properties.putAll(mavenProperties); | ||
when(mavenProject.getProperties()).thenReturn(properties); | ||
} | ||
return mavenProject; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
asciidoctor-maven-plugin/src/test/java/org/asciidoctor/maven/test/MojoMockerTest.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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.asciidoctor.maven.test; | ||
|
||
import org.asciidoctor.maven.AsciidoctorMojo; | ||
import org.asciidoctor.maven.log.LogHandler; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class MojoMockerTest { | ||
|
||
private final MojoMocker mojoMocker = new MojoMocker(); | ||
|
||
@Test | ||
void should_mock_mojo() { | ||
AsciidoctorMojo mock = mojoMocker.mock(AsciidoctorMojo.class, null, null); | ||
|
||
assertThat(mock).isNotNull(); | ||
} | ||
|
||
@Test | ||
void should_mock_mojo_with_properties() { | ||
Map<String, String> properties = Map.of("a-key", "a-value"); | ||
AsciidoctorMojo mock = mojoMocker.mock(AsciidoctorMojo.class, properties, null); | ||
|
||
assertThat(mock).isNotNull(); | ||
} | ||
|
||
@Test | ||
void should_mock_mojo_with_logHandler() { | ||
AsciidoctorMojo mock = mojoMocker.mock(AsciidoctorMojo.class, null, new LogHandler()); | ||
|
||
assertThat(mock).isNotNull(); | ||
} | ||
} |
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
Oops, something went wrong.