From bb5c0bd64ead705298a1a086e1c1e5393bb80424 Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Sun, 14 Mar 2021 23:36:08 +0100 Subject: [PATCH] Migrate AsciidoctorMojoExtensionsTest from Groovy to Java --- CHANGELOG.adoc | 1 + .../test/AsciidoctorMojoExtensionsTest.groovy | 418 ------------------ .../maven/AsciidoctorAsserter.java | 5 + .../maven/AsciidoctorMojoExtensionsTest.java | 415 +++++++++++++++++ 4 files changed, 421 insertions(+), 418 deletions(-) delete mode 100644 src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoExtensionsTest.groovy create mode 100644 src/test/java/org/asciidoctor/maven/AsciidoctorMojoExtensionsTest.java diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 489b1d03..350e386c 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -24,6 +24,7 @@ Maintenance:: * Upgrade `netty-codec-http` to 4.1.29.Final and refactor `AsciidoctorHttpServer` (#511) * Rewrite `AsciidoctorMojoTest` to Java to make it more approachable (#512) * Rewrite `AsciidoctorMojoLogHandlerTest` to Java to make it more approachable (#514) + * Rewrite `AsciidoctorMojoExtensionsTest` to Java to make it more approachable (#515) == v2.1.0 (2020-09-15) diff --git a/src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoExtensionsTest.groovy b/src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoExtensionsTest.groovy deleted file mode 100644 index d1ee05bc..00000000 --- a/src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoExtensionsTest.groovy +++ /dev/null @@ -1,418 +0,0 @@ -package org.asciidoctor.maven.test - -import org.apache.maven.plugin.MojoExecutionException -import org.asciidoctor.maven.AsciidoctorMojo -import org.asciidoctor.maven.extensions.ExtensionConfiguration -import org.asciidoctor.maven.test.plexus.MockPlexusContainer -import org.asciidoctor.maven.test.processors.ChangeAttributeValuePreprocessor -import org.asciidoctor.maven.test.processors.FailingPreprocessor -import org.asciidoctor.maven.test.processors.GistBlockMacroProcessor -import org.asciidoctor.maven.test.processors.ManpageInlineMacroProcessor -import org.asciidoctor.maven.test.processors.MetaDocinfoProcessor -import org.asciidoctor.maven.test.processors.UriIncludeProcessor -import org.asciidoctor.maven.test.processors.YellBlockProcessor - -import spock.lang.Specification -import spock.lang.Unroll - -import static org.asciidoctor.maven.io.TestFilesHelper.newOutputTestDirectory - -/** - * Specific tests to validate usage of AsciidoctorJ extension in AsciidoctorMojo. - * - * Most of the examples have been directly adapted from the ones found in AsciidoctorJ - * documentation (https://github.com/asciidoctor/asciidoctorj/blob/master/README.adoc) - * - * @author abelsromero - */ -class AsciidoctorMojoExtensionsTest extends Specification { - - def setupSpec() { - MockPlexusContainer.initializeMockContext(AsciidoctorMojo) - } - - static final String SRC_DIR = 'target/test-classes/src/asciidoctor/' - - - def "fails because processor is not found in classpath"() { - setup: - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('preprocessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.attributes['linkcss!'] = '' - mojo.extensions = [ - [className: 'non.existent.Processor'] as ExtensionConfiguration - ] - mojo.execute() - then: - outputDir.list().size() == 0 - def e = thrown(MojoExecutionException) - e.message.contains(mojo.extensions.get(0).className) - e.message.contains('not found in classpath') - } - - // This test is added to keep track of possible changes in the extension's SPI - def "plugin fails because processor throws an uncaught exception"() { - setup: - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('preprocessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.attributes['linkcss!'] = '' - mojo.extensions = [ - [className: FailingPreprocessor.class.canonicalName] as ExtensionConfiguration - ] - mojo.execute() - then: - // since v 1.5.4 resources are copied before conversion, so some files remain - outputDir.list().size() > 0 - thrown(RuntimeException) - } - - - /** - * Redirects output to validate specific traces left in the processors - */ - @Unroll - def "tests that a #processorType is registered, initialized and executed"() { - - setup: - ByteArrayOutputStream systemOut = new ByteArrayOutputStream() - System.out = new PrintStream(systemOut) - - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('preprocessor') - - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.extensions = [ - [className: "org.asciidoctor.maven.test.processors.${processorClass}"] as ExtensionConfiguration - ] - mojo.execute() - - expect: - systemOut.toString().contains(initializationMessage) - systemOut.toString().contains(executionMessage) - - where: - processorClass | processorType || initializationMessage || executionMessage - 'ChangeAttributeValuePreprocessor'|'Preprocessor' || "ChangeAttributeValuePreprocessor(Preprocessor) initialized" || 'Processing ChangeAttributeValuePreprocessor' - 'DummyTreeprocessor' |'Treeprocessor' || "DummyTreeprocessor(Treeprocessor) initialized" || 'Processing DummyTreeprocessor' - 'DummyPostprocessor' |'Postprocessor' || "DummyPostprocessor(Postprocessor) initialized" || 'Processing DummyPostprocessor' - 'MetaDocinfoProcessor' |'DocinfoProcessor' || "MetaDocinfoProcessor(DocinfoProcessor) initialized" || 'Processing MetaDocinfoProcessor' - 'UriIncludeProcessor' |'IncludeProcessor' || "UriIncludeProcessor(IncludeProcessor) initialized" || 'Processing UriIncludeProcessor' - } - - def "successfully converts html with a preprocessor"() { - setup: - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('preprocessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.extensions = [ - [className: ChangeAttributeValuePreprocessor.class.canonicalName] as ExtensionConfiguration - ] - mojo.execute() - then: - outputDir.list().toList().isEmpty() == false - outputDir.list().toList().contains('processors-sample.html') - - File sampleOutput = new File(outputDir, 'processors-sample.html') - sampleOutput.length() > 0 - String text = sampleOutput.getText() - text.count(ChangeAttributeValuePreprocessor.AUTHOR_NAME) == 2 - } - - def "successfully converts html with a blockprocessor"() { - setup: - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('blockprocessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.extensions = [ - [className: YellBlockProcessor.class.canonicalName, blockName:'yell'] as ExtensionConfiguration - ] - mojo.execute() - then: - outputDir.list().toList().isEmpty() == false - outputDir.list().toList().contains('processors-sample.html') - - File sampleOutput = new File(outputDir, 'processors-sample.html') - sampleOutput.length() > 0 - sampleOutput.getText().contains('The time is now. Get a move on.'.toUpperCase()) - } - - def "successfully converts html and adds meta tag with a DocinfoProcessor"() { - setup: - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('docinfoProcessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.extensions = [ - [className: MetaDocinfoProcessor.class.canonicalName] as ExtensionConfiguration - ] - mojo.execute() - then: - outputDir.list().toList().isEmpty() == false - outputDir.list().toList().contains('processors-sample.html') - - File sampleOutput = new File(outputDir, 'processors-sample.html') - sampleOutput.length() > 0 - sampleOutput.text.contains("") - } - - def "successfully converts html and modifies output with a BlockMacroProcessor"() { - setup: - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('blockMacroProcessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.extensions = [ - [className: GistBlockMacroProcessor.class.canonicalName, blockName:'gist'] as ExtensionConfiguration - ] - mojo.execute() - then: - outputDir.list().toList().isEmpty() == false - outputDir.list().toList().contains('processors-sample.html') - - File sampleOutput = new File(outputDir, 'processors-sample.html') - sampleOutput.length() > 0 - sampleOutput.text.contains("") - } - - def "successfully converts html and modifies output with a InlineMacroProcessor"() { - setup: - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('inlineMacroProcessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.extensions = [ - [className: ManpageInlineMacroProcessor.class.canonicalName, blockName:'man'] as ExtensionConfiguration - ] - mojo.execute() - then: - outputDir.list().toList().isEmpty() == false - outputDir.list().toList().contains('processors-sample.html') - - File sampleOutput = new File(outputDir, 'processors-sample.html') - sampleOutput.length() > 0 - sampleOutput.text.contains("

See gittutorial to get started.

") - } - - def "successfully converts html and modifies output with an IncludeProcessor"() { - setup: - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('includeProcessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.extensions = [ - [className: UriIncludeProcessor.class.canonicalName] as ExtensionConfiguration - ] - mojo.execute() - then: - outputDir.list().toList().isEmpty() == false - outputDir.list().toList().contains('processors-sample.html') - - File sampleOutput = new File(outputDir, 'processors-sample.html') - sampleOutput.length() > 0 - sampleOutput.text.contains("source 'https://rubygems.org'") - } - - def "executes the same preprocessor twice"() { - setup: - ByteArrayOutputStream systemOut = new ByteArrayOutputStream() - System.out = new PrintStream(systemOut) - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('preprocessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.extensions = [ - [className: ChangeAttributeValuePreprocessor.class.canonicalName] as ExtensionConfiguration, - [className: ChangeAttributeValuePreprocessor.class.canonicalName] as ExtensionConfiguration - ] - mojo.execute() - then: - outputDir.list().toList().isEmpty() == false - outputDir.list().toList().contains('processors-sample.html') - - File sampleOutput = new File(outputDir, 'processors-sample.html') - sampleOutput.length() > 0 - String text = sampleOutput.getText() - text.count(ChangeAttributeValuePreprocessor.AUTHOR_NAME) == 2 - - systemOut.toString().count('Processing ChangeAttributeValuePreprocessor') == 2 - } - - - // Adding a BlockMacroProcessor or BlockProcessor makes the conversion fail - def "successfully converts html with Preprocessor, DocinfoProcessor, InlineMacroProcessor and IncludeProcessor"() { - setup: - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('preprocessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.extensions = [ - // Preprocessor - [className: 'org.asciidoctor.maven.test.processors.ChangeAttributeValuePreprocessor'] as ExtensionConfiguration, - // DocinfoProcessor - [className: 'org.asciidoctor.maven.test.processors.MetaDocinfoProcessor'] as ExtensionConfiguration, - // InlineMacroProcessor - [className: 'org.asciidoctor.maven.test.processors.ManpageInlineMacroProcessor', blockName:'man'] as ExtensionConfiguration, - // IncludeProcessor - [className: 'org.asciidoctor.maven.test.processors.UriIncludeProcessor'] as ExtensionConfiguration, - ] - mojo.execute() - then: - outputDir.list().toList().isEmpty() == false - outputDir.list().toList().contains('processors-sample.html') - - File sampleOutput = new File(outputDir, 'processors-sample.html') - sampleOutput.length() > 0 - - String text = sampleOutput.text - text.count(ChangeAttributeValuePreprocessor.AUTHOR_NAME) == 2 - text.contains("") - text.contains("

See gittutorial to get started.

") - text.contains("source 'https://rubygems.org'") - } - - def "converts html when using all types of extensions"() { - setup: - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('preprocessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = '' - mojo.attributes['linkcss'] = '' - mojo.attributes['copycss!'] = '' - mojo.extensions = [ - // Preprocessor - [className: 'org.asciidoctor.maven.test.processors.ChangeAttributeValuePreprocessor'] as ExtensionConfiguration, - // DocinfoProcessor - [className: 'org.asciidoctor.maven.test.processors.MetaDocinfoProcessor'] as ExtensionConfiguration, - // InlineMacroProcessor - [className: 'org.asciidoctor.maven.test.processors.ManpageInlineMacroProcessor', blockName:'man'] as ExtensionConfiguration, - // IncludeProcessor - [className: 'org.asciidoctor.maven.test.processors.UriIncludeProcessor'] as ExtensionConfiguration, - // BlockMacroProcessor - [className: 'org.asciidoctor.maven.test.processors.GistBlockMacroProcessor', blockName:'gist'] as ExtensionConfiguration, - // BlockProcessor - [className: 'org.asciidoctor.maven.test.processors.YellBlockProcessor', blockName:'yell'] as ExtensionConfiguration - ] - mojo.execute() - then: - outputDir.list().toList().isEmpty() == false - outputDir.list().toList().contains('processors-sample.html') - - File sampleOutput = new File(outputDir, 'processors-sample.html') - sampleOutput.length() > 0 - - String text = sampleOutput.text - text.contains('') - text.contains('') - text.contains('

See gittutorial to get started.

') - text.contains('

THE TIME IS NOW. GET A MOVE ON.

') - } - - /** - * Manual test to validate automatic extension registration. - * To execute, copy _org.asciidoctor.extension.spi.ExtensionRegistry to - * /src/test/resources/META-INF/services/ and execute - */ - @spock.lang.Ignore - def "property extension"() { - setup: - File srcDir = new File(SRC_DIR) - File outputDir = newOutputTestDirectory('preprocessor') - when: - AsciidoctorMojo mojo = new AsciidoctorMojo() - mojo.backend = 'html' - mojo.sourceDirectory = srcDir - mojo.sourceDocumentName = 'processors-sample.adoc' - mojo.outputDirectory = outputDir - mojo.headerFooter = true - mojo.attributes['toc'] = null - mojo.attributes['linkcss!'] = '' - mojo.execute() - then: - outputDir.list().toList().isEmpty() == false - outputDir.list().toList().contains('processors-sample.html') - - File sampleOutput = new File(outputDir, 'processors-sample.html') - sampleOutput.length() > 0 - String text = sampleOutput.getText() - text.count(ChangeAttributeValuePreprocessor.AUTHOR_NAME) == 2 - } - -} diff --git a/src/test/java/org/asciidoctor/maven/AsciidoctorAsserter.java b/src/test/java/org/asciidoctor/maven/AsciidoctorAsserter.java index 734a3b56..b713688a 100644 --- a/src/test/java/org/asciidoctor/maven/AsciidoctorAsserter.java +++ b/src/test/java/org/asciidoctor/maven/AsciidoctorAsserter.java @@ -34,6 +34,11 @@ public AsciidoctorAsserter contains(String text) { return this; } + public AsciidoctorAsserter containsPattern(String regex) { + contentAssert.containsPattern(regex); + return this; + } + public AsciidoctorAsserter containsOnlyOnce(String text) { contentAssert.containsOnlyOnce(text); return this; diff --git a/src/test/java/org/asciidoctor/maven/AsciidoctorMojoExtensionsTest.java b/src/test/java/org/asciidoctor/maven/AsciidoctorMojoExtensionsTest.java new file mode 100644 index 00000000..07d6f427 --- /dev/null +++ b/src/test/java/org/asciidoctor/maven/AsciidoctorMojoExtensionsTest.java @@ -0,0 +1,415 @@ +package org.asciidoctor.maven; + +import lombok.SneakyThrows; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.asciidoctor.maven.extensions.ExtensionConfiguration; +import org.asciidoctor.maven.io.ConsoleHolder; +import org.asciidoctor.maven.test.processors.*; +import org.assertj.core.api.Assertions; +import org.junit.Ignore; +import org.junit.Test; + +import java.io.File; +import java.util.Arrays; + +import static java.util.Collections.singletonList; +import static org.asciidoctor.maven.TestUtils.map; +import static org.asciidoctor.maven.TestUtils.mockAsciidoctorMojo; +import static org.asciidoctor.maven.io.TestFilesHelper.newOutputTestDirectory; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Specific tests to validate usage of AsciidoctorJ extension in AsciidoctorMojo. + * + * Most of the examples have been directly adapted from the ones found in AsciidoctorJ + * documentation (https://github.com/asciidoctor/asciidoctorj/blob/master/README.adoc) + * + * @author abelsromero + */ +public class AsciidoctorMojoExtensionsTest { + + private static final String SRC_DIR = "target/test-classes/src/asciidoctor/"; + + @Test + public void should_fail_when_extension_is_not_found_in_classpath() { + // given + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("preprocessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null, "linkcss!", ""); + mojo.extensions = Arrays.asList(extensionConfiguration("non.existent.Processor")); + Throwable throwable = Assertions.catchThrowable(mojo::execute); + // then + assertThat(throwable) + .isInstanceOf(MojoExecutionException.class) + .hasMessageContaining("not found in classpath"); + assertThat(outputDir) + .isEmptyDirectory(); + } + + // This test is added to keep track of possible changes in the extension"s SPI + @Test + public void should_fail_when_extension_throws_an_uncaught_exception() { + // given + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("preprocessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null, "linkcss!", ""); + mojo.extensions = Arrays.asList(extensionConfiguration(FailingPreprocessor.class)); + Throwable throwable = Assertions.catchThrowable(mojo::execute); + // then + // since v 1.5.4 resources are copied before conversion, so some files remain + assertThat(throwable) + .isInstanceOf(RuntimeException.class); + assertThat(outputDir) + .isNotEmptyDirectory(); + } + + @Test + public void should_register_and_run_Preprocessor() { + + String extensionClassName = "ChangeAttributeValuePreprocessor"; + String expectedMessage = "ChangeAttributeValuePreprocessor(Preprocessor) initialized"; + String executionMessage = "Processing ChangeAttributeValuePreprocessor"; + + shouldRegisterAndRunExtension(extensionClassName, expectedMessage, executionMessage); + } + + @Test + public void should_register_and_run_Treeprocessor() { + + String extensionClassName = "DummyTreeprocessor"; + String expectedMessage = "DummyTreeprocessor(Treeprocessor) initialized"; + String executionMessage = "Processing DummyTreeprocessor"; + + shouldRegisterAndRunExtension(extensionClassName, expectedMessage, executionMessage); + } + + @Test + public void should_register_and_run_PostProcessor() { + + String extensionClassName = "DummyPostprocessor"; + String expectedMessage = "DummyPostprocessor(Postprocessor) initialized"; + String executionMessage = "Processing DummyPostprocessor"; + + shouldRegisterAndRunExtension(extensionClassName, expectedMessage, executionMessage); + } + + @Test + public void should_register_and_run_DocinfoProcessor() { + + String extensionClassName = "MetaDocinfoProcessor"; + String expectedMessage = "MetaDocinfoProcessor(DocinfoProcessor) initialized"; + String executionMessage = "Processing MetaDocinfoProcessor"; + + shouldRegisterAndRunExtension(extensionClassName, expectedMessage, executionMessage); + } + + @Test + public void should_register_and_run_IncludeProcessor() { + + String extensionClassName = "UriIncludeProcessor"; + String expectedMessage = "UriIncludeProcessor(IncludeProcessor) initialized"; + String executionMessage = "Processing UriIncludeProcessor"; + + shouldRegisterAndRunExtension(extensionClassName, expectedMessage, executionMessage); + } + + @SneakyThrows + private void shouldRegisterAndRunExtension(String extensionClassName, String initializationMessage, String executionMessage) { + // given + ConsoleHolder consoleHolder = ConsoleHolder.start(); + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("preprocessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null); + mojo.extensions = singletonList(extensionConfiguration("org.asciidoctor.maven.test.processors." + extensionClassName)); + mojo.execute(); + // then + assertThat(consoleHolder.getOutput()) + .contains(initializationMessage) + .contains(executionMessage); + // cleanup + consoleHolder.release(); + } + + @Test + public void should_convert_to_html_with_a_preprocessor() throws MojoFailureException, MojoExecutionException { + // given + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("preprocessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null); + mojo.extensions = Arrays.asList(extensionConfiguration(ChangeAttributeValuePreprocessor.class)); + mojo.execute(); + // then + AsciidoctorAsserter.assertThat(outputDir, "processors-sample.html") + .isNotEmpty() + .containsPattern("(" + ChangeAttributeValuePreprocessor.AUTHOR_NAME + "([\\s\\S])*){2}"); + } + + @Test + public void should_convert_to_html_with_a_blockprocessor() throws MojoFailureException, MojoExecutionException { + // given + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("blockprocessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null); + mojo.extensions = singletonList(extensionConfiguration(YellBlockProcessor.class, "yell")); + mojo.execute(); + // then + AsciidoctorAsserter.assertThat(outputDir, "processors-sample.html") + .isNotEmpty() + .contains("The time is now. Get a move on.".toUpperCase()); + } + + @Test + public void should_convert_to_html_and_add_meta_tag_with_a_DocinfoProcessor() throws MojoFailureException, MojoExecutionException { + // given + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("docinfoProcessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null); + mojo.extensions = singletonList(extensionConfiguration(MetaDocinfoProcessor.class, "yell")); + mojo.execute(); + // then + AsciidoctorAsserter.assertThat(outputDir, "processors-sample.html") + .isNotEmpty() + .contains(""); + } + + @Test + public void should_convert_to_html_and_modify_output_with_a_BlockMacroProcessor() throws MojoFailureException, MojoExecutionException { + // given + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("blockMacroProcessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null); + mojo.extensions = singletonList(extensionConfiguration(GistBlockMacroProcessor.class, "gist")); + mojo.execute(); + // then + AsciidoctorAsserter.assertThat(outputDir, "processors-sample.html") + .isNotEmpty() + .contains(""); + } + + @Test + public void should_convert_to_html_and_modify_output_with_a_InlineMacroProcessor() throws MojoFailureException, MojoExecutionException { + // given + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("inlineMacroProcessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null); + mojo.extensions = singletonList(extensionConfiguration(ManpageInlineMacroProcessor.class, "man")); + mojo.execute(); + // then + AsciidoctorAsserter.assertThat(outputDir, "processors-sample.html") + .isNotEmpty() + .contains("

See gittutorial to get started.

"); + } + + @Test + public void should_convert_to_html_and_modify_output_with_an_IncludeProcessor() throws MojoFailureException, MojoExecutionException { + // given + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("includeProcessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null); + mojo.extensions = singletonList(extensionConfiguration(UriIncludeProcessor.class)); + mojo.execute(); + // then + AsciidoctorAsserter.assertThat(outputDir, "processors-sample.html") + .isNotEmpty() + .contains("source 'https://rubygems.org'"); + } + + @Test + public void should_run_the_same_preprocessor_twice_when_registered_twice() throws MojoFailureException, MojoExecutionException { + // given + ConsoleHolder consoleHolder = ConsoleHolder.start(); + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("preprocessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null); + mojo.extensions = Arrays.asList( + extensionConfiguration(ChangeAttributeValuePreprocessor.class), + extensionConfiguration(ChangeAttributeValuePreprocessor.class) + ); + mojo.execute(); + // then + AsciidoctorAsserter.assertThat(outputDir, "processors-sample.html") + .isNotEmpty() + .containsPattern("(" + ChangeAttributeValuePreprocessor.AUTHOR_NAME + "([\\s\\S])*){2}"); + + assertThat(consoleHolder.getOutput()) + .containsPattern("(Processing ChangeAttributeValuePreprocessor([\\s\\S])*){2}"); + // cleanup + consoleHolder.release(); + } + + // Adding a BlockMacroProcessor or BlockProcessor makes the conversion fail + @Test + public void should_convert_to_html_with_Preprocessor_DocinfoProcessor_InlineMacroProcessor_and_IncludeProcessor() throws MojoFailureException, MojoExecutionException { + // given + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("preprocessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null); + mojo.extensions = Arrays.asList( + extensionConfiguration("org.asciidoctor.maven.test.processors.ChangeAttributeValuePreprocessor"), + extensionConfiguration("org.asciidoctor.maven.test.processors.MetaDocinfoProcessor"), + extensionConfiguration("org.asciidoctor.maven.test.processors.ManpageInlineMacroProcessor", "man"), + extensionConfiguration("org.asciidoctor.maven.test.processors.UriIncludeProcessor") + ); + mojo.execute(); + // then + AsciidoctorAsserter.assertThat(outputDir, "processors-sample.html") + .containsPattern("(" + ChangeAttributeValuePreprocessor.AUTHOR_NAME + "([\\s\\S])*){2}") + .contains("") + .contains("

See gittutorial to get started.

") + .contains("source 'https://rubygems.org'"); + } + + @Test + public void should_convert_to_html_using_all_extension_types() throws MojoFailureException, MojoExecutionException { + // given + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("preprocessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", "", + "linkcss", "", + "copycss!", ""); + mojo.extensions = Arrays.asList( + extensionConfiguration("org.asciidoctor.maven.test.processors.ChangeAttributeValuePreprocessor"), + extensionConfiguration("org.asciidoctor.maven.test.processors.MetaDocinfoProcessor"), + extensionConfiguration("org.asciidoctor.maven.test.processors.ManpageInlineMacroProcessor", "man"), + extensionConfiguration("org.asciidoctor.maven.test.processors.UriIncludeProcessor"), + extensionConfiguration("org.asciidoctor.maven.test.processors.GistBlockMacroProcessor", "gist"), + extensionConfiguration("org.asciidoctor.maven.test.processors.YellBlockProcessor", "yell") + ); + mojo.execute(); + // then + AsciidoctorAsserter.assertThat(outputDir, "processors-sample.html") + .contains("") + .contains("") + .contains("

See gittutorial to get started.

") + .contains("

THE TIME IS NOW. GET A MOVE ON.

"); + } + + /** + * Manual test to validate automatic extension registration. + * To execute, copy _org.asciidoctor.extension.spi.ExtensionRegistry to + * /src/test/resources/META-INF/services/ and execute + */ + @Ignore + @Test + public void property_extension() throws MojoFailureException, MojoExecutionException { + // given + File srcDir = new File(SRC_DIR); + File outputDir = newOutputTestDirectory("preprocessor"); + // when + AsciidoctorMojo mojo = mockAsciidoctorMojo(); + mojo.backend = "html"; + mojo.sourceDirectory = srcDir; + mojo.sourceDocumentName = "processors-sample.adoc"; + mojo.outputDirectory = outputDir; + mojo.headerFooter = true; + mojo.attributes = map("toc", null, "linkcss!", ""); + mojo.execute(); + // then + AsciidoctorAsserter.assertThat(outputDir, "processors-sample.html") + .containsPattern("(" + ChangeAttributeValuePreprocessor.AUTHOR_NAME + " ([\\s\\S])*){2}"); + } + + private ExtensionConfiguration extensionConfiguration(String className, String blockName) { + ExtensionConfiguration extensionConfiguration = new ExtensionConfiguration(); + extensionConfiguration.setClassName(className); + extensionConfiguration.setBlockName(blockName); + return extensionConfiguration; + } + + private ExtensionConfiguration extensionConfiguration(String className) { + return extensionConfiguration(className, null); + } + + private ExtensionConfiguration extensionConfiguration(Class clazz) { + return extensionConfiguration(clazz.getCanonicalName(), null); + } + + private ExtensionConfiguration extensionConfiguration(Class clazz, String blockName) { + return extensionConfiguration(clazz.getCanonicalName(), blockName); + } +}