forked from asciidoctor/asciidoctor-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace reflection by direct JavaExtensionRegistry calls
* Stops any possible usage of AsciidoctorJ v1.6.x (long not supported) * Fixes issue where registering a non-Processor class was ignored without error * Small improvements to JavaDocs closes asciidoctor#568
- Loading branch information
1 parent
a245ea6
commit 0867747
Showing
3 changed files
with
177 additions
and
74 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
16 changes: 10 additions & 6 deletions
16
...doctor-maven-plugin/src/main/java/org/asciidoctor/maven/extensions/ExtensionRegistry.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,18 +1,22 @@ | ||
package org.asciidoctor.maven.extensions; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.asciidoctor.extension.Processor; | ||
|
||
/** | ||
* Base interface for registering AsciidoctorJ extension in the plugin. | ||
* | ||
* @author abelsromero | ||
*/ | ||
public interface ExtensionRegistry { | ||
|
||
/** | ||
* Checks if {@code extensionClassName} belongs to a valid {@link Processor} | ||
* class and if it can be found in the classpath | ||
* Registers an AsciidoctorJ extension by full class name. | ||
* | ||
* @param extensionClassName fully qualified name of the class implementing the extension | ||
* @param blockName required when declaring | ||
* @throws MojoExecutionException if extension could not be registered | ||
* @throws RuntimeException if {@code extensionClassName} belongs to a valid {@link Processor}, | ||
* class, or if it can be found in the classpath | ||
*/ | ||
void register(String extensionClassName, String blockName) throws MojoExecutionException; | ||
void register(String extensionClassName, String blockName); | ||
|
||
} | ||
} |
148 changes: 148 additions & 0 deletions
148
...gin/src/test/java/org/asciidoctor/maven/extensions/AsciidoctorJExtensionRegistryTest.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,148 @@ | ||
package org.asciidoctor.maven.extensions; | ||
|
||
import org.asciidoctor.Asciidoctor; | ||
import org.asciidoctor.extension.*; | ||
import org.asciidoctor.maven.test.processors.*; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class AsciidoctorJExtensionRegistryTest { | ||
|
||
private JavaExtensionRegistry javaExtensionRegistry; | ||
private AsciidoctorJExtensionRegistry pluginExtensionRegistry; | ||
|
||
|
||
@BeforeEach | ||
void beforeEach() { | ||
final Asciidoctor mockAsciidoctor = Mockito.mock(Asciidoctor.class); | ||
javaExtensionRegistry = Mockito.mock(JavaExtensionRegistry.class); | ||
Mockito.when(mockAsciidoctor.javaExtensionRegistry()).thenReturn(javaExtensionRegistry); | ||
pluginExtensionRegistry = new AsciidoctorJExtensionRegistry(mockAsciidoctor); | ||
} | ||
|
||
|
||
@Test | ||
void should_fail_when_not_an_extension() { | ||
final String className = String.class.getCanonicalName(); | ||
|
||
Exception e = Assertions.catchException(() -> pluginExtensionRegistry.register(className, null)); | ||
|
||
assertThat(e) | ||
.isInstanceOf(RuntimeException.class) | ||
.hasMessage(String.format("'%s' is not a valid AsciidoctorJ processor class", className)); | ||
} | ||
|
||
@Test | ||
void should_fail_when_extension_class_is_not_available() { | ||
final String className = "not.a.real.Class"; | ||
|
||
Exception e = Assertions.catchException(() -> pluginExtensionRegistry.register(className, null)); | ||
|
||
assertThat(e) | ||
.isInstanceOf(RuntimeException.class) | ||
.hasMessage(String.format("'%s' not found in classpath", className)); | ||
} | ||
|
||
@Test | ||
void should_register_a_DocinfoProcessor() { | ||
final Class<? extends DocinfoProcessor> clazz = MetaDocinfoProcessor.class; | ||
final String className = clazz.getCanonicalName(); | ||
|
||
pluginExtensionRegistry.register(className, null); | ||
Mockito.verify(javaExtensionRegistry).docinfoProcessor(clazz); | ||
} | ||
|
||
@Test | ||
void should_register_a_Preprocessor() { | ||
final Class<? extends Preprocessor> clazz = ChangeAttributeValuePreprocessor.class; | ||
final String className = clazz.getCanonicalName(); | ||
|
||
pluginExtensionRegistry.register(className, null); | ||
Mockito.verify(javaExtensionRegistry).preprocessor(clazz); | ||
} | ||
|
||
@Test | ||
void should_register_a_Postprocessor() { | ||
final Class<? extends Postprocessor> clazz = DummyPostprocessor.class; | ||
final String className = clazz.getCanonicalName(); | ||
|
||
pluginExtensionRegistry.register(className, null); | ||
Mockito.verify(javaExtensionRegistry).postprocessor(clazz); | ||
} | ||
|
||
@Test | ||
void should_register_a_Treeprocessor() { | ||
final Class<? extends Treeprocessor> clazz = DummyTreeprocessor.class; | ||
final String className = clazz.getCanonicalName(); | ||
|
||
pluginExtensionRegistry.register(className, null); | ||
Mockito.verify(javaExtensionRegistry).treeprocessor(clazz); | ||
} | ||
|
||
@Test | ||
void should_register_a_BlockProcessor() { | ||
final Class<? extends BlockProcessor> clazz = YellBlockProcessor.class; | ||
final String className = clazz.getCanonicalName(); | ||
|
||
pluginExtensionRegistry.register(className, null); | ||
Mockito.verify(javaExtensionRegistry).block(clazz); | ||
} | ||
|
||
@Test | ||
void should_register_a_BlockProcessor_with_name() { | ||
final Class<? extends BlockProcessor> clazz = YellBlockProcessor.class; | ||
final String className = clazz.getCanonicalName(); | ||
|
||
pluginExtensionRegistry.register(className, "block_name"); | ||
Mockito.verify(javaExtensionRegistry).block("block_name", clazz); | ||
} | ||
|
||
@Test | ||
void should_register_a_IncludeProcessor() { | ||
final Class<? extends IncludeProcessor> clazz = UriIncludeProcessor.class; | ||
final String className = clazz.getCanonicalName(); | ||
|
||
pluginExtensionRegistry.register(className, null); | ||
Mockito.verify(javaExtensionRegistry).includeProcessor(clazz); | ||
} | ||
|
||
@Test | ||
void should_register_a_BlockMacroProcessor() { | ||
final Class<? extends BlockMacroProcessor> clazz = GistBlockMacroProcessor.class; | ||
final String className = clazz.getCanonicalName(); | ||
|
||
pluginExtensionRegistry.register(className, null); | ||
Mockito.verify(javaExtensionRegistry).blockMacro(clazz); | ||
} | ||
|
||
@Test | ||
void should_register_a_BlockMacroProcessor_with_name() { | ||
final Class<? extends BlockMacroProcessor> clazz = GistBlockMacroProcessor.class; | ||
final String className = clazz.getCanonicalName(); | ||
|
||
pluginExtensionRegistry.register(className, "block_name"); | ||
Mockito.verify(javaExtensionRegistry).blockMacro("block_name", clazz); | ||
} | ||
|
||
@Test | ||
void should_register_a_InlineMacroProcessor() { | ||
final Class<? extends InlineMacroProcessor> clazz = ManpageInlineMacroProcessor.class; | ||
final String className = clazz.getCanonicalName(); | ||
|
||
pluginExtensionRegistry.register(className, null); | ||
Mockito.verify(javaExtensionRegistry).inlineMacro(clazz); | ||
} | ||
|
||
@Test | ||
void should_register_a_InlineMacroProcessor_with_name() { | ||
final Class<? extends InlineMacroProcessor> clazz = ManpageInlineMacroProcessor.class; | ||
final String className = clazz.getCanonicalName(); | ||
|
||
pluginExtensionRegistry.register(className, "block_name"); | ||
Mockito.verify(javaExtensionRegistry).inlineMacro("block_name", clazz); | ||
} | ||
} |