From 221c3e35b8bb3ee2407bf3dd753f80dc5cbac586 Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Wed, 24 Mar 2021 00:17:14 +0100 Subject: [PATCH] Migrate AsciidoctorDoxiaParserTest from Groovy to Java * Remove Groovy infrastructure --- pom.xml | 58 +--- .../site/AsciidoctorDoxiaParserTest.groovy | 259 ---------------- .../site/AsciidoctorDoxiaParserTest.java | 280 ++++++++++++++++++ 3 files changed, 286 insertions(+), 311 deletions(-) delete mode 100644 src/test/groovy/org/asciidoctor/maven/test/site/AsciidoctorDoxiaParserTest.groovy create mode 100644 src/test/java/org/asciidoctor/maven/site/AsciidoctorDoxiaParserTest.java diff --git a/pom.xml b/pom.xml index 42b63e0d..7c518700 100644 --- a/pom.xml +++ b/pom.xml @@ -60,8 +60,6 @@ UTF-8 1.8 JavaSE-1.8 - 0.7-groovy-2.0 - 2.1.3 2.4.1 9.2.13.0 4.3.0 @@ -75,19 +73,8 @@ 4.1.59.Final 2.5 1.8 - 1.5 - - - - org.codehaus.groovy - groovy-all - ${groovy.version} - - - - org.jruby @@ -135,12 +122,6 @@ commons-lang3 3.8.1 - - org.spockframework - spock-core - ${spock.version} - test - io.netty netty-codec-http @@ -161,6 +142,12 @@ doxia-module-xhtml ${doxia.version} + + junit + junit + 4.13 + test + org.assertj assertj-core @@ -224,39 +211,6 @@ ${project.java.version} - - org.codehaus.gmaven - gmaven-plugin - ${gmaven.version} - - - - 2.0 - - - testCompile - - - - - - org.codehaus.gmaven.runtime - gmaven-runtime-2.0 - ${gmaven.version} - - - org.codehaus.groovy - groovy-all - - - - - org.codehaus.groovy - groovy-all - ${groovy.version} - - - org.eluder.coveralls coveralls-maven-plugin diff --git a/src/test/groovy/org/asciidoctor/maven/test/site/AsciidoctorDoxiaParserTest.groovy b/src/test/groovy/org/asciidoctor/maven/test/site/AsciidoctorDoxiaParserTest.groovy deleted file mode 100644 index eddec836..00000000 --- a/src/test/groovy/org/asciidoctor/maven/test/site/AsciidoctorDoxiaParserTest.groovy +++ /dev/null @@ -1,259 +0,0 @@ -package org.asciidoctor.maven.test.site - -import org.apache.maven.doxia.sink.Sink -import org.apache.maven.project.MavenProject -import org.asciidoctor.maven.site.AsciidoctorDoxiaParser -import org.codehaus.plexus.util.xml.Xpp3DomBuilder -import spock.lang.Specification - -class AsciidoctorDoxiaParserTest extends Specification { - - public static final String TEST_DOCS_PATH = 'src/test/resources/src/asciidoctor' - - def "should convert html without any configuration"() { - given: - final File srcAsciidoc = new File("$TEST_DOCS_PATH/sample.asciidoc") - final Sink sink = createSinkMock() - - AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser() - parser.@mavenProjectProvider = createMavenProjectMock() - - when: - parser.parse(new FileReader(srcAsciidoc), sink) - - then: - String outputText = sink.sinkedText - outputText.contains '

Document Title

' - outputText.contains '
' - outputText.contains '
' - outputText.contains "require 'asciidoctor'" - // icons as text - outputText.contains '
Note
' - } - - def "should convert html with an attribute"() { - given: - final File srcAsciidoc = new File("$TEST_DOCS_PATH/sample.asciidoc") - Reader reader = new FileReader(srcAsciidoc) - final Sink sink = createSinkMock() - - AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser() - parser.@mavenProjectProvider = createMavenProjectMock(''' - - - - font - - - ''') - - when: - parser.parse(reader, sink) - - then: - String outputText = sink.sinkedText - // :icons: font - outputText.contains '' - } - - def "should convert html with baseDir option"() { - given: - final File srcAsciidoc = new File("$TEST_DOCS_PATH/main-document.adoc") - final Sink sink = createSinkMock() - - AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser() - parser.@mavenProjectProvider = createMavenProjectMock(""" - - - ${new File(srcAsciidoc.parent).absolutePath} - - """) - - when: - parser.parse(new FileReader(srcAsciidoc), sink) - - then: 'include works' - String outputText = sink.sinkedText - outputText.contains '

Include test

' - outputText.contains 'println "HelloWorld from Groovy on ${new Date()}"' - } - - def "should convert html with relative baseDir option"() { - given: - final File srcAsciidoc = new File("$TEST_DOCS_PATH/main-document.adoc") - final Sink sink = createSinkMock() - - AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser() - parser.@mavenProjectProvider = createMavenProjectMock(""" - - - ${TEST_DOCS_PATH} - - """) - - when: - parser.parse(new FileReader(srcAsciidoc), sink) - - then: 'include works' - String outputText = sink.sinkedText - outputText.contains '

Include test

' - outputText.contains 'println "HelloWorld from Groovy on ${new Date()}"' - } - - def "should convert html with templateDir option"() { - given: - final File srcAsciidoc = new File("$TEST_DOCS_PATH/sample.asciidoc") - final Sink sink = createSinkMock() - - AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser() - parser.@mavenProjectProvider = createMavenProjectMock(""" - - - - ${TEST_DOCS_PATH}/templates - - - """) - - when: - parser.parse(new FileReader(srcAsciidoc), sink) - - then: - String outputText = sink.sinkedText - outputText.contains '

Document Title

' - outputText.contains '

' - } - - def "should convert html with attributes and baseDir option"() { - given: - final File srcAsciidoc = new File("$TEST_DOCS_PATH/main-document.adoc") - final Sink sink = createSinkMock() - - AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser() - parser.@mavenProjectProvider = createMavenProjectMock(""" - - - ${new File(srcAsciidoc.parent).absolutePath} - - - font - Hello World!! - - - """) - - when: - parser.parse(new FileReader(srcAsciidoc), sink) - - then: - String outputText = sink.sinkedText - outputText.contains '

Include test

' - outputText.contains '

1. Code

' - outputText.contains '

2. Optional section

' - outputText.contains 'println "HelloWorld from Groovy on ${new Date()}"' - outputText.contains 'Hello World!!' - outputText.contains '' - } - - def "should process empty self-closing XML attributes"() { - given: - final File srcAsciidoc = new File("$TEST_DOCS_PATH/sample.asciidoc") - final Sink sink = createSinkMock() - - AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser() - parser.@mavenProjectProvider = createMavenProjectMock(""" - - - - - - - """) - - when: - parser.parse(new FileReader(srcAsciidoc), sink) - - then: - String outputText = sink.sinkedText - outputText.contains '

1. Section A

' - outputText.contains '

1.1. Section A Subsection

' - } - - def "should process empty value XML attributes"() { - given: - final File srcAsciidoc = new File("$TEST_DOCS_PATH/sample.asciidoc") - final Sink sink = createSinkMock() - - AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser() - parser.@mavenProjectProvider = createMavenProjectMock(""" - - - - - - - """) - - when: - parser.parse(new FileReader(srcAsciidoc), sink) - - then: - String outputText = sink.sinkedText - outputText.contains '

1. Section A

' - outputText.contains '

1.1. Section A Subsection

' - } - - def "should fail when logHandler failIf = WARNING"() { - setup: - final File srcAsciidoc = new File("$TEST_DOCS_PATH/errors/document-with-missing-include.adoc") - final Sink sink = createSinkMock() - - AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser() - parser.@mavenProjectProvider = createMavenProjectMock(""" - - - - - - WARN - - - - """) - - when: - parser.parse(new FileReader(srcAsciidoc), sink) - - then: 'issues with WARN and ERROR are returned' - def e = thrown(org.apache.maven.doxia.parser.ParseException) - e.message.contains('Found 4 issue(s) of severity WARN or higher during conversion') - } - - private javax.inject.Provider createMavenProjectMock(final String configuration = null) { - MavenProject mp = [getGoalConfiguration: { pluginGroupId, pluginArtifactId, executionId, goalId -> - configuration ? Xpp3DomBuilder.build(new StringReader(configuration)) : null - }, - getBasedir : { - new File('.') - }] as MavenProject - - return [get: { mp }] as javax.inject.Provider - } - - /** - * Creates a {@link Sink} mock that allows retrieving a text previously sinked. - */ - private Sink createSinkMock() { - String text - return [rawText : { t -> - text = t - }, getSinkedText: { - text - }] as MySink - } - - interface MySink extends Sink { - String getSinkedText() - } - -} diff --git a/src/test/java/org/asciidoctor/maven/site/AsciidoctorDoxiaParserTest.java b/src/test/java/org/asciidoctor/maven/site/AsciidoctorDoxiaParserTest.java new file mode 100644 index 00000000..69457b95 --- /dev/null +++ b/src/test/java/org/asciidoctor/maven/site/AsciidoctorDoxiaParserTest.java @@ -0,0 +1,280 @@ +package org.asciidoctor.maven.site; + +import lombok.SneakyThrows; +import org.apache.maven.doxia.parser.ParseException; +import org.apache.maven.doxia.sink.Sink; +import org.apache.maven.doxia.sink.impl.AbstractTextSink; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.xml.Xpp3DomBuilder; +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.*; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.codehaus.plexus.util.ReflectionUtils.setVariableValueInObject; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +public class AsciidoctorDoxiaParserTest { + + private static final String TEST_DOCS_PATH = "src/test/resources/src/asciidoctor"; + + @Test + public void should_convert_html_without_any_configuration() throws FileNotFoundException, ParseException { + // given + final File srcAsciidoc = new File(TEST_DOCS_PATH, "sample.asciidoc"); + final Sink sink = createSinkMock(); + + AsciidoctorDoxiaParser parser = mockAsciidoctorDoxiaParser(); + + // when + parser.parse(new FileReader(srcAsciidoc), sink); + + // then + assertThat(((TextProviderSink) sink).text) + .contains("

Document Title

") + .contains("
") + .contains("
") + .contains("require 'asciidoctor'") + .contains("
Note
"); + } + + @Test + public void should_convert_html_with_an_attribute() throws FileNotFoundException, ParseException { + // given + final File srcAsciidoc = new File(TEST_DOCS_PATH, "sample.asciidoc"); + Reader reader = new FileReader(srcAsciidoc); + + Sink sink = createSinkMock(); + AsciidoctorDoxiaParser parser = mockAsciidoctorDoxiaParser( + "\n" + + " \n" + + " \n" + + " font\n" + + " \n" + + " \n" + + ""); + + // when + parser.parse(reader, sink); + + // then + assertThat(((TextProviderSink) sink).text) + .contains(""); + } + + @Test + public void should_convert_html_with_baseDir_option() throws FileNotFoundException, ParseException { + // given + final File srcAsciidoc = new File(TEST_DOCS_PATH, "main-document.adoc"); + final Sink sink = createSinkMock(); + + AsciidoctorDoxiaParser parser = mockAsciidoctorDoxiaParser( + "\n" + + " \n" + + " " + new File(srcAsciidoc.getParent()).getAbsolutePath() + "\n" + + " \n" + + ""); + + // when + parser.parse(new FileReader(srcAsciidoc), sink); + + // then: 'include works' + assertThat(((TextProviderSink) sink).text) + .contains("

Include test

") + .contains("println \"HelloWorld from Groovy on ${new Date()}\""); + } + + @Test + public void should_convert_html_with_relative_baseDir_option() throws FileNotFoundException, ParseException { + // given + final File srcAsciidoc = new File(TEST_DOCS_PATH, "main-document.adoc"); + final Sink sink = createSinkMock(); + + AsciidoctorDoxiaParser parser = mockAsciidoctorDoxiaParser( + "\n" + + " \n" + + " " + TEST_DOCS_PATH + "\n" + + " \n" + + ""); + + // when + parser.parse(new FileReader(srcAsciidoc), sink); + + // then: 'include works' + assertThat(((TextProviderSink) sink).text) + .contains("

Include test

") + .contains("println \"HelloWorld from Groovy on ${new Date()}\""); + } + + @Test + public void should_convert_html_with_templateDir_option() throws FileNotFoundException, ParseException { + // given + final File srcAsciidoc = new File(TEST_DOCS_PATH, "sample.asciidoc"); + final Sink sink = createSinkMock(); + + AsciidoctorDoxiaParser parser = mockAsciidoctorDoxiaParser( + "\n" + + " \n" + + " \n" + + " " + TEST_DOCS_PATH + "/templates\n" + + " \n" + + " \n" + + ""); + + // when + parser.parse(new FileReader(srcAsciidoc), sink); + + // then + assertThat(((TextProviderSink) sink).text) + .contains("

Document Title

") + .contains("

"); + } + + @Test + public void should_convert_html_with_attributes_and_baseDir_option() throws FileNotFoundException, ParseException { + // given + final File srcAsciidoc = new File(TEST_DOCS_PATH, "main-document.adoc"); + final Sink sink = createSinkMock(); + + AsciidoctorDoxiaParser parser = mockAsciidoctorDoxiaParser( + "\n" + + " \n" + + " " + new File(srcAsciidoc.getParent()).getAbsolutePath() + "\n" + + " \n" + + " \n" + + " font\n" + + " Hello World!!\n" + + " \n" + + " \n" + + ""); + + // when + parser.parse(new FileReader(srcAsciidoc), sink); + + // then + assertThat(((TextProviderSink) sink).text) + .contains("

Include test

") + .contains("

1. Code

") + .contains("

2. Optional section

") + .contains("println \"HelloWorld from Groovy on ${new Date()}\"") + .contains("Hello World!!") + .contains(""); + } + + @Test + public void should_process_empty_selfclosing_XML_attributes() throws FileNotFoundException, ParseException { + // given + final File srcAsciidoc = new File(TEST_DOCS_PATH, "sample.asciidoc"); + final Sink sink = createSinkMock(); + + AsciidoctorDoxiaParser parser = mockAsciidoctorDoxiaParser( + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""); + + // when + parser.parse(new FileReader(srcAsciidoc), sink); + + // then + assertThat(((TextProviderSink) sink).text) + .contains("

1. Section A

") + .contains("

1.1. Section A Subsection

"); + } + + @Test + public void should_process_empty_value_XML_attributes() throws FileNotFoundException, ParseException { + // given + final File srcAsciidoc = new File(TEST_DOCS_PATH, "sample.asciidoc"); + final Sink sink = createSinkMock(); + + AsciidoctorDoxiaParser parser = mockAsciidoctorDoxiaParser( + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""); + + // when + parser.parse(new FileReader(srcAsciidoc), sink); + + // then + assertThat(((TextProviderSink) sink).text) + .contains("

1. Section A

") + .contains("

1.1. Section A Subsection

"); + } + + @Test + public void should_fail_when_logHandler_failIf_is_WARNING() throws FileNotFoundException, ParseException { + // given + final File srcAsciidoc = new File(TEST_DOCS_PATH, "errors/document-with-missing-include.adoc"); + final Sink sink = createSinkMock(); + + AsciidoctorDoxiaParser parser = mockAsciidoctorDoxiaParser( + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " WARN\n" + + " \n" + + " \n" + + " \n" + + ""); + + // when + Throwable throwable = catchThrowable(() -> parser.parse(new FileReader(srcAsciidoc), sink)); + + + // then: 'issues with WARN and ERROR are returned' + assertThat(throwable) + .isInstanceOf(org.apache.maven.doxia.parser.ParseException.class) + .hasMessageContaining("Found 4 issue(s) of severity WARN or higher during conversion"); + } + + @SneakyThrows + private javax.inject.Provider createMavenProjectMock(String configuration) { + MavenProject mockProject = Mockito.mock(MavenProject.class); + when(mockProject.getBasedir()) + .thenReturn(new File(".")); + when(mockProject.getGoalConfiguration(anyString(), anyString(), anyString(), anyString())) + .thenReturn(configuration != null ? Xpp3DomBuilder.build(new StringReader(configuration)) : null); + + return () -> mockProject; + } + + @SneakyThrows + private AsciidoctorDoxiaParser mockAsciidoctorDoxiaParser() { + AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser(); + setVariableValueInObject(parser, "mavenProjectProvider", createMavenProjectMock(null)); + return parser; + } + + @SneakyThrows + private AsciidoctorDoxiaParser mockAsciidoctorDoxiaParser(String configuration) { + AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser(); + setVariableValueInObject(parser, "mavenProjectProvider", createMavenProjectMock(configuration)); + return parser; + } + + private Sink createSinkMock() { + return new TextProviderSink(); + } + + class TextProviderSink extends AbstractTextSink { + public String text; + + @Override + public void rawText(String text) { + this.text = text; + } + } +}