diff --git a/asciidoctorj-arquillian-extension/build.gradle b/asciidoctorj-arquillian-extension/build.gradle index 214e3c82c..a5f8b8b73 100644 --- a/asciidoctorj-arquillian-extension/build.gradle +++ b/asciidoctorj-arquillian-extension/build.gradle @@ -1,10 +1,11 @@ dependencies { compileOnly project(':asciidoctorj') - api project(':asciidoctorj-test-support') + implementation(project(':asciidoctorj-test-support')) { transitive = false } api "org.jboss.arquillian.container:arquillian-container-spi:$arquillianVersion" api "org.jboss.arquillian.container:arquillian-container-test-spi:$arquillianVersion" api "org.jboss.arquillian.container:arquillian-container-impl-base:$arquillianVersion" api "org.jboss.arquillian.container:arquillian-container-test-impl-base:$arquillianVersion" + api "junit:junit:$junit4Version" } def javaApiUrl = "https://docs.oracle.com/javase/8/docs/api/" diff --git a/asciidoctorj-arquillian-extension/src/main/java/org/asciidoctor/arquillian/AsciidoctorTestObserver.java b/asciidoctorj-arquillian-extension/src/main/java/org/asciidoctor/arquillian/AsciidoctorTestObserver.java index 99a0f50c4..a87afe72b 100644 --- a/asciidoctorj-arquillian-extension/src/main/java/org/asciidoctor/arquillian/AsciidoctorTestObserver.java +++ b/asciidoctorj-arquillian-extension/src/main/java/org/asciidoctor/arquillian/AsciidoctorTestObserver.java @@ -4,7 +4,7 @@ import org.asciidoctor.arquillian.api.Shared; import org.asciidoctor.arquillian.api.Unshared; import org.asciidoctor.jruby.AsciidoctorJRuby; -import org.asciidoctor.util.ClasspathResources; +import org.asciidoctor.junit.ClasspathResources; import org.jboss.arquillian.core.api.InstanceProducer; import org.jboss.arquillian.core.api.annotation.ApplicationScoped; import org.jboss.arquillian.core.api.annotation.Inject; @@ -122,7 +122,7 @@ public void afterTestClassShutdownSharedTemporaryFolderInstance(@Observes AfterC } private boolean isSharedInstanceRequired(Class testClass, Class resourceClass) { - for (Field f: SecurityActions.getFieldsWithAnnotation(testClass, ArquillianResource.class)) { + for (Field f : SecurityActions.getFieldsWithAnnotation(testClass, ArquillianResource.class)) { ArquillianResource arquillianResource = SecurityActions.getAnnotation(f, ArquillianResource.class); if (f.getType() == resourceClass && arquillianResource.value() == Shared.class) { return true; @@ -132,7 +132,7 @@ private boolean isSharedInstanceRequired(Class testClass, Class resourceCl } private boolean isUnsharedInstanceRequired(Class testClass, Class resourceClass) { - for (Field f: SecurityActions.getFieldsWithAnnotation(testClass, ArquillianResource.class)) { + for (Field f : SecurityActions.getFieldsWithAnnotation(testClass, ArquillianResource.class)) { ArquillianResource arquillianResource = SecurityActions.getAnnotation(f, ArquillianResource.class); if (f.getType() == resourceClass && (arquillianResource.value() == ArquillianResource.class || arquillianResource.value() == Unshared.class)) { @@ -171,4 +171,4 @@ private boolean isUnsharedInstanceRequired(Method testMethod, Class resourceC } return false; } -} \ No newline at end of file +} diff --git a/asciidoctorj-arquillian-extension/src/main/java/org/asciidoctor/arquillian/ClasspathResourcesResourceProvider.java b/asciidoctorj-arquillian-extension/src/main/java/org/asciidoctor/arquillian/ClasspathResourcesResourceProvider.java index fa6ce7d40..54e49e2b0 100644 --- a/asciidoctorj-arquillian-extension/src/main/java/org/asciidoctor/arquillian/ClasspathResourcesResourceProvider.java +++ b/asciidoctorj-arquillian-extension/src/main/java/org/asciidoctor/arquillian/ClasspathResourcesResourceProvider.java @@ -1,6 +1,6 @@ package org.asciidoctor.arquillian; -import org.asciidoctor.util.ClasspathResources; +import org.asciidoctor.junit.ClasspathResources; import org.jboss.arquillian.core.api.Instance; import org.jboss.arquillian.core.api.annotation.ApplicationScoped; import org.jboss.arquillian.core.api.annotation.Inject; @@ -24,4 +24,4 @@ public boolean canProvide(Class type) { public Object lookup(ArquillianResource resource, Annotation... qualifiers) { return classpathResourcesInstance.get(); } -} \ No newline at end of file +} diff --git a/asciidoctorj-test-support/src/main/java/org/asciidoctor/util/ClasspathResources.java b/asciidoctorj-arquillian-extension/src/main/java/org/asciidoctor/junit/ClasspathResources.java similarity index 70% rename from asciidoctorj-test-support/src/main/java/org/asciidoctor/util/ClasspathResources.java rename to asciidoctorj-arquillian-extension/src/main/java/org/asciidoctor/junit/ClasspathResources.java index abf81ba38..e66a1c8b1 100644 --- a/asciidoctorj-test-support/src/main/java/org/asciidoctor/util/ClasspathResources.java +++ b/asciidoctorj-arquillian-extension/src/main/java/org/asciidoctor/junit/ClasspathResources.java @@ -1,5 +1,6 @@ -package org.asciidoctor.util; +package org.asciidoctor.junit; +import org.asciidoctor.test.extension.ClasspathHelper; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; @@ -9,17 +10,19 @@ *

* Delegates to {@link ClasspathHelper}. */ -public class ClasspathResources extends ClasspathHelper implements TestRule { +public class ClasspathResources implements TestRule { + + private ClasspathHelper classpathHelper; public ClasspathResources() { } public ClasspathResources(Class clazz) { - setClassloader(clazz); + classpathHelper = new ClasspathHelper(clazz); } protected void before(Class clazz) { - super.setClassloader(clazz); + classpathHelper = new ClasspathHelper(clazz); } @Override diff --git a/asciidoctorj-cli/src/test/java/org/asciidoctor/cli/WhenAsciidoctorIsCalledUsingCli.java b/asciidoctorj-cli/src/test/java/org/asciidoctor/cli/WhenAsciidoctorIsCalledUsingCli.java index c4f83a798..64415a30e 100644 --- a/asciidoctorj-cli/src/test/java/org/asciidoctor/cli/WhenAsciidoctorIsCalledUsingCli.java +++ b/asciidoctorj-cli/src/test/java/org/asciidoctor/cli/WhenAsciidoctorIsCalledUsingCli.java @@ -1,13 +1,14 @@ package org.asciidoctor.cli; import org.asciidoctor.cli.jruby.AsciidoctorInvoker; -import org.asciidoctor.util.ClasspathHelper; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.ClasspathExtension; import org.assertj.core.api.Assertions; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; import java.io.ByteArrayOutputStream; @@ -22,31 +23,24 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.core.StringStartsWith.startsWith; +@ExtendWith(ClasspathExtension.class) public class WhenAsciidoctorIsCalledUsingCli { - private static final String SAMPLE_FILE = "rendersample.asciidoc"; - private static final String BROKEN_INCLUDE_ASCIIDOC = "brokeninclude.asciidoc"; - private static final String TOC_SAMPLE_ASCIIDOC = "tocsample.asciidoc"; + static final String SOURCE_EXTENSION_PATTERN = "\\.asciidoc$"; - private static final String SOURCE_EXTENSION_PATTERN = "\\.asciidoc$"; + @ClasspathResource("rendersample.asciidoc") + private File renderSampleDocument; + @ClasspathResource("brokeninclude.asciidoc") + private File brokenIncludeDocument; + @ClasspathResource("tocsample.asciidoc") + private File tocSampleDocument; - private static ClasspathHelper classpath; + final String pwd = new File("").getAbsolutePath(); - @TempDir - public File temporaryFolder; - - public String pwd = new File("").getAbsolutePath(); - - @BeforeAll - static void beforeAll() { - classpath = new ClasspathHelper(); - classpath.setClassloader(WhenAsciidoctorIsCalledUsingCli.class); - } @Test - public void with_no_options_file_should_be_rendered_in_place_and_in_html5_format() throws IOException { - File inputFile = classpath.getResource(SAMPLE_FILE); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); + void with_no_options_file_should_be_rendered_in_place_and_in_html5_format() throws IOException { + final String inputPath = renderSampleDocument.getPath().substring(pwd.length() + 1); new AsciidoctorInvoker().invoke(inputPath); @@ -56,9 +50,8 @@ public void with_no_options_file_should_be_rendered_in_place_and_in_html5_format } @Test - public void should_honor_doctype_defined_in_document_by_default() throws IOException { - File inputFile = classpath.getResource("sample-book.adoc"); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); + void should_honor_doctype_defined_in_document_by_default(@ClasspathResource("sample-book.adoc") File sampleBookDocument) throws IOException { + final String inputPath = sampleBookDocument.getPath().substring(pwd.length() + 1); new AsciidoctorInvoker().invoke(inputPath); @@ -72,9 +65,8 @@ public void should_honor_doctype_defined_in_document_by_default() throws IOExcep } @Test - public void file_should_be_rendered_to_docbook_with_docbook_backend() throws IOException { - File inputFile = classpath.getResource(SAMPLE_FILE); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); + void file_should_be_rendered_to_docbook_with_docbook_backend() throws IOException { + final String inputPath = renderSampleDocument.getPath().substring(pwd.length() + 1); new AsciidoctorInvoker().invoke("-b", "docbook", inputPath); @@ -84,9 +76,8 @@ public void file_should_be_rendered_to_docbook_with_docbook_backend() throws IOE } @Test - public void single_attributes_should_be_interpreted_as_boolean() throws IOException { - File inputFile = classpath.getResource(SAMPLE_FILE); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); + void single_attributes_should_be_interpreted_as_boolean() throws IOException { + final String inputPath = renderSampleDocument.getPath().substring(pwd.length() + 1); new AsciidoctorInvoker().invoke("-a", "linkcss!", inputPath); @@ -102,9 +93,8 @@ public void single_attributes_should_be_interpreted_as_boolean() throws IOExcept } @Test - public void composed_attributes_should_be_built_as_attributes_map() throws IOException { - File inputFile = classpath.getResource(SAMPLE_FILE); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); + void composed_attributes_should_be_built_as_attributes_map() throws IOException { + final String inputPath = renderSampleDocument.getPath().substring(pwd.length() + 1); new AsciidoctorInvoker().invoke("-a", "stylesheet=mystyles.css", "-a", "linkcss", inputPath); @@ -119,20 +109,18 @@ public void composed_attributes_should_be_built_as_attributes_map() throws IOExc } @Test - public void destination_dir_should_render_files_to_ouput_directory() throws IOException { - File outputDirectory = temporaryFolder; + void destination_dir_should_render_files_to_ouput_directory(@TempDir File temporaryFolder) throws IOException { + final String inputPath = renderSampleDocument.getPath().substring(pwd.length() + 1); - File inputFile = classpath.getResource(SAMPLE_FILE); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); - new AsciidoctorInvoker().invoke("-D", outputDirectory.getAbsolutePath(), inputPath); + new AsciidoctorInvoker().invoke("-D", temporaryFolder.getAbsolutePath(), inputPath); - File expectedFile = new File(outputDirectory, inputFile.getName().replaceFirst(SOURCE_EXTENSION_PATTERN, ".html")); + File expectedFile = new File(temporaryFolder, renderSampleDocument.getName().replaceFirst(SOURCE_EXTENSION_PATTERN, ".html")); assertThat(expectedFile.exists(), is(true)); } @Test - public void empty_input_file_name_should_throw_an_exception() { + void empty_input_file_name_should_throw_an_exception() { Throwable throwable = catchThrowable(() -> new AsciidoctorInvoker().invoke("")); Assertions.assertThat(throwable) @@ -140,7 +128,7 @@ public void empty_input_file_name_should_throw_an_exception() { } @Test - public void version_flag_should_print_version_and_exit() throws IOException { + void version_flag_should_print_version_and_exit() throws IOException { PrintStream oldOs = System.out; ByteArrayOutputStream os = new ByteArrayOutputStream(); System.setOut(new PrintStream(os)); @@ -153,7 +141,7 @@ public void version_flag_should_print_version_and_exit() throws IOException { } @Test - public void invalid_input_file_should_throw_an_exception() { + void invalid_input_file_should_throw_an_exception() { Throwable throwable = catchThrowable(() -> new AsciidoctorInvoker().invoke("myunknown.adoc")); Assertions.assertThat(throwable) @@ -161,11 +149,9 @@ public void invalid_input_file_should_throw_an_exception() { } @Test - public void should_convert_multiple_inputs() throws IOException { - File inputFile1 = classpath.getResource(SAMPLE_FILE); - String inputPath1 = inputFile1.getPath().substring(pwd.length() + 1); - File inputFile2 = classpath.getResource(TOC_SAMPLE_ASCIIDOC); - String inputPath2 = inputFile2.getPath().substring(pwd.length() + 1); + void should_convert_multiple_inputs() throws IOException { + final String inputPath1 = renderSampleDocument.getPath().substring(pwd.length() + 1); + final String inputPath2 = tocSampleDocument.getPath().substring(pwd.length() + 1); new AsciidoctorInvoker().invoke(inputPath1, inputPath2); @@ -179,10 +165,9 @@ public void should_convert_multiple_inputs() throws IOException { } @Test - public void glob_expression_can_be_used_to_render_AsciiDoc_files() throws IOException { - File inputFile = classpath.getResource(TOC_SAMPLE_ASCIIDOC); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); - String inputGlob = new File(new File(inputPath).getParentFile(), "toc*ple.asciidoc").getPath(); + void glob_expression_can_be_used_to_render_AsciiDoc_files() throws IOException { + final String inputPath = tocSampleDocument.getPath().substring(pwd.length() + 1); + final String inputGlob = new File(new File(inputPath).getParentFile(), "toc*ple.asciidoc").getPath(); new AsciidoctorInvoker().invoke(inputGlob); @@ -193,8 +178,8 @@ public void glob_expression_can_be_used_to_render_AsciiDoc_files() throws IOExce } @Test - public void help_option_should_show_usage_information() throws IOException { - ByteArrayOutputStream output = redirectStdout(); + void help_option_should_show_usage_information() throws IOException { + final ByteArrayOutputStream output = redirectStdout(); new AsciidoctorInvoker().invoke("--help"); @@ -203,8 +188,8 @@ public void help_option_should_show_usage_information() throws IOException { } @Test - public void no_parameters_should_show_usage_information() throws IOException { - ByteArrayOutputStream output = redirectStdout(); + void no_parameters_should_show_usage_information() throws IOException { + final ByteArrayOutputStream output = redirectStdout(); new AsciidoctorInvoker().invoke(); @@ -213,10 +198,9 @@ public void no_parameters_should_show_usage_information() throws IOException { } @Test - public void output_file_hyphen_symbol_should_render_output_to_stdout() throws IOException { - ByteArrayOutputStream output = redirectStdout(); - File inputFile = classpath.getResource(SAMPLE_FILE); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); + void output_file_hyphen_symbol_should_render_output_to_stdout() throws IOException { + final ByteArrayOutputStream output = redirectStdout(); + final String inputPath = renderSampleDocument.getPath().substring(pwd.length() + 1); new AsciidoctorInvoker().invoke("-o", "-", inputPath); @@ -227,10 +211,9 @@ public void output_file_hyphen_symbol_should_render_output_to_stdout() throws IO } @Test - public void verbose_option_should_fill_monitor_map() throws IOException { - ByteArrayOutputStream output = redirectStdout(); - File inputFile = classpath.getResource(SAMPLE_FILE); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); + void verbose_option_should_fill_monitor_map() throws IOException { + final ByteArrayOutputStream output = redirectStdout(); + final String inputPath = renderSampleDocument.getPath().substring(pwd.length() + 1); new AsciidoctorInvoker().invoke("--timings", inputPath); @@ -240,10 +223,9 @@ public void verbose_option_should_fill_monitor_map() throws IOException { } @Test - public void quiet_option_should_not_write_to_console() throws IOException { - ByteArrayOutputStream output = redirectStdout(); - File inputFile = classpath.getResource(BROKEN_INCLUDE_ASCIIDOC); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); + void quiet_option_should_not_write_to_console() throws IOException { + final ByteArrayOutputStream output = redirectStdout(); + final String inputPath = brokenIncludeDocument.getPath().substring(pwd.length() + 1); new AsciidoctorInvoker().invoke("--quiet", inputPath); @@ -252,9 +234,8 @@ public void quiet_option_should_not_write_to_console() throws IOException { } @Test - public void should_exit_with_zero_status_even_if_errors_were_logged() throws IOException { - File inputFile = classpath.getResource(BROKEN_INCLUDE_ASCIIDOC); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); + void should_exit_with_zero_status_even_if_errors_were_logged() throws IOException { + final String inputPath = brokenIncludeDocument.getPath().substring(pwd.length() + 1); int status = new AsciidoctorInvoker().invoke(inputPath); @@ -262,9 +243,8 @@ public void should_exit_with_zero_status_even_if_errors_were_logged() throws IOE } @Test - public void should_exit_with_nonzero_status_if_logged_severity_was_at_least_failure_level() throws IOException { - File inputFile = classpath.getResource(BROKEN_INCLUDE_ASCIIDOC); - String inputPath = inputFile.getPath().substring(pwd.length() + 1); + void should_exit_with_nonzero_status_if_logged_severity_was_at_least_failure_level() throws IOException { + String inputPath = brokenIncludeDocument.getPath().substring(pwd.length() + 1); int status = new AsciidoctorInvoker().invoke("--failure-level", "warn", inputPath); @@ -272,9 +252,8 @@ public void should_exit_with_nonzero_status_if_logged_severity_was_at_least_fail } @Test - public void with_absolute_path_file_should_be_rendered() throws IOException { - File inputFile = classpath.getResource(SAMPLE_FILE); - String inputPath = inputFile.getAbsolutePath(); + void with_absolute_path_file_should_be_rendered() throws IOException { + String inputPath = renderSampleDocument.getAbsolutePath(); new AsciidoctorInvoker().invoke(inputPath); @@ -284,10 +263,9 @@ public void with_absolute_path_file_should_be_rendered() throws IOException { } @Test - public void should_convert_to_subdirectories() throws IOException { - File inputFile = classpath.getResource("relative/sub/test.adoc"); - File srcDir = inputFile.getParentFile().getParentFile(); - File toDir = new File(srcDir, "target"); + void should_convert_to_subdirectories(@ClasspathResource("relative/sub/test.adoc") File relativePathDocument) throws IOException { + final File srcDir = relativePathDocument.getParentFile().getParentFile(); + final File toDir = new File(srcDir, "target"); new AsciidoctorInvoker().invoke("-R", srcDir.getPath(), "-D", toDir.getPath(), srcDir.getAbsolutePath() + "/**/*.adoc"); @@ -301,5 +279,4 @@ private ByteArrayOutputStream redirectStdout() { System.setOut(new PrintStream(output)); return output; } - } diff --git a/asciidoctorj-core/build.gradle b/asciidoctorj-core/build.gradle index 6246205b8..b666ce03f 100644 --- a/asciidoctorj-core/build.gradle +++ b/asciidoctorj-core/build.gradle @@ -33,7 +33,7 @@ dependencies { testImplementation "com.google.guava:guava:$guavaVersion" testImplementation "org.jsoup:jsoup:$jsoupVersion" testImplementation "io.netty:netty-all:$nettyVersion" - testImplementation project(':asciidoctorj-arquillian-extension') + testImplementation project(':asciidoctorj-test-support') compileOnly "org.osgi:osgi.annotation:$osgiVersion" } @@ -138,6 +138,3 @@ task pollutedTest(type: Test) { } test.dependsOn pollutedTest - - - diff --git a/asciidoctorj-core/src/test/groovy/org/asciidoctor/WhenSlimTemplatesAreUsed.groovy b/asciidoctorj-core/src/test/groovy/org/asciidoctor/WhenSlimTemplatesAreUsed.groovy index b1b18a119..862a643c9 100644 --- a/asciidoctorj-core/src/test/groovy/org/asciidoctor/WhenSlimTemplatesAreUsed.groovy +++ b/asciidoctorj-core/src/test/groovy/org/asciidoctor/WhenSlimTemplatesAreUsed.groovy @@ -1,7 +1,7 @@ package org.asciidoctor import org.asciidoctor.jruby.internal.JRubyAsciidoctor -import org.asciidoctor.util.ClasspathHelper +import org.asciidoctor.test.extension.ClasspathHelper import org.jsoup.Jsoup import org.jsoup.nodes.Document import org.jsoup.nodes.Element @@ -9,15 +9,10 @@ import spock.lang.Specification class WhenSlimTemplatesAreUsed extends Specification { - ClasspathHelper classpath = new ClasspathHelper() - - def setup() { - classpath = new ClasspathHelper() - classpath.classloader = WhenSlimTemplatesAreUsed - } - Asciidoctor asciidoctor = JRubyAsciidoctor.create() + ClasspathHelper classpath = new ClasspathHelper(WhenSlimTemplatesAreUsed) + def 'the slim paragraph template should be used when rendering a document inline'() { given: Options options = Options.builder() diff --git a/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenABlockMacroProcessorCreatesATable.groovy b/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenABlockMacroProcessorCreatesATable.groovy index 45c5e14db..ff694bfed 100644 --- a/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenABlockMacroProcessorCreatesATable.groovy +++ b/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenABlockMacroProcessorCreatesATable.groovy @@ -3,7 +3,7 @@ package org.asciidoctor.extension import org.asciidoctor.Asciidoctor import org.asciidoctor.Options import org.asciidoctor.SafeMode -import org.asciidoctor.util.ClasspathHelper +import org.asciidoctor.test.extension.ClasspathHelper import org.asciidoctor.util.TestHttpServer import org.jsoup.Jsoup import org.jsoup.nodes.Document @@ -36,8 +36,7 @@ githubcontributors::asciidoctor/asciidoctorj[] def setup() { asciidoctor = Asciidoctor.Factory.create() - classpathResources = new ClasspathHelper() - classpathResources.classloader = WhenABlockMacroProcessorCreatesATable + classpathResources = new ClasspathHelper(WhenABlockMacroProcessorCreatesATable) TestHttpServer.start(['http://api.github.com/repos/asciidoctor/asciidoctorj/contributors': classpathResources.getResource('githubcontributors.json')]) } diff --git a/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenExtensionsAreRegisteredAsService.groovy b/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenExtensionsAreRegisteredAsService.groovy index a15c0db5b..1ee03d7bd 100644 --- a/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenExtensionsAreRegisteredAsService.groovy +++ b/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenExtensionsAreRegisteredAsService.groovy @@ -3,7 +3,7 @@ package org.asciidoctor.extension import org.asciidoctor.Asciidoctor import org.asciidoctor.Options import org.asciidoctor.SafeMode -import org.asciidoctor.util.ClasspathHelper +import org.asciidoctor.test.extension.ClasspathHelper import org.jsoup.Jsoup import org.jsoup.nodes.Document import org.jsoup.nodes.Element @@ -24,8 +24,7 @@ class WhenExtensionsAreRegisteredAsService extends Specification { def setup() { asciidoctor = Asciidoctor.Factory.create() - classpath = new ClasspathHelper() - classpath.classloader = WhenExtensionsAreRegisteredAsService + classpath = new ClasspathHelper(WhenExtensionsAreRegisteredAsService) originalTCCL = Thread.currentThread().contextClassLoader } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAnAsciidoctorClassIsInstantiated.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAnAsciidoctorClassIsInstantiated.java index 0b95a9936..d95f43339 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAnAsciidoctorClassIsInstantiated.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAnAsciidoctorClassIsInstantiated.java @@ -1,16 +1,16 @@ package org.asciidoctor; import com.google.common.io.CharStreams; -import org.asciidoctor.arquillian.api.Unshared; import org.asciidoctor.jruby.AsciiDocDirectoryWalker; import org.asciidoctor.jruby.internal.AsciidoctorCoreException; -import org.asciidoctor.util.ClasspathResources; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.assertj.core.api.Assertions; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilderFactory; @@ -18,12 +18,11 @@ import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; import java.text.Format; import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Calendar; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import static org.asciidoctor.AttributesBuilder.attributes; import static org.asciidoctor.OptionsBuilder.options; @@ -32,19 +31,24 @@ import static org.hamcrest.Matchers.isEmptyString; import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize; import static org.junit.Assert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.xmlmatchers.xpath.HasXPath.hasXPath; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenAnAsciidoctorClassIsInstantiated { - @ArquillianResource - private ClasspathResources classpath; + @AsciidoctorInstance + private Asciidoctor asciidoctor; - @ArquillianResource - private TemporaryFolder testFolder; + @ClasspathResource("src") + private File pathToWalk; + + @ClasspathResource("rendersample.asciidoc") + private File renderSampleDocument; + + @TempDir + private File tempFolder; - @ArquillianResource(Unshared.class) - private Asciidoctor asciidoctor; @Test public void should_accept_empty_string_as_empty_content_when_output_is_String() { @@ -64,7 +68,7 @@ public void should_accept_null_string_as_empty_content_when_output_is_String() { @Test public void should_accept_null_string_as_empty_content_when_output_is_File() { - File expectedFile = new File(testFolder.getRoot(), "expected_empty.html"); + File expectedFile = new File(tempFolder, "expected_empty.html"); Options options = Options.builder() .safe(SafeMode.UNSAFE) .toFile(expectedFile) @@ -88,10 +92,10 @@ public void should_fail_when_reader_is_null() { } @Test - public void content_should_be_read_from_reader_and_written_to_writer() throws IOException, SAXException, - ParserConfigurationException { + public void content_should_be_read_from_reader_and_written_to_writer() + throws IOException, SAXException, ParserConfigurationException { - FileReader inputAsciidoctorFile = new FileReader(classpath.getResource("rendersample.asciidoc")); + FileReader inputAsciidoctorFile = new FileReader(renderSampleDocument); StringWriter rendererWriter = new StringWriter(); asciidoctor.convert(inputAsciidoctorFile, rendererWriter, options().asMap()); @@ -104,20 +108,18 @@ public void content_should_be_read_from_reader_and_written_to_writer() throws IO public void file_document_should_be_rendered_into_default_backend() throws IOException, SAXException, ParserConfigurationException { - String render_file = asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), - options().toFile(false).get()); + Options options = Options.builder().toFile(false).build(); + String render_file = asciidoctor.convertFile(renderSampleDocument, options); assertRenderedFile(render_file); - } @Test public void file_document_should_be_rendered_into_current_directory_using_options_class() { Options options = options().inPlace(true).get(); - File inputFile = classpath.getResource("rendersample.asciidoc"); - String renderContent = asciidoctor.convertFile(inputFile, options); + String renderContent = asciidoctor.convertFile(renderSampleDocument, options); - File expectedFile = new File(inputFile.getParent(), "rendersample.html"); + File expectedFile = new File(renderSampleDocument.getParent(), "rendersample.html"); assertThat(expectedFile.exists(), is(true)); assertThat(renderContent, is(nullValue())); @@ -128,11 +130,10 @@ public void file_document_should_be_rendered_into_current_directory_using_option @Test public void file_document_should_be_rendered_into_current_directory() { - File inputFile = classpath.getResource("rendersample.asciidoc"); - String renderContent = asciidoctor.convertFile(inputFile, options() + String renderContent = asciidoctor.convertFile(renderSampleDocument, options() .inPlace(true).asMap()); - File expectedFile = new File(inputFile.getParent(), "rendersample.html"); + File expectedFile = new File(renderSampleDocument.getParent(), "rendersample.html"); assertThat(expectedFile.exists(), is(true)); assertThat(renderContent, is(nullValue())); @@ -143,11 +144,14 @@ public void file_document_should_be_rendered_into_current_directory() { @Test public void file_document_should_be_rendered_into_foreign_directory() { - Map options = options().inPlace(false).safe(SafeMode.UNSAFE).toDir(testFolder.getRoot()) - .asMap(); - String renderContent = asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + final Options options = Options.builder() + .inPlace(false) + .safe(SafeMode.UNSAFE) + .toDir(tempFolder) + .build(); + String renderContent = asciidoctor.convertFile(renderSampleDocument, options); - File expectedFile = new File(testFolder.getRoot(), "rendersample.html"); + File expectedFile = new File(tempFolder, "rendersample.html"); assertThat(expectedFile.exists(), is(true)); assertThat(renderContent, is(nullValue())); @@ -156,12 +160,16 @@ public void file_document_should_be_rendered_into_foreign_directory() { } @Test - public void file_document_should_be_rendered_from_base_dir() throws IOException { - - File output = testFolder.newFolder("asciidoc", "docs"); - Options options = options().inPlace(false).baseDir(testFolder.getRoot()) - .toFile(new File("asciidoc/docs/rendersample.html")).get(); - String renderContent = asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + public void file_document_should_be_rendered_from_base_dir() { + + final File output = new File(tempFolder, "asciidoc/docs"); + output.mkdirs(); + final Options options = Options.builder() + .inPlace(false) + .baseDir(tempFolder) + .toFile(new File("asciidoc/docs/rendersample.html")) + .build(); + String renderContent = asciidoctor.convertFile(renderSampleDocument, options); File renderedFile = new File(output, "rendersample.html"); @@ -172,11 +180,11 @@ public void file_document_should_be_rendered_from_base_dir() throws IOException @Test public void file_document_should_be_rendered_into_foreign_directory_using_options_class() { - Options options = options().inPlace(false).safe(SafeMode.UNSAFE).toDir(testFolder.getRoot()).get(); + Options options = options().inPlace(false).safe(SafeMode.UNSAFE).toDir(tempFolder).get(); - String renderContent = asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + String renderContent = asciidoctor.convertFile(renderSampleDocument, options); - File expectedFile = new File(testFolder.getRoot(), "rendersample.html"); + File expectedFile = new File(tempFolder, "rendersample.html"); assertThat(expectedFile.exists(), is(true)); assertThat(renderContent, is(nullValue())); @@ -188,10 +196,9 @@ public void docbook_document_should_be_rendered_into_current_directory() { Map attributes = attributes().backend("docbook").asMap(); Map options = options().inPlace(true).attributes(attributes).asMap(); - File inputFile = classpath.getResource("rendersample.asciidoc"); - String renderContent = asciidoctor.convertFile(inputFile, options); + String renderContent = asciidoctor.convertFile(renderSampleDocument, options); - File expectedFile = new File(inputFile.getParent(), "rendersample.xml"); + File expectedFile = new File(renderSampleDocument.getParent(), "rendersample.xml"); assertThat(expectedFile.exists(), is(true)); assertThat(renderContent, is(nullValue())); @@ -205,10 +212,9 @@ public void docbook_document_should_be_rendered_into_current_directory_using_opt Attributes attributes = attributes().backend("docbook").get(); Options options = options().inPlace(true).attributes(attributes).get(); - File inputFile = classpath.getResource("rendersample.asciidoc"); - String renderContent = asciidoctor.convertFile(inputFile, options); + String renderContent = asciidoctor.convertFile(renderSampleDocument, options); - File expectedFile = new File(inputFile.getParent(), "rendersample.xml"); + File expectedFile = new File(renderSampleDocument.getParent(), "rendersample.xml"); assertThat(expectedFile.exists(), is(true)); assertThat(renderContent, is(nullValue())); @@ -221,10 +227,9 @@ public void docbook_document_should_be_rendered_into_current_directory_using_opt Options options = options().inPlace(true).backend("docbook").get(); - File inputFile = classpath.getResource("rendersample.asciidoc"); - String renderContent = asciidoctor.convertFile(inputFile, options); + String renderContent = asciidoctor.convertFile(renderSampleDocument, options); - File expectedFile = new File(inputFile.getParent(), "rendersample.xml"); + File expectedFile = new File(renderSampleDocument.getParent(), "rendersample.xml"); assertThat(expectedFile.exists(), is(true)); assertThat(renderContent, is(nullValue())); @@ -233,10 +238,9 @@ public void docbook_document_should_be_rendered_into_current_directory_using_opt } @Test - public void string_content_with_custom_date_should_be_rendered() throws IOException, SAXException, - ParserConfigurationException { - - InputStream content = new FileInputStream(classpath.getResource("documentwithdate.asciidoc")); + public void string_content_with_custom_date_should_be_rendered( + @ClasspathResource("documentwithdate.asciidoc") Path documentWithDate) + throws IOException, SAXException, ParserConfigurationException { Calendar customDate = Calendar.getInstance(); customDate.set(Calendar.YEAR, 2012); @@ -246,17 +250,16 @@ public void string_content_with_custom_date_should_be_rendered() throws IOExcept Map attributes = attributes().localDate(customDate.getTime()).asMap(); Map options = options().attributes(attributes).asMap(); - String render_file = asciidoctor.convert(toString(content), options); + String render_file = asciidoctor.convert(Files.readString(documentWithDate), options); assertRenderedLocalDateContent(render_file, "2012-12-05."); } @Test - public void string_content_with_custom_time_should_be_rendered() throws IOException, SAXException, + public void string_content_with_custom_time_should_be_rendered( + @ClasspathResource("documentwithtime.asciidoc") Path documentWithDate) throws IOException, SAXException, ParserConfigurationException { - InputStream content = new FileInputStream(classpath.getResource("documentwithtime.asciidoc")); - Calendar customTime = Calendar.getInstance(); customTime.set(Calendar.HOUR_OF_DAY, 23); customTime.set(Calendar.MINUTE, 15); @@ -265,7 +268,7 @@ public void string_content_with_custom_time_should_be_rendered() throws IOExcept Map attributes = attributes().localTime(customTime.getTime()).asMap(); Map options = options().attributes(attributes).asMap(); - String render_file = asciidoctor.convert(toString(content), options); + String render_file = asciidoctor.convert(Files.readString(documentWithDate), options); Format TIME_FORMAT = new SimpleDateFormat("HH:mm:ss z"); @@ -277,8 +280,7 @@ public void string_content_with_custom_time_should_be_rendered() throws IOExcept public void string_content_document_should_be_rendered_into_default_backend() throws IOException, SAXException, ParserConfigurationException { - InputStream content = new FileInputStream(classpath.getResource("rendersample.asciidoc")); - String render_file = asciidoctor.convert(toString(content), new HashMap()); + String render_file = asciidoctor.convert(Files.readString(renderSampleDocument.toPath()), new HashMap<>()); assertRenderedFile(render_file); } @@ -287,19 +289,21 @@ public void string_content_document_should_be_rendered_into_default_backend() th public void all_files_from_a_collection_should_be_rendered_into_an_array() { String[] allRenderedFiles = asciidoctor.convertFiles( - Arrays.asList(classpath.getResource("rendersample.asciidoc")), options().toFile(false).get()); + List.of(renderSampleDocument), options().toFile(false).get()); assertThat(allRenderedFiles, is(arrayWithSize(1))); - } @Test public void all_files_from_a_collection_should_be_rendered_into_files_and_not_in_array() { - Map options = options().inPlace(false).safe(SafeMode.UNSAFE).toDir(testFolder.getRoot()) - .asMap(); + Options options = Options.builder() + .inPlace(false) + .safe(SafeMode.UNSAFE) + .toDir(tempFolder) + .build(); String[] allRenderedFiles = asciidoctor.convertFiles( - Arrays.asList(classpath.getResource("rendersample.asciidoc")), options); + Arrays.asList(renderSampleDocument), options); assertThat(allRenderedFiles, is(arrayWithSize(0))); } @@ -307,11 +311,9 @@ public void all_files_from_a_collection_should_be_rendered_into_files_and_not_in @Test public void all_files_from_directory_and_subdirectories_should_be_rendered_into_an_array() { - File pathToWalk = classpath.getResource("src"); - - String[] allRenderedFiles = asciidoctor.convertDirectory(new AsciiDocDirectoryWalker( - pathToWalk.getPath()), - options().toFile(false).get()); + String[] allRenderedFiles = asciidoctor.convertDirectory( + new AsciiDocDirectoryWalker(pathToWalk.getPath()), + Options.builder().toFile(false).build()); assertThat(allRenderedFiles, is(arrayWithSize(4))); } @@ -319,10 +321,11 @@ public void all_files_from_directory_and_subdirectories_should_be_rendered_into_ @Test public void all_files_from_directory_and_subdirectories_should_be_rendered_into_files_and_not_in_array() { - File pathToWalk = classpath.getResource("src"); - Map options = options().inPlace(false).safe(SafeMode.UNSAFE).toDir(testFolder.getRoot()) - .asMap(); - + final Options options = Options.builder() + .inPlace(false) + .safe(SafeMode.UNSAFE) + .toDir(tempFolder) + .build(); String[] allRenderedFiles = asciidoctor.convertDirectory( new AsciiDocDirectoryWalker(pathToWalk.getAbsolutePath()), options); @@ -330,12 +333,15 @@ public void all_files_from_directory_and_subdirectories_should_be_rendered_into_ } - @Test(expected = AsciidoctorCoreException.class) + @Test public void an_exception_should_be_thrown_if_backend_cannot_be_resolved() { - Options options = options().inPlace(true).backend("mybackend").get(); - - File inputFile = classpath.getResource("rendersample.asciidoc"); - asciidoctor.convertFile(inputFile, options); + assertThrows(AsciidoctorCoreException.class, () -> { + final Options options = Options.builder() + .inPlace(true) + .backend("mybackend") + .build(); + asciidoctor.convertFile(renderSampleDocument, options); + }); } private void assertRenderedLocalDateContent(String render_content, String contentDateOrTime) throws IOException, diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAnAsciidoctorJInstanceIsRequired.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAnAsciidoctorJInstanceIsRequired.java index bf54985a3..2bb1b850e 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAnAsciidoctorJInstanceIsRequired.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAnAsciidoctorJInstanceIsRequired.java @@ -1,23 +1,23 @@ package org.asciidoctor; import org.asciidoctor.jruby.AsciidoctorJRuby; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertThat; public class WhenAnAsciidoctorJInstanceIsRequired { - private static final String DOC = "[yell]\nHello World"; + private static final String DOC = "[yell]\nHello World"; - @Test - public void shouldUnwrapAsciidoctorInstanceAndRegisterRubyExtension() { - AsciidoctorJRuby asciidoctorj = Asciidoctor.Factory.create().unwrap(AsciidoctorJRuby.class); - asciidoctorj.rubyExtensionRegistry().loadClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")).block("yell", "YellRubyBlock"); + @Test + public void shouldUnwrapAsciidoctorInstanceAndRegisterRubyExtension() { + AsciidoctorJRuby asciidoctorj = Asciidoctor.Factory.create().unwrap(AsciidoctorJRuby.class); + asciidoctorj.rubyExtensionRegistry().loadClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")).block("yell", "YellRubyBlock"); - String html = asciidoctorj.convert(DOC, OptionsBuilder.options().standalone(false)); + String html = asciidoctorj.convert(DOC, OptionsBuilder.options().standalone(false)); - assertThat(html, containsString("HELLO WORLD")); - } + assertThat(html, containsString("HELLO WORLD")); + } } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciiDocIsLoadedToDocument.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciiDocIsLoadedToDocument.java index d2b56e015..8c8ddd994 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciiDocIsLoadedToDocument.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciiDocIsLoadedToDocument.java @@ -1,24 +1,24 @@ package org.asciidoctor; -import org.asciidoctor.arquillian.api.Unshared; import org.asciidoctor.ast.*; -import org.asciidoctor.jruby.internal.IOUtils; -import org.asciidoctor.util.ClasspathResources; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.assertj.core.api.Assertions; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; import java.util.HashMap; import java.util.List; import java.util.Map; import static java.util.Collections.emptyMap; import static java.util.stream.Collectors.toList; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -27,7 +27,7 @@ import static org.hamcrest.collection.IsMapContaining.hasKey; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenAsciiDocIsLoadedToDocument { private static final String DOCUMENT = "= Document Title\n" + @@ -66,11 +66,9 @@ public class WhenAsciiDocIsLoadedToDocument { "\n" + "content"; - @ArquillianResource(Unshared.class) + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; - @ArquillianResource - private ClasspathResources classpath = new ClasspathResources(); @Test public void should_return_empty_sources_when_document_is_null() { @@ -164,11 +162,12 @@ public void should_return_options_from_parsed_string_when_passed_as_options_obje } @Test - public void should_return_options_from_parsed_file_when_passed_as_options_object() { + public void should_return_options_from_parsed_file_when_passed_as_options_object( + @ClasspathResource("sourcelocation.adoc") File resource) { + Options options = Options.builder() .compact(true) .build(); - File resource = classpath.getResource("sourcelocation.adoc"); Document document = asciidoctor.loadFile(resource, options); Map documentOptions = document.getOptions(); @@ -289,15 +288,17 @@ public void should_be_able_to_normalize_web_path() { } @Test - public void should_be_able_to_read_asset() throws FileNotFoundException { + public void should_be_able_to_read_asset( + @ClasspathResource("rendersample.asciidoc") File inputFile) throws IOException { + Map options = OptionsBuilder.options().safe(SafeMode.SAFE) .attributes(AttributesBuilder.attributes().dataUri(false)) .compact(true) .asMap(); Document document = asciidoctor.load(DOCUMENT, options); - File inputFile = classpath.getResource("rendersample.asciidoc"); + String content = document.readAsset(inputFile.getAbsolutePath(), new HashMap<>()); - assertThat(content.replace("\r", ""), is(IOUtils.readFull(new FileReader(inputFile)).replace("\r", ""))); + assertThat(content.replace("\r", ""), is(Files.readString(inputFile.toPath()).replace("\r", ""))); } @Test @@ -321,9 +322,8 @@ public void should_be_able_to_set_attribute_on_attributes_map() { } @Test - public void should_be_able_to_get_source_location() { - // Given - File file = classpath.getResource("sourcelocation.adoc"); + public void should_be_able_to_get_source_location( + @ClasspathResource("sourcelocation.adoc") File file) { // When Document document = asciidoctor.loadFile(file, OptionsBuilder.options().sourcemap(true).docType("book").asMap()); diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciidoctorLogsToConsole.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciidoctorLogsToConsole.java index 4548078e4..73bea9f04 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciidoctorLogsToConsole.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciidoctorLogsToConsole.java @@ -9,15 +9,16 @@ import org.asciidoctor.log.LogRecord; import org.asciidoctor.log.Severity; import org.asciidoctor.log.TestLogHandlerService; -import org.asciidoctor.util.ClasspathResources; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.hamcrest.Matchers; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import java.io.File; import java.io.IOException; @@ -29,29 +30,34 @@ import java.util.stream.Collectors; import static org.asciidoctor.OptionsBuilder.options; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; import static org.hamcrest.Matchers.*; import static org.hamcrest.core.IsNull.nullValue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenAsciidoctorLogsToConsole { - @ArquillianResource - private ClasspathResources classpath = new ClasspathResources(); + @AsciidoctorInstance(scope = PER_METHOD) + private Asciidoctor asciidoctor; - @ArquillianResource - private TemporaryFolder testFolder; + @ClasspathResource("documentwithnotexistingfile.adoc") + private File documentWithNotExistingFile; - private Asciidoctor asciidoctor; + @ClasspathResource("documentwithinvalidrefs.adoc") + private File documentWithInvalidRefs; + + @TempDir + private File tempFolder; - @Before + + @BeforeEach public void before() { - asciidoctor = Asciidoctor.Factory.create(); TestLogHandlerService.clear(); } - @After + @AfterEach public void cleanup() throws IOException { LogManager.getLogManager().readConfiguration(); TestLogHandlerService.clear(); @@ -61,7 +67,7 @@ public void cleanup() throws IOException { public void shouldRedirectToJUL() { final MemoryLogHandler memoryLogHandler = registerMemoryLogHandler(); - File inputFile = classpath.getResource("documentwithnotexistingfile.adoc"); + File inputFile = documentWithNotExistingFile; String renderContent = asciidoctor.convertFile(inputFile, options() .inPlace(true) @@ -93,7 +99,7 @@ public void shouldNotifyLogHandler() { asciidoctor.registerLogHandler(logRecords::add); - File inputFile = classpath.getResource("documentwithnotexistingfile.adoc"); + File inputFile = documentWithNotExistingFile; String renderContent = asciidoctor.convertFile(inputFile, options() .inPlace(true) @@ -127,7 +133,7 @@ public void shouldLogInvalidRefs() { asciidoctor.registerLogHandler(logRecords::add); - File inputFile = classpath.getResource("documentwithinvalidrefs.adoc"); + File inputFile = documentWithInvalidRefs; String renderContent = asciidoctor.convertFile(inputFile, options() .inPlace(true) @@ -153,7 +159,7 @@ public void shouldOnlyNotifyFromRegisteredAsciidoctor() { asciidoctor.registerLogHandler(logRecords::add); // Now render via second instance and check that there is no notification - File inputFile = classpath.getResource("documentwithnotexistingfile.adoc"); + File inputFile = documentWithNotExistingFile; String renderContent1 = secondInstance.convertFile(inputFile, options() .inPlace(true) @@ -195,7 +201,7 @@ public void shouldNoLongerNotifyAfterUnregisterOnlyNotifyFromRegisteredAsciidoct final LogHandler logHandler = logRecords::add; asciidoctor.registerLogHandler(logHandler); - File inputFile = classpath.getResource("documentwithnotexistingfile.adoc"); + File inputFile = documentWithNotExistingFile; String renderContent = asciidoctor.convertFile(inputFile, options() .inPlace(true) @@ -229,7 +235,7 @@ public void shouldNoLongerNotifyAfterUnregisterOnlyNotifyFromRegisteredAsciidoct @Test public void shouldNotifyLogHandlerService() { - File inputFile = classpath.getResource("documentwithnotexistingfile.adoc"); + File inputFile = documentWithNotExistingFile; String renderContent = asciidoctor.convertFile(inputFile, options() .inPlace(true) @@ -312,6 +318,5 @@ public void should_fail_convert_when_logHandler_throws_an_exception() { assertThat(cause.getMessage(), is(errorMessage)); } } - } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAttributesAreUsedInAsciidoctor.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAttributesAreUsedInAsciidoctor.java index fd6bbfaa4..3bed3b072 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAttributesAreUsedInAsciidoctor.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAttributesAreUsedInAsciidoctor.java @@ -1,32 +1,27 @@ package org.asciidoctor; import com.google.common.io.CharStreams; -import org.asciidoctor.arquillian.api.Unshared; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.Files; import java.util.Map; import static org.asciidoctor.AttributesBuilder.attributes; @@ -37,94 +32,106 @@ import static org.junit.Assert.assertEquals; import static org.xmlmatchers.xpath.HasXPath.hasXPath; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenAttributesAreUsedInAsciidoctor { - @ArquillianResource - private ClasspathResources classpath = new ClasspathResources(); + @AsciidoctorInstance + private Asciidoctor asciidoctor; - @ArquillianResource - private TemporaryFolder testFolder; + @ClasspathResource("documentwithnote.asciidoc") + private File documentWithNote; + + @ClasspathResource("tocsample.asciidoc") + private File tocSample; + + @ClasspathResource("toc2sample.asciidoc") + private File toc2Sample; + + @ClasspathResource("rendersample.asciidoc") + private File renderSample; + + @TempDir + private File tempFolder; - @ArquillianResource(Unshared.class) - private Asciidoctor asciidoctor; @Test public void qualified_http_url_inline_with_hide_uri_scheme_set() { - + Attributes attributes = attributes().hiddenUriScheme(true).get(); - - String content = asciidoctor.convert("The AsciiDoc project is located at http://asciidoc.org.", OptionsBuilder.options().attributes(attributes)); + + String content = asciidoctor.convert("The AsciiDoc project is located at https://asciidoc.org.", OptionsBuilder.options().attributes(attributes)); Document doc = Jsoup.parse(content, "UTF-8"); Element link = doc.getElementsByTag("a").first(); assertThat(link.text(), is("asciidoc.org")); - + } - + @Test public void compat_mode_should_change_how_document_is_rendered_to_legacy_system() { - + Attributes attributes = attributes().attribute("version", "1.0.0").compatMode(CompatMode.LEGACY).get(); - + String content = asciidoctor.convert("The `AsciiDoc {version}` project.", OptionsBuilder.options().attributes(attributes)); Document doc = Jsoup.parse(content, "UTF-8"); Element code = doc.getElementsByTag("code").first(); assertThat(code.text(), containsString("{version}")); - + } @Test public void no_compat_mode_should_change_how_document_is_rendered_to_new_system() { - + Attributes attributes = attributes().attribute("version", "1.0.0").get(); - + String content = asciidoctor.convert("The `AsciiDoc {version}` project.", OptionsBuilder.options().attributes(attributes)); Document doc = Jsoup.parse(content, "UTF-8"); Element code = doc.getElementsByTag("code").first(); assertThat(code.text(), containsString("1.0.0")); - + } @Test public void should_preload_open_cache_uri_gem() { - + Attributes attributes = attributes().cacheUri(true).get(); - + String content = asciidoctor.convert("read my lips", OptionsBuilder.options().attributes(attributes)); assertThat(content, is(notNullValue())); - + } @Test - public void should_add_AsciiMath_delimiters_around_math_block_content_if_math_attribute_not_latexmath() throws IOException { - + public void should_add_AsciiMath_delimiters_around_math_block_content_if_math_attribute_not_latexmath( + @ClasspathResource("math.asciidoc") File sourceDocument) throws IOException { + Attributes attributes = attributes().math("asciimath").get(); - + Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()).attributes(attributes).get(); + .toDir(tempFolder).attributes(attributes).get(); + + asciidoctor.convertFile(sourceDocument, options); - asciidoctor.convertFile(classpath.getResource("math.asciidoc"), options); + Document doc = Jsoup.parse(new File(tempFolder, "math.html"), "UTF-8"); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), "math.html"), "UTF-8"); - assertThat(doc.getElementsByAttributeValue("type", "text/x-mathjax-config").size(), is(1)); } @Test - public void should_use_custom_appendix_caption_if_specified() throws IOException { - + public void should_use_custom_appendix_caption_if_specified( + @ClasspathResource("appendix.asciidoc") File appendixDocument) throws IOException { + Attributes attributes = attributes().appendixCaption("App").get(); - + Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()).attributes(attributes).get(); + .toDir(tempFolder).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("appendix.asciidoc"), options); + asciidoctor.convertFile(appendixDocument, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), + Document doc = Jsoup.parse(new File(tempFolder, "appendix.html"), "UTF-8"); Element attributeElement = doc.getElementById("_attribute_options"); @@ -133,47 +140,51 @@ public void should_use_custom_appendix_caption_if_specified() throws IOException @Test public void should_add_a_hardbreak_at_end_of_each_line_when_hardbreaks_option_is_set() { - + Attributes attributes = attributes().hardbreaks(true).get(); - + String content = asciidoctor.convert("read\nmy\nlips", OptionsBuilder.options().attributes(attributes)); Document doc = Jsoup.parse(content, "UTF-8"); - Element paragraph = doc.getElementsByAttributeValue("class","paragraph").first(); + Element paragraph = doc.getElementsByAttributeValue("class", "paragraph").first(); assertThat(paragraph.getElementsByTag("br").size(), is(2)); } - + @Test - public void sect_num_levels_attribute_should_only_number_levels_up_to_value_defined_by_sectnumlevels_attribute() throws IOException { - + public void sect_num_levels_attribute_should_only_number_levels_up_to_value_defined_by_sectnumlevels_attribute( + @ClasspathResource("multiple_levels.asciidoc") File multiLevelDocument) throws IOException { + Attributes attributes = attributes().sectionNumbers(true).sectNumLevels(2).get(); - - Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("multiple_levels.asciidoc"), options); + Options options = options() + .inPlace(false) + .safe(SafeMode.UNSAFE) + .toDir(tempFolder).attributes(attributes) + .get(); + + asciidoctor.convertFile(multiLevelDocument, options); + + Document doc = Jsoup.parse(new File(tempFolder, "multiple_levels.html"), "UTF-8"); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), "multiple_levels.html"), "UTF-8"); - assertThat(doc.getElementById("_level_1").text(), startsWith("1.")); assertThat(doc.getElementById("_level_2").text(), startsWith("1.1.")); assertThat(doc.getElementById("_level_3").text(), not(startsWith("1.1.1."))); assertThat(doc.getElementById("_level_4").text(), not(startsWith("1.1.1.1."))); } - + @Test public void no_footer_attribute_should_not_show_footer_info() throws IOException { - + Attributes attributes = attributes().noFooter(true).get(); - + Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()).attributes(attributes).get(); + .toDir(tempFolder).attributes(attributes).get(); + + asciidoctor.convertFile(renderSample, options); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + Document doc = Jsoup.parse(new File(tempFolder, "rendersample.html"), "UTF-8"); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), "rendersample.html"), "UTF-8"); - assertThat(doc.getElementById("footer"), is(nullValue())); } @@ -182,12 +193,12 @@ public void no_footer_attribute_should_not_show_footer_info() throws IOException @Test public void show_title_true_attribute_should_show_title_on_embedded_document() { final Options options = options() - .attributes(attributes().showTitle(true).get()) - .toFile(false) - .standalone(false) - .get(); + .attributes(attributes().showTitle(true).get()) + .toFile(false) + .standalone(false) + .get(); - final Document doc = Jsoup.parse(asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options)); + final Document doc = Jsoup.parse(asciidoctor.convertFile(renderSample, options)); assertEquals(1, doc.getElementsByTag("h1").size()); assertEquals("Document Title", doc.getElementsByTag("h1").get(0).text()); @@ -196,12 +207,12 @@ public void show_title_true_attribute_should_show_title_on_embedded_document() { @Test public void show_title_false_then_true_attribute_should_show_title_on_embedded_document() { final Options options = options() - .attributes(attributes().showTitle(false).showTitle(true).get()) - .toFile(false) - .standalone(false) - .get(); + .attributes(attributes().showTitle(false).showTitle(true).get()) + .toFile(false) + .standalone(false) + .get(); - final Document doc = Jsoup.parse(asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options)); + final Document doc = Jsoup.parse(asciidoctor.convertFile(renderSample, options)); assertEquals(1, doc.getElementsByTag("h1").size()); assertEquals("Document Title", doc.getElementsByTag("h1").get(0).text()); @@ -210,12 +221,12 @@ public void show_title_false_then_true_attribute_should_show_title_on_embedded_d @Test public void show_title_false_attribute_should_hide_title_on_embedded_document() { final Options options = options() - .attributes(attributes().showTitle(false).get()) - .toFile(false) - .standalone(false) - .get(); + .attributes(attributes().showTitle(false).get()) + .toFile(false) + .standalone(false) + .get(); - final Document doc = Jsoup.parse(asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options)); + final Document doc = Jsoup.parse(asciidoctor.convertFile(renderSample, options)); assertEquals(0, doc.getElementsByTag("h1").size()); } @@ -223,25 +234,26 @@ public void show_title_false_attribute_should_hide_title_on_embedded_document() @Test public void show_title_true_then_false_attribute_should_hide_title_on_embedded_document() { final Options options = options() - .attributes(attributes().showTitle(true).showTitle(false).get()) - .toFile(false) - .standalone(false) - .get(); + .attributes(attributes().showTitle(true).showTitle(false).get()) + .toFile(false) + .standalone(false) + .get(); - final Document doc = Jsoup.parse(asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options)); + final Document doc = Jsoup.parse(asciidoctor.convertFile(renderSample, options)); assertEquals(0, doc.getElementsByTag("h1").size()); } + @Test public void show_title_true_attribute_should_show_title_on_standalone_document() { final Options options = options() - .attributes(attributes().showTitle(true).get()) - .toFile(false) - .standalone(true) - .get(); + .attributes(attributes().showTitle(true).get()) + .toFile(false) + .standalone(true) + .get(); - final Document doc = Jsoup.parse(asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options)); + final Document doc = Jsoup.parse(asciidoctor.convertFile(renderSample, options)); assertEquals(1, doc.getElementsByTag("h1").size()); assertEquals("Document Title", doc.getElementsByTag("h1").get(0).text()); @@ -251,12 +263,12 @@ public void show_title_true_attribute_should_show_title_on_standalone_document() public void show_title_false_attribute_should_hide_title_on_standalone_document() { final Options options = options() - .attributes(attributes().showTitle(false).get()) - .toFile(false) - .standalone(true) - .get(); + .attributes(attributes().showTitle(false).get()) + .toFile(false) + .standalone(true) + .get(); - final Document doc = Jsoup.parse(asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options)); + final Document doc = Jsoup.parse(asciidoctor.convertFile(renderSample, options)); assertEquals(0, doc.getElementsByTag("h1").size()); } @@ -265,12 +277,12 @@ public void show_title_false_attribute_should_hide_title_on_standalone_document( public void show_title_true_then_false_attribute_should_hide_title_on_standalone_document() { final Options options = options() - .attributes(attributes().showTitle(false).get()) - .toFile(false) - .standalone(true) - .get(); + .attributes(attributes().showTitle(false).get()) + .toFile(false) + .standalone(true) + .get(); - final Document doc = Jsoup.parse(asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options)); + final Document doc = Jsoup.parse(asciidoctor.convertFile(renderSample, options)); assertEquals(0, doc.getElementsByTag("h1").size()); } @@ -278,46 +290,48 @@ public void show_title_true_then_false_attribute_should_hide_title_on_standalone @Test public void source_highlighter_attribute_should_add_required_javascript_libraries_as_highlighter() throws IOException { - + Attributes attributes = attributes().sourceHighlighter("prettify").get(); - + Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()).attributes(attributes).get(); + .toDir(tempFolder).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "rendersample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "rendersample.html"), "UTF-8"); Elements link = doc.select("link"); assertThat(link.get(1).attr("href"), containsString("prettify.min.css")); - + } - + @Test public void render_content_without_attributes_should_embed_css_by_default() throws IOException { - - Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()).get(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + Options options = options() + .inPlace(false) + .safe(SafeMode.UNSAFE) + .toDir(tempFolder) + .get(); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "rendersample.html"), "UTF-8"); + asciidoctor.convertFile(renderSample, options); + + Document doc = Jsoup.parse(new File(tempFolder, "rendersample.html"), "UTF-8"); Elements cssStyle = doc.select("style"); assertThat(cssStyle.html(), is(not(""))); Elements link = doc.select("link"); assertThat(link.html(), is("".trim())); - + } - + @Test - public void should_skip_front_matter_if_specified_by_skip_front_matter_attribute() { + public void should_skip_front_matter_if_specified_by_skip_front_matter_attribute( + @ClasspathResource("renderwithfrontmatter.adoc") File renderWithFrontMatter) { Attributes attributes = attributes().skipFrontMatter(true).get(); Options options = options().toFile(false).inPlace(false).attributes(attributes).get(); - String content = asciidoctor.convertFile(classpath.getResource("renderwithfrontmatter.adoc"), options); + String content = asciidoctor.convertFile(renderWithFrontMatter, options); Document doc = Jsoup.parse(content, "UTF-8"); Elements hrElements = doc.getElementsByTag("hr"); @@ -331,7 +345,7 @@ public void set_anchors_attribute_should_add_anchor_to_sections() { Attributes attributes = attributes().setAnchors(true).get(); Options options = options().inPlace(false).toFile(false).attributes(attributes).get(); - String content = asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + String content = asciidoctor.convertFile(renderSample, options); Document doc = Jsoup.parse(content, "UTF-8"); Element anchorElement = doc.select("a[class=anchor]").first(); @@ -341,13 +355,14 @@ public void set_anchors_attribute_should_add_anchor_to_sections() { } @Test - public void ignore_undefined_attributes_should_keep_lines_with_undefined_attributes() { + public void ignore_undefined_attributes_should_keep_lines_with_undefined_attributes( + @ClasspathResource("documentwithundefinedattribute.asciidoc") File sourceDocument) { Attributes attributes = attributes().ignoreUndefinedAttributes(true).get(); Options options = options().toFile(false).attributes(attributes).get(); String renderContent = asciidoctor.convertFile( - classpath.getResource("documentwithundefinedattribute.asciidoc"), + sourceDocument, options); assertThat(renderContent, containsString("{bogus-attribute}")); } @@ -356,12 +371,16 @@ public void ignore_undefined_attributes_should_keep_lines_with_undefined_attribu public void setting_toc_attribute_table_of_contents_should_be_generated() throws IOException { Attributes attributes = Attributes.builder().tableOfContents(true).build(); - Options options = Options.builder().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).build(); + Options options = Options.builder() + .inPlace(false) + .toDir(tempFolder) + .safe(SafeMode.UNSAFE) + .attributes(attributes) + .build(); - asciidoctor.convertFile(classpath.getResource("tocsample.asciidoc"), options); + asciidoctor.convertFile(tocSample, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "tocsample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "tocsample.html"), "UTF-8"); Elements tocElement = doc.select("div.toc"); assertThat(tocElement.hasClass("toc"), is(true)); @@ -370,12 +389,11 @@ public void setting_toc_attribute_table_of_contents_should_be_generated() throws @Test public void setting_toc_attribute_left_should_work() throws IOException { Attributes attributes = Attributes.builder().tableOfContents(Placement.LEFT).build(); - Options options = Options.builder().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).build(); + Options options = Options.builder().inPlace(false).toDir(tempFolder).safe(SafeMode.UNSAFE).attributes(attributes).build(); - asciidoctor.convertFile(classpath.getResource("tocsample.asciidoc"), options); + asciidoctor.convertFile(tocSample, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "tocsample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "tocsample.html"), "UTF-8"); Elements tocElement = doc.select("body.toc-left > div#header > div#toc"); assertThat(tocElement.size(), is(1)); } @@ -383,12 +401,16 @@ public void setting_toc_attribute_left_should_work() throws IOException { @Test public void setting_toc_attribute_right_should_work() throws IOException { Attributes attributes = Attributes.builder().tableOfContents(Placement.RIGHT).build(); - Options options = Options.builder().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).build(); + Options options = Options.builder() + .inPlace(false) + .toDir(tempFolder) + .safe(SafeMode.UNSAFE) + .attributes(attributes) + .build(); - asciidoctor.convertFile(classpath.getResource("tocsample.asciidoc"), options); + asciidoctor.convertFile(tocSample, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "tocsample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "tocsample.html"), "UTF-8"); Elements tocElement = doc.select("body.toc-right > div#header > div#toc"); assertThat(tocElement.size(), is(1)); } @@ -396,25 +418,35 @@ public void setting_toc_attribute_right_should_work() throws IOException { @Test public void setting_toc_attribute_preamble_should_work() throws IOException { Attributes attributes = Attributes.builder().tableOfContents(Placement.PREAMBLE).build(); - Options options = Options.builder().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).build(); + Options options = Options.builder() + .inPlace(false) + .toDir(tempFolder) + .safe(SafeMode.UNSAFE) + .attributes(attributes) + .build(); - asciidoctor.convertFile(classpath.getResource("tocsample.asciidoc"), options); + asciidoctor.convertFile(tocSample, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "tocsample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "tocsample.html"), "UTF-8"); Elements tocElement = doc.select("body.article > div#content > div#preamble > div#toc"); assertThat(tocElement.size(), is(1)); } @Test - public void setting_toc_attribute_macro_should_work() throws IOException { + public void setting_toc_attribute_macro_should_work( + @ClasspathResource("tocsamplemacro.asciidoc") File tocSampleMacro) throws IOException { + Attributes attributes = Attributes.builder().tableOfContents(Placement.MACRO).build(); - Options options = Options.builder().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).build(); + Options options = Options.builder() + .inPlace(false) + .toDir(tempFolder) + .safe(SafeMode.UNSAFE) + .attributes(attributes) + .build(); - asciidoctor.convertFile(classpath.getResource("tocsamplemacro.asciidoc"), options); + asciidoctor.convertFile(tocSampleMacro, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "tocsamplemacro.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "tocsamplemacro.html"), "UTF-8"); Elements tocElement = doc.select("body.article > div#content > div.sect1 > div.sectionbody > div#toc"); assertThat(tocElement.size(), is(1)); } @@ -434,28 +466,28 @@ public void attribute_missing_should_drop_line_should_drop_line_with_reference_t @Test public void attribute_undefined_should_not_drop_line_with_attribute_unassignment_if_attribute_undefined_is_drop() { - + Attributes attributes = attributes().attributeUndefined("drop").get(); Options options = options().attributes(attributes).get(); - + String renderContent = asciidoctor.convert(":foo:\n\n{set:foo!}\n{foo}yes", options); Document doc = Jsoup.parse(renderContent, "UTF-8"); Element paragraph = doc.getElementsByTag("p").first(); assertThat(paragraph.text(), is("{foo}yes")); } - + @Test public void table_of_content_should_be_placeable() throws IOException { Attributes attributes = attributes().tableOfContents(Placement.RIGHT).get(); Options options = options().inPlace(false) - .toFile(new File(testFolder.getRoot(), "toc2sample.html")) + .toFile(new File(tempFolder, "toc2sample.html")) .safe(SafeMode.UNSAFE).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("toc2sample.asciidoc"), options); + asciidoctor.convertFile(toc2Sample, options); - File renderedFile = new File(testFolder.getRoot(), "toc2sample.html"); + File renderedFile = new File(tempFolder, "toc2sample.html"); Document doc = Jsoup.parse(renderedFile, "UTF-8"); Elements body = doc.select("body"); String classAttribute = body.attr("class"); @@ -471,12 +503,12 @@ public void table_of_content_2_should_be_placeable() throws IOException { Attributes attributes = attributes().tableOfContents2(Placement.RIGHT).get(); Options options = options().inPlace(false) - .toFile(new File(testFolder.getRoot(), "toc2sample.html")) + .toFile(new File(tempFolder, "toc2sample.html")) .safe(SafeMode.UNSAFE).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("toc2sample.asciidoc"), options); + asciidoctor.convertFile(toc2Sample, options); - File renderedFile = new File(testFolder.getRoot(), "toc2sample.html"); + File renderedFile = new File(tempFolder, "toc2sample.html"); Document doc = Jsoup.parse(renderedFile, "UTF-8"); Elements body = doc.select("body"); String classAttribute = body.attr("class"); @@ -490,17 +522,16 @@ public void table_of_content_2_should_be_placeable() throws IOException { @Test public void setting_linkcss_as_false_in_string_should_embed_css_file() throws IOException { - Attributes attributes = attributes("linkcss!").get(); + Attributes attributes = attributes("linkcss!").get(); Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()).attributes(attributes).get(); + .toDir(tempFolder).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); // String readFull = IOUtils.readFull(new FileInputStream(new // File(testFolder.getRoot(), "rendersample.html"))); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "rendersample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "rendersample.html"), "UTF-8"); Elements cssStyle = doc.select("style"); assertThat(cssStyle.html(), is(not(""))); @@ -508,17 +539,16 @@ public void setting_linkcss_as_false_in_string_should_embed_css_file() throws IO assertThat(link.html(), is("".trim())); } - + @Test public void setting_toc_attribute_and_numbered_in_string_form_table_of_contents_should_be_generated() throws IOException { Attributes attributes = attributes("toc sectnums").get(); - Options options = options().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).get(); + Options options = options().inPlace(false).toDir(tempFolder).safe(SafeMode.UNSAFE).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("tocsample.asciidoc"), options); + asciidoctor.convertFile(tocSample, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "tocsample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "tocsample.html"), "UTF-8"); Elements tocElement = doc.select("div.toc"); assertThat(tocElement.hasClass("toc"), is(true)); @@ -529,13 +559,12 @@ public void setting_toc_attribute_and_numbered_in_string_form_table_of_contents_ @Test public void setting_toc_attribute_and_numbered_in_array_form_table_of_contents_should_be_generated() throws IOException { - Attributes attributes = attributes(new String[] { "toc", "sectnums" }).get(); - Options options = options().inPlace(false).toDir(testFolder.getRoot()).safe(SafeMode.UNSAFE).attributes(attributes).get(); + Attributes attributes = attributes(new String[]{"toc", "sectnums"}).get(); + Options options = options().inPlace(false).toDir(tempFolder).safe(SafeMode.UNSAFE).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("tocsample.asciidoc"), options); + asciidoctor.convertFile(tocSample, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "tocsample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "tocsample.html"), "UTF-8"); Elements tocElement = doc.select("div.toc"); assertThat(tocElement.hasClass("toc"), is(true)); @@ -549,8 +578,7 @@ public void unsetting_toc_attribute_table_of_contents_should_not_be_generated() Attributes attributes = attributes().tableOfContents(false).get(); Options options = options().toFile(false).attributes(attributes).get(); - String renderContent = asciidoctor.convertFile( - classpath.getResource("tocsample.asciidoc"), options); + String renderContent = asciidoctor.convertFile(tocSample, options); Document doc = Jsoup.parse(renderContent, "UTF-8"); Elements tocElement = doc.select("div.toc"); @@ -565,13 +593,11 @@ public void styleSheetName_is_set_custom_stylesheet_should_be_used_() Attributes attributes = attributes().linkCss(true) .styleSheetName("mycustom.css").get(); Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()).attributes(attributes).get(); + .toDir(tempFolder).attributes(attributes).get(); - asciidoctor.convertFile( - classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "rendersample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "rendersample.html"), "UTF-8"); Elements link = doc.select("link[href]"); String attr = link.attr("href"); assertThat(attr, is("./mycustom.css")); @@ -584,12 +610,11 @@ public void unsetting_styleSheetName_should_leave_document_without_style() Attributes attributes = attributes().unsetStyleSheet().get(); Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()).attributes(attributes).get(); + .toDir(tempFolder).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "rendersample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "rendersample.html"), "UTF-8"); Elements link = doc.select("link[href]"); String attr = link.attr("href"); assertThat(attr, is("")); @@ -602,14 +627,13 @@ public void styles_dir_is_set_css_routes_should_use_it() throws IOException { Attributes attributes = attributes().stylesDir("./styles") .linkCss(true).styleSheetName("mycustom.css").get(); Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()) + .toDir(tempFolder) .mkDirs(true) .attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "rendersample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "rendersample.html"), "UTF-8"); Elements link = doc.select("link[href]"); String attr = link.attr("href"); assertThat(attr, is("./styles/mycustom.css")); @@ -621,15 +645,14 @@ public void unsetting_linkcss_should_embed_css_file() throws IOException { Attributes attributes = attributes().linkCss(false).get(); Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()).attributes(attributes).get(); + .toDir(tempFolder).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); // String readFull = IOUtils.readFull(new FileInputStream(new // File(testFolder.getRoot(), "rendersample.html"))); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "rendersample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "rendersample.html"), "UTF-8"); Elements cssStyle = doc.select("style"); assertThat(cssStyle.html(), is(not(""))); @@ -643,12 +666,11 @@ public void linkcss_should_not_embed_css_file() throws IOException { Attributes attributes = attributes().linkCss(true).get(); Options options = options().inPlace(false).safe(SafeMode.UNSAFE) - .toDir(testFolder.getRoot()).attributes(attributes).get(); + .toDir(tempFolder).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); - Document doc = Jsoup.parse(new File(testFolder.getRoot(), - "rendersample.html"), "UTF-8"); + Document doc = Jsoup.parse(new File(tempFolder, "rendersample.html"), "UTF-8"); Elements link = doc.select("link[href]"); String attr = link.get(1).attr("href"); assertThat(attr, is("./asciidoctor.css")); @@ -661,10 +683,9 @@ public void copycss_with_in_place_should_copy_css_to_rendered_directory() { Options options = options().inPlace(true).safe(SafeMode.UNSAFE) .attributes(attributes).get(); - File inputFile = classpath.getResource("rendersample.asciidoc"); - asciidoctor.convertFile(inputFile, options); + asciidoctor.convertFile(renderSample, options); - File cssFile = new File(inputFile.getParent(),"asciidoctor.css"); + File cssFile = new File(renderSample.getParent(), "asciidoctor.css"); assertThat(cssFile.exists(), is(true)); cssFile.delete(); @@ -676,11 +697,10 @@ public void copycss_negated_with_in_place_should_not_copy_css_to_rendered_direct Options options = options().inPlace(true).safe(SafeMode.UNSAFE) .attributes(attributes).get(); - File inputFile = classpath.getResource("rendersample.asciidoc"); - asciidoctor.convertFile(inputFile, options); + asciidoctor.convertFile(renderSample, options); try { - File cssFile = new File(inputFile.getParent(), "asciidoctor.css"); + File cssFile = new File(renderSample.getParent(), "asciidoctor.css"); assertThat(cssFile.exists(), is(false)); } catch (RuntimeException e) { assertThat(e.getCause(), is(instanceOf(FileNotFoundException.class))); @@ -695,11 +715,10 @@ public void copycss_and_linkcss_negated_should_not_copy_css_to_rendered_file() { Options options = options().inPlace(true).safe(SafeMode.UNSAFE) .attributes(attributes).get(); - File inputFile = classpath.getResource("rendersample.asciidoc"); - asciidoctor.convertFile(inputFile, options); + asciidoctor.convertFile(renderSample, options); try { - File cssFile = new File(inputFile.getParent(), "asciidoctor.css"); + File cssFile = new File(renderSample.getParent(), "asciidoctor.css"); assertThat(cssFile.exists(), is(false)); } catch (RuntimeException e) { assertThat(e.getCause(), is(instanceOf(FileNotFoundException.class))); @@ -712,12 +731,12 @@ public void copycss_with_to_file_should_copy_css_to_to_file_directory() { Attributes attributes = attributes().linkCss(true).copyCss(true).get(); Options options = options().inPlace(false) - .toFile(new File(testFolder.getRoot(), "output.html")) + .toFile(new File(tempFolder, "output.html")) .safe(SafeMode.UNSAFE).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); - File cssFile = new File(testFolder.getRoot(), "asciidoctor.css"); + File cssFile = new File(tempFolder, "asciidoctor.css"); assertThat(cssFile.exists(), is(true)); } @@ -726,12 +745,12 @@ public void copycss_with_to_file_should_copy_css_to_to_file_directory() { public void copycss_with_to_dir_should_copy_css_to_to_dir_directory() { Attributes attributes = attributes().linkCss(true).copyCss(true).get(); - Options options = options().inPlace(false).toDir(testFolder.getRoot()) + Options options = options().inPlace(false).toDir(tempFolder) .safe(SafeMode.UNSAFE).attributes(attributes).get(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); - File cssFile = new File(testFolder.getRoot(), "asciidoctor.css"); + File cssFile = new File(tempFolder, "asciidoctor.css"); assertThat(cssFile.exists(), is(true)); } @@ -741,12 +760,12 @@ public void copycss_with_render_to_file_should_copy_css_to_to_file_directory() { Attributes attributes = attributes().linkCss(true).copyCss(true).get(); Options options = options().inPlace(false) - .toFile(new File(testFolder.getRoot(), "output.html")) + .toFile(new File(tempFolder, "output.html")) .safe(SafeMode.UNSAFE).attributes(attributes).get(); asciidoctor.convert("This is Asciidoctor", options); - File cssFile = new File(testFolder.getRoot(), "asciidoctor.css"); + File cssFile = new File(tempFolder, "asciidoctor.css"); assertThat(cssFile.exists(), is(true)); } @@ -755,12 +774,12 @@ public void copycss_with_render_to_file_should_copy_css_to_to_file_directory() { public void copycss_with_render_to_dir_should_copy_css_to_to_dir_directory() { Attributes attributes = attributes().linkCss(true).copyCss(true).get(); - Options options = options().inPlace(false).toDir(testFolder.getRoot()) + Options options = options().inPlace(false).toDir(tempFolder) .safe(SafeMode.UNSAFE).attributes(attributes).get(); asciidoctor.convert("This is Asciidoctor", options); - File cssFile = new File(testFolder.getRoot(), "asciidoctor.css"); + File cssFile = new File(tempFolder, "asciidoctor.css"); assertThat(cssFile.exists(), is(true)); } @@ -769,14 +788,11 @@ public void copycss_with_render_to_dir_should_copy_css_to_to_dir_directory() { public void string_content_with_icons_enabled_should_be_rendered() throws IOException, SAXException, ParserConfigurationException { - InputStream content = new FileInputStream( - classpath.getResource("documentwithnote.asciidoc")); - Map attributes = attributes().icons( Attributes.IMAGE_ICONS).asMap(); Map options = options().attributes(attributes).asMap(); - String result = asciidoctor.convert(toString(content), options); + String result = asciidoctor.convert(Files.readString(documentWithNote.toPath()), options); result = result.replaceAll("", ""); assertRenderedAdmonitionIcon(result); @@ -786,8 +802,7 @@ public void string_content_with_icons_enabled_should_be_rendered() public void string_content_with_fontawesome_icons_enabled_should_be_rendered() throws IOException, SAXException, ParserConfigurationException { - InputStream content = new FileInputStream( - classpath.getResource("documentwithnote.asciidoc")); + InputStream content = new FileInputStream(documentWithNote); Map attributes = attributes().icons( Attributes.FONT_ICONS).asMap(); @@ -802,8 +817,7 @@ public void string_content_with_fontawesome_icons_enabled_should_be_rendered() public void string_content_with_icons_enabled_and_iconsdir_set_should_be_rendered_with_iconsdir() throws IOException { - InputStream content = new FileInputStream( - classpath.getResource("documentwithnote.asciidoc")); + InputStream content = new FileInputStream(documentWithNote); Map attributes = attributes() .icons(Attributes.IMAGE_ICONS).iconsDir("icons").asMap(); @@ -857,10 +871,9 @@ public void iconfont_attributes_should_be_used_for_using_custom_font_css_icons() .iconFontRemote(true).iconFontCdn(new URI("http://mycdn/css/font-awesome.min.css")).get(); Options options = options().inPlace(true).attributes(attributes).get(); - File inputFile = classpath.getResource("documentwithnote.asciidoc"); - asciidoctor.convertFile(inputFile, options); + asciidoctor.convertFile(documentWithNote, options); - File expectedFile = new File(inputFile.getParent(), "documentwithnote.html"); + File expectedFile = new File(documentWithNote.getParent(), "documentwithnote.html"); Document doc = Jsoup.parse(expectedFile, "UTF-8"); Elements stylesheetLinks = doc diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenConvertingWithExpectedType.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenConvertingWithExpectedType.java index efa832147..778bd2323 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenConvertingWithExpectedType.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenConvertingWithExpectedType.java @@ -1,19 +1,20 @@ package org.asciidoctor; -import org.asciidoctor.arquillian.api.Unshared; import org.asciidoctor.ast.Document; import org.asciidoctor.converter.ObjectConverter; import org.asciidoctor.converter.ObjectConverter.ObjectConverterResult; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import java.io.ByteArrayOutputStream; -import java.io.IOException; +import java.io.File; import java.io.OutputStream; +import java.nio.file.Path; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; import static org.assertj.core.api.Assertions.assertThat; /** @@ -21,16 +22,16 @@ * - when using `toFile`, `toStream` returns Document, otherwise String. * - Java converter returns custom type unless `toFile` ot `toStream` is set. */ -@RunWith(Arquillian.class) +@ExtendWith(AsciidoctorExtension.class) public class WhenConvertingWithExpectedType { private static final String SAMPLE_CONTENT = "= Document Title\n\nThis is the preamble\n"; - @ArquillianResource(Unshared.class) + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; - @ArquillianResource - private TemporaryFolder testFolder; + @TempDir + private Path tempFolder; @Test public void shouldReturnNullWhenExpectedTypeIsStringButSetDocument() { @@ -43,10 +44,10 @@ public void shouldReturnNullWhenExpectedTypeIsStringButSetDocument() { } @Test - public void shouldReturnNullWhenExpectedTypeIsDocumentButSetString() throws IOException { + public void shouldReturnNullWhenExpectedTypeIsDocumentButSetString() { Options options = Options.builder() .safe(SafeMode.UNSAFE) - .toFile(testFolder.newFile()) + .toFile(outputFile()) .build(); String inRealityIsDocument = asciidoctor.convert(SAMPLE_CONTENT, options, String.class); @@ -80,10 +81,10 @@ public void shouldReturnStringWhenUsingToFile() { } @Test - public void shouldReturnDocumentWhenUsingToFile() throws IOException { + public void shouldReturnDocumentWhenUsingToFile() { Options options = Options.builder() .safe(SafeMode.UNSAFE) - .toFile(testFolder.newFile()) + .toFile(outputFile()) .build(); Document document = asciidoctor.convert(SAMPLE_CONTENT, options, Document.class); @@ -122,13 +123,12 @@ public void shouldReturnCustomTypeWhenUsingJavaConverter() { .hasFieldOrPropertyWithValue("value", ObjectConverter.FIXED_RESULT); } - @Test - public void shouldReturnNullWhenExpectingCustomTypeAndUsingToFile() throws IOException { + public void shouldReturnNullWhenExpectingCustomTypeAndUsingToFile() { Options options = Options.builder() .backend(ObjectConverter.BACKEND) .safe(SafeMode.UNSAFE) - .toFile(testFolder.newFile()) + .toFile(outputFile()) .build(); asciidoctor.javaConverterRegistry().register(ObjectConverter.class); @@ -149,4 +149,8 @@ public void shouldReturnNullWhenExpectingCustomTypeAndUsingToStream() { assertThat(document).isNull(); } + + private File outputFile() { + return tempFolder.resolve("output").toFile(); + } } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenCustomTemplatesAreUsed.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenCustomTemplatesAreUsed.java index f35467fb6..16f4774f9 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenCustomTemplatesAreUsed.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenCustomTemplatesAreUsed.java @@ -1,37 +1,42 @@ package org.asciidoctor; -import static org.asciidoctor.OptionsBuilder.options; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; - -import org.asciidoctor.arquillian.api.Unshared; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; -@RunWith(Arquillian.class) -public class WhenCustomTemplatesAreUsed { +import java.io.File; - @ArquillianResource - private ClasspathResources classpath; +import static org.asciidoctor.OptionsBuilder.options; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) +public class WhenCustomTemplatesAreUsed { - @ArquillianResource(Unshared.class) + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; - + + @ClasspathResource("src/custom-backends/haml/html5-tweaks") + private File html5TweaksDir; + + @ClasspathResource("rendersample.asciidoc") + private File renderSample; + @Test public void document_should_be_rendered_using_given_template_dir() { - - Options options = options().templateDir(classpath.getResource("src/custom-backends/haml/html5-tweaks")).toFile(false).get(); - String renderContent = asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); - + + Options options = options().templateDir(html5TweaksDir).toFile(false).get(); + String renderContent = asciidoctor.convertFile(renderSample, options); + Document doc = Jsoup.parse(renderContent, "UTF-8"); Element paragraph = doc.select("div.content").first(); - assertThat(paragraph, notNullValue()); + assertThat(paragraph).isNotNull(); } - + } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenDirectoriesWithAsciidocFilesAreScanned.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenDirectoriesWithAsciidocFilesAreScanned.java index 0c141ed4f..1c3a261d8 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenDirectoriesWithAsciidocFilesAreScanned.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenDirectoriesWithAsciidocFilesAreScanned.java @@ -1,75 +1,66 @@ package org.asciidoctor; +import org.asciidoctor.jruby.AsciiDocDirectoryWalker; +import org.asciidoctor.jruby.DirectoryWalker; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; +import org.asciidoctor.test.extension.ClasspathHelper; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.junit.Assert.assertThat; -import java.io.File; -import java.util.ArrayList; -import java.util.List; +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) +public class WhenDirectoriesWithAsciidocFilesAreScanned { -import org.asciidoctor.arquillian.api.Shared; -import org.asciidoctor.jruby.AsciiDocDirectoryWalker; -import org.asciidoctor.jruby.DirectoryWalker; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; + @Test + public void only_asciidoc_files_should_be_returned(@ClasspathResource("src/documents") File documentsFolder) { -@RunWith(Arquillian.class) -public class WhenDirectoriesWithAsciidocFilesAreScanned { + DirectoryWalker abstractDirectoryWalker = new AsciiDocDirectoryWalker(documentsFolder.getPath()); + List asciidocFiles = abstractDirectoryWalker.scan(); + + // Converto to absolute paths, otherwise Hamcrest's matchers fails + List asciidocFilesPaths = new ArrayList<>(); + for (File f : asciidocFiles) { + asciidocFilesPaths.add(f.getAbsolutePath()); + } + + final ClasspathHelper classpath = new ClasspathHelper(this.getClass()); + assertThat(asciidocFilesPaths, hasSize(4)); + assertThat( + asciidocFilesPaths, + containsInAnyOrder( + classpath.getResource("src/documents/sample.ad").getAbsolutePath(), + classpath.getResource("src/documents/sample.adoc").getAbsolutePath(), + classpath.getResource("src/documents/sample.asciidoc").getAbsolutePath(), + classpath.getResource("src/documents/sample.asc").getAbsolutePath())); + + } - @ArquillianResource - private ClasspathResources classpath; + @Test + public void empty_directory_should_return_no_documents(@TempDir File temporaryFolder) { + DirectoryWalker abstractDirectoryWalker = new AsciiDocDirectoryWalker(temporaryFolder.getAbsolutePath()); + List asciidocFiles = abstractDirectoryWalker.scan(); - @ArquillianResource(Shared.class) - public TemporaryFolder temporaryFolder; - - @Test - public void only_asciidoc_files_should_be_returned() { - - DirectoryWalker abstractDirectoryWalker = new AsciiDocDirectoryWalker(classpath.getResource("src/documents").getPath()); - List asciidocFiles = abstractDirectoryWalker.scan(); + assertThat(asciidocFiles, is(empty())); + } - // Converto to absolute paths, otherwise Hamcrest's matchers fails - List asciidocFilesPaths = new ArrayList(); - for (File f: asciidocFiles) { - asciidocFilesPaths.add(f.getAbsolutePath()); - } - - assertThat(asciidocFilesPaths, hasSize(4)); - assertThat( - asciidocFilesPaths, - containsInAnyOrder( - classpath.getResource("src/documents/sample.ad").getAbsolutePath(), - classpath.getResource("src/documents/sample.adoc").getAbsolutePath(), - classpath.getResource("src/documents/sample.asciidoc").getAbsolutePath(), - classpath.getResource("src/documents/sample.asc").getAbsolutePath())); + @Test + public void none_existing_directories_should_return_no_documents() { + DirectoryWalker abstractDirectoryWalker = new AsciiDocDirectoryWalker("my_udirectory"); + List asciidocFiles = abstractDirectoryWalker.scan(); - } + assertThat(asciidocFiles, is(empty())); + } - @Test - public void empty_directory_should_return_no_documents() { - - DirectoryWalker abstractDirectoryWalker = new AsciiDocDirectoryWalker(temporaryFolder.getRoot().getAbsolutePath()); - List asciidocFiles = abstractDirectoryWalker.scan(); - - assertThat(asciidocFiles, is(empty())); - - } - - @Test - public void none_existing_directories_should_return_no_documents() { - - DirectoryWalker abstractDirectoryWalker = new AsciiDocDirectoryWalker("my_udirectory"); - List asciidocFiles = abstractDirectoryWalker.scan(); - - assertThat(asciidocFiles, is(empty())); - - } - } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenDocumentHeaderIsRequired.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenDocumentHeaderIsRequired.java index 15de2216c..c4c5374f4 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenDocumentHeaderIsRequired.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenDocumentHeaderIsRequired.java @@ -1,108 +1,106 @@ package org.asciidoctor; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.collection.IsCollectionWithSize.hasSize; -import static org.junit.Assert.assertThat; +import org.asciidoctor.ast.Author; +import org.asciidoctor.ast.DocumentHeader; +import org.asciidoctor.ast.RevisionInfo; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; +import java.io.File; import java.util.List; import java.util.Map; -import org.asciidoctor.arquillian.api.Unshared; -import org.asciidoctor.ast.Author; -import org.asciidoctor.ast.DocumentHeader; -import org.asciidoctor.ast.RevisionInfo; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; - -@RunWith(Arquillian.class) +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.junit.Assert.assertThat; + +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenDocumentHeaderIsRequired { - @ArquillianResource - private ClasspathResources classpath; - - @ArquillianResource - public TemporaryFolder testFolder; - - @ArquillianResource(Unshared.class) - private Asciidoctor asciidoctor; - - @Test - public void doctitle_blocks_and_attributes_should_be_returned() { - - DocumentHeader header = asciidoctor.readDocumentHeader(classpath.getResource("documentheaders.asciidoc")); - - - assertThat(header.getDocumentTitle().getMain(), is("Sample Document")); - assertThat(header.getPageTitle(), is("Sample Document")); - - Map attributes = header.getAttributes(); - assertThat((String)attributes.get("revdate"), is("2013-05-20")); - assertThat((String)attributes.get("revnumber"), is("1.0")); - assertThat((String)attributes.get("revremark"), is("First draft")); - //attributes should be incasesensitive - assertThat((String)attributes.get("tags"), is("[document, example]")); - assertThat((String)attributes.get("Tags"), is("[document, example]")); - assertThat((String)attributes.get("author"), is("Doc Writer")); - assertThat((String)attributes.get("email"), is("doc.writer@asciidoc.org")); - - } - - @Test - public void author_info_should_be_bound_into_author_class() { - - DocumentHeader header = asciidoctor.readDocumentHeader(classpath.getResource("documentheaders.asciidoc")); - - Author author = header.getAuthor(); - assertThat(author.getEmail(), is("doc.writer@asciidoc.org")); - assertThat(author.getFullName(), is("Doc Writer")); - assertThat(author.getFirstName(), is("Doc")); - assertThat(author.getLastName(), is("Writer")); - assertThat(author.getInitials(), is("DW")); - - } - - @Test - public void revision_info_should_be_bound_into_revision_info_class() { - - DocumentHeader header = asciidoctor.readDocumentHeader(classpath.getResource("documentheaders.asciidoc")); - - RevisionInfo revisionInfo = header.getRevisionInfo(); - - assertThat(revisionInfo.getDate(), is("2013-05-20")); - assertThat(revisionInfo.getNumber(), is("1.0")); - assertThat(revisionInfo.getRemark(), is("First draft")); - - } - - - @Test - public void multiple_authors_should_be_bound_into_list_of_authors() { - - DocumentHeader header = asciidoctor.readDocumentHeader(classpath.getResource("documentheaders.asciidoc")); - - List authors = header.getAuthors(); - assertThat(authors, hasSize(2)); - - Author author1 = authors.get(0); - - assertThat(author1.getEmail(), is("doc.writer@asciidoc.org")); - assertThat(author1.getFullName(), is("Doc Writer")); - assertThat(author1.getFirstName(), is("Doc")); - assertThat(author1.getLastName(), is("Writer")); - assertThat(author1.getInitials(), is("DW")); - - Author author2 = authors.get(1); - - assertThat(author2.getEmail(), is("john.smith@asciidoc.org")); - assertThat(author2.getFullName(), is("John Smith")); - assertThat(author2.getFirstName(), is("John")); - assertThat(author2.getLastName(), is("Smith")); - assertThat(author2.getInitials(), is("JS")); - - } - + @AsciidoctorInstance(scope = PER_METHOD) + private Asciidoctor asciidoctor; + + @ClasspathResource("documentheaders.asciidoc") + private File documentHeaders; + + @TempDir + public File tempFolder; + + + @Test + public void doctitle_blocks_and_attributes_should_be_returned() { + + DocumentHeader header = asciidoctor.readDocumentHeader(documentHeaders); + + + assertThat(header.getDocumentTitle().getMain(), is("Sample Document")); + assertThat(header.getPageTitle(), is("Sample Document")); + + Map attributes = header.getAttributes(); + assertThat((String) attributes.get("revdate"), is("2013-05-20")); + assertThat((String) attributes.get("revnumber"), is("1.0")); + assertThat((String) attributes.get("revremark"), is("First draft")); + //attributes should be incasesensitive + assertThat((String) attributes.get("tags"), is("[document, example]")); + assertThat((String) attributes.get("Tags"), is("[document, example]")); + assertThat((String) attributes.get("author"), is("Doc Writer")); + assertThat((String) attributes.get("email"), is("doc.writer@asciidoc.org")); + } + + @Test + public void author_info_should_be_bound_into_author_class() { + + DocumentHeader header = asciidoctor.readDocumentHeader(documentHeaders); + + Author author = header.getAuthor(); + assertThat(author.getEmail(), is("doc.writer@asciidoc.org")); + assertThat(author.getFullName(), is("Doc Writer")); + assertThat(author.getFirstName(), is("Doc")); + assertThat(author.getLastName(), is("Writer")); + assertThat(author.getInitials(), is("DW")); + } + + @Test + public void revision_info_should_be_bound_into_revision_info_class() { + + DocumentHeader header = asciidoctor.readDocumentHeader(documentHeaders); + + RevisionInfo revisionInfo = header.getRevisionInfo(); + + assertThat(revisionInfo.getDate(), is("2013-05-20")); + assertThat(revisionInfo.getNumber(), is("1.0")); + assertThat(revisionInfo.getRemark(), is("First draft")); + } + + @Test + public void multiple_authors_should_be_bound_into_list_of_authors() { + + DocumentHeader header = asciidoctor.readDocumentHeader(documentHeaders); + + List authors = header.getAuthors(); + assertThat(authors, hasSize(2)); + + Author author1 = authors.get(0); + + assertThat(author1.getEmail(), is("doc.writer@asciidoc.org")); + assertThat(author1.getFullName(), is("Doc Writer")); + assertThat(author1.getFirstName(), is("Doc")); + assertThat(author1.getLastName(), is("Writer")); + assertThat(author1.getInitials(), is("DW")); + + Author author2 = authors.get(1); + + assertThat(author2.getEmail(), is("john.smith@asciidoc.org")); + assertThat(author2.getFullName(), is("John Smith")); + assertThat(author2.getFirstName(), is("John")); + assertThat(author2.getLastName(), is("Smith")); + assertThat(author2.getInitials(), is("JS")); + } + } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenGlobExpressionIsUsedForScanning.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenGlobExpressionIsUsedForScanning.java index 6f1bd7fe2..884ca150d 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenGlobExpressionIsUsedForScanning.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenGlobExpressionIsUsedForScanning.java @@ -2,63 +2,57 @@ import org.asciidoctor.jruby.DirectoryWalker; import org.asciidoctor.jruby.GlobDirectoryWalker; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import java.util.List; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.collection.IsEmptyCollection.empty; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenGlobExpressionIsUsedForScanning { - @ArquillianResource - private ClasspathResources classpath; + @ClasspathResource("src") + private File pathToWalk; + + @ClasspathResource("src/documents/_sample.adoc") + private File hiddenSample; + + @ClasspathResource("src/documents/sample.adoc") + private File sample; + @Test public void all_files_with_given_extension_of_current_directory_should_be_returned() { - - File pathToWalk = classpath.getResource("src"); + DirectoryWalker globDirectoryWalker = new GlobDirectoryWalker(pathToWalk.getPath() + File.separator + "documents/*.adoc"); List asciidocFiles = globDirectoryWalker.scan(); - - assertThat( - asciidocFiles, - containsInAnyOrder( - classpath.getResource("src/documents/_sample.adoc"), - classpath.getResource("src/documents/sample.adoc"))); + + assertThat(asciidocFiles) + .containsExactlyInAnyOrder(hiddenSample, sample); } - + @Test public void all_files_with_given_extension_should_be_returned_recursively() { - - File pathToWalk = classpath.getResource("src"); + DirectoryWalker globDirectoryWalker = new GlobDirectoryWalker(pathToWalk.getPath() + File.separator + "**/*.adoc"); List asciidocFiles = globDirectoryWalker.scan(); - - assertThat( - asciidocFiles, - containsInAnyOrder( - classpath.getResource("src/documents/_sample.adoc"), - classpath.getResource("src/documents/sample.adoc"))); + + assertThat(asciidocFiles) + .containsExactlyInAnyOrder(hiddenSample, sample); } - + @Test public void no_should_be_returned_if_glob_expression_does_not_match() { - - File pathToWalk = classpath.getResource("src"); + DirectoryWalker globDirectoryWalker = new GlobDirectoryWalker(pathToWalk.getPath() + File.separator + "**/*.a"); List asciidocFiles = globDirectoryWalker.scan(); - - assertThat(asciidocFiles, is(empty())); + + assertThat(asciidocFiles).isEmpty(); } @Test @@ -67,10 +61,10 @@ public void should_not_fail_with_file_in_root_dir() { final String fileNameInRootDir = "/this_file_does_not_exist.adoc"; DirectoryWalker globDirectoryWalker = new GlobDirectoryWalker(fileNameInRootDir); - List asciidocFiles = globDirectoryWalker.scan(); - assertThat(asciidocFiles, contains(new File(fileNameInRootDir).getAbsoluteFile())); + assertThat(asciidocFiles) + .contains(new File(fileNameInRootDir).getAbsoluteFile()); } @Test @@ -79,10 +73,9 @@ public void should_not_fail_with_file_in_current_working_dir() { final String fileNameInCurrentWorkingDir = "this_file_does_not_exist.adoc"; DirectoryWalker globDirectoryWalker = new GlobDirectoryWalker(fileNameInCurrentWorkingDir); - List asciidocFiles = globDirectoryWalker.scan(); - assertThat(asciidocFiles, contains(new File(fileNameInCurrentWorkingDir).getAbsoluteFile())); + assertThat(asciidocFiles) + .containsExactly(new File(fileNameInCurrentWorkingDir).getAbsoluteFile()); } - } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenMultipleDocumentsAreRendered.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenMultipleDocumentsAreRendered.java index 5eb8305ee..818e29690 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenMultipleDocumentsAreRendered.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenMultipleDocumentsAreRendered.java @@ -1,48 +1,44 @@ package org.asciidoctor; -import org.asciidoctor.jruby.internal.IOUtils; -import org.asciidoctor.jruby.internal.JRubyAsciidoctor; -import org.asciidoctor.util.ClasspathResources; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; -import static org.asciidoctor.OptionsBuilder.options; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenMultipleDocumentsAreRendered { - @Rule - public ClasspathResources classpath = new ClasspathResources(); - - @Rule - public TemporaryFolder testFolder = new TemporaryFolder(); + @TempDir + public File tempDir; + @AsciidoctorInstance private Asciidoctor asciidoctor; - @Before - public void before() { - asciidoctor = JRubyAsciidoctor.create(); - } + @ClasspathResource("documenttitle.adoc") + private Path documentTitleDocument; @Test - public void should_render_the_same_document_a_100_times() throws FileNotFoundException { + public void should_render_the_same_document_a_100_times() throws IOException { // final MemoryLogHandler memoryLogHandler = registerMemoryLogHandler(); - final File inputFile = classpath.getResource("documenttitle.adoc"); int i = 0; String renderContent = null; for (; i < 100; i++) { - renderContent = asciidoctor.convert(IOUtils.readFull(new FileInputStream(inputFile)), - options() + renderContent = asciidoctor.convert(Files.readString(documentTitleDocument), + Options.builder() .safe(SafeMode.UNSAFE) - .asMap()); + .build()); } assertThat(i, equalTo(100)); diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenSourceHighlightingIsUsed.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenSourceHighlightingIsUsed.java index b543acca2..8c03c895a 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenSourceHighlightingIsUsed.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenSourceHighlightingIsUsed.java @@ -1,40 +1,41 @@ package org.asciidoctor; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; + +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.greaterThan; import static org.junit.Assert.assertThat; -@RunWith(Arquillian.class) +@ExtendWith(AsciidoctorExtension.class) public class WhenSourceHighlightingIsUsed { private static final String DOCUMENT = "[source,java]\n" + - "----\n" + - "public class Main {\n" + - " public static void main(String[] args) {\n" + - " println(\"Hello World\")\n" + - " }\n" + - "}\n" + - "----\n"; - - @ArquillianResource + "----\n" + + "public class Main {\n" + + " public static void main(String[] args) {\n" + + " println(\"Hello World\")\n" + + " }\n" + + "}\n" + + "----\n"; + + @AsciidoctorInstance private Asciidoctor asciidoctor; @Test public void should_render_with_rouge() { String html = asciidoctor.convert(DOCUMENT, - OptionsBuilder.options() - .standalone(true) - .safe(SafeMode.UNSAFE) - .attributes( - AttributesBuilder.attributes() - .sourceHighlighter("rouge"))); + OptionsBuilder.options() + .standalone(true) + .safe(SafeMode.UNSAFE) + .attributes( + AttributesBuilder.attributes() + .sourceHighlighter("rouge"))); Document doc = Jsoup.parse(html); @@ -45,12 +46,12 @@ public void should_render_with_rouge() { @Test public void should_render_with_coderay() { String html = asciidoctor.convert(DOCUMENT, - OptionsBuilder.options() - .standalone(true) - .safe(SafeMode.UNSAFE) - .attributes( - AttributesBuilder.attributes() - .sourceHighlighter("coderay"))); + OptionsBuilder.options() + .standalone(true) + .safe(SafeMode.UNSAFE) + .attributes( + AttributesBuilder.attributes() + .sourceHighlighter("coderay"))); Document doc = Jsoup.parse(html); diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenUserRequiresTheAsciidoctorRuntimeVersion.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenUserRequiresTheAsciidoctorRuntimeVersion.java index 691520469..4a7755c8d 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenUserRequiresTheAsciidoctorRuntimeVersion.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenUserRequiresTheAsciidoctorRuntimeVersion.java @@ -1,28 +1,22 @@ package org.asciidoctor; -import static org.junit.Assert.assertThat; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; -import org.asciidoctor.arquillian.api.Unshared; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Test; -import org.junit.runner.RunWith; +import static org.assertj.core.api.Assertions.assertThat; -@RunWith(Arquillian.class) +@ExtendWith(AsciidoctorExtension.class) public class WhenUserRequiresTheAsciidoctorRuntimeVersion { - @ArquillianResource(Unshared.class) + @AsciidoctorInstance private Asciidoctor asciidoctor; @Test public void current_version_should_be_retrieved() { - - String asciidoctorVersion = asciidoctor.asciidoctorVersion(); - - assertThat(asciidoctorVersion, is(notNullValue())); - + final String asciidoctorVersion = asciidoctor.asciidoctorVersion(); + + assertThat(asciidoctorVersion).isNotNull(); } - } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/converter/WhenConverterIsRegistered.java b/asciidoctorj-core/src/test/java/org/asciidoctor/converter/WhenConverterIsRegistered.java index 1d9e18351..caace655c 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/converter/WhenConverterIsRegistered.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/converter/WhenConverterIsRegistered.java @@ -4,47 +4,46 @@ import org.asciidoctor.MemoryLogHandler; import org.asciidoctor.OptionsBuilder; import org.asciidoctor.SafeMode; -import org.asciidoctor.arquillian.api.Unshared; import org.asciidoctor.ast.Document; import org.asciidoctor.ast.Section; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.extension.Treeprocessor; import org.asciidoctor.jruby.internal.JRubyAsciidoctor; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.After; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import java.io.File; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.logging.Logger; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenConverterIsRegistered { - @ArquillianResource(Unshared.class) + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; - @ArquillianResource - private ClasspathResources classpath; + @ClasspathResource("simple.adoc") + private File simpleDocument; - @ArquillianResource - private TemporaryFolder tmp; - @After - public void cleanUp() { - asciidoctor.javaConverterRegistry().unregisterAll(); - } +// @After +// public void cleanUp() { +// asciidoctor.javaConverterRegistry().unregisterAll(); +// } @Test public void shouldCleanUpRegistry() { @@ -134,7 +133,7 @@ void processNode(StructuralNode block) { if (block instanceof Section && block.getTitle().equals("Test2")) { block.setLevel(42); } - for (StructuralNode node: block.getBlocks()) { + for (StructuralNode node : block.getBlocks()) { processNode(node); } } @@ -150,11 +149,13 @@ void processNode(StructuralNode block) { } @Test - public void shouldRegisterConverterViaConverterRegistryExecutor() throws Exception { + public void shouldRegisterConverterViaConverterRegistryExecutor( + @ClasspathResource("serviceloadertest/3") File serviceLoader) throws MalformedURLException { + ClassLoader oldTCCL = Thread.currentThread().getContextClassLoader(); try { - Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[]{classpath.getResource("serviceloadertest/3").toURI().toURL()})); + Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[]{serviceLoader.toURI().toURL()})); asciidoctor = JRubyAsciidoctor.create(); String result = asciidoctor.convert("== Hello\n\nWorld!\n\n- a\n- b", OptionsBuilder.options().backend("extensiontext")); @@ -166,29 +167,27 @@ public void shouldRegisterConverterViaConverterRegistryExecutor() throws Excepti @Test - public void shouldWriteFileWithSuffixFromConverterWithAnnotation() throws Exception { + public void shouldWriteFileWithSuffixFromConverterWithAnnotation(@TempDir File tempDir) throws Exception { asciidoctor.javaConverterRegistry().register(TextConverter.class); - File todir = tmp.newFolder(); - asciidoctor.convertFile(classpath.getResource("simple.adoc"), OptionsBuilder.options().backend(TextConverter.DEFAULT_FORMAT).toDir(todir).safe(SafeMode.UNSAFE)); + File todir = tempDir; + asciidoctor.convertFile(simpleDocument, OptionsBuilder.options().backend(TextConverter.DEFAULT_FORMAT).toDir(todir).safe(SafeMode.UNSAFE)); assertThat(new File(todir, "simple.html").exists(), is(false)); assertThat(new File(todir, "simple.txt").exists(), is(true)); - } @Test - public void shouldWriteFileWithSuffixFromConverterThatInvokesSetOutfileSuffix() throws Exception { + public void shouldWriteFileWithSuffixFromConverterThatInvokesSetOutfileSuffix(@TempDir File tempDir) { asciidoctor.javaConverterRegistry().register(TextConverterWithSuffix.class); - File todir = tmp.newFolder(); - asciidoctor.convertFile(classpath.getResource("simple.adoc"), OptionsBuilder.options().backend(TextConverterWithSuffix.DEFAULT_FORMAT).toDir(todir).safe(SafeMode.UNSAFE)); + File todir = tempDir; + asciidoctor.convertFile(simpleDocument, OptionsBuilder.options().backend(TextConverterWithSuffix.DEFAULT_FORMAT).toDir(todir).safe(SafeMode.UNSAFE)); assertThat(new File(todir, "simple.html").exists(), is(false)); assertThat(new File(todir, "simple.text").exists(), is(true)); - } } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/BlockMacroRegistrationTest.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/BlockMacroRegistrationTest.java index 8ea905cd7..16700c209 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/BlockMacroRegistrationTest.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/BlockMacroRegistrationTest.java @@ -4,32 +4,36 @@ import org.asciidoctor.OptionsBuilder; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.jruby.internal.AsciidoctorCoreException; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.jsoup.Jsoup; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; -import java.lang.annotation.*; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; import java.util.Map; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; -import static java.util.stream.Collectors.joining; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class BlockMacroRegistrationTest { - @ArquillianResource + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; private String document(String blockName, String text) { return "= Test\n" + - "\n" + - "== A section\n" + - "\n" + - blockName + "::" + text + "[]"; + "\n" + + "== A section\n" + + "\n" + + blockName + "::" + text + "[]"; } public static class AbstractTestProcessor extends BlockMacroProcessor { @@ -122,12 +126,11 @@ public void testRegisterNamedClassAsInstanceWithExplicitName() { check("YET ANOTHER TEST", result); } - @Test(expected = IllegalArgumentException.class) + @Test public void testRegisterClassWithoutExplicitName() { - asciidoctor.javaExtensionRegistry().blockMacro(AbstractTestProcessor.class); + assertThrows(IllegalArgumentException.class, () -> asciidoctor.javaExtensionRegistry().blockMacro(AbstractTestProcessor.class)); } - @Test public void testRegisterClassAsClassWithExplicitName() { final String explicitblockname = "anotherexplicitname"; @@ -136,10 +139,12 @@ public void testRegisterClassAsClassWithExplicitName() { check("HELLO EXPLICIT CLASS", result); } - @Test(expected = AsciidoctorCoreException.class) + @Test public void testRegisterClassAsInstance() { - asciidoctor.javaExtensionRegistry().blockMacro(new AbstractTestProcessor()); - asciidoctor.convert(document("foo", "Hello Explicit Instance"), OptionsBuilder.options()); + assertThrows(AsciidoctorCoreException.class, () -> { + asciidoctor.javaExtensionRegistry().blockMacro(new AbstractTestProcessor()); + asciidoctor.convert(document("foo", "Hello Explicit Instance"), OptionsBuilder.options()); + }); } @Test @@ -151,10 +156,9 @@ public void testRegisterClassAsInstanceWithExplicitName() { } - - @Test(expected = IllegalArgumentException.class) + @Test public void testRegisterClassWithNameAsClass() { - asciidoctor.javaExtensionRegistry().blockMacro(TestProcessorWithName.class); + assertThrows(IllegalArgumentException.class, () -> asciidoctor.javaExtensionRegistry().blockMacro(TestProcessorWithName.class)); } @Test diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/BlockProcessorRegistrationTest.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/BlockProcessorRegistrationTest.java index 1617f0d27..b533e7bb2 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/BlockProcessorRegistrationTest.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/BlockProcessorRegistrationTest.java @@ -4,43 +4,45 @@ import org.asciidoctor.OptionsBuilder; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.jruby.internal.AsciidoctorCoreException; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; import org.jsoup.Jsoup; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.util.List; import java.util.Map; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; -@RunWith(Arquillian.class) +@ExtendWith(AsciidoctorExtension.class) public class BlockProcessorRegistrationTest { - @ArquillianResource + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; private String document(String blockName, String text) { return "= Test\n" + - "\n" + - "== A section\n" + - "\n" + - "[" + blockName + "]\n" + - text + "\n"; + "\n" + + "== A section\n" + + "\n" + + "[" + blockName + "]\n" + + text + "\n"; } public static class AbstractTestProcessor extends BlockProcessor { @Override public Object process(StructuralNode parent, Reader reader, Map attributes) { List s = reader.readLines().stream() - .map(line -> - line.chars() - .mapToObj(c -> Character.toString((char) c)) - .collect(joining(" "))) - .collect(toList()); + .map(line -> + line.chars() + .mapToObj(c -> Character.toString((char) c)) + .collect(joining(" "))) + .collect(toList()); return createBlock(parent, "paragraph", s); } } @@ -56,11 +58,11 @@ public TestProcessorWithName() { @Override public Object process(StructuralNode parent, Reader reader, Map attributes) { List s = reader.readLines().stream() - .map(line -> - line.chars() - .mapToObj(c -> Character.toString((char) c)) - .collect(joining(" "))) - .collect(toList()); + .map(line -> + line.chars() + .mapToObj(c -> Character.toString((char) c)) + .collect(joining(" "))) + .collect(toList()); return createBlock(parent, "paragraph", s); } } @@ -100,15 +102,15 @@ public void testRegisterNamedClassAsInstanceWithExplicitName() { check("Y e t A n o t h e r T e s t", result); } - @Test(expected = IllegalArgumentException.class) + @Test public void testRegisterClassWithoutExplicitName() { - asciidoctor.javaExtensionRegistry().block(AbstractTestProcessor.class); + assertThrows(IllegalArgumentException.class, () -> asciidoctor.javaExtensionRegistry().block(AbstractTestProcessor.class)); } - @Test(expected = IllegalArgumentException.class) + @Test public void testRegisterClassAsClass() { - asciidoctor.javaExtensionRegistry().block(AbstractTestProcessor.class); + assertThrows(IllegalArgumentException.class, () -> asciidoctor.javaExtensionRegistry().block(AbstractTestProcessor.class)); } @Test @@ -119,10 +121,12 @@ public void testRegisterClassAsClassWithExplicitName() { check("H e l l o E x p l i c i t C l a s s", result); } - @Test(expected = AsciidoctorCoreException.class) + @Test public void testRegisterClassAsInstance() { - asciidoctor.javaExtensionRegistry().block(new AbstractTestProcessor()); - asciidoctor.convert(document("foo", "Hello Explicit Instance"), OptionsBuilder.options()); + assertThrows(AsciidoctorCoreException.class, () -> { + asciidoctor.javaExtensionRegistry().block(new AbstractTestProcessor()); + asciidoctor.convert(document("foo", "Hello Explicit Instance"), OptionsBuilder.options()); + }); } @Test @@ -134,10 +138,9 @@ public void testRegisterClassAsInstanceWithExplicitName() { } - - @Test(expected = IllegalArgumentException.class) + @Test public void testRegisterClassWithNameAsClass() { - asciidoctor.javaExtensionRegistry().block(TestProcessorWithName.class); + assertThrows(IllegalArgumentException.class, () -> asciidoctor.javaExtensionRegistry().block(TestProcessorWithName.class)); } @Test diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/InlineMacroRegistrationTest.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/InlineMacroRegistrationTest.java index 8251c8c3f..de63e6489 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/InlineMacroRegistrationTest.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/InlineMacroRegistrationTest.java @@ -5,38 +5,41 @@ import org.asciidoctor.ast.PhraseNode; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.jruby.internal.AsciidoctorCoreException; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.jsoup.Jsoup; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.util.Map; import static java.util.stream.Collectors.joining; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class InlineMacroRegistrationTest { - @ArquillianResource + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; private String document(String blockName, String text) { return "= Test\n" + - "\n" + - "== A section\n" + - "\n" + - "\nHello " + blockName + ":" + text + "[]"; + "\n" + + "== A section\n" + + "\n" + + "\nHello " + blockName + ":" + text + "[]"; } public static class AbstractTestProcessor extends InlineMacroProcessor { @Override public PhraseNode process(StructuralNode parent, String target, Map attributes) { String transformed = target.chars() - .mapToObj(c -> Character.isUpperCase(c) ? " " + (char) c : Character.toString((char) c)) - .collect(joining()) - .toUpperCase().trim(); + .mapToObj(c -> Character.isUpperCase(c) ? " " + (char) c : Character.toString((char) c)) + .collect(joining()) + .toUpperCase().trim(); return createPhraseNode(parent, "quoted", transformed); } } @@ -52,9 +55,9 @@ public TestProcessorWithName() { @Override public PhraseNode process(StructuralNode parent, String target, Map attributes) { String transformed = target.chars() - .mapToObj(c -> Character.isUpperCase(c) ? " " + (char) c : Character.toString((char) c)) - .collect(joining()) - .toUpperCase().trim(); + .mapToObj(c -> Character.isUpperCase(c) ? " " + (char) c : Character.toString((char) c)) + .collect(joining()) + .toUpperCase().trim(); return createPhraseNode(parent, "quoted", transformed); } } @@ -94,9 +97,9 @@ public void testRegisterNamedClassAsInstanceWithExplicitName() { check("Hello YET ANOTHER TEST", result); } - @Test(expected = IllegalArgumentException.class) + @Test public void testRegisterClassWithoutExplicitName() { - asciidoctor.javaExtensionRegistry().inlineMacro(AbstractTestProcessor.class); + assertThrows(IllegalArgumentException.class, () -> asciidoctor.javaExtensionRegistry().inlineMacro(AbstractTestProcessor.class)); } @@ -108,10 +111,12 @@ public void testRegisterClassAsClassWithExplicitName() { check("Hello EXPLICIT CLASS", result); } - @Test(expected = AsciidoctorCoreException.class) + @Test public void testRegisterClassAsInstance() { - asciidoctor.javaExtensionRegistry().inlineMacro(new AbstractTestProcessor()); - asciidoctor.convert(document("foo", "HelloExplicitInstance"), OptionsBuilder.options()); + assertThrows(AsciidoctorCoreException.class, () -> { + asciidoctor.javaExtensionRegistry().inlineMacro(new AbstractTestProcessor()); + asciidoctor.convert(document("foo", "HelloExplicitInstance"), OptionsBuilder.options()); + }); } @Test @@ -123,9 +128,9 @@ public void testRegisterClassAsInstanceWithExplicitName() { } - @Test(expected = IllegalArgumentException.class) + @Test public void testRegisterClassWithNameAsClass() { - asciidoctor.javaExtensionRegistry().inlineMacro(TestProcessorWithName.class); + assertThrows(IllegalArgumentException.class, () -> asciidoctor.javaExtensionRegistry().inlineMacro(TestProcessorWithName.class)); } @Test diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/PreprocessorTest.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/PreprocessorTest.java index a0e164038..1d3858ae0 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/PreprocessorTest.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/PreprocessorTest.java @@ -3,21 +3,23 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.ast.Document; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.jsoup.Jsoup; import org.jsoup.nodes.Element; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.util.Arrays; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; import static org.assertj.core.api.Assertions.assertThat; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class PreprocessorTest { - @ArquillianResource + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; @Test diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/TerminalCommandTreeprocessor.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/TerminalCommandTreeprocessor.java index 254999cb8..6881c2fc2 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/TerminalCommandTreeprocessor.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/TerminalCommandTreeprocessor.java @@ -1,19 +1,19 @@ package org.asciidoctor.extension; -import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.ast.Block; import org.asciidoctor.ast.Document; +import org.asciidoctor.ast.StructuralNode; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class TerminalCommandTreeprocessor extends Treeprocessor { - private Document document; + private Document document; - public TerminalCommandTreeprocessor() {} + public TerminalCommandTreeprocessor() { + } public TerminalCommandTreeprocessor(Map config) { super(config); @@ -22,7 +22,7 @@ public TerminalCommandTreeprocessor(Map config) { @Override public Document process(Document document) { this.document = document; - processBlock((StructuralNode) this.document); + processBlock(this.document); return this.document; } @@ -32,7 +32,7 @@ private void processBlock(StructuralNode block) { for (int i = 0; i < blocks.size(); i++) { final StructuralNode currentBlock = blocks.get(i); - if(currentBlock instanceof StructuralNode) { + if (currentBlock instanceof StructuralNode) { if ("paragraph".equals(currentBlock.getContext())) { List lines = ((Block) currentBlock).getLines(); if (lines.size() > 0 && lines.get(0).startsWith("$")) { @@ -45,6 +45,7 @@ private void processBlock(StructuralNode block) { } } } + public Block convertToTerminalListing(Block block) { Map attributes = block.getAttributes(); @@ -63,8 +64,8 @@ public Block convertToTerminalListing(Block block) { } } - return createBlock(this.document, "listing", Arrays.asList(resultLines.toString()), attributes, - new HashMap()); + return createBlock(this.document, "listing", + List.of(resultLines.toString()), attributes, new HashMap<>()); } } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/UriIncludeProcessor.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/UriIncludeProcessor.java index 11f32f3e4..e282595f4 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/UriIncludeProcessor.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/UriIncludeProcessor.java @@ -1,22 +1,19 @@ package org.asciidoctor.extension; +import org.asciidoctor.ast.Document; +import org.asciidoctor.util.TestHttpServer; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.net.InetSocketAddress; -import java.net.MalformedURLException; -import java.net.Proxy; -import java.net.URL; -import java.net.URLConnection; +import java.net.*; import java.util.Map; -import org.asciidoctor.ast.Document; -import org.asciidoctor.util.TestHttpServer; - public class UriIncludeProcessor extends IncludeProcessor { - public UriIncludeProcessor() {} + public UriIncludeProcessor() { + } public UriIncludeProcessor(Map config) { super(config); @@ -29,11 +26,10 @@ public boolean handles(String target) { @Override public void process(Document document, PreprocessorReader reader, String target, - Map attributes) { + Map attributes) { StringBuilder content = readContent(target); reader.push_include(content.toString(), target, target, 1, attributes); - } private StringBuilder readContent(String target) { @@ -47,7 +43,7 @@ private StringBuilder readContent(String target) { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(openStream)); - String line = null; + String line; while ((line = bufferedReader.readLine()) != null) { content.append(line); } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionGroupIsRegistered.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionGroupIsRegistered.java index 645f0555c..56e695366 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionGroupIsRegistered.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionGroupIsRegistered.java @@ -9,69 +9,83 @@ import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.log.LogRecord; import org.asciidoctor.log.TestLogHandlerService; -import org.asciidoctor.util.ClasspathResources; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.asciidoctor.util.TestHttpServer; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; import org.jsoup.Jsoup; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; - -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; + +import java.io.*; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.net.URLConnection; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; import java.util.logging.LogManager; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.startsWith; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; +import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasSize; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenJavaExtensionGroupIsRegistered { public static final String ASCIIDOCTORCLASS_PREFIX = "module AsciidoctorJ include_package 'org.asciidoctor'"; - @ArquillianResource - private ClasspathResources classpath; + @AsciidoctorInstance(scope = PER_METHOD) + private Asciidoctor asciidoctor; - @ArquillianResource - public TemporaryFolder testFolder; + @ClasspathResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb") + private File asciidoctorRubyClass; + + @ClasspathResource("rendersample.asciidoc") + private File renderSample; + + @ClasspathResource("simple.adoc") + private File simpleDocument; + + @ClasspathResource("changeattribute.adoc") + private File changeAttribute; + + @ClasspathResource("sample-with-terminal-command.ad") + private File sampleWithTerminalCommand; + + @ClasspathResource("sample-with-uri-include.ad") + private File sampleWithUriInclude; + + @ClasspathResource("sample-with-yell-block.ad") + private File sampleWithYellBlock; + + @ClasspathResource("sample-with-gist-macro.ad") + private File sampleWithGistMacro; + + @ClasspathResource("sample-with-man-link.ad") + private File sampleWithManLink; + + @ClasspathResource("sample-with-yell-listing-block.ad") + private File sampleWithYellListingBlock; + + @TempDir + public File tempFolder; - private Asciidoctor asciidoctor; - @Before + @BeforeEach public void before() { - asciidoctor = Asciidoctor.Factory.create(); TestLogHandlerService.clear(); } - @After + @AfterEach public void tearDown() throws IOException { if (TestHttpServer.getInstance() != null) { TestHttpServer.getInstance().stop(); @@ -127,14 +141,14 @@ public boolean handles(String target) { @Test public void an_inner_class_should_be_registered() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); this.asciidoctor.createGroup() - .includeProcessor(new RubyIncludeSource(new HashMap())) - .register(); + .includeProcessor(new RubyIncludeSource(new HashMap<>())) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -148,51 +162,51 @@ public void an_inner_class_should_be_registered() { @Test public void an_inner_anonymous_class_should_be_registered() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); this.asciidoctor.createGroup() - .includeProcessor(new IncludeProcessor(new HashMap()) { + .includeProcessor(new IncludeProcessor(new HashMap<>()) { - @Override - public void process(Document document, PreprocessorReader reader, String target, - Map attributes) { - StringBuilder content = readContent(target); - reader.push_include(content.toString(), target, target, 1, attributes); - } + @Override + public void process(Document document, PreprocessorReader reader, String target, + Map attributes) { + StringBuilder content = readContent(target); + reader.push_include(content.toString(), target, target, 1, attributes); + } - private StringBuilder readContent(String target) { - StringBuilder content = new StringBuilder(); + private StringBuilder readContent(String target) { + StringBuilder content = new StringBuilder(); - try { + try { - URL url = new URL(target); - URLConnection connection = url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", TestHttpServer.getInstance().getLocalPort()))); - InputStream openStream = connection.getInputStream(); + URL url = new URL(target); + URLConnection connection = url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", TestHttpServer.getInstance().getLocalPort()))); + InputStream openStream = connection.getInputStream(); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(openStream)); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(openStream)); - String line = null; - while ((line = bufferedReader.readLine()) != null) { - content.append(line); - } + String line = null; + while ((line = bufferedReader.readLine()) != null) { + content.append(line); + } - bufferedReader.close(); + bufferedReader.close(); - } catch (IOException e) { - throw new IllegalArgumentException(e); - } - return content; - } + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + return content; + } - @Override - public boolean handles(String target) { - return target.startsWith("http://") || target.startsWith("https://"); - } - }) - .register(); + @Override + public boolean handles(String target) { + return target.startsWith("http://") || target.startsWith("https://"); + } + }) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -206,11 +220,11 @@ public boolean handles(String target) { @Test public void a_docinfoprocessor_should_be_executed_and_add_meta_in_header_by_default() { asciidoctor.createGroup() - .docinfoProcessor(MetaRobotsDocinfoProcessor.class.getCanonicalName()) - .register(); + .docinfoProcessor(MetaRobotsDocinfoProcessor.class.getCanonicalName()) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("simple.adoc"), + simpleDocument, Options.builder().standalone(true).safe(SafeMode.SERVER).toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -222,22 +236,22 @@ public void a_docinfoprocessor_should_be_executed_and_add_meta_in_header_by_defa @Test public void a_docinfoprocessor_should_be_executed_and_add_meta_in_footer() { - Map options = new HashMap(); + Map options = new HashMap<>(); options.put("location", ":footer"); MetaRobotsDocinfoProcessor metaRobotsDocinfoProcessor = new MetaRobotsDocinfoProcessor(options); this.asciidoctor.createGroup() - .docinfoProcessor(metaRobotsDocinfoProcessor) - .register(); + .docinfoProcessor(metaRobotsDocinfoProcessor) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("simple.adoc"), + simpleDocument, Options.builder().standalone(true).safe(SafeMode.SERVER).toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element footer = doc.getElementById("footer"); - // Since Asciidoctor 1.5.3 the docinfo in the footer is a sibling to the footer element + // Since Asciidoctor 1.5.3 the docinfo in the footer is a sibling to the footer element assertEquals("robots", footer.nextElementSibling().attr("name")); } @@ -245,29 +259,27 @@ public void a_docinfoprocessor_should_be_executed_and_add_meta_in_footer() { public void a_preprocessor_should_be_executed_before_document_is_rendered() { this.asciidoctor.createGroup() - .preprocessor(ChangeAttributeValuePreprocessor.class) - .register(); + .preprocessor(ChangeAttributeValuePreprocessor.class) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("changeattribute.adoc"), + changeAttribute, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); assertThat(doc.getElementsByTag("p").first().text(), is("sample Alex")); - } @Test public void a_preprocessor_as_string_should_be_executed_before_document_is_rendered() { - this.asciidoctor.createGroup() - .preprocessor("org.asciidoctor.extension.ChangeAttributeValuePreprocessor") - .register(); + .preprocessor("org.asciidoctor.extension.ChangeAttributeValuePreprocessor") + .register(); String content = asciidoctor.convertFile( - classpath.getResource("changeattribute.adoc"), + changeAttribute, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -280,11 +292,11 @@ public void a_preprocessor_as_string_should_be_executed_before_document_is_rende public void a_preprocessor_instance_should_be_executed_before_document_is_rendered() { this.asciidoctor.createGroup() - .preprocessor(new ChangeAttributeValuePreprocessor(new HashMap())) - .register(); + .preprocessor(new ChangeAttributeValuePreprocessor(new HashMap<>())) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("changeattribute.adoc"), + changeAttribute, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -297,17 +309,17 @@ public void a_preprocessor_instance_should_be_executed_before_document_is_render public void a_postprocessor_as_string_should_be_executed_after_document_is_rendered() throws IOException { this.asciidoctor.createGroup() - .postprocessor("org.asciidoctor.extension.CustomFooterPostProcessor") - .register(); + .postprocessor("org.asciidoctor.extension.CustomFooterPostProcessor") + .register(); - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) .safe(SafeMode.UNSAFE). build(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(simpleDocument, options); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -319,16 +331,16 @@ public void a_postprocessor_as_string_should_be_executed_after_document_is_rende public void a_postprocessor_should_be_executed_after_document_is_rendered() throws IOException { this.asciidoctor.createGroup() - .postprocessor(CustomFooterPostProcessor.class) - .register(); + .postprocessor(CustomFooterPostProcessor.class) + .register(); - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) .safe(SafeMode.UNSAFE).build(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(simpleDocument, options); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -340,16 +352,16 @@ public void a_postprocessor_should_be_executed_after_document_is_rendered() thro public void a_postprocessor_instance_should_be_executed_after_document_is_rendered() throws IOException { this.asciidoctor.createGroup() - .postprocessor(new CustomFooterPostProcessor(new HashMap())) - .register(); + .postprocessor(new CustomFooterPostProcessor(new HashMap<>())) + .register(); - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) .safe(SafeMode.UNSAFE).build(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(simpleDocument, options); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -360,14 +372,14 @@ public void a_postprocessor_instance_should_be_executed_after_document_is_render @Test public void a_include_processor_as_string_should_be_executed_when_include_macro_is_found() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); this.asciidoctor.createGroup() - .includeProcessor("org.asciidoctor.extension.UriIncludeProcessor") - .register(); + .includeProcessor("org.asciidoctor.extension.UriIncludeProcessor") + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -381,14 +393,14 @@ public void a_include_processor_as_string_should_be_executed_when_include_macro_ @Test public void a_include_processor_should_be_executed_when_include_macro_is_found() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); this.asciidoctor.createGroup() - .includeProcessor(UriIncludeProcessor.class) - .register(); + .includeProcessor(UriIncludeProcessor.class) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -402,14 +414,14 @@ public void a_include_processor_should_be_executed_when_include_macro_is_found() @Test public void a_include_instance_processor_should_be_executed_when_include_macro_is_found() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); this.asciidoctor.createGroup() - .includeProcessor(new UriIncludeProcessor(new HashMap())) - .register(); + .includeProcessor(new UriIncludeProcessor(new HashMap<>())) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -421,13 +433,14 @@ public void a_include_instance_processor_should_be_executed_when_include_macro_i } @Test - public void a_include_processor_should_only_handle_its_handles() { + public void a_include_processor_should_only_handle_its_handles( + @ClasspathResource("sample-with-include.ad") File sampleWithInclude) { this.asciidoctor.createGroup() - .includeProcessor(UriIncludeProcessor.class) - .register(); + .includeProcessor(UriIncludeProcessor.class) + .register(); - String content = asciidoctor.convertFile(classpath.getResource("sample-with-include.ad"), + String content = asciidoctor.convertFile(sampleWithInclude, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -439,13 +452,14 @@ public void a_include_processor_should_only_handle_its_handles() { } @Test - public void a_include_processor_can_handle_positional_attrs() { + public void a_include_processor_can_handle_positional_attrs( + @ClasspathResource("sample-with-include-pos-attrs.ad") File sampleWithIncludePosAttrs) { this.asciidoctor.createGroup() - .includeProcessor(PositionalAttrsIncludeProcessor.class) - .register(); + .includeProcessor(PositionalAttrsIncludeProcessor.class) + .register(); - String content = asciidoctor.convertFile(classpath.getResource("sample-with-include-pos-attrs.ad"), + String content = asciidoctor.convertFile(sampleWithIncludePosAttrs, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -456,37 +470,38 @@ public void a_include_processor_can_handle_positional_attrs() { } - @Test + @Test public void a_treeprocessor_should_be_executed_in_document() { - this.asciidoctor.createGroup() - .treeprocessor(TerminalCommandTreeprocessor.class) - .register(); + this.asciidoctor.createGroup() + .treeprocessor(TerminalCommandTreeprocessor.class) + .register(); - String content = asciidoctor.convertFile( - classpath.getResource("sample-with-terminal-command.ad"), - Options.builder().toFile(false).build()); + String content = asciidoctor.convertFile( + sampleWithTerminalCommand, + Options.builder().toFile(false).build()); - org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); + org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); - Element contentElement = doc.getElementsByAttributeValue("class", "command").first(); - assertThat(contentElement.text(), is("echo \"Hello, World!\"")); + Element contentElement = doc.getElementsByAttributeValue("class", "command").first(); + assertThat(contentElement.text(), is("echo \"Hello, World!\"")); - contentElement = doc.getElementsByAttributeValue("class", "command").last(); - assertThat(contentElement.text(), is("gem install asciidoctor")); + contentElement = doc.getElementsByAttributeValue("class", "command").last(); + assertThat(contentElement.text(), is("gem install asciidoctor")); } @Test - public void a_treeprocessor_and_blockmacroprocessor_should_be_executed_in_document() { + public void a_treeprocessor_and_blockmacroprocessor_should_be_executed_in_document( + @ClasspathResource("sample-with-terminal-command-and-gist-macro.ad") File sampleWithTerminalCommand) { this.asciidoctor.createGroup() - .treeprocessor(TerminalCommandTreeprocessor.class) - .blockMacro("gist", GistMacro.class) - .register(); + .treeprocessor(TerminalCommandTreeprocessor.class) + .blockMacro("gist", GistMacro.class) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-terminal-command-and-gist-macro.ad"), + sampleWithTerminalCommand, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -508,11 +523,11 @@ public void a_treeprocessor_and_blockmacroprocessor_should_be_executed_in_docume public void a_treeprocessor_as_string_should_be_executed_in_document() { this.asciidoctor.createGroup() - .treeprocessor("org.asciidoctor.extension.TerminalCommandTreeprocessor") - .register(); + .treeprocessor("org.asciidoctor.extension.TerminalCommandTreeprocessor") + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-terminal-command.ad"), + sampleWithTerminalCommand, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -529,11 +544,11 @@ public void a_treeprocessor_as_string_should_be_executed_in_document() { public void a_treeprocessor_instance_should_be_executed_in_document() { this.asciidoctor.createGroup() - .treeprocessor(new TerminalCommandTreeprocessor(new HashMap())) - .register(); + .treeprocessor(new TerminalCommandTreeprocessor(new HashMap<>())) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-terminal-command.ad"), + sampleWithTerminalCommand, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -547,23 +562,24 @@ public void a_treeprocessor_instance_should_be_executed_in_document() { } @Test - @Ignore - public void extensions_should_be_correctly_added_using_extension_registry() throws IOException { + @Disabled + public void extensions_should_be_correctly_added_using_extension_registry( + @ClasspathResource("arrows-and-boxes-example.ad") File arrowsAndBoxesExample) throws IOException { // To avoid registering the same extension over and over for all tests, // service is instantiated manually. this.asciidoctor.createGroup() - .postprocessor(ArrowsAndBoxesIncludesPostProcessor.class) - .block("arrowsAndBoxes", ArrowsAndBoxesBlock.class) - .register(); + .postprocessor(ArrowsAndBoxesIncludesPostProcessor.class) + .block("arrowsAndBoxes", ArrowsAndBoxesBlock.class) + .register(); - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) .safe(SafeMode.UNSAFE).build(); - asciidoctor.convertFile(classpath.getResource("arrows-and-boxes-example.ad"), options); + asciidoctor.convertFile(arrowsAndBoxesExample, options); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -583,11 +599,11 @@ public void a_block_macro_extension_should_be_executed_when_macro_is_detected() this.asciidoctor.createGroup() - .blockMacro("gist", GistMacro.class) - .register(); + .blockMacro("gist", GistMacro.class) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-gist-macro.ad"), + sampleWithGistMacro, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -600,11 +616,11 @@ public void a_block_macro_extension_should_be_executed_when_macro_is_detected() public void a_block_macro_extension_instance_should_be_executed_when_macro_is_detected() { this.asciidoctor.createGroup() - .blockMacro(new GistMacro("gist", new HashMap())) - .register(); + .blockMacro(new GistMacro("gist", new HashMap<>())) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-gist-macro.ad"), + sampleWithGistMacro, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -617,11 +633,11 @@ public void a_block_macro_extension_instance_should_be_executed_when_macro_is_de public void a_block_macro_as_string_extension_should_be_executed_when_macro_is_detected() { this.asciidoctor.createGroup() - .blockMacro("gist", "org.asciidoctor.extension.GistMacro") - .register(); + .blockMacro("gist", "org.asciidoctor.extension.GistMacro") + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-gist-macro.ad"), + sampleWithGistMacro, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -637,11 +653,11 @@ public void a_block_macro_as_instance_extension_should_be_executed_when_macro_is options.put(ContentModel.KEY, ContentModel.RAW); this.asciidoctor.createGroup() - .blockMacro(new GistMacro("gist", options)) - .register(); + .blockMacro(new GistMacro("gist", options)) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-gist-macro.ad"), + sampleWithGistMacro, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -654,11 +670,11 @@ public void a_block_macro_as_instance_extension_should_be_executed_when_macro_is public void an_inline_macro_as_string_extension_should_be_executed_when_an_inline_macro_is_detected() { this.asciidoctor.createGroup() - .inlineMacro("man", "org.asciidoctor.extension.ManpageMacro") - .register(); + .inlineMacro("man", "org.asciidoctor.extension.ManpageMacro") + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-man-link.ad"), + sampleWithManLink, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -673,11 +689,11 @@ public void an_inline_macro_as_string_extension_should_be_executed_when_an_inlin public void an_inline_macro_extension_should_be_executed_when_an_inline_macro_is_detected() { this.asciidoctor.createGroup() - .inlineMacro("man", ManpageMacro.class) - .register(); + .inlineMacro("man", ManpageMacro.class) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-man-link.ad"), + sampleWithManLink, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -691,16 +707,16 @@ public void an_inline_macro_extension_should_be_executed_when_an_inline_macro_is @Test public void an_inline_macro_as_instance_extension_should_be_executed_when_regexp_is_set_as_option_inline_macro_is_detected() { - Map options = new HashMap(); + Map options = new HashMap<>(); options.put(InlineMacroProcessor.REGEXP, "man(?:page)?:(\\S+?)\\[(.*?)\\]"); ManpageMacro inlineMacroProcessor = new ManpageMacro("man", options); this.asciidoctor.createGroup() - .inlineMacro(inlineMacroProcessor) - .register(); + .inlineMacro(inlineMacroProcessor) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-man-link.ad"), + sampleWithManLink, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -715,16 +731,16 @@ public void an_inline_macro_as_instance_extension_should_be_executed_when_regexp @Test public void an_inline_macro_as_instance_extension_should_not_be_executed_when_regexp_is_set_and_does_not_match() { - Map options = new HashMap(); + Map options = new HashMap<>(); options.put(InlineMacroProcessor.REGEXP, "man(?:page)?:(ThisDoesNotMatch)\\[(.*?)\\]"); ManpageMacro inlineMacroProcessor = new ManpageMacro("man", options); this.asciidoctor.createGroup() - .inlineMacro(inlineMacroProcessor) - .register(); + .inlineMacro(inlineMacroProcessor) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-man-link.ad"), + sampleWithManLink, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -738,15 +754,15 @@ public void an_inline_macro_as_instance_extension_should_not_be_executed_when_re @Test public void an_inline_macro_as_instance_extension_should_be_executed_when_an_inline_macro_is_detected() { - Map options = new HashMap(); + Map options = new HashMap<>(); ManpageMacro inlineMacroProcessor = new ManpageMacro("man", options); this.asciidoctor.createGroup() - .inlineMacro(inlineMacroProcessor) - .register(); + .inlineMacro(inlineMacroProcessor) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-man-link.ad"), + sampleWithManLink, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -762,10 +778,10 @@ public void an_inline_macro_as_instance_extension_should_be_executed_when_an_inl public void should_unregister_all_current_registered_extensions() throws IOException { this.asciidoctor.createGroup() - .postprocessor(CustomFooterPostProcessor.class) - .register(); + .postprocessor(CustomFooterPostProcessor.class) + .register(); - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) @@ -773,7 +789,7 @@ public void should_unregister_all_current_registered_extensions() throws IOExcep .build(); asciidoctor.unregisterAllExtensions(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(simpleDocument, options); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -786,10 +802,10 @@ public void a_block_processor_as_string_should_be_executed_when_registered_block throws IOException { this.asciidoctor.createGroup() - .block("yell", "org.asciidoctor.extension.YellStaticBlock") - .register(); + .block("yell", "org.asciidoctor.extension.YellStaticBlock") + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), + sampleWithYellBlock, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -803,10 +819,10 @@ public void a_block_processor_as_string_should_be_executed_when_registered_block public void a_block_processor_should_be_executed_when_registered_block_is_found_in_document() { this.asciidoctor.createGroup() - .block("yell", YellStaticBlock.class) - .register(); + .block("yell", YellStaticBlock.class) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), + sampleWithYellBlock, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -819,15 +835,15 @@ public void a_block_processor_should_be_executed_when_registered_block_is_found_ @Test public void a_block_processor_instance_should_be_executed_when_registered_block_is_found_in_document() { - Map config = new HashMap(); + Map config = new HashMap<>(); config.put(Contexts.KEY, Arrays.asList(Contexts.PARAGRAPH)); config.put(ContentModel.KEY, ContentModel.SIMPLE); YellBlock yellBlock = new YellBlock("yell", config); this.asciidoctor.createGroup() - .block(yellBlock) - .register(); + .block(yellBlock) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), + sampleWithYellBlock, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); @@ -840,10 +856,10 @@ public void a_block_processor_instance_should_be_executed_when_registered_block_ public void a_block_processor_should_be_executed_when_registered_listing_block_is_found_in_document() { this.asciidoctor.createGroup() - .block("yell", YellStaticListingBlock.class) - .register(); + .block("yell", YellStaticListingBlock.class) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-listing-block.ad"), + sampleWithYellListingBlock, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -856,15 +872,15 @@ public void a_block_processor_should_be_executed_when_registered_listing_block_i @Test public void a_block_processor_instance_should_be_executed_when_registered_listing_block_is_found_in_document() { - Map config = new HashMap(); + Map config = new HashMap<>(); config.put(Contexts.KEY, Arrays.asList(Contexts.LISTING)); config.put(ContentModel.KEY, ContentModel.SIMPLE); YellBlock yellBlock = new YellBlock("yell", config); this.asciidoctor.createGroup() - .block(yellBlock) - .register(); + .block(yellBlock) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-listing-block.ad"), + sampleWithYellListingBlock, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); @@ -874,25 +890,27 @@ public void a_block_processor_instance_should_be_executed_when_registered_listin } @Test - public void should_create_toc_with_treeprocessor() { + public void should_create_toc_with_treeprocessor( + @ClasspathResource("documentwithtoc.adoc") File documentWithToc) { + this.asciidoctor.createGroup() - .treeprocessor(new Treeprocessor() { - @Override - public Document process(Document document) { - List blocks=document.getBlocks(); - for (StructuralNode block : blocks) { - for (StructuralNode block2 : block.getBlocks()) { - if(block2 instanceof Section) - System.out.println(((Section) block2).getId()); + .treeprocessor(new Treeprocessor() { + @Override + public Document process(Document document) { + List blocks = document.getBlocks(); + for (StructuralNode block : blocks) { + for (StructuralNode block2 : block.getBlocks()) { + if (block2 instanceof Section) + System.out.println(block2.getId()); + } } + return document; } - return document; - } - }) - .register(); + }) + .register(); String content = asciidoctor.convertFile( - classpath.getResource("documentwithtoc.adoc"), + documentWithToc, Options.builder().standalone(true).toFile(false).safe(SafeMode.UNSAFE).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -902,21 +920,22 @@ public Document process(Document document) { assertThat(elements.size(), is(1)); } - public void should_unregister_postprocessor() throws IOException { + @Test + public void should_unregister_postprocessor() throws IOException { // Given: A registered Postprocessor ExtensionGroup extensionGroup = asciidoctor.createGroup(UUID.randomUUID().toString()) - .postprocessor(CustomFooterPostProcessor.class); + .postprocessor(CustomFooterPostProcessor.class); // When: I render a document without registering the ExtensionGroup - File renderedFile = testFolder.newFile("rendersample.html"); - { + File renderedFile = new File(tempFolder, "rendersample.html"); + { Options options = Options.builder() .inPlace(false) .toFile(renderedFile) - .safe(SafeMode.UNSAFE).build(); + .safe(SafeMode.UNSAFE).build(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(simpleDocument, options); // Then: it is invoked org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -928,9 +947,9 @@ public void should_unregister_postprocessor() throws IOException { { extensionGroup.register(); Options options = Options.builder().inPlace(false).toFile(renderedFile) - .safe(SafeMode.UNSAFE).build(); + .safe(SafeMode.UNSAFE).build(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(simpleDocument, options); // Then: it is invoked org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -939,15 +958,15 @@ public void should_unregister_postprocessor() throws IOException { } // When: I unregister the Postprocessor and render again with the same Asciidoctor instance { - extensionGroup.unregister();; + extensionGroup.unregister(); - File renderedFile2 = testFolder.newFile("rendersample2.html"); + File renderedFile2 = new File(tempFolder, "rendersample2.html"); Options options2 = Options.builder() .inPlace(false) .toFile(renderedFile2) .safe(SafeMode.UNSAFE) .build(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options2); + asciidoctor.convertFile(simpleDocument, options2); org.jsoup.nodes.Document doc2 = Jsoup.parse(renderedFile2, "UTF-8"); Element footer2 = doc2.getElementById("footer-text"); @@ -958,7 +977,7 @@ public void should_unregister_postprocessor() throws IOException { @Test public void should_unregister_block_processor() { - Map config = new HashMap(); + Map config = new HashMap<>(); config.put("contexts", Arrays.asList(":paragraph")); config.put("content_model", ":simple"); YellBlock yellBlock = new YellBlock("yell", config); @@ -967,8 +986,8 @@ public void should_unregister_block_processor() { { String contentWithoutBlock = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), - Options.builder().toFile(false).build()); + sampleWithYellBlock, + Options.builder().toFile(false).build()); org.jsoup.nodes.Document docWithoutBlock = Jsoup.parse(contentWithoutBlock, "UTF-8"); Elements elementsWithoutBlock = docWithoutBlock.getElementsByClass("paragraph"); assertThat(elementsWithoutBlock.size(), is(1)); @@ -978,8 +997,8 @@ public void should_unregister_block_processor() { { extensionGroup.register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), - Options.builder().toFile(false).build()); + sampleWithYellBlock, + Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); assertThat(elements.size(), is(1)); @@ -988,8 +1007,8 @@ public void should_unregister_block_processor() { { extensionGroup.unregister(); String contentWithoutBlock = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), - Options.builder().toFile(false).build()); + sampleWithYellBlock, + Options.builder().toFile(false).build()); org.jsoup.nodes.Document docWithoutBlock = Jsoup.parse(contentWithoutBlock, "UTF-8"); Elements elementsWithoutBlock = docWithoutBlock.getElementsByClass("paragraph"); assertThat(elementsWithoutBlock.size(), is(1)); diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionIsRegistered.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionIsRegistered.java index c4bfd9584..d0ce97a29 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionIsRegistered.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionIsRegistered.java @@ -9,71 +9,85 @@ import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.log.LogRecord; import org.asciidoctor.log.TestLogHandlerService; -import org.asciidoctor.util.ClasspathResources; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.asciidoctor.util.TestHttpServer; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; import org.jsoup.Jsoup; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; - -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; + +import java.io.*; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.net.URLConnection; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; import java.util.logging.LogManager; import java.util.stream.Collectors; import static org.asciidoctor.OptionsBuilder.options; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.startsWith; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; +import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasSize; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; +import static org.junit.Assert.*; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenJavaExtensionIsRegistered { public static final String ASCIIDOCTORCLASS_PREFIX = "module AsciidoctorJ include_package 'org.asciidoctor'"; - @ArquillianResource - private ClasspathResources classpath; + @AsciidoctorInstance(scope = PER_METHOD) + private Asciidoctor asciidoctor; - @ArquillianResource - public TemporaryFolder testFolder; + @ClasspathResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb") + private File asciidoctorRubyClass; - private Asciidoctor asciidoctor; + @ClasspathResource("rendersample.asciidoc") + private File renderSample; + + @ClasspathResource("simple.adoc") + private File simpleDocument; + + @ClasspathResource("changeattribute.adoc") + private File changeAttribute; + + @ClasspathResource("sample-with-terminal-command.ad") + private File sampleWithTerminalCommand; + + @ClasspathResource("sample-with-uri-include.ad") + private File sampleWithUriInclude; + + @ClasspathResource("sample-with-yell-block.ad") + private File sampleWithYellBlock; + + @ClasspathResource("sample-with-gist-macro.ad") + private File sampleWithGistMacro; + + @ClasspathResource("sample-with-man-link.ad") + private File sampleWithManLink; - @Before + @ClasspathResource("sample-with-yell-listing-block.ad") + private File sampleWithYellListingBlock; + + @TempDir + public File tempFolder; + + + @BeforeEach public void before() { - asciidoctor = Asciidoctor.Factory.create(); TestLogHandlerService.clear(); } - @After + @AfterEach public void tearDown() throws IOException { if (TestHttpServer.getInstance() != null) { TestHttpServer.getInstance().stop(); @@ -129,14 +143,14 @@ public boolean handles(String target) { @Test public void an_inner_class_should_be_registered() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.includeProcessor(new RubyIncludeSource(new HashMap<>())); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -150,7 +164,7 @@ public void an_inner_class_should_be_registered() { @Test public void an_inner_anonymous_class_should_be_registered() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); @@ -194,7 +208,7 @@ public boolean handles(String target) { }); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -212,7 +226,7 @@ public void a_docinfoprocessor_should_be_executed_and_add_meta_in_header_by_defa javaExtensionRegistry.docinfoProcessor(MetaRobotsDocinfoProcessor.class.getCanonicalName()); String content = asciidoctor.convertFile( - classpath.getResource("simple.adoc"), + simpleDocument, options().standalone(true).safe(SafeMode.SERVER).toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -232,7 +246,7 @@ public void a_docinfoprocessor_should_be_executed_and_add_meta_in_footer() { javaExtensionRegistry.docinfoProcessor(metaRobotsDocinfoProcessor); String content = asciidoctor.convertFile( - classpath.getResource("simple.adoc"), + simpleDocument, options().standalone(true).safe(SafeMode.SERVER).toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -250,7 +264,7 @@ public void a_preprocessor_should_be_executed_before_document_is_rendered() { javaExtensionRegistry.preprocessor(ChangeAttributeValuePreprocessor.class); String content = asciidoctor.convertFile( - classpath.getResource("changeattribute.adoc"), + changeAttribute, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -267,7 +281,7 @@ public void a_preprocessor_as_string_should_be_executed_before_document_is_rende javaExtensionRegistry.preprocessor("org.asciidoctor.extension.ChangeAttributeValuePreprocessor"); String content = asciidoctor.convertFile( - classpath.getResource("changeattribute.adoc"), + changeAttribute, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -284,7 +298,7 @@ public void a_preprocessor_instance_should_be_executed_before_document_is_render javaExtensionRegistry.preprocessor(new ChangeAttributeValuePreprocessor(new HashMap<>())); String content = asciidoctor.convertFile( - classpath.getResource("changeattribute.adoc"), + changeAttribute, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -300,14 +314,14 @@ public void a_postprocessor_as_string_should_be_executed_after_document_is_rende javaExtensionRegistry.postprocessor("org.asciidoctor.extension.CustomFooterPostProcessor"); - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) .safe(SafeMode.UNSAFE) .build(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -322,14 +336,14 @@ public void a_postprocessor_should_be_executed_after_document_is_rendered() thro javaExtensionRegistry.postprocessor(CustomFooterPostProcessor.class); - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) .safe(SafeMode.UNSAFE) .build(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -344,13 +358,13 @@ public void a_postprocessor_instance_should_be_executed_after_document_is_render javaExtensionRegistry.postprocessor(new CustomFooterPostProcessor(new HashMap<>())); - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) .safe(SafeMode.UNSAFE).get(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -361,14 +375,14 @@ public void a_postprocessor_instance_should_be_executed_after_document_is_render @Test public void a_include_processor_as_string_should_be_executed_when_include_macro_is_found() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.includeProcessor("org.asciidoctor.extension.UriIncludeProcessor"); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -382,14 +396,14 @@ public void a_include_processor_as_string_should_be_executed_when_include_macro_ @Test public void a_include_processor_should_be_executed_when_include_macro_is_found() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.includeProcessor(UriIncludeProcessor.class); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -403,14 +417,14 @@ public void a_include_processor_should_be_executed_when_include_macro_is_found() @Test public void a_include_instance_processor_should_be_executed_when_include_macro_is_found() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.includeProcessor(new UriIncludeProcessor(new HashMap<>())); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -422,13 +436,14 @@ public void a_include_instance_processor_should_be_executed_when_include_macro_i } @Test - public void a_include_processor_should_only_handle_its_handles() { + public void a_include_processor_should_only_handle_its_handles( + @ClasspathResource("sample-with-include.ad") File sampleWithInclude) { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.includeProcessor(UriIncludeProcessor.class); - String content = asciidoctor.convertFile(classpath.getResource("sample-with-include.ad"), + String content = asciidoctor.convertFile(sampleWithInclude, Options.builder().toFile(false).build()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -440,13 +455,14 @@ public void a_include_processor_should_only_handle_its_handles() { } @Test - public void a_include_processor_can_handle_positional_attrs() { + public void a_include_processor_can_handle_positional_attrs( + @ClasspathResource("sample-with-include-pos-attrs.ad") File sampleWithIncludePosAttrs) { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.includeProcessor(PositionalAttrsIncludeProcessor.class); - String content = asciidoctor.convertFile(classpath.getResource("sample-with-include-pos-attrs.ad"), + String content = asciidoctor.convertFile(sampleWithIncludePosAttrs, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -465,7 +481,7 @@ public void a_treeprocessor_should_be_executed_in_document() { javaExtensionRegistry.treeprocessor(TerminalCommandTreeprocessor.class); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-terminal-command.ad"), + sampleWithTerminalCommand, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -479,13 +495,15 @@ public void a_treeprocessor_should_be_executed_in_document() { } @Test - public void a_treeprocessor_and_blockmacroprocessor_should_be_executed_in_document() { + public void a_treeprocessor_and_blockmacroprocessor_should_be_executed_in_document( + @ClasspathResource("sample-with-terminal-command-and-gist-macro.ad") File sampleWithTerminalCommand) { + this.asciidoctor.javaExtensionRegistry() .treeprocessor(TerminalCommandTreeprocessor.class) .blockMacro("gist", GistMacro.class); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-terminal-command-and-gist-macro.ad"), + sampleWithTerminalCommand, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -508,7 +526,7 @@ public void a_treeprocessor_as_string_should_be_executed_in_document() { javaExtensionRegistry.treeprocessor("org.asciidoctor.extension.TerminalCommandTreeprocessor"); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-terminal-command.ad"), + sampleWithTerminalCommand, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -529,7 +547,7 @@ public void a_treeprocessor_instance_should_be_executed_in_document() { javaExtensionRegistry.treeprocessor(new TerminalCommandTreeprocessor(new HashMap<>())); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-terminal-command.ad"), + sampleWithTerminalCommand, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -543,21 +561,22 @@ public void a_treeprocessor_instance_should_be_executed_in_document() { } @Test - @Ignore - public void extensions_should_be_correctly_added_using_extension_registry() throws IOException { + @Disabled + public void extensions_should_be_correctly_added_using_extension_registry( + @ClasspathResource("arrows-and-boxes-example.ad") File arrowsAndBoxesExample) throws IOException { // To avoid registering the same extension over and over for all tests, // service is instantiated manually. new ArrowsAndBoxesExtension().register(asciidoctor); - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) .safe(SafeMode.UNSAFE) .build(); - asciidoctor.convertFile(classpath.getResource("arrows-and-boxes-example.ad"), options); + asciidoctor.convertFile(arrowsAndBoxesExample, options); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -580,7 +599,7 @@ public void a_block_macro_extension_should_be_executed_when_macro_is_detected() javaExtensionRegistry.blockMacro("gist", GistMacro.class); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-gist-macro.ad"), + sampleWithGistMacro, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -597,7 +616,7 @@ public void a_block_macro_extension_instance_should_be_executed_when_macro_is_de javaExtensionRegistry.blockMacro(new GistMacro("gist", new HashMap<>())); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-gist-macro.ad"), + sampleWithGistMacro, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -614,7 +633,7 @@ public void a_block_macro_as_string_extension_should_be_executed_when_macro_is_d javaExtensionRegistry.blockMacro("gist", "org.asciidoctor.extension.GistMacro"); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-gist-macro.ad"), + sampleWithGistMacro, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -634,7 +653,7 @@ public void a_block_macro_as_instance_extension_should_be_executed_when_macro_is javaExtensionRegistry.blockMacro(new GistMacro("gist", options)); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-gist-macro.ad"), + sampleWithGistMacro, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -651,7 +670,7 @@ public void an_inline_macro_as_string_extension_should_be_executed_when_an_inlin javaExtensionRegistry.inlineMacro("man", "org.asciidoctor.extension.ManpageMacro"); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-man-link.ad"), + sampleWithManLink, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -670,7 +689,7 @@ public void an_inline_macro_extension_should_be_executed_when_an_inline_macro_is javaExtensionRegistry.inlineMacro("man", ManpageMacro.class); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-man-link.ad"), + sampleWithManLink, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -693,7 +712,7 @@ public void an_inline_macro_as_instance_extension_should_be_executed_when_regexp javaExtensionRegistry.inlineMacro(inlineMacroProcessor); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-man-link.ad"), + sampleWithManLink, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -717,7 +736,7 @@ public void an_inline_macro_as_instance_extension_should_not_be_executed_when_re javaExtensionRegistry.inlineMacro(inlineMacroProcessor); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-man-link.ad"), + sampleWithManLink, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -739,7 +758,7 @@ public void an_inline_macro_as_instance_extension_should_be_executed_when_an_inl javaExtensionRegistry.inlineMacro(inlineMacroProcessor); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-man-link.ad"), + sampleWithManLink, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -780,14 +799,14 @@ public void should_unregister_all_current_registered_extensions() throws IOExcep javaExtensionRegistry.postprocessor(CustomFooterPostProcessor.class); - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) .safe(SafeMode.UNSAFE).get(); asciidoctor.unregisterAllExtensions(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -802,7 +821,7 @@ public void a_block_processor_as_string_should_be_executed_when_registered_block javaExtensionRegistry.block("yell", "org.asciidoctor.extension.YellStaticBlock"); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), + sampleWithYellBlock, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -819,7 +838,7 @@ public void a_block_processor_should_be_executed_when_registered_block_is_found_ javaExtensionRegistry.block("yell", YellStaticBlock.class); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), + sampleWithYellBlock, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -837,7 +856,7 @@ public void a_block_processor_class_should_be_executed_twice() { javaExtensionRegistry.block("yell", YellStaticBlock.class); for (int i = 0; i < 2; i++) { String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), + sampleWithYellBlock, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -858,7 +877,7 @@ public void a_block_processor_instance_should_be_executed_when_registered_block_ YellBlock yellBlock = new YellBlock("yell", config); javaExtensionRegistry.block(yellBlock); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), + sampleWithYellBlock, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); @@ -880,7 +899,7 @@ public void a_block_processor_instance_should_be_executed_twice() { for (int i = 0; i < 2; i++) { String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), + sampleWithYellBlock, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); @@ -892,7 +911,7 @@ public void a_block_processor_instance_should_be_executed_twice() { @Test public void a_include_processor_class_should_be_executed_twice() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); @@ -900,7 +919,7 @@ public void a_include_processor_class_should_be_executed_twice() { for (int i = 0; i < 2; i++) { String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -914,7 +933,7 @@ public void a_include_processor_class_should_be_executed_twice() { @Test public void a_include_processor_instance_should_be_executed_twice() { - TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/jruby/internal/asciidoctorclass.rb"))); + TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", asciidoctorRubyClass)); JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); @@ -922,7 +941,7 @@ public void a_include_processor_instance_should_be_executed_twice() { for (int i = 0; i < 2; i++) { String content = asciidoctor.convertFile( - classpath.getResource("sample-with-uri-include.ad"), + sampleWithUriInclude, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -940,7 +959,7 @@ public void a_block_processor_should_be_executed_when_registered_listing_block_i javaExtensionRegistry.block("yell", YellStaticListingBlock.class); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-listing-block.ad"), + sampleWithYellListingBlock, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -961,7 +980,7 @@ public void a_block_processor_instance_should_be_executed_when_registered_listin YellBlock yellBlock = new YellBlock("yell", config); javaExtensionRegistry.block(yellBlock); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-listing-block.ad"), + sampleWithYellListingBlock, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); @@ -971,7 +990,9 @@ public void a_block_processor_instance_should_be_executed_when_registered_listin } @Test - public void should_create_toc_with_treeprocessor() { + public void should_create_toc_with_treeprocessor( + @ClasspathResource("documentwithtoc.adoc") File documentWithToc) { + asciidoctor.javaExtensionRegistry().treeprocessor(new Treeprocessor() { @Override public org.asciidoctor.ast.Document process(org.asciidoctor.ast.Document document) { @@ -987,7 +1008,7 @@ public org.asciidoctor.ast.Document process(org.asciidoctor.ast.Document documen }); String content = asciidoctor.convertFile( - classpath.getResource("documentwithtoc.adoc"), + documentWithToc, options().standalone(true).toFile(false).safe(SafeMode.UNSAFE).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); @@ -1005,14 +1026,14 @@ public void should_unregister_postprocessor() throws IOException { // When: I render a document without registering the ExtensionGroup { - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) .safe(SafeMode.UNSAFE) .build(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); // Then: it is invoked org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -1023,14 +1044,14 @@ public void should_unregister_postprocessor() throws IOException { // When: I register the ExtensionGroup and render a document { extensionGroup.register(); - File renderedFile = testFolder.newFile("rendersample.html"); + File renderedFile = new File(tempFolder, "rendersample.html"); Options options = Options.builder() .inPlace(false) .toFile(renderedFile) .safe(SafeMode.UNSAFE) .build(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); + asciidoctor.convertFile(renderSample, options); // Then: it is invoked org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); @@ -1042,13 +1063,13 @@ public void should_unregister_postprocessor() throws IOException { extensionGroup.unregister(); ; - File renderedFile2 = new File(testFolder.getRoot(), "rendersample2.html"); + File renderedFile2 = new File(tempFolder, "rendersample2.html"); Options options2 = Options.builder() .inPlace(false) .toFile(renderedFile2) .safe(SafeMode.UNSAFE) .build(); - asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options2); + asciidoctor.convertFile(renderSample, options2); org.jsoup.nodes.Document doc2 = Jsoup.parse(renderedFile2, "UTF-8"); Element footer2 = doc2.getElementById("footer-text"); @@ -1068,7 +1089,7 @@ public void should_unregister_block_processor() { { String contentWithoutBlock = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), + sampleWithYellBlock, options().toFile(false).get()); org.jsoup.nodes.Document docWithoutBlock = Jsoup.parse(contentWithoutBlock, "UTF-8"); Elements elementsWithoutBlock = docWithoutBlock.getElementsByClass("paragraph"); @@ -1079,7 +1100,7 @@ public void should_unregister_block_processor() { { extensionGroup.register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), + sampleWithYellBlock, options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); @@ -1089,7 +1110,7 @@ public void should_unregister_block_processor() { { extensionGroup.unregister(); String contentWithoutBlock = asciidoctor.convertFile( - classpath.getResource("sample-with-yell-block.ad"), + sampleWithYellBlock, options().toFile(false).get()); org.jsoup.nodes.Document docWithoutBlock = Jsoup.parse(contentWithoutBlock, "UTF-8"); Elements elementsWithoutBlock = docWithoutBlock.getElementsByClass("paragraph"); @@ -1106,8 +1127,8 @@ public static class TestBlock extends BlockProcessor { public Object process(StructuralNode parent, Reader reader, Map attributes) { assertSame(asciidoctor, unwrap(Asciidoctor.class)); List processed = reader.readLines().stream() - .map(String::toLowerCase) - .collect(Collectors.toList()); + .map(String::toLowerCase) + .collect(Collectors.toList()); return createBlock(parent, "paragraph", processed, attributes, new HashMap<>()); } @@ -1129,8 +1150,8 @@ public void a_extension_registered_as_class_can_get_its_asciidoctor_instance() { javaExtensionRegistry.block("quiet", TestBlock.class); String content = asciidoctor.convert( - "[quiet]\nHello World", - options().toFile(false).get()); + "[quiet]\nHello World", + options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); assertThat(elements.size(), is(1)); @@ -1150,8 +1171,8 @@ public void a_extension_registered_as_instance_can_get_its_asciidoctor_instance( javaExtensionRegistry.block(new TestBlock()); String content = asciidoctor.convert( - "[quiet]\nHello Again", - options().toFile(false).get()); + "[quiet]\nHello Again", + options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); assertThat(elements.size(), is(1)); @@ -1160,5 +1181,4 @@ public void a_extension_registered_as_instance_can_get_its_asciidoctor_instance( TestBlock.asciidoctor = null; } - } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenReaderIsManipulatedInExtension.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenReaderIsManipulatedInExtension.java index 165a081be..6d158631d 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenReaderIsManipulatedInExtension.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenReaderIsManipulatedInExtension.java @@ -1,79 +1,63 @@ package org.asciidoctor.extension; import org.asciidoctor.Asciidoctor; -import org.asciidoctor.arquillian.api.Unshared; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import java.util.Map; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; +import static org.assertj.core.api.Assertions.assertThat; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenReaderIsManipulatedInExtension { - @ArquillianResource - private ClasspathResources classpath; - - @ArquillianResource(Unshared.class) + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; + @ClasspathResource("rendersample.asciidoc") + private File renderSample; + + @Test public void currentLineNumberShouldBeReturned() { - - JavaExtensionRegistry javaExtensionRegistry = asciidoctor - .javaExtensionRegistry(); + var javaExtensionRegistry = asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.preprocessor(NumberLinesPreprocessor.class); - File inputFile = classpath.getResource("rendersample.asciidoc"); - asciidoctor.convertFile(inputFile, Map.of()); + asciidoctor.convertFile(renderSample, Map.of()); - File outpuFile = new File(inputFile.getParent(), "rendersample.asciidoc"); - assertThat(outpuFile.exists(), is(true)); + File outpuFile = new File(renderSample.getParent(), "rendersample.asciidoc"); + assertThat(outpuFile).exists(); } @Test public void hasMoreLinesShouldBeReturned() { - - JavaExtensionRegistry javaExtensionRegistry = asciidoctor - .javaExtensionRegistry(); + var javaExtensionRegistry = asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.preprocessor(HasMoreLinesPreprocessor.class); - asciidoctor.convertFile( - classpath.getResource("rendersample.asciidoc"), - Map.of()); - - File inputFile = classpath.getResource("rendersample.asciidoc"); - asciidoctor.convertFile(inputFile, Map.of()); + asciidoctor.convertFile(renderSample, Map.of()); - File outpuFile = new File(inputFile.getParent(), "rendersample.asciidoc"); - assertThat(outpuFile.exists(), is(true)); + File outpuFile = new File(renderSample.getParent(), "rendersample.asciidoc"); + assertThat(outpuFile).exists(); } @Test public void isNextLineEmptyShouldBeReturned() { - - JavaExtensionRegistry javaExtensionRegistry = asciidoctor - .javaExtensionRegistry(); + var javaExtensionRegistry = asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.preprocessor(NextLineEmptyPreprocessor.class); - asciidoctor.convertFile( - classpath.getResource("rendersample.asciidoc"), - Map.of()); - - File inputFile = classpath.getResource("rendersample.asciidoc"); - asciidoctor.convertFile(inputFile, Map.of()); + asciidoctor.convertFile(renderSample, Map.of()); - File outpuFile = new File(inputFile.getParent(), "rendersample.asciidoc"); - assertThat(outpuFile.exists(), is(true)); + File outpuFile = new File(renderSample.getParent(), "rendersample.asciidoc"); + assertThat(outpuFile).exists(); } } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionGroupIsRegistered.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionGroupIsRegistered.java index 7dd2bd1e3..6915551eb 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionGroupIsRegistered.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionGroupIsRegistered.java @@ -2,52 +2,56 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.SafeMode; -import org.asciidoctor.arquillian.api.Unshared; import org.asciidoctor.jruby.internal.JRubyAsciidoctor; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.nodes.TextNode; import org.jsoup.select.Elements; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import java.io.File; import java.util.Iterator; import static java.util.Collections.singletonList; import static org.asciidoctor.OptionsBuilder.options; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenRubyExtensionGroupIsRegistered { - @ArquillianResource - private ClasspathResources classpath; - - @ArquillianResource(Unshared.class) + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; + @ClasspathResource("sample-with-ruby-yell-block.ad") + private File sampleWithRubyYellBlock; + + @ClasspathResource("ruby-extensions") + private File rubyExtensionsDir; + @Test public void ruby_extension_should_be_registered_with_explicit_block_name() { - + this.asciidoctor.createGroup() - .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")).rubyBlock("rubyyell", "YellRubyBlock") - .register(); + .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")) + .rubyBlock("rubyyell", "YellRubyBlock") + .register(); String content = asciidoctor.convert( - "= Block Yell Example\n" + - "\n" + - "content\n" + - "\n" + - "[rubyyell]\n" + - "The time is now. Get a move on.", + "= Block Yell Example\n" + + "\n" + + "content\n" + + "\n" + + "[rubyyell]\n" + + "The time is now. Get a move on.", options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); @@ -60,16 +64,17 @@ public void ruby_extension_should_be_registered_with_explicit_block_name() { public void ruby_extension_should_be_registered_with_implicit_block_name() { this.asciidoctor.createGroup() - .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")).rubyBlock("YellRubyBlock") - .register(); + .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")) + .rubyBlock("YellRubyBlock") + .register(); String content = asciidoctor.convert( - "= Block Yell Example\n" + - "\n" + - "content\n" + - "\n" + - "[yell]\n" + - "The time is now. Get a move on.", + "= Block Yell Example\n" + + "\n" + + "content\n" + + "\n" + + "[yell]\n" + + "The time is now. Get a move on.", options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); @@ -82,13 +87,13 @@ public void ruby_extension_should_be_registered_with_implicit_block_name() { public void ruby_extension_should_be_unregistered() { ExtensionGroup extensionGroup = this.asciidoctor.createGroup() - .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")) - .rubyBlock("rubyyell", "YellRubyBlock"); + .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")) + .rubyBlock("rubyyell", "YellRubyBlock"); { String contentWithoutBlock = asciidoctor.convertFile( - classpath.getResource("sample-with-ruby-yell-block.ad"), - options().toFile(false).get()); + sampleWithRubyYellBlock, + options().toFile(false).get()); Document docWithoutBlock = Jsoup.parse(contentWithoutBlock, "UTF-8"); Elements elementsWithoutBlock = docWithoutBlock.getElementsByClass("paragraph"); @@ -98,8 +103,8 @@ public void ruby_extension_should_be_unregistered() { { extensionGroup.register(); String content = asciidoctor.convertFile( - classpath.getResource("sample-with-ruby-yell-block.ad"), - options().toFile(false).get()); + sampleWithRubyYellBlock, + options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); @@ -110,8 +115,8 @@ public void ruby_extension_should_be_unregistered() { extensionGroup.unregister(); String contentWithoutBlock = asciidoctor.convertFile( - classpath.getResource("sample-with-ruby-yell-block.ad"), - options().toFile(false).get()); + sampleWithRubyYellBlock, + options().toFile(false).get()); Document docWithoutBlock = Jsoup.parse(contentWithoutBlock, "UTF-8"); Elements elementsWithoutBlock = docWithoutBlock.getElementsByClass("paragraph"); @@ -125,13 +130,14 @@ public void ruby_extension_should_be_unregistered() { public void ruby_block_macro_processor_should_be_registered_with_block_name() { this.asciidoctor.createGroup() - .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/gist-block-macro.rb")).rubyBlockMacro("mygist", "GistBlockMacro") - .register(); + .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/gist-block-macro.rb")) + .rubyBlockMacro("mygist", "GistBlockMacro") + .register(); String content = asciidoctor.convert( - ".My Gist\n" + - "mygist::123456[]", - options().toFile(false).get()); + ".My Gist\n" + + "mygist::123456[]", + options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByTag("script"); @@ -144,13 +150,14 @@ public void ruby_block_macro_processor_should_be_registered_with_block_name() { public void ruby_block_macro_processor_should_be_registered_with_implicit_block_name() { this.asciidoctor.createGroup() - .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/gist-block-macro.rb")).rubyBlockMacro("GistBlockMacro") - .register(); + .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/gist-block-macro.rb")) + .rubyBlockMacro("GistBlockMacro") + .register(); String content = asciidoctor.convert( - ".My Gist\n" + - "gist::42[]", - options().toFile(false).get()); + ".My Gist\n" + + "gist::42[]", + options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByTag("script"); @@ -163,14 +170,15 @@ public void ruby_block_macro_processor_should_be_registered_with_implicit_block_ public void ruby_inline_macro_processor_should_be_registered_with_macro_name() { this.asciidoctor.createGroup() - .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/man-inline-macro.rb")).rubyInlineMacro("myman", "ManInlineMacro") - .register(); + .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/man-inline-macro.rb")) + .rubyInlineMacro("myman", "ManInlineMacro") + .register(); String content = asciidoctor.convert( - "= Man Inline Macro Extension\n" + - "\n" + - "See myman:gittutorial[7] to get started.", - options().toFile(false).get()); + "= Man Inline Macro Extension\n" + + "\n" + + "See myman:gittutorial[7] to get started.", + options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByTag("a"); @@ -183,14 +191,15 @@ public void ruby_inline_macro_processor_should_be_registered_with_macro_name() { public void ruby_inline_macro_processor_should_be_registered_with_implicit_macro_name() { this.asciidoctor.createGroup() - .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/man-inline-macro.rb")).rubyInlineMacro("ManInlineMacro") - .register(); + .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/man-inline-macro.rb")) + .rubyInlineMacro("ManInlineMacro") + .register(); String content = asciidoctor.convert( - "= Man Inline Macro Extension\n" + - "\n" + - "See man:dockertutorial[7] to get started.", - options().toFile(false).get()); + "= Man Inline Macro Extension\n" + + "\n" + + "See man:dockertutorial[7] to get started.", + options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByTag("a"); @@ -203,16 +212,16 @@ public void ruby_inline_macro_processor_should_be_registered_with_implicit_macro public void ruby_treeprocessor_should_be_registered() { this.asciidoctor.createGroup() - .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/shell-session-tree-processor.rb")) - .rubyTreeprocessor("ShellSessionTreeProcessor") - .register(); + .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/shell-session-tree-processor.rb")) + .rubyTreeprocessor("ShellSessionTreeProcessor") + .register(); String content = this.asciidoctor.convert( - " $ echo \"Hello, World!\"\n" + - " > Hello, World!\n" + - "\n" + - " $ gem install asciidoctor", - options().toFile(false).get()); + " $ echo \"Hello, World!\"\n" + + " > Hello, World!\n" + + "\n" + + " $ gem install asciidoctor", + options().toFile(false).get()); final Document document = Jsoup.parse(content); final TextNode commandElement = document.getElementsByClass("command").get(0).textNodes().get(0); @@ -224,16 +233,16 @@ public void ruby_treeprocessor_should_be_registered() { @Test public void ruby_postprocessor_should_be_registered() { - final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath(); + final String rubyExtPath = rubyExtensionsDir.getAbsolutePath(); final Asciidoctor asciidoctor = JRubyAsciidoctor.create(singletonList(rubyExtPath)); asciidoctor.createGroup() - .requireRubyLibrary("xml-entity-postprocessor.rb") - .rubyPostprocessor("XmlEntityPostprocessor") - .register(); + .requireRubyLibrary("xml-entity-postprocessor.rb") + .rubyPostprocessor("XmlEntityPostprocessor") + .register(); String content = asciidoctor.convert( - "Read §2 and it'll all be clear.", - options().toFile(false).get()); + "Read §2 and it'll all be clear.", + options().toFile(false).get()); System.out.println(content); assertThat(content, containsString("Read §2 and it'll all be clear.")); @@ -242,84 +251,83 @@ public void ruby_postprocessor_should_be_registered() { @Test public void ruby_includeprocessor_should_be_registered() { asciidoctor.createGroup() - .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/response-include-processor.rb")) - .rubyIncludeProcessor("ResponseIncludeProcessor") - .register(); + .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/response-include-processor.rb")) + .rubyIncludeProcessor("ResponseIncludeProcessor") + .register(); String content = asciidoctor.convert( - "The response to everything is\n" + - "\n" + - "include::response[]" + - "", - options().toFile(false).safe(SafeMode.SAFE).get()); + "The response to everything is\n" + + "\n" + + "include::response[]" + + "", + options().toFile(false).safe(SafeMode.SAFE).get()); final Document document = Jsoup.parse(content); assertThat( - document.getElementsByClass("paragraph").get(1).getElementsByTag("p").get(0).toString(), - is("

42

")); + document.getElementsByClass("paragraph").get(1).getElementsByTag("p").get(0).toString(), + is("

42

")); } - @Test public void ruby_preprocessor_should_be_registered() { this.asciidoctor.createGroup() - .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/front-matter-preprocessor.rb")) - .rubyPreprocessor("FrontMatterPreprocessor") - .register(); + .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/front-matter-preprocessor.rb")) + .rubyPreprocessor("FrontMatterPreprocessor") + .register(); String content = this.asciidoctor.convert( - "---\n" + - "tags: [announcement, website]\n" + - "---\n" + - "= Document Title\n" + - "\n" + - "content\n" + - "\n" + - "[subs=\"attributes,specialcharacters\"]\n" + - ".Captured front matter\n" + - "....\n" + "---\n" + - "{front-matter}\n" + - "---\n" + - "....", - options().toFile(false).get()); + "tags: [announcement, website]\n" + + "---\n" + + "= Document Title\n" + + "\n" + + "content\n" + + "\n" + + "[subs=\"attributes,specialcharacters\"]\n" + + ".Captured front matter\n" + + "....\n" + + "---\n" + + "{front-matter}\n" + + "---\n" + + "....", + options().toFile(false).get()); final Document document = Jsoup.parse(content); final Element contentElement = document.getElementsByClass("content").get(0); final Element literalElement = contentElement.getElementsByTag("pre").get(0); assertThat(literalElement.toString().replace("\r", ""), - containsString("---\n" + - "tags: [announcement, website]\n" + - "---")); + containsString("---\n" + + "tags: [announcement, website]\n" + + "---")); } @Test public void ruby_docinfoprocessor_should_be_registered() { - final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath(); + final String rubyExtPath = rubyExtensionsDir.getAbsolutePath(); final Asciidoctor asciidoctor = JRubyAsciidoctor.create(singletonList(rubyExtPath)); asciidoctor.createGroup() - .requireRubyLibrary("view-result-docinfoprocessor.rb") - .rubyDocinfoProcessor("ViewResultDocinfoProcessor") - .register(); + .requireRubyLibrary("view-result-docinfoprocessor.rb") + .rubyDocinfoProcessor("ViewResultDocinfoProcessor") + .register(); String content = asciidoctor.convert( - "= View Result Sample \n" + - " \n" + - ".This will have a link next to it\n" + - "---- \n" + - "* always displayed \n" + - "* always displayed 2 \n" + - "---- \n" + - " \n" + - "[.result] \n" + - "==== \n" + - "* hidden till clicked \n" + - "* hidden till clicked 2 \n" + - "==== ", - options().toFile(false).safe(SafeMode.SAFE).standalone(true).get()); + "= View Result Sample \n" + + " \n" + + ".This will have a link next to it\n" + + "---- \n" + + "* always displayed \n" + + "* always displayed 2 \n" + + "---- \n" + + " \n" + + "[.result] \n" + + "==== \n" + + "* hidden till clicked \n" + + "* hidden till clicked 2 \n" + + "==== ", + options().toFile(false).safe(SafeMode.SAFE).standalone(true).get()); final Document document = Jsoup.parse(content); final Iterator elems = document.getElementsByTag("style").iterator(); diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionIsRegistered.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionIsRegistered.java index fc10f0bdd..47d6cfca3 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionIsRegistered.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionIsRegistered.java @@ -1,72 +1,77 @@ package org.asciidoctor.extension; import org.asciidoctor.SafeMode; -import org.asciidoctor.arquillian.api.Unshared; import org.asciidoctor.jruby.AsciidoctorJRuby; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.nodes.TextNode; import org.jsoup.select.Elements; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import java.io.File; import java.util.Iterator; import static java.util.Collections.singletonList; import static org.asciidoctor.OptionsBuilder.options; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenRubyExtensionIsRegistered { - @ArquillianResource - private ClasspathResources classpath; - - @ArquillianResource(Unshared.class) + @AsciidoctorInstance(scope = PER_METHOD) private AsciidoctorJRuby asciidoctor; + @ClasspathResource("ruby-extensions") + private File rubyExtensionsDir; + + @Test public void ruby_block_processor_should_be_registered_with_explicit_block_name() { - + RubyExtensionRegistry rubyExtensionRegistry = asciidoctor.rubyExtensionRegistry(); - rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")).block("rubyyell", "YellRubyBlock"); + rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")) + .block("rubyyell", "YellRubyBlock"); String content = asciidoctor.convert( "= Block Yell Example\n" + - "\n" + - "content\n" + - "\n" + - "[rubyyell]\n" + - "The time is now. Get a move on.", + "\n" + + "content\n" + + "\n" + + "[rubyyell]\n" + + "The time is now. Get a move on.", options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); assertThat(elements.size(), is(2)); assertThat(elements.get(1).text(), is("THE TIME IS NOW! GET A MOVE ON!")); - + } @Test public void ruby_block_processor_should_be_registered_with_implicit_block_name() { RubyExtensionRegistry rubyExtensionRegistry = asciidoctor.rubyExtensionRegistry(); - rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")).block("YellRubyBlock"); + rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")) + .block("YellRubyBlock"); String content = asciidoctor.convert( - "= Block Yell Example\n" + - "\n" + - "content\n" + - "\n" + - "[yell]\n" + - "The time is now. Get a move on.", + "= Block Yell Example\n" + + "\n" + + "content\n" + + "\n" + + "[yell]\n" + + "The time is now. Get a move on.", options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); @@ -80,11 +85,12 @@ public void ruby_block_processor_should_be_registered_with_implicit_block_name() public void ruby_block_macro_processor_should_be_registered_with_block_name() { RubyExtensionRegistry rubyExtensionRegistry = asciidoctor.rubyExtensionRegistry(); - rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/gist-block-macro.rb")).blockMacro("mygist", "GistBlockMacro"); + rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/gist-block-macro.rb")) + .blockMacro("mygist", "GistBlockMacro"); String content = asciidoctor.convert( ".My Gist\n" + - "mygist::123456[]", + "mygist::123456[]", options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); @@ -98,11 +104,12 @@ public void ruby_block_macro_processor_should_be_registered_with_block_name() { public void ruby_block_macro_processor_should_be_registered_with_implicit_block_name() { RubyExtensionRegistry rubyExtensionRegistry = asciidoctor.rubyExtensionRegistry(); - rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/gist-block-macro.rb")).blockMacro("GistBlockMacro"); + rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/gist-block-macro.rb")) + .blockMacro("GistBlockMacro"); String content = asciidoctor.convert( ".My Gist\n" + - "gist::42[]", + "gist::42[]", options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); @@ -116,12 +123,13 @@ public void ruby_block_macro_processor_should_be_registered_with_implicit_block_ public void ruby_inline_macro_processor_should_be_registered_with_macro_name() { RubyExtensionRegistry rubyExtensionRegistry = asciidoctor.rubyExtensionRegistry(); - rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/man-inline-macro.rb")).inlineMacro("myman", "ManInlineMacro"); + rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/man-inline-macro.rb")) + .inlineMacro("myman", "ManInlineMacro"); String content = asciidoctor.convert( "= Man Inline Macro Extension\n" + - "\n" + - "See myman:gittutorial[7] to get started.", + "\n" + + "See myman:gittutorial[7] to get started.", options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); @@ -136,12 +144,12 @@ public void ruby_inline_macro_processor_should_be_registered_with_implicit_macro RubyExtensionRegistry rubyExtensionRegistry = asciidoctor.rubyExtensionRegistry(); rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/man-inline-macro.rb")) - .inlineMacro("ManInlineMacro"); + .inlineMacro("ManInlineMacro"); String content = asciidoctor.convert( - "= Man Inline Macro Extension\n" + - "\n" + - "See man:dockertutorial[7] to get started.", + "= Man Inline Macro Extension\n" + + "\n" + + "See man:dockertutorial[7] to get started.", options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); @@ -154,17 +162,17 @@ public void ruby_inline_macro_processor_should_be_registered_with_implicit_macro @Test public void ruby_treeprocessor_should_be_registered() { - final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath(); + final String rubyExtPath = rubyExtensionsDir.getAbsolutePath(); final AsciidoctorJRuby asciidoctor = AsciidoctorJRuby.Factory.create(singletonList(rubyExtPath)); asciidoctor.rubyExtensionRegistry() - .requireLibrary("shell-session-tree-processor.rb") - .treeprocessor("ShellSessionTreeProcessor"); + .requireLibrary("shell-session-tree-processor.rb") + .treeprocessor("ShellSessionTreeProcessor"); String content = asciidoctor.convert( - " $ echo \"Hello, World!\"\n" + - " > Hello, World!\n" + - "\n" + - " $ gem install asciidoctor", + " $ echo \"Hello, World!\"\n" + + " > Hello, World!\n" + + "\n" + + " $ gem install asciidoctor", options().toFile(false).get()); final Document document = Jsoup.parse(content); @@ -177,33 +185,33 @@ public void ruby_treeprocessor_should_be_registered() { @Test public void ruby_includeprocessor_should_be_registered() { asciidoctor.rubyExtensionRegistry() - .loadClass(getClass().getResourceAsStream("/ruby-extensions/response-include-processor.rb")) - .includeProcessor("ResponseIncludeProcessor"); + .loadClass(getClass().getResourceAsStream("/ruby-extensions/response-include-processor.rb")) + .includeProcessor("ResponseIncludeProcessor"); String content = asciidoctor.convert( - "The response to everything is\n" + - "\n" + - "include::response[]" + - "", - options().toFile(false).safe(SafeMode.SAFE).get()); + "The response to everything is\n" + + "\n" + + "include::response[]" + + "", + options().toFile(false).safe(SafeMode.SAFE).get()); final Document document = Jsoup.parse(content); assertThat( - document.getElementsByClass("paragraph").get(1).getElementsByTag("p").get(0).toString(), - is("

42

")); + document.getElementsByClass("paragraph").get(1).getElementsByTag("p").get(0).toString(), + is("

42

")); } @Test public void ruby_postprocessor_should_be_registered() { - final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath(); + final String rubyExtPath = rubyExtensionsDir.getAbsolutePath(); final AsciidoctorJRuby asciidoctor = AsciidoctorJRuby.Factory.create(singletonList(rubyExtPath)); asciidoctor.rubyExtensionRegistry() - .requireLibrary("xml-entity-postprocessor.rb") - .postprocessor("XmlEntityPostprocessor"); + .requireLibrary("xml-entity-postprocessor.rb") + .postprocessor("XmlEntityPostprocessor"); String content = asciidoctor.convert( - "Read §2 and it'll all be clear.", + "Read §2 and it'll all be clear.", options().toFile(false).get()); assertThat(content, containsString("Read §2 and it'll all be clear.")); @@ -212,61 +220,61 @@ public void ruby_postprocessor_should_be_registered() { @Test public void ruby_preprocessor_should_be_registered() { - final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath(); + final String rubyExtPath = rubyExtensionsDir.getAbsolutePath(); final AsciidoctorJRuby asciidoctor = AsciidoctorJRuby.Factory.create(singletonList(rubyExtPath)); asciidoctor.rubyExtensionRegistry() - .requireLibrary("front-matter-preprocessor.rb") - .preprocessor("FrontMatterPreprocessor"); + .requireLibrary("front-matter-preprocessor.rb") + .preprocessor("FrontMatterPreprocessor"); String content = asciidoctor.convert( - "---\n" + - "tags: [announcement, website]\n" + - "---\n" + - "= Document Title\n" + - "\n" + - "content\n" + - "\n" + - "[subs=\"attributes,specialcharacters\"]\n" + - ".Captured front matter\n" + - "....\n" + - "---\n" + - "{front-matter}\n" + "---\n" + - "....", + "tags: [announcement, website]\n" + + "---\n" + + "= Document Title\n" + + "\n" + + "content\n" + + "\n" + + "[subs=\"attributes,specialcharacters\"]\n" + + ".Captured front matter\n" + + "....\n" + + "---\n" + + "{front-matter}\n" + + "---\n" + + "....", options().toFile(false).get()); final Document document = Jsoup.parse(content); final Element contentElement = document.getElementsByClass("content").get(0); final Element literalElement = contentElement.getElementsByTag("pre").get(0); assertThat(literalElement.toString().replace("\r", ""), - containsString("---\n" + - "tags: [announcement, website]\n" + - "---")); + containsString("---\n" + + "tags: [announcement, website]\n" + + "---")); } @Test public void ruby_docinfoprocessor_should_be_registered() { - final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath(); + final String rubyExtPath = rubyExtensionsDir.getAbsolutePath(); final AsciidoctorJRuby asciidoctor = AsciidoctorJRuby.Factory.create(singletonList(rubyExtPath)); asciidoctor.rubyExtensionRegistry() - .requireLibrary("view-result-docinfoprocessor.rb") - .docinfoProcessor("ViewResultDocinfoProcessor"); + .requireLibrary("view-result-docinfoprocessor.rb") + .docinfoProcessor("ViewResultDocinfoProcessor"); String content = asciidoctor.convert( - "= View Result Sample \n" + - " \n" + - ".This will have a link next to it\n" + - "---- \n" + - "* always displayed \n" + - "* always displayed 2 \n" + - "---- \n" + - " \n" + - "[.result] \n" + - "==== \n" + - "* hidden till clicked \n" + - "* hidden till clicked 2 \n" + - "==== ", + "= View Result Sample \n" + + " \n" + + ".This will have a link next to it\n" + + "---- \n" + + "* always displayed \n" + + "* always displayed 2 \n" + + "---- \n" + + " \n" + + "[.result] \n" + + "==== \n" + + "* hidden till clicked \n" + + "* hidden till clicked 2 \n" + + "==== ", options().toFile(false).safe(SafeMode.SAFE).standalone(true).get()); final Document document = Jsoup.parse(content); diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenTheInlineMacroProcessorRunsTwice.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenTheInlineMacroProcessorRunsTwice.java index 74fe3bc42..139936430 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenTheInlineMacroProcessorRunsTwice.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenTheInlineMacroProcessorRunsTwice.java @@ -3,7 +3,8 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.ast.PhraseNode; import org.asciidoctor.ast.StructuralNode; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.util.Collections; import java.util.HashMap; diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/YellStaticBlock.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/YellStaticBlock.java index 98e859c5e..3d9d0f243 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/YellStaticBlock.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/YellStaticBlock.java @@ -10,10 +10,10 @@ public class YellStaticBlock extends BlockProcessor { - private static Map configs = new HashMap() {{ - put(Contexts.KEY, Arrays.asList(Contexts.PARAGRAPH)); - put(ContentModel.KEY, ContentModel.SIMPLE); - }}; + private static Map configs = Map.of( + Contexts.KEY, Arrays.asList(Contexts.PARAGRAPH), + ContentModel.KEY, ContentModel.SIMPLE + ); public YellStaticBlock(String name) { super(name, configs); diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/YellStaticListingBlock.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/YellStaticListingBlock.java index f746d4863..5aa5a0764 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/YellStaticListingBlock.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/YellStaticListingBlock.java @@ -10,10 +10,10 @@ public class YellStaticListingBlock extends BlockProcessor { - private static Map configs = new HashMap() {{ - put(Contexts.KEY, Arrays.asList(Contexts.LISTING)); - put(ContentModel.KEY, ContentModel.SIMPLE); - }}; + private static Map configs = Map.of( + Contexts.KEY, Arrays.asList(Contexts.LISTING), + ContentModel.KEY, ContentModel.SIMPLE + ); public YellStaticListingBlock(String name) { super(name, configs); diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/SectionImplTest.java b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/SectionImplTest.java index 53febe3e4..3294154b0 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/SectionImplTest.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/SectionImplTest.java @@ -5,7 +5,8 @@ import org.asciidoctor.Options; import org.asciidoctor.ast.Document; import org.asciidoctor.ast.Section; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.util.Collections; diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenClassloaderIsRequired.java b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenClassloaderIsRequired.java index 9a6ab0978..a92e03d36 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenClassloaderIsRequired.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenClassloaderIsRequired.java @@ -1,13 +1,14 @@ package org.asciidoctor.jruby.internal; import org.asciidoctor.Asciidoctor; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; class AsciiDoctorJClassloaderTestRunnable implements Runnable { + private boolean loadingsucceeded = false; private ClassLoader classloader = null; @@ -43,7 +44,7 @@ public class WhenClassloaderIsRequired { inside the runnable to verify we have created the same error as happens inside sbt. */ @Test - @Ignore("Behavior changed in JRuby 9000. It no longer only uses the TCCL by default") + @Disabled("Behavior changed in JRuby 9000. It no longer only uses the TCCL by default") public void contentsOfJRubyCompleteShouldFailToLoadWithoutPassingClassloader() throws Exception { ClassLoader currentclassloader = this.getClass().getClassLoader(); ClassLoader rootclassloader = currentclassloader.getParent(); diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenDocumentIsRenderedWithPreloading.java b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenDocumentIsRenderedWithPreloading.java index 1f0a540ab..7a986d5ef 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenDocumentIsRenderedWithPreloading.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenDocumentIsRenderedWithPreloading.java @@ -1,25 +1,25 @@ package org.asciidoctor.jruby.internal; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -import java.io.File; -import java.util.Map; - import org.asciidoctor.Asciidoctor; import org.asciidoctor.AttributesBuilder; import org.asciidoctor.OptionsBuilder; -import org.asciidoctor.arquillian.api.Unshared; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; import org.jruby.RubyBoolean; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; -@RunWith(Arquillian.class) +import java.io.File; +import java.util.Map; + +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +@ExtendWith(AsciidoctorExtension.class) public class WhenDocumentIsRenderedWithPreloading { - @ArquillianResource(Unshared.class) + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; @Test @@ -31,7 +31,6 @@ public void coderay_gem_should_be_preloaded() { ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'coderay'"); assertThat(evalScriptlet.isFalse(), is(true)); - } @Test @@ -43,7 +42,6 @@ public void not_coderay_gem_should_not_be_preloaded() { ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'coderay'"); assertThat(evalScriptlet.isTrue(), is(true)); - } @Test @@ -54,7 +52,6 @@ public void erubis_gem_should_be_preloaded() { ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'erubis'"); assertThat(evalScriptlet.isFalse(), is(true)); - } @Test @@ -65,7 +62,6 @@ public void not_erubis_gem_should_be_preloaded() { ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'erubis'"); assertThat(evalScriptlet.isTrue(), is(true)); - } @Test @@ -76,7 +72,6 @@ public void template_dir_should_preload_tilt() { ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'tilt'"); assertThat(evalScriptlet.isFalse(), is(true)); - } @Test @@ -88,7 +83,5 @@ public void data_uri_gem_should_be_preloaded() { ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'base64'"); assertThat(evalScriptlet.isFalse(), is(true)); - } - } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenEnvironmentVariablesAreSet.java b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenEnvironmentVariablesAreSet.java index 12bd3352c..35ec75c97 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenEnvironmentVariablesAreSet.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenEnvironmentVariablesAreSet.java @@ -5,7 +5,8 @@ import org.jruby.RubyString; import org.jruby.runtime.builtin.IRubyObject; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; public class WhenEnvironmentVariablesAreSet { diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenLoadingExtensionFromUnusualPackage.java b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenLoadingExtensionFromUnusualPackage.java index 420d805bd..08afe44ef 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenLoadingExtensionFromUnusualPackage.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenLoadingExtensionFromUnusualPackage.java @@ -1,40 +1,36 @@ package org.asciidoctor.jruby.internal; import org.asciidoctor.Asciidoctor; -import org.asciidoctor.arquillian.api.Unshared; import org.asciidoctor.extension.JavaExtensionRegistry; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import unusual.extension.BoldifyPostProcessor; -@RunWith(Arquillian.class) -public class WhenLoadingExtensionFromUnusualPackage { - - @ArquillianResource - private ClasspathResources classpath; - - @ArquillianResource(Unshared.class) - private Asciidoctor asciidoctor; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; - @Test - public void shouldAllowLoadingUsingInstance() { - final JavaExtensionRegistry registry = asciidoctor.javaExtensionRegistry(); - registry.postprocessor(new unusual.extension.BoldifyPostProcessor()); - } - - @Test - public void shouldAllowLoadingByClassName() { - final JavaExtensionRegistry registry = asciidoctor.javaExtensionRegistry(); - registry.postprocessor(BoldifyPostProcessor.class.getCanonicalName()); - } - - @Test - public void shouldAllowLoadingByClass() { - final JavaExtensionRegistry registry = asciidoctor.javaExtensionRegistry(); - registry.postprocessor(BoldifyPostProcessor.class); - } +@ExtendWith(AsciidoctorExtension.class) +public class WhenLoadingExtensionFromUnusualPackage { + @AsciidoctorInstance(scope = PER_METHOD) + private Asciidoctor asciidoctor; + + @Test + public void shouldAllowLoadingUsingInstance() { + final JavaExtensionRegistry registry = asciidoctor.javaExtensionRegistry(); + registry.postprocessor(new unusual.extension.BoldifyPostProcessor()); + } + + @Test + public void shouldAllowLoadingByClassName() { + final JavaExtensionRegistry registry = asciidoctor.javaExtensionRegistry(); + registry.postprocessor(BoldifyPostProcessor.class.getCanonicalName()); + } + + @Test + public void shouldAllowLoadingByClass() { + final JavaExtensionRegistry registry = asciidoctor.javaExtensionRegistry(); + registry.postprocessor(BoldifyPostProcessor.class); + } } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingImagesFromCatalogAsset.java b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingImagesFromCatalogAsset.java index 85941f47a..4c2ddbfaf 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingImagesFromCatalogAsset.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingImagesFromCatalogAsset.java @@ -4,35 +4,33 @@ import org.asciidoctor.Attributes; import org.asciidoctor.Options; import org.asciidoctor.SafeMode; -import org.asciidoctor.arquillian.api.Unshared; import org.asciidoctor.ast.Document; import org.asciidoctor.ast.ImageReference; import org.asciidoctor.jruby.ast.impl.TestImageReference; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenReadingImagesFromCatalogAsset { - @ArquillianResource - private ClasspathResources classpath; - - @ArquillianResource(Unshared.class) + @AsciidoctorInstance private Asciidoctor asciidoctor; - @ArquillianResource - private TemporaryFolder testFolder; + @ClasspathResource("sample-with-images.adoc") + private File sampleWithImages; static final TestImageReference[] BLOCK_IMAGES = new TestImageReference[]{ new TestImageReference("images/block-image.jpg") @@ -135,15 +133,14 @@ public void shouldNotCatalogImagesWhenCatalogAssetsIsFalse() { } @Test - public void shouldCatalogAllImagesWhenUsingConvertFile() throws IOException { + public void shouldCatalogAllImagesWhenUsingConvertFile(@TempDir Path tempFolder) { final Options options = Options.builder() .catalogAssets(true) .safe(SafeMode.UNSAFE) - .toFile(testFolder.newFile()) + .toFile(outputFile(tempFolder)) .build(); - final File file = getAsciiDocWithImagesFile(); - var document = asciidoctor.convertFile(file, options, Document.class); + var document = asciidoctor.convertFile(sampleWithImages, options, Document.class); assertThat(document) .isInstanceOf(Document.class); @@ -155,11 +152,11 @@ public void shouldCatalogAllImagesWhenUsingConvertFile() throws IOException { } @Test - public void shouldCatalogAllImagesWhenUsingConvert() throws IOException { + public void shouldCatalogAllImagesWhenUsingConvert(@TempDir Path tempFolder) { final Options options = Options.builder() .catalogAssets(true) .safe(SafeMode.UNSAFE) - .toFile(testFolder.newFile()) + .toFile(outputFile(tempFolder)) .build(); final String content = getAsciiDocWithImagesContent(); @@ -176,13 +173,13 @@ public void shouldCatalogAllImagesWhenUsingConvert() throws IOException { private String getAsciiDocWithImagesContent() { try { - return Files.readString(getAsciiDocWithImagesFile().toPath()); + return Files.readString(sampleWithImages.toPath()); } catch (IOException e) { throw new RuntimeException(e); } } - private File getAsciiDocWithImagesFile() { - return classpath.getResource("sample-with-images.adoc"); + private File outputFile(Path tempFolder) { + return tempFolder.resolve("output").toFile(); } } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingImagesFromCatalogAssetFromConverter.java b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingImagesFromCatalogAssetFromConverter.java index ce49b3da5..63a753a31 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingImagesFromCatalogAssetFromConverter.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingImagesFromCatalogAssetFromConverter.java @@ -2,36 +2,38 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; -import org.asciidoctor.arquillian.api.Unshared; import org.asciidoctor.ast.ContentNode; import org.asciidoctor.ast.Document; import org.asciidoctor.ast.ImageReference; import org.asciidoctor.ast.StructuralNode; import org.asciidoctor.converter.StringConverter; import org.asciidoctor.jruby.ast.impl.TestImageReference; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.List; import java.util.Map; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; import static org.assertj.core.api.Assertions.assertThat; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenReadingImagesFromCatalogAssetFromConverter { - @ArquillianResource - private ClasspathResources classpath; - - @ArquillianResource(Unshared.class) + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; + @ClasspathResource("sample-with-images.adoc") + private File sampleWithImages; + static final String CONVERTER_BACKEND = "custom-backend"; static final TestImageReference[] BLOCK_IMAGES = new TestImageReference[]{ @@ -46,7 +48,7 @@ public class WhenReadingImagesFromCatalogAssetFromConverter { private static List imagesBeforeConvert; private static List imagesAfterConvert; - @Before + @BeforeEach public void beforeEach() { final var javaConverterRegistry = asciidoctor.javaConverterRegistry(); javaConverterRegistry.converters().clear(); @@ -109,7 +111,7 @@ private static Options optionsWithConverter() { private String getAsciiDodWithImagesDocument() { try { - return Files.readString(classpath.getResource("sample-with-images.adoc").toPath()); + return Files.readString(sampleWithImages.toPath()); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingLinksFromCatalogAsset.java b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingLinksFromCatalogAsset.java index 0a40dee44..c8424a13d 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingLinksFromCatalogAsset.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingLinksFromCatalogAsset.java @@ -3,34 +3,33 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.SafeMode; -import org.asciidoctor.arquillian.api.Unshared; import org.asciidoctor.ast.Document; import org.asciidoctor.ast.Link; -import org.asciidoctor.util.ClasspathResources; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; import static org.assertj.core.api.Assertions.assertThat; -@RunWith(Arquillian.class) +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenReadingLinksFromCatalogAsset { - @ArquillianResource - private ClasspathResources classpath; - - @ArquillianResource(Unshared.class) + @AsciidoctorInstance(scope = PER_METHOD) private Asciidoctor asciidoctor; - @ArquillianResource - private TemporaryFolder testFolder; + @ClasspathResource("sample-with-links.adoc") + private File sampleWithLinks; static final String[] ALL_LINKS = new String[]{ "https://docs.asciidoctor.org", @@ -67,7 +66,7 @@ public void shouldNotReturnLinksWhenCatalogAssetsIsFalse() { final Options options = Options.builder() .catalogAssets(false) .build(); - final File file = getAsciiDocWithLinksFile(); + final File file = sampleWithLinks; Document document = asciidoctor.convertFile(file, options, Document.class); @@ -79,7 +78,7 @@ public void shouldNotReturnLinksWhenCatalogAssetsIsFalse() { @Test public void shouldReturnLinksWhenConvertingFile() { final Options options = catalogAssetsEnabled(); - final File file = getAsciiDocWithLinksFile(); + final File file = sampleWithLinks; Document document = asciidoctor.convertFile(file, options, Document.class); @@ -90,11 +89,11 @@ public void shouldReturnLinksWhenConvertingFile() { } @Test - public void shouldReturnLinksWhenConvertingString() throws IOException { + public void shouldReturnLinksWhenConvertingString(@TempDir Path tempFolder) { final Options options = Options.builder() .catalogAssets(true) .safe(SafeMode.UNSAFE) - .toFile(testFolder.newFile()) + .toFile(outputFile(tempFolder)) .build(); final String content = getAsciiDocWithLinksContent(); @@ -108,19 +107,19 @@ public void shouldReturnLinksWhenConvertingString() throws IOException { private String getAsciiDocWithLinksContent() { try { - return Files.readString(getAsciiDocWithLinksFile().toPath()); + return Files.readString(sampleWithLinks.toPath()); } catch (IOException e) { throw new RuntimeException(e); } } - private File getAsciiDocWithLinksFile() { - return classpath.getResource("sample-with-links.adoc"); - } - private static Options catalogAssetsEnabled() { return Options.builder() .catalogAssets(true) .build(); } + + private File outputFile(Path tempFolder) { + return tempFolder.resolve("output").toFile(); + } } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/util/TestHttpServer.java b/asciidoctorj-core/src/test/java/org/asciidoctor/util/TestHttpServer.java index b9b8948bf..55d6624cc 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/util/TestHttpServer.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/util/TestHttpServer.java @@ -3,28 +3,19 @@ import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundHandlerAdapter; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelPipeline; -import io.netty.channel.EventLoopGroup; +import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; -import io.netty.handler.codec.http.DefaultFullHttpResponse; -import io.netty.handler.codec.http.FullHttpRequest; -import io.netty.handler.codec.http.HttpObjectAggregator; -import io.netty.handler.codec.http.HttpResponseStatus; -import io.netty.handler.codec.http.HttpServerCodec; -import org.asciidoctor.jruby.internal.IOUtils; +import io.netty.handler.codec.http.*; import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; import java.net.InetSocketAddress; +import java.nio.file.Files; import java.util.Map; +import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND; +import static io.netty.handler.codec.http.HttpResponseStatus.OK; + public class TestHttpServer { @@ -44,7 +35,7 @@ private TestHttpServer(Map resources) { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(eventLoopGroup) .channel(NioServerSocketChannel.class) - .childHandler(new ChannelInitializer() { + .childHandler(new ChannelInitializer<>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); @@ -53,21 +44,16 @@ protected void initChannel(Channel ch) throws Exception { pipeline.addLast(new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - FullHttpRequest request = (FullHttpRequest) msg; - File resourceFile = TestHttpServer.instance.resources.get(request.getUri()); + final FullHttpRequest request = (FullHttpRequest) msg; + final File resourceFile = TestHttpServer.instance.resources.get(request.getUri()); + final HttpVersion protocolVersion = request.getProtocolVersion(); if (resourceFile == null) { ByteBuf notFoundResponse = Unpooled.copiedBuffer("

Sorry, no content found

".getBytes()); - ctx.writeAndFlush(new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.NOT_FOUND, notFoundResponse)); + ctx.writeAndFlush(new DefaultFullHttpResponse(protocolVersion, NOT_FOUND, notFoundResponse)); } else { - InputStream in = null; - try { - in = new FileInputStream(resourceFile); - String response = IOUtils.readFull(in); - ctx.writeAndFlush(new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK, Unpooled.copiedBuffer(response.getBytes()))); - } finally { - in.close(); - } + ByteBuf content = Unpooled.copiedBuffer(Files.readAllBytes(resourceFile.toPath())); + ctx.writeAndFlush(new DefaultFullHttpResponse(protocolVersion, OK, content)); } ctx.close(); } diff --git a/asciidoctorj-core/src/test/resources/src/documents/sample.ad b/asciidoctorj-core/src/test/resources/src/documents/sample.ad index 0b8061ee4..d745550e0 100644 --- a/asciidoctorj-core/src/test/resources/src/documents/sample.ad +++ b/asciidoctorj-core/src/test/resources/src/documents/sample.ad @@ -5,7 +5,7 @@ Doc Writer Preamble paragraph. -NOTE: This is test, only a test. +NOTE: This is a test, only a test. == Section A diff --git a/asciidoctorj-core/src/test/resources/src/documents/sample.adoc b/asciidoctorj-core/src/test/resources/src/documents/sample.adoc index 6a742a7c9..9ec8274e6 100644 --- a/asciidoctorj-core/src/test/resources/src/documents/sample.adoc +++ b/asciidoctorj-core/src/test/resources/src/documents/sample.adoc @@ -5,7 +5,7 @@ Doc Writer Preamble paragraph. -NOTE: This is test, only a test. +NOTE: This is a test, only a test. == Section A @@ -22,4 +22,4 @@ NOTE: This is test, only a test. .Section B list * Item 1 * Item 2 -* Item 3 \ No newline at end of file +* Item 3 diff --git a/asciidoctorj-core/src/test/resources/src/documents/sample.asc b/asciidoctorj-core/src/test/resources/src/documents/sample.asc index 0b8061ee4..d745550e0 100644 --- a/asciidoctorj-core/src/test/resources/src/documents/sample.asc +++ b/asciidoctorj-core/src/test/resources/src/documents/sample.asc @@ -5,7 +5,7 @@ Doc Writer Preamble paragraph. -NOTE: This is test, only a test. +NOTE: This is a test, only a test. == Section A diff --git a/asciidoctorj-core/src/test/resources/src/documents/sample.asciidoc b/asciidoctorj-core/src/test/resources/src/documents/sample.asciidoc index 0b8061ee4..d745550e0 100644 --- a/asciidoctorj-core/src/test/resources/src/documents/sample.asciidoc +++ b/asciidoctorj-core/src/test/resources/src/documents/sample.asciidoc @@ -5,7 +5,7 @@ Doc Writer Preamble paragraph. -NOTE: This is test, only a test. +NOTE: This is a test, only a test. == Section A diff --git a/asciidoctorj-core/src/test/resources/src/documents/sample.d b/asciidoctorj-core/src/test/resources/src/documents/sample.d index 51e1e45eb..9ec8274e6 100644 --- a/asciidoctorj-core/src/test/resources/src/documents/sample.d +++ b/asciidoctorj-core/src/test/resources/src/documents/sample.d @@ -5,7 +5,7 @@ Doc Writer Preamble paragraph. -NOTE: This is test, only a test. +NOTE: This is a test, only a test. == Section A diff --git a/asciidoctorj-core/src/test/resources/src/documents/sample.doc b/asciidoctorj-core/src/test/resources/src/documents/sample.doc index 0b8061ee4..d745550e0 100644 --- a/asciidoctorj-core/src/test/resources/src/documents/sample.doc +++ b/asciidoctorj-core/src/test/resources/src/documents/sample.doc @@ -5,7 +5,7 @@ Doc Writer Preamble paragraph. -NOTE: This is test, only a test. +NOTE: This is a test, only a test. == Section A diff --git a/asciidoctorj-core/src/test/resources/src/documents/sample.txt b/asciidoctorj-core/src/test/resources/src/documents/sample.txt index 0b8061ee4..d745550e0 100644 --- a/asciidoctorj-core/src/test/resources/src/documents/sample.txt +++ b/asciidoctorj-core/src/test/resources/src/documents/sample.txt @@ -5,7 +5,7 @@ Doc Writer Preamble paragraph. -NOTE: This is test, only a test. +NOTE: This is a test, only a test. == Section A diff --git a/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenAPdfDocumentIsRenderedToStream.java b/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenAPdfDocumentIsRenderedToStream.java index f100e24c7..a0e8a498d 100644 --- a/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenAPdfDocumentIsRenderedToStream.java +++ b/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenAPdfDocumentIsRenderedToStream.java @@ -1,7 +1,9 @@ package org.asciidoctor; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; import java.io.ByteArrayOutputStream; @@ -14,17 +16,15 @@ import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith(AsciidoctorExtension.class) public class WhenAPdfDocumentIsRenderedToStream { private static final String BACKEND_PDF = "pdf"; private static final String ASCIIDOCTOR_DIAGRAM = "asciidoctor-diagram"; + @AsciidoctorInstance private Asciidoctor asciidoctor; - @BeforeEach - public void initAsciidoctor() { - this.asciidoctor = Asciidoctor.Factory.create(); - } @Test void should_render_PDF_to_ByteArrayOutputStream(@TempDir File testFolder) throws IOException { diff --git a/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenBackendIsPdf.java b/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenBackendIsPdf.java index b577e1545..ab1879e7c 100644 --- a/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenBackendIsPdf.java +++ b/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenBackendIsPdf.java @@ -1,12 +1,15 @@ package org.asciidoctor; -import org.asciidoctor.util.ClasspathHelper; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.asciidoctor.util.RougeColors; import org.asciidoctor.util.pdf.ColorsProcessor; import org.asciidoctor.util.pdf.ImageProcessor; import org.jruby.runtime.builtin.IRubyObject; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.awt.*; import java.io.File; @@ -17,23 +20,17 @@ import static org.asciidoctor.OptionsBuilder.options; import static org.assertj.core.api.Assertions.assertThat; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenBackendIsPdf { - public static final String DOCUMENT = "= A document\n\n Test"; + private static final String DOCUMENT = "= A document\n\n Test"; + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpath; - @BeforeEach - public void initAsciidoctor() { - this.asciidoctor = Asciidoctor.Factory.create(); - this.classpath = new ClasspathHelper(); - this.classpath.setClassloader(this.getClass()); - } @Test - public void pdf_should_be_rendered_to_object_for_pdf_backend() { + void pdf_should_be_rendered_to_object_for_pdf_backend() { // The asciidoctor-pdf backend returns the converter itself on convert. // If the result should be written to a file the write method will convert to a PDF stream // Therefore, if the result should not be written to a file the PDF converter should be returned. @@ -42,34 +39,30 @@ public void pdf_should_be_rendered_to_object_for_pdf_backend() { } @Test - public void pdf_should_include_images() throws IOException { - String filename = "image-sample"; - File inputFile = classpath.getResource(filename + ".adoc"); - File outputFile1 = new File(inputFile.getParentFile(), filename + ".pdf"); + void pdf_should_include_images(@ClasspathResource("image-sample.adoc") File inputFile) throws IOException { asciidoctor.convertFile(inputFile, options().backend("pdf").safe(SafeMode.UNSAFE).get()); - assertThat(outputFile1).exists(); + File outputFile = new File(inputFile.getParentFile(), "image-sample.pdf"); + assertThat(outputFile).exists(); ImageProcessor imageProcessor = new ImageProcessor(); - imageProcessor.parse(outputFile1.getAbsolutePath()); + imageProcessor.parse(outputFile.getAbsolutePath()); List images = imageProcessor.getImages(); assertThat(images).hasSize(2); - outputFile1.delete(); + outputFile.delete(); } @Test - public void pdf_source_code_should_be_highlighted() throws IOException { - String filename = "code-sample"; - File inputFile = classpath.getResource(filename + ".adoc"); - File outputFile1 = new File(inputFile.getParentFile(), filename + ".pdf"); + void pdf_source_code_should_be_highlighted(@ClasspathResource("code-sample.adoc") File inputFile) throws IOException { asciidoctor.convertFile(inputFile, options().backend("pdf").safe(SafeMode.UNSAFE).get()); - assertThat(outputFile1).exists(); + File outputFile = new File(inputFile.getParentFile(), "code-sample.pdf"); + assertThat(outputFile).exists(); ColorsProcessor colorsProcessor = new ColorsProcessor("program", "System.out.println", "printHello", "HelloWorld", "", "else", "Math.sqrt"); - colorsProcessor.parse(outputFile1.getAbsolutePath()); + colorsProcessor.parse(outputFile.getAbsolutePath()); Map> colors = colorsProcessor.getColors(); assertThat(colors.get("program").get(0)).isEqualTo(RougeColors.GREY); assertThat(colors.get("System.out.println").get(0)).isEqualTo(RougeColors.LIGHT_BLUE); @@ -80,17 +73,14 @@ public void pdf_source_code_should_be_highlighted() throws IOException { assertThat(colors.get("else").get(0)).isEqualTo(RougeColors.GREEN); assertThat(colors.get("Math.sqrt").get(0)).isEqualTo(RougeColors.LIGHT_BLUE); - outputFile1.delete(); + outputFile.delete(); } @Test - public void pdf_should_not_fail_with_empty_tables() { - String filename = "empty-table"; - File inputFile = classpath.getResource(filename + ".adoc"); - File outputFile1 = new File(inputFile.getParentFile(), filename + ".pdf"); - + void pdf_should_not_fail_with_empty_tables(@ClasspathResource("empty-table.adoc") File inputFile) { asciidoctor.convertFile(inputFile, options().backend("pdf").safe(SafeMode.SAFE).get()); - assertThat(outputFile1).exists(); + File outputFile = new File(inputFile.getParentFile(), "empty-table.pdf"); + assertThat(outputFile).exists(); } } diff --git a/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenBackendIsRevealJs.java b/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenBackendIsRevealJs.java index 5427c53c7..585ae6bb3 100644 --- a/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenBackendIsRevealJs.java +++ b/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenBackendIsRevealJs.java @@ -1,11 +1,15 @@ package org.asciidoctor; import org.asciidoctor.cli.jruby.AsciidoctorInvoker; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import java.io.IOException; @@ -14,21 +18,17 @@ import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenBackendIsRevealJs { + @AsciidoctorInstance private Asciidoctor asciidoctor; - @BeforeEach - public void initAsciidoctor() { - this.asciidoctor = Asciidoctor.Factory.create(); - } @Test - public void should_create_simple_slides() throws IOException { - String filename = "sample"; - File inputFile = new File("build/resources/test/" + filename + ".adoc"); - File outputFile1 = new File(inputFile.getParentFile(), filename + ".html"); - removeFileIfItExists(outputFile1); + void should_create_simple_slides(@ClasspathResource("sample.adoc") File inputFile) throws IOException { + File outputFile = new File(inputFile.getParentFile(), "sample.html"); + removeFileIfItExists(outputFile); AsciidoctorInvoker.main(new String[]{ "-b", "revealjs", @@ -37,9 +37,9 @@ public void should_create_simple_slides() throws IOException { inputFile.getAbsolutePath() }); - Document doc = Jsoup.parse(outputFile1, "UTF-8"); + Document doc = Jsoup.parse(outputFile, "UTF-8"); - assertThat(outputFile1).exists(); + assertThat(outputFile).exists(); List stylesheets = doc.head().getElementsByTag("link").stream() .filter(element -> "stylesheet".equals(element.attr("rel"))) diff --git a/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenDitaaDiagramIsRendered.java b/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenDitaaDiagramIsRendered.java index 207bef81b..f2c88cc8e 100644 --- a/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenDitaaDiagramIsRendered.java +++ b/asciidoctorj-distribution/src/test/java/org/asciidoctor/WhenDitaaDiagramIsRendered.java @@ -1,7 +1,10 @@ package org.asciidoctor; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; import java.io.File; @@ -9,16 +12,14 @@ import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class WhenDitaaDiagramIsRendered { private static final String ASCIIDOCTOR_DIAGRAM = "asciidoctor-diagram"; + @AsciidoctorInstance private Asciidoctor asciidoctor; - @BeforeEach - public void initAsciidoctor() { - this.asciidoctor = Asciidoctor.Factory.create(); - } @Test void should_render_ditaa_diagram_to_HTML(@TempDir File testFolder) { diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/OptionsTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/OptionsTest.java index 7d513eec3..5f505d1a8 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/OptionsTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/OptionsTest.java @@ -1,37 +1,34 @@ package org.asciidoctor.integrationguide; -import org.apache.commons.io.IOUtils; import org.asciidoctor.Asciidoctor; import org.asciidoctor.Attributes; import org.asciidoctor.Options; import org.asciidoctor.SafeMode; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.CleanupMode; import org.junit.jupiter.api.io.TempDir; import java.io.File; -import java.io.FileReader; +import java.nio.file.Files; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.assertTrue; +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class OptionsTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; @TempDir(cleanup = CleanupMode.NEVER) public File tempDir; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } @Test public void simple_options_example_embeddable_document() { @@ -68,7 +65,7 @@ public void options_for_pdf_document() { } @Test - public void convert_in_unsafe_mode() { + public void convert_in_unsafe_mode(@ClasspathResource("includingcontent.adoc") File inputDocument) { //tag::unsafeConversion[] File sourceFile = //end::unsafeConversion[] @@ -79,7 +76,7 @@ public void convert_in_unsafe_mode() { new File("includingcontent.adoc"); //end::unsafeConversion[] */ - classpathResources.getResource("includingcontent.adoc"); + inputDocument; //tag::unsafeConversion[] String result = asciidoctor.convertFile( sourceFile, @@ -109,7 +106,7 @@ public void convert_to_dedicated_file() throws Exception { assertTrue(targetFile.exists()); assertThat( - IOUtils.toString(new FileReader(targetFile)), + Files.readString(targetFile.toPath()), containsString("

Hello World")); //end::optionToFile[] } diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/converter/TextConverterTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/converter/TextConverterTest.java index 712954c3f..ab72393cb 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/converter/TextConverterTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/converter/TextConverterTest.java @@ -2,30 +2,28 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.OptionsBuilder; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import java.net.URL; import java.net.URLClassLoader; -import java.util.Arrays; -import java.util.List; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasItem; +import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class TextConverterTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } + @ClasspathResource("textconvertertest.adoc") + private File testDocument; + @Test public void should_use_text_converter_for_conversion() { @@ -33,7 +31,7 @@ public void should_use_text_converter_for_conversion() { //tag::include[] File test_adoc = //... //end::include[] - classpathResources.getResource("textconvertertest.adoc"); + testDocument; //tag::include[] @@ -50,28 +48,20 @@ public void should_use_text_converter_for_conversion() { verifyResult(result); } - private void verifyResult(String result) { - List lines = Arrays.asList(result.split("\\n")); - assertThat(lines, hasItem("== This is section 1 ==")); - assertThat(lines, hasItem("Paragraph 1")); - assertThat(lines, hasItem("== This is section 2 ==")); - assertThat(lines, hasItem("Paragraph 2")); - } - - @Test - public void should_use_text_converter_for_conversion_registered_as_service_impl() throws Exception { + public void should_use_text_converter_for_conversion_registered_as_service_impl( + @ClasspathResource("converterregistry") File extensionRegistryDir) throws Exception { ClassLoader oldTCCL = Thread.currentThread().getContextClassLoader(); try { - URL serviceDir = classpathResources.getResource("converterregistry").toURI().toURL(); + URL serviceDir = extensionRegistryDir.toURI().toURL(); URLClassLoader tccl = new URLClassLoader(new URL[]{serviceDir}); Thread.currentThread().setContextClassLoader(tccl); Asciidoctor asciidoctor = Asciidoctor.Factory.create(); File test_adoc = //... - classpathResources.getResource("textconvertertest.adoc"); + testDocument; String result = asciidoctor.convertFile( test_adoc, @@ -85,4 +75,12 @@ public void should_use_text_converter_for_conversion_registered_as_service_impl( } } + private void verifyResult(String result) { + var lines = result.split("\\n"); + assertThat(lines) + .contains("== This is section 1 ==") + .contains("Paragraph 1") + .contains("== This is section 2 ==") + .contains("Paragraph 2"); + } } diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/ASTShowcase.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/ASTShowcase.java index b341a00cf..94fbdd261 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/ASTShowcase.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/ASTShowcase.java @@ -2,39 +2,43 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.OptionsBuilder; -import org.asciidoctor.jruby.internal.IOUtils; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; -import java.io.FileReader; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class ASTShowcase { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } + @ClasspathResource("ast-demo.adoc") + private File astDemo; + + @ClasspathResource("ast-demo-result.txt") + private Path astDemoResult; + @Test public void createAstStructure() throws Exception { asciidoctor.javaExtensionRegistry().treeprocessor(ASTExtractorTreeprocessor.class); - asciidoctor.loadFile(classpathResources.getResource("ast-demo.adoc"), OptionsBuilder.options().asMap()); + asciidoctor.loadFile(astDemo, OptionsBuilder.options().asMap()); assertThat( ASTExtractorTreeprocessor.result.toString(), - is(IOUtils.readFull(new FileReader(classpathResources.getResource("ast-demo-result.txt"))).replaceAll("\\r", ""))); + is(Files.readString(astDemoResult).replaceAll("\\r", ""))); } } diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/CommentPreprocessorTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/CommentPreprocessorTest.java index f5857bdec..a5749bc0a 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/CommentPreprocessorTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/CommentPreprocessorTest.java @@ -2,27 +2,28 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class CommentPreprocessorTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } + @ClasspathResource("comment.adoc") + private File commentDocument; + @ClasspathResource("comment-with-note.adoc") + private File commentWithNoteDocument; @Test public void should_render_comments_as_notes() { @@ -30,12 +31,12 @@ public void should_render_comments_as_notes() { //tag::include[] File comment_adoc = //... //end::include[] - classpathResources.getResource("comment.adoc"); + commentDocument; //tag::include[] File comment_with_note_adoc = //... //end::include[] - classpathResources.getResource("comment-with-note.adoc"); + commentWithNoteDocument; //tag::include[] asciidoctor.javaExtensionRegistry().preprocessor(CommentPreprocessor.class); // <1> diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/CopyrightFooterPostprocessorTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/CopyrightFooterPostprocessorTest.java index fc45450de..4ae2817ba 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/CopyrightFooterPostprocessorTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/CopyrightFooterPostprocessorTest.java @@ -2,27 +2,26 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class CopyrightFooterPostprocessorTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } + @ClasspathResource("comment.adoc") + private File commentDocument; @Test public void should_render_comments_as_notes() { @@ -30,7 +29,7 @@ public void should_render_comments_as_notes() { //tag::include[] File doc = //... //end::include[] - classpathResources.getResource("comment.adoc"); + commentDocument; //tag::include[] asciidoctor.javaExtensionRegistry().postprocessor(CopyrightFooterPostprocessor.class); // <1> diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/GistBlockMacroProcessorTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/GistBlockMacroProcessorTest.java index b22570ecc..774705179 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/GistBlockMacroProcessorTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/GistBlockMacroProcessorTest.java @@ -2,35 +2,33 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class GistBlockMacroProcessorTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } @Test - public void should_create_script_element_for_block_macro() { + public void should_create_script_element_for_block_macro(@ClasspathResource("gist-macro.adoc") File gistMacroDocument) { //tag::include[] File gistmacro_adoc = //... //end::include[] - classpathResources.getResource("gist-macro.adoc"); + gistMacroDocument; + //tag::include[] asciidoctor.javaExtensionRegistry().blockMacro(GistBlockMacroProcessor.class); // <1> @@ -44,9 +42,8 @@ public void should_create_script_element_for_block_macro() { } @Test - public void should_create_script_element_for_block_macro_with_positional_attributes() { + public void should_create_script_element_for_block_macro_with_positional_attributes(@ClasspathResource("gist-macro-attributes.adoc") File gistmacro_adoc) { - File gistmacro_adoc = classpathResources.getResource("gist-macro-attributes.adoc"); asciidoctor.javaExtensionRegistry().blockMacro(GistBlockMacroPositionalAttributesProcessor.class); String result = asciidoctor.convertFile(gistmacro_adoc, Options.builder().toFile(false).build()); diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/ImageInlineMacroProcessorTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/ImageInlineMacroProcessorTest.java index a3b0dcfbf..32b05d452 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/ImageInlineMacroProcessorTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/ImageInlineMacroProcessorTest.java @@ -3,25 +3,20 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.SafeMode; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; - +@ExtendWith(AsciidoctorExtension.class) public class ImageInlineMacroProcessorTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } @Test public void should_create_context_menu() { diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/IssueInlineMacroProcessorTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/IssueInlineMacroProcessorTest.java index 44592515f..5ffb03d6b 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/IssueInlineMacroProcessorTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/IssueInlineMacroProcessorTest.java @@ -2,35 +2,34 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class IssueInlineMacroProcessorTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } @Test - public void should_create_anchor_elements_for_inline_macros() { + public void should_create_anchor_elements_for_inline_macros( + @ClasspathResource("issue-inline-macro.adoc") File issueinlinemacro) { //tag::include[] File issueinlinemacro_adoc = //... //end::include[] - classpathResources.getResource("issue-inline-macro.adoc"); + issueinlinemacro; + //tag::include[] asciidoctor.javaExtensionRegistry().inlineMacro(IssueInlineMacroProcessor.class); // <1> @@ -50,10 +49,11 @@ public void should_create_anchor_elements_for_inline_macros() { } @Test - public void should_create_anchor_elements_for_inline_macros_with_positional_attributes() { + public void should_create_anchor_elements_for_inline_macros_with_positional_attributes( + @ClasspathResource("issue-inline-macro-positional.adoc") File issueinlinemacro) { File issueinlinemacro_adoc = //... - classpathResources.getResource("issue-inline-macro-positional.adoc"); + issueinlinemacro; asciidoctor.javaExtensionRegistry().inlineMacro(IssueInlineMacroPositionalAttributesProcessor.class); diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/KeyboardInlineMacroProcessorTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/KeyboardInlineMacroProcessorTest.java index 13f848140..11a1d46f9 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/KeyboardInlineMacroProcessorTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/KeyboardInlineMacroProcessorTest.java @@ -3,25 +3,20 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.SafeMode; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; - +@ExtendWith(AsciidoctorExtension.class) public class KeyboardInlineMacroProcessorTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } @Test public void should_create_keyboard_elements() { diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/LoggingBlockMacroProcessorTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/LoggingBlockMacroProcessorTest.java index 3c703009b..0e81e5c82 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/LoggingBlockMacroProcessorTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/LoggingBlockMacroProcessorTest.java @@ -3,9 +3,12 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.log.LogRecord; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import java.util.ArrayList; @@ -14,18 +17,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class LoggingBlockMacroProcessorTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } + @ClasspathResource("logging-macro.adoc") + private File loggingmacroDocument; + @Test public void should_log_from_extension() { @@ -33,7 +33,8 @@ public void should_log_from_extension() { //tag::include[] File loggingmacro_adoc = // ... //end::include[] - classpathResources.getResource("logging-macro.adoc"); + loggingmacroDocument; + //tag::include[] List logRecords = new ArrayList<>(); asciidoctor.registerLogHandler(logRecords::add); diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/LsIncludeProcessorTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/LsIncludeProcessorTest.java index a728ac652..7809280e4 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/LsIncludeProcessorTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/LsIncludeProcessorTest.java @@ -2,27 +2,27 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class LsIncludeProcessorTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } + @ClasspathResource("ls-include.adoc") + private File lsincludeDocument; + @Test public void should_create_anchor_elements_for_inline_macros() { @@ -30,7 +30,8 @@ public void should_create_anchor_elements_for_inline_macros() { //tag::include[] File lsinclude_adoc = //... //end::include[] - classpathResources.getResource("ls-include.adoc"); + lsincludeDocument; + //tag::include[] String firstFileName = new File(".").listFiles()[0].getName(); diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/RobotsDocinfoProcessorTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/RobotsDocinfoProcessorTest.java index 9fa67546c..41eebd676 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/RobotsDocinfoProcessorTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/RobotsDocinfoProcessorTest.java @@ -3,27 +3,22 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.SafeMode; -import org.asciidoctor.util.ClasspathHelper; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.extension.AsciidoctorExtension; import org.jsoup.Jsoup; import org.jsoup.nodes.Element; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; - +@ExtendWith(AsciidoctorExtension.class) public class RobotsDocinfoProcessorTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } @Test public void should_create_anchor_elements_for_inline_macros() { diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/TerminalCommandTreeprocessorTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/TerminalCommandTreeprocessorTest.java index e9383ff6d..3163f23f0 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/TerminalCommandTreeprocessorTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/TerminalCommandTreeprocessorTest.java @@ -3,35 +3,37 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.OptionsBuilder; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class TerminalCommandTreeprocessorTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } + @ClasspathResource("treeprocessorresult.adoc") + private File referenceDocument; + + @ClasspathResource("treeprocessorcontent.adoc") + private File treeprocessorContent; + @Test public void should_process_terminal_listings() { - File referenceDocument = classpathResources.getResource("treeprocessorresult.adoc"); - String referenceResult = asciidoctor.convertFile( referenceDocument, OptionsBuilder.options() @@ -41,7 +43,7 @@ public void should_process_terminal_listings() { //tag::include[] File src = //... //end::include[] - classpathResources.getResource("treeprocessorcontent.adoc"); + treeprocessorContent; //tag::include[] asciidoctor.javaExtensionRegistry() @@ -57,9 +59,8 @@ public void should_process_terminal_listings() { } @Test - public void should_process_terminal_listings_after_registering_via_extension_registry() throws Exception { - - File referenceDocument = classpathResources.getResource("treeprocessorresult.adoc"); + public void should_process_terminal_listings_after_registering_via_extension_registry( + @ClasspathResource("extensionregistry") File extensionRegistryDir) throws MalformedURLException { String referenceResult = asciidoctor.convertFile( referenceDocument, @@ -70,13 +71,13 @@ public void should_process_terminal_listings_after_registering_via_extension_reg //tag::include-extension-registry[] File src = //... //end::include-extension-registry[] - classpathResources.getResource("treeprocessorcontent.adoc"); + treeprocessorContent; ClassLoader oldTCCL = Thread.currentThread().getContextClassLoader(); try { - URL serviceDir = classpathResources.getResource("extensionregistry").toURI().toURL(); + URL serviceDir = extensionRegistryDir.toURI().toURL(); URLClassLoader tccl = new URLClassLoader(new URL[]{serviceDir}); Thread.currentThread().setContextClassLoader(tccl); diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/YellBlockProcessorTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/YellBlockProcessorTest.java index 8a4688295..244bb1681 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/YellBlockProcessorTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/YellBlockProcessorTest.java @@ -3,34 +3,31 @@ import org.asciidoctor.Asciidoctor; import org.asciidoctor.Options; import org.asciidoctor.OptionsBuilder; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import java.io.File; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class YellBlockProcessorTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } @Test - public void should_invoke_block_processor() { + public void should_invoke_block_processor(@ClasspathResource("yell-block.adoc") File yellBlock) { //tag::include[] File yellblock_adoc = //... //end::include[] - classpathResources.getResource("yell-block.adoc"); + yellBlock; //tag::include[] @@ -43,9 +40,9 @@ public void should_invoke_block_processor() { } @Test - public void should_invoke_block_processor_with_attributes() { + public void should_invoke_block_processor_with_attributes(@ClasspathResource("yell-block-attributes.adoc") File yellBlock) { File yellblock_adoc = //... - classpathResources.getResource("yell-block-attributes.adoc"); + yellBlock; asciidoctor.javaExtensionRegistry().block(YellBlockProcessorWithAttributes.class); @@ -55,9 +52,9 @@ public void should_invoke_block_processor_with_attributes() { } @Test - public void should_invoke_block_processor_with_positional_attributes() { + public void should_invoke_block_processor_with_positional_attributes(@ClasspathResource("yell-block-positional.adoc") File yellBlock) { File yellblock_adoc = //... - classpathResources.getResource("yell-block-positional.adoc"); + yellBlock; asciidoctor.javaExtensionRegistry().block(YellBlockProcessorWithPositionalAttributes.class); diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/HighlightJsHighlighterTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/HighlightJsHighlighterTest.java index 893fc814f..494900355 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/HighlightJsHighlighterTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/HighlightJsHighlighterTest.java @@ -1,44 +1,40 @@ package org.asciidoctor.integrationguide.syntaxhighlighter; -import org.apache.commons.io.IOUtils; -import org.asciidoctor.Asciidoctor; -import org.asciidoctor.AttributesBuilder; -import org.asciidoctor.OptionsBuilder; -import org.asciidoctor.SafeMode; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.*; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; import java.io.File; -import java.io.FileReader; +import java.nio.file.Files; +import java.nio.file.Path; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertTrue; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class HighlightJsHighlighterTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; + + @ClasspathResource("sources.adoc") + private File sourcesDocument; @TempDir public File tempDir; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } - @Test public void should_invoke_syntax_highlighter() { //tag::include[] File sources_adoc = //... //end::include[] - classpathResources.getResource("sources.adoc"); + sourcesDocument; //tag::include[] @@ -49,7 +45,7 @@ public void should_invoke_syntax_highlighter() { OptionsBuilder.options() .standalone(true) // <2> .toFile(false) - .attributes(AttributesBuilder.attributes().sourceHighlighter("myhighlightjs"))); // <3> + .attributes(Attributes.builder().sourceHighlighter("myhighlightjs").build())); // <3> assertThat(result, containsString("")); @@ -58,9 +54,7 @@ public void should_invoke_syntax_highlighter() { @Test public void should_invoke_syntax_highlighter_with_3_params() { - File sources_adoc = - classpathResources.getResource("sources.adoc"); - + File sources_adoc = sourcesDocument; asciidoctor.syntaxHighlighterRegistry() .register(org.asciidoctor.integrationguide.syntaxhighlighter.threeparams.HighlightJsHighlighter.class, "myhighlightjs"); @@ -77,9 +71,7 @@ public void should_invoke_syntax_highlighter_with_3_params() { @Test public void should_invoke_formatting_syntax_highlighter() { - File sources_adoc = - classpathResources.getResource("sources.adoc"); - + File sources_adoc = sourcesDocument; //tag::includeformatter[] @@ -90,7 +82,7 @@ public void should_invoke_formatting_syntax_highlighter() { OptionsBuilder.options() .standalone(true) .toFile(false) - .attributes(AttributesBuilder.attributes().sourceHighlighter("myhighlightjs"))); + .attributes(Attributes.builder().sourceHighlighter("myhighlightjs").build())); assertThat(result, containsString("")); @@ -101,8 +93,7 @@ public void should_invoke_formatting_syntax_highlighter() { @Test public void should_invoke_stylesheet_writing_syntax_highlighter() throws Exception { - File sources_adoc = - classpathResources.getResource("sources.adoc"); + File sources_adoc = sourcesDocument; //tag::includestylesheetwriter[] File toDir = // ... @@ -132,11 +123,9 @@ public void should_invoke_stylesheet_writing_syntax_highlighter() throws Excepti File jsFile = new File(toDir, "highlight.min.js"); assertTrue(jsFile.exists()); - try (FileReader docReader = new FileReader(new File(toDir, "sources.html"))) { - String html = IOUtils.toString(docReader); - assertThat(html, containsString("")); - assertThat(html, containsString("")); - } + String html = Files.readString(Path.of(toDir.toURI()).resolve("sources.html")); + assertThat(html, containsString("")); + assertThat(html, containsString("")); //end::includestylesheetwriter[] } diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/HighlightJsWithOfflineStylesHighlighter.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/HighlightJsWithOfflineStylesHighlighter.java index 54bb01d1d..184167ed6 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/HighlightJsWithOfflineStylesHighlighter.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/HighlightJsWithOfflineStylesHighlighter.java @@ -8,15 +8,9 @@ import org.asciidoctor.syntaxhighlighter.StylesheetWriter; import org.asciidoctor.syntaxhighlighter.SyntaxHighlighterAdapter; -import javax.net.ssl.HttpsURLConnection; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.net.MalformedURLException; import java.net.URL; -import java.net.URLConnection; import java.util.Map; //tag::include[] @@ -78,4 +72,4 @@ public void writeStylesheet(Document doc, File toDir) { } } } -//end::include[] \ No newline at end of file +//end::include[] diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/OrderTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/OrderTest.java index 3ad2e2757..7c02046f7 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/OrderTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/OrderTest.java @@ -4,9 +4,12 @@ import org.asciidoctor.Attributes; import org.asciidoctor.Options; import org.asciidoctor.SafeMode; -import org.asciidoctor.util.ClasspathHelper; -import org.junit.jupiter.api.BeforeEach; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.CleanupMode; import org.junit.jupiter.api.io.TempDir; @@ -16,27 +19,28 @@ import static org.apache.commons.io.FileUtils.readFileToString; import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class OrderTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; + + @ClasspathResource("syntax-highlighting-order.adoc") + private File syntaxHighlightingDocument; + + @ClasspathResource("syntax-highlighting-order-output.txt") + private File syntaxHighlightingOutput; @TempDir(cleanup = CleanupMode.NEVER) public File tempDir; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } @Test public void should_invoke_syntax_highlighter() throws Exception { File sources_adoc = //... - classpathResources.getResource("syntax-highlighting-order.adoc"); + syntaxHighlightingDocument; String expectedHighlighterMessages = - readFileToString(classpathResources.getResource("syntax-highlighting-order-output.txt")) + readFileToString(syntaxHighlightingOutput) .replaceAll("\r\n", "\n"); File toDir = // ... diff --git a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/PrismJsHighlighterTest.java b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/PrismJsHighlighterTest.java index a1793ac1f..f5aeff5da 100644 --- a/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/PrismJsHighlighterTest.java +++ b/asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/syntaxhighlighter/PrismJsHighlighterTest.java @@ -4,12 +4,15 @@ import org.asciidoctor.AttributesBuilder; import org.asciidoctor.OptionsBuilder; import org.asciidoctor.SafeMode; -import org.asciidoctor.util.ClasspathHelper; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.ClasspathResource; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.asciidoctor.test.extension.ClasspathExtension; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.CleanupMode; import org.junit.jupiter.api.io.TempDir; @@ -18,28 +21,25 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; - +@ExtendWith({AsciidoctorExtension.class, ClasspathExtension.class}) public class PrismJsHighlighterTest { + @AsciidoctorInstance private Asciidoctor asciidoctor; - private ClasspathHelper classpathResources; + + @ClasspathResource("sources.adoc") + private File sourcesDocument; @TempDir(cleanup = CleanupMode.NEVER) public File tempDir; - @BeforeEach - public void beforeEach() { - asciidoctor = Asciidoctor.Factory.create(); - classpathResources = new ClasspathHelper(); - classpathResources.setClassloader(this.getClass()); - } @Test public void should_invoke_syntax_highlighter() throws Exception { //tag::include[] File sources_adoc = //... //end::include[] - classpathResources.getResource("sources.adoc"); + sourcesDocument; //tag::include[] File toDir = // ... diff --git a/asciidoctorj-test-support/build.gradle b/asciidoctorj-test-support/build.gradle index eafd4f734..3933d40ef 100644 --- a/asciidoctorj-test-support/build.gradle +++ b/asciidoctorj-test-support/build.gradle @@ -1,7 +1,11 @@ apply plugin: 'java' dependencies { - api "junit:junit:${junit4Version}" + api(platform("org.junit:junit-bom:$junit5Version")) + api "org.junit.jupiter:junit-jupiter-api" + + implementation project(':asciidoctorj-api') + testImplementation project(':asciidoctorj') } def javaApiUrl = "https://docs.oracle.com/javase/8/docs/api/" diff --git a/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/AsciidoctorInstance.java b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/AsciidoctorInstance.java new file mode 100644 index 000000000..9c1e7538e --- /dev/null +++ b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/AsciidoctorInstance.java @@ -0,0 +1,41 @@ +package org.asciidoctor.test; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_CLASS; + +/** + * Marks and {@link org.asciidoctor.Asciidoctor} property to be automatically injected. + */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface AsciidoctorInstance { + + /** + * When the {@link org.asciidoctor.Asciidoctor} instance will be initialized. + *

+ * Based on {@link org.junit.jupiter.api.TestInstance.Lifecycle}. + */ + enum InstanceScope { + + /** + * Once per class, before running any tests. The same instance will + * be shared across all tests. + */ + PER_CLASS, + /** + * Once per test, before running every test method. + */ + PER_METHOD + } + + + /** + * Scope for the Asciidoctor instance being created. + */ + InstanceScope scope() default PER_CLASS; + +} diff --git a/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/ClasspathResource.java b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/ClasspathResource.java new file mode 100644 index 000000000..f3e9abece --- /dev/null +++ b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/ClasspathResource.java @@ -0,0 +1,17 @@ +package org.asciidoctor.test; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The annotated resource will be initialized before the test is run. + */ +@Target({ElementType.FIELD, ElementType.PARAMETER}) +@Retention(RetentionPolicy.RUNTIME) +public @interface ClasspathResource { + + String value(); + +} diff --git a/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/AsciidoctorExtension.java b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/AsciidoctorExtension.java new file mode 100644 index 000000000..ab56c4f72 --- /dev/null +++ b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/AsciidoctorExtension.java @@ -0,0 +1,71 @@ +package org.asciidoctor.test.extension; + +import org.asciidoctor.Asciidoctor; +import org.asciidoctor.test.AsciidoctorInstance; +import org.asciidoctor.test.AsciidoctorInstance.InstanceScope; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; +import org.junit.jupiter.api.extension.TestInstancePostProcessor; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +import static org.asciidoctor.test.extension.ReflectionUtils.findFields; +import static org.asciidoctor.test.extension.ReflectionUtils.hasAnnotation; + + +/** + * Injects and Asciidoctor instance in any field of type {@link Asciidoctor} annotated with + * either {@link AsciidoctorInstance}. + *

+ * Note that each field will receive its own instance regardless of the initialization method. + */ +public class AsciidoctorExtension implements TestInstancePostProcessor, BeforeEachCallback { + + private static final Namespace TEST_CONTEXT_NAMESPACE = Namespace.create(AsciidoctorExtension.class); + + @Override + public void postProcessTestInstance(Object testInstance, ExtensionContext context) { + Class contextKey = context.getRequiredTestClass(); + getStore(context) + .getOrComputeIfAbsent(contextKey, + keyType -> { + final List asciidoctorFields = findFields(testInstance, Asciidoctor.class); + final List sharedFields = new ArrayList<>(); + final List notSharedFields = new ArrayList<>(); + for (Field field : asciidoctorFields) { + if (hasAnnotation(field, AsciidoctorInstance.class)) { + final InstanceScope scope = field.getAnnotation(AsciidoctorInstance.class).scope(); + switch (scope) { + case PER_CLASS: + sharedFields.add(field); + break; + case PER_METHOD: + default: + notSharedFields.add(field); + } + } + } + return new TestContextManager(sharedFields, notSharedFields); + }, + TestContextManager.class); + } + + private static ExtensionContext.Store getStore(ExtensionContext context) { + return context.getRoot().getStore(TEST_CONTEXT_NAMESPACE); + } + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + // https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-instance-lifecycle + context.getTestInstance() + .ifPresent(testInstance -> { + var tcm = getStore(context) + .get(context.getRequiredTestClass(), TestContextManager.class); + tcm.initSharedFields(testInstance); + tcm.initTestFields(testInstance); + }); + } +} diff --git a/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/ClasspathExtension.java b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/ClasspathExtension.java new file mode 100644 index 000000000..4d3b09407 --- /dev/null +++ b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/ClasspathExtension.java @@ -0,0 +1,133 @@ +package org.asciidoctor.test.extension; + +import org.asciidoctor.test.ClasspathResource; +import org.junit.jupiter.api.extension.*; +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; + +import java.io.File; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Parameter; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.asciidoctor.test.extension.ReflectionUtils.findFields; +import static org.asciidoctor.test.extension.ReflectionUtils.injectValue; + +/** + * Handles the initialization of {@link File} fields annotated with {@link ClasspathExtension}. + *

+ * To prevent classpath traversing fields are parsed and values are initialized + * only once and cached in {@link ClasspathExtension#postProcessTestInstance(Object, ExtensionContext)}. + * Then assigned on every test {@link ClasspathExtension#beforeEach(ExtensionContext)}. + *

+ * JUnit5's {@link org.junit.jupiter.api.extension.ExtensionContext.Store} is + * used to hande initialization in a safe way. + */ +public class ClasspathExtension implements TestInstancePostProcessor, BeforeEachCallback, ParameterResolver { + + private static final Namespace TEST_CONTEXT_NAMESPACE = Namespace.create(ClasspathExtension.class); + + @Override + public void postProcessTestInstance(Object testInstance, ExtensionContext context) { + Class contextKey = context.getRequiredTestClass(); + getStore(context) + .getOrComputeIfAbsent(contextKey, + keyType -> { + final List fileFields = findFields(testInstance, File.class, ClasspathResource.class); + final Map values = matchValues(testInstance, fileFields, File.class); + + final List pathFields = findFields(testInstance, Path.class, ClasspathResource.class); + final Map values2 = matchValues(testInstance, pathFields, Path.class); + + values2.forEach(values::put); + + return new ResourcesCache(values); + }, + ResourcesCache.class); + } + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + context.getTestInstance() + .ifPresent(testInstance -> { + getStore(context) + .get(context.getRequiredTestClass(), ResourcesCache.class) + .initializeFields(testInstance); + }); + } + + private static ExtensionContext.Store getStore(ExtensionContext context) { + return context.getRoot().getStore(TEST_CONTEXT_NAMESPACE); + } + + private Map matchValues(Object testInstance, List resourcesFields, Class targetType) { + final ClasspathHelper classpathHelper = new ClasspathHelper(testInstance.getClass()); + final Map assignedValues = new HashMap<>(); + for (Field field : resourcesFields) { + final String path = getAnnotationValue(field); + final File resource = classpathHelper.getResource(path); + assignedValues.put(field, new Resource(resource, targetType)); + } + return assignedValues; + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext context) { + boolean annotated = parameterContext.isAnnotated(ClasspathResource.class); + if (annotated && parameterContext.getDeclaringExecutable() instanceof Constructor) { + throw new ParameterResolutionException("@ClasspathResource is not supported on constructor parameters."); + } + return annotated; + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) throws ParameterResolutionException { + final Parameter parameter = parameterContext.getParameter(); + Class parameterType = parameter.getType(); + assertSupportedType(parameterType); + + final String path = getAnnotationValue(parameter); + final File resource = new ClasspathHelper(context.getRequiredTestClass()).getResource(path); + return parameterType == File.class ? resource : resource.toPath(); + } + + private static String getAnnotationValue(AnnotatedElement parameter) { + return parameter.getAnnotation(ClasspathResource.class).value(); + } + + private void assertSupportedType(Class type) { + if (type != File.class && type != Path.class) { + throw new ExtensionConfigurationException("Only File or Path are supported"); + } + } + + class Resource { + private final File file; + private final Class targetType; + + Resource(File file, Class targetType) { + this.file = file; + this.targetType = targetType; + } + } + + class ResourcesCache { + + private final Map cache; + + ResourcesCache(Map cache) { + this.cache = cache; + } + + public void initializeFields(Object testInstance) { + cache.forEach((field, resource) -> { + final Object value = resource.targetType == File.class ? resource.file : resource.file.toPath(); + injectValue(testInstance, field, value); + }); + } + } +} diff --git a/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/ClasspathHelper.java b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/ClasspathHelper.java new file mode 100644 index 000000000..a3e5de555 --- /dev/null +++ b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/ClasspathHelper.java @@ -0,0 +1,39 @@ +package org.asciidoctor.test.extension; + +import java.io.File; +import java.io.FileNotFoundException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +/** + * Helps to get files from the classpath. + */ +public class ClasspathHelper { + + private final ClassLoader classloader; + + public ClasspathHelper(Class cpReference) { + this.classloader = cpReference.getClassLoader(); + } + + /** + * Gets a resource in a similar way as {@link File#File(String)}. + */ + public File getResource(String path) { + return new File(getUri(path)); + } + + private URI getUri(String path) { + try { + URL resource = classloader.getResource(path); + if (resource != null) { + return classloader.getResource(path).toURI(); + } else { + throw new RuntimeException(new FileNotFoundException(path)); + } + } catch (URISyntaxException e) { + throw new RuntimeException(e.getMessage(), e); + } + } +} diff --git a/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/ReflectionUtils.java b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/ReflectionUtils.java new file mode 100644 index 000000000..4df1f8f1d --- /dev/null +++ b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/ReflectionUtils.java @@ -0,0 +1,45 @@ +package org.asciidoctor.test.extension; + +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +class ReflectionUtils { + + static boolean hasAnnotation(AnnotatedElement field, Class annotation) { + return field.getAnnotation(annotation) != null; + } + + static List findFields(Object instance, Class assignable) { + return Arrays.stream(instance.getClass().getDeclaredFields()) + .filter(field -> !Modifier.isFinal(field.getModifiers())) + .filter(field -> assignable.isAssignableFrom((field.getType()))) + .collect(Collectors.toList()); + } + + static List findFields(Object instance, Class assignable, Class annotation) { + return Arrays.stream(instance.getClass().getDeclaredFields()) + .filter(field -> !Modifier.isFinal(field.getModifiers())) + .filter(field -> assignable.isAssignableFrom(field.getType()) && hasAnnotation(field, annotation)) + .collect(Collectors.toList()); + } + + static void injectValue(Object target, Field field, Object value) { + try { + if (Modifier.isPrivate(field.getModifiers())) { + field.setAccessible(true); + field.set(target, value); + field.setAccessible(false); + } else { + field.set(target, value); + } + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/TestContextManager.java b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/TestContextManager.java new file mode 100644 index 000000000..83a85baa0 --- /dev/null +++ b/asciidoctorj-test-support/src/main/java/org/asciidoctor/test/extension/TestContextManager.java @@ -0,0 +1,30 @@ +package org.asciidoctor.test.extension; + +import org.asciidoctor.Asciidoctor; + +import java.lang.reflect.Field; +import java.util.List; + +import static org.asciidoctor.test.extension.ReflectionUtils.injectValue; + + +public class TestContextManager { + + private final List sharedFields; + private final List testFields; + private final Asciidoctor sharedInstance; + + public TestContextManager(List sharedFields, List testFields) { + this.sharedFields = sharedFields; + this.testFields = testFields; + this.sharedInstance = Asciidoctor.Factory.create(); + } + + public void initTestFields(Object testInstance) { + this.testFields.forEach(field -> injectValue(testInstance, field, Asciidoctor.Factory.create())); + } + + public void initSharedFields(Object testInstance) { + this.sharedFields.forEach(field -> injectValue(testInstance, field, sharedInstance)); + } +} diff --git a/asciidoctorj-test-support/src/main/java/org/asciidoctor/util/ClasspathHelper.java b/asciidoctorj-test-support/src/main/java/org/asciidoctor/util/ClasspathHelper.java deleted file mode 100644 index 06fc289c2..000000000 --- a/asciidoctorj-test-support/src/main/java/org/asciidoctor/util/ClasspathHelper.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.asciidoctor.util; - -import java.io.File; -import java.io.FileNotFoundException; -import java.net.URISyntaxException; -import java.net.URL; - -/** - * Helps to get files from the classpath. - */ -public class ClasspathHelper { - - private ClassLoader classloader; - - /** - * Gets a resource in a similar way as {@link File#File(String)} - */ - public File getResource(String pathname) { - try { - URL resource = classloader.getResource(pathname); - if (resource != null) { - return new File(classloader.getResource(pathname).toURI()); - } else { - throw new RuntimeException(new FileNotFoundException(pathname)); - } - } catch (URISyntaxException e) { - throw new RuntimeException(e.getMessage(), e); - } - } - - /** - * Gets a resource in a similar way as {@link File#File(String, String)} - */ - public File getResource(String parent, String child) { - return new File(getResource(parent), child); - } - - public ClassLoader getClassloader() { - return classloader; - } - - public void setClassloader(ClassLoader classloader) { - this.classloader = classloader; - } - - public void setClassloader(Class clazz) { - this.classloader = clazz.getClassLoader(); - } -} diff --git a/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/ClassPathResourceTest.java b/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/ClassPathResourceTest.java new file mode 100644 index 000000000..3a83b324d --- /dev/null +++ b/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/ClassPathResourceTest.java @@ -0,0 +1,67 @@ +package org.asciidoctor.test; + +import org.asciidoctor.test.extension.ClasspathExtension; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; + +import java.io.File; +import java.nio.file.Path; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(ClasspathExtension.class) +@TestMethodOrder(OrderAnnotation.class) +public class ClassPathResourceTest { + + private static final String RESOURCE_PATH = "test.txt"; + + + @Nested + class WhenResourceIsDeclaredAsFile { + + @ClasspathResource(RESOURCE_PATH) + private File resource; + + @Order(1) + @Test + void should_load_resource_from_classpath_as_field() { + assertResourceContent(resource.toPath()); + } + + @Order(2) + @Test + void should_load_resource_classpath_as_parameter(@ClasspathResource(RESOURCE_PATH) File param) { + assertResourceContent(param.toPath()); + assertResourceContent(resource.toPath()); + } + } + + @Nested + class WhenResourceIsDeclaredAsPath { + + @ClasspathResource(RESOURCE_PATH) + private Path resource; + + @Order(1) + @Test + void should_load_resource_from_classpath_as_field() { + assertResourceContent(resource); + } + + @Order(2) + @Test + void should_load_resource_classpath_as_parameter(@ClasspathResource(RESOURCE_PATH) Path param) { + assertResourceContent(param); + assertResourceContent(resource); + } + + } + + private void assertResourceContent(Path resource) { + assertThat(resource).content().contains("Simple resource located in the classpath"); + } +} diff --git a/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/ClassScopedAsciidoctorInstanceTest.java b/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/ClassScopedAsciidoctorInstanceTest.java new file mode 100644 index 000000000..1a84afe85 --- /dev/null +++ b/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/ClassScopedAsciidoctorInstanceTest.java @@ -0,0 +1,35 @@ +package org.asciidoctor.test; + +import org.asciidoctor.Asciidoctor; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(AsciidoctorExtension.class) +@TestMethodOrder(OrderAnnotation.class) +public class ClassScopedAsciidoctorInstanceTest { + + @AsciidoctorInstance + private Asciidoctor asciidoctor; + + private static Asciidoctor copy; + + @Order(1) + @Test + void should_inject_asciidoctor() { + assertThat(asciidoctor).isNotNull(); + copy = asciidoctor; + } + + @Order(2) + @Test + void should_not_initialize_asciidoctor() { + assertThat(asciidoctor).isNotNull(); + assertThat(asciidoctor).isSameAs(copy); + } +} diff --git a/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/MultipleAsciidoctorInstancesTest.java b/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/MultipleAsciidoctorInstancesTest.java new file mode 100644 index 000000000..3ef1fdb0b --- /dev/null +++ b/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/MultipleAsciidoctorInstancesTest.java @@ -0,0 +1,74 @@ +package org.asciidoctor.test; + +import org.asciidoctor.Asciidoctor; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; + +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_CLASS; +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(AsciidoctorExtension.class) +@TestMethodOrder(OrderAnnotation.class) +public class MultipleAsciidoctorInstancesTest { + + @AsciidoctorInstance + private Asciidoctor firstSharedAsciidoctor; + @AsciidoctorInstance(scope = PER_CLASS) + private Asciidoctor secondSharedAsciidoctor; + + @AsciidoctorInstance(scope = PER_METHOD) + private Asciidoctor firstMethodAsciidoctor; + @AsciidoctorInstance(scope = PER_METHOD) + private Asciidoctor secondMethodAsciidoctor; + + private static Asciidoctor copy; + + public MultipleAsciidoctorInstancesTest() { + } + + @Order(1) + @Test + void should_inject_asciidoctor() { + assertThat(firstSharedAsciidoctor).isNotNull(); + assertThat(secondSharedAsciidoctor) + .isNotNull() + .isSameAs(firstSharedAsciidoctor); + + assertThat(firstMethodAsciidoctor) + .isNotNull() + .isNotSameAs(firstSharedAsciidoctor) + .isNotSameAs(secondSharedAsciidoctor); + assertThat(secondMethodAsciidoctor) + .isNotNull() + .isNotSameAs(firstSharedAsciidoctor) + .isNotSameAs(secondSharedAsciidoctor); + assertThat(firstMethodAsciidoctor).isNotSameAs(secondMethodAsciidoctor); + + copy = firstSharedAsciidoctor; + } + + @Order(2) + @Test + void should_only_initialize_new_shared_asciidoctor_instances() { + assertThat(firstSharedAsciidoctor) + .isNotNull() + .isSameAs(secondSharedAsciidoctor) + .isSameAs(copy); + assertThat(secondSharedAsciidoctor) + .isNotNull() + .isSameAs(copy); + + assertThat(firstMethodAsciidoctor) + .isNotNull() + .isNotSameAs(copy); + assertThat(secondMethodAsciidoctor) + .isNotNull() + .isNotSameAs(copy); + assertThat(firstMethodAsciidoctor).isNotSameAs(secondMethodAsciidoctor); + } +} diff --git a/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/TestScopedAsciidoctorInstanceTest.java b/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/TestScopedAsciidoctorInstanceTest.java new file mode 100644 index 000000000..83d1eb7ff --- /dev/null +++ b/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/TestScopedAsciidoctorInstanceTest.java @@ -0,0 +1,36 @@ +package org.asciidoctor.test; + +import org.asciidoctor.Asciidoctor; +import org.asciidoctor.test.extension.AsciidoctorExtension; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; + +import static org.asciidoctor.test.AsciidoctorInstance.InstanceScope.PER_METHOD; +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(AsciidoctorExtension.class) +@TestMethodOrder(OrderAnnotation.class) +public class TestScopedAsciidoctorInstanceTest { + + @AsciidoctorInstance(scope = PER_METHOD) + private Asciidoctor asciidoctor; + + private static Asciidoctor copy; + + @Order(1) + @Test + void should_inject_asciidoctor() { + assertThat(asciidoctor).isNotNull(); + copy = asciidoctor; + } + + @Order(2) + @Test + void should_initialize_a_new_asciidoctor() { + assertThat(asciidoctor).isNotNull(); + assertThat(asciidoctor).isNotSameAs(copy); + } +} diff --git a/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/extension/ReflectionUtilsTest.java b/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/extension/ReflectionUtilsTest.java new file mode 100644 index 000000000..83b551ce8 --- /dev/null +++ b/asciidoctorj-test-support/src/test/java/org/asciidoctor/test/extension/ReflectionUtilsTest.java @@ -0,0 +1,62 @@ +package org.asciidoctor.test.extension; + +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; + +import static org.asciidoctor.test.extension.ReflectionUtils.injectValue; +import static org.assertj.core.api.Assertions.assertThat; + +public class ReflectionUtilsTest { + + @Test + void should_inject_a_private_field() throws NoSuchFieldException { + final TestClass instance = new TestClass(); + final Field field = instance.getClass().getDeclaredField("privateText"); + final String value = "uno"; + + injectValue(instance, field, value); + + assertThat(instance.privateText).isEqualTo(value); + } + + @Test + void should_inject_a_public_field() throws NoSuchFieldException { + final TestClass instance = new TestClass(); + final Field field = instance.getClass().getDeclaredField("publicText"); + final String value = "dos"; + + injectValue(instance, field, value); + + assertThat(instance.publicText).isEqualTo(value); + } + + @Test + void should_inject_a_protected_field() throws NoSuchFieldException { + final TestClass instance = new TestClass(); + final Field field = instance.getClass().getDeclaredField("protectedText"); + final String value = "tres"; + + injectValue(instance, field, value); + + assertThat(instance.protectedText).isEqualTo(value); + } + + @Test + void should_inject_a_package_protected_field() throws NoSuchFieldException { + final TestClass instance = new TestClass(); + final Field field = instance.getClass().getDeclaredField("packageProtectedText"); + final String value = "cuatro"; + + injectValue(instance, field, value); + + assertThat(instance.packageProtectedText).isEqualTo(value); + } + + class TestClass { + public String publicText; + private String privateText; + protected String protectedText; + String packageProtectedText; + } +} diff --git a/asciidoctorj-test-support/src/test/resources/test.txt b/asciidoctorj-test-support/src/test/resources/test.txt new file mode 100644 index 000000000..a4ba52faf --- /dev/null +++ b/asciidoctorj-test-support/src/test/resources/test.txt @@ -0,0 +1 @@ +Simple resource located in the classpath. diff --git a/build.gradle b/build.gradle index ee6e9ff6b..da775ace1 100644 --- a/build.gradle +++ b/build.gradle @@ -153,8 +153,6 @@ subprojects { } testImplementation "org.codehaus.groovy:groovy-all:$groovyVersion" - testRuntimeOnly("org.junit.vintage:junit-vintage-engine") - codenarc("org.codenarc:CodeNarc:$codenarcVersion") constraints { @@ -175,13 +173,6 @@ subprojects { } } - if (usesArquillian(it)) { - dependencies { - testImplementation "org.jboss.arquillian.junit:arquillian-junit-container:$arquillianVersion" - testImplementation "org.jboss.arquillian.spock:arquillian-spock-container:$arquillianSpockVersion" - } - } - test { useJUnitPlatform() @@ -206,11 +197,6 @@ boolean usesSpock(def project) { project.name in ['asciidoctorj'] } -boolean usesArquillian(def project) { - project.name in ['asciidoctorj'] -} - - // apply JRuby and sources/javadocs packaging stuff for all subprojects except the distribution configure(subprojects.findAll { !it.isDistribution() }) {