Skip to content

Commit

Permalink
(main) Handle mojos default value in tests (#756)
Browse files Browse the repository at this point in the history
* Refactor Mojo mocking logic in ParametersInitializer & MojoMocker
* Properly set default values in Mojos
  • Loading branch information
abelsromero authored Jan 25, 2024
1 parent 45bb8b2 commit 92f7120
Show file tree
Hide file tree
Showing 19 changed files with 452 additions and 112 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Build / Infrastructure::
* Use latest Maven and remove Dependabot exclusion (CI test ensure backward compatibility) (#722)
* Test artifact's signature with Maven in CI (#736)
* Automate release using GH Actions (#141)
* Ensure Mojos use correct default values in unit tests (#609)


Maintenance::
* Replace use of reflection by direct JavaExtensionRegistry calls to register extensions (#596)
Expand Down
6 changes: 6 additions & 0 deletions asciidoctor-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<artifactId>netty-codec-http</artifactId>
<version>4.1.106.Final</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.14.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class AsciidoctorHttpMojo extends AsciidoctorRefreshMojo {

public static final String PREFIX = AsciidoctorMaven.PREFIX + "http.";

@Parameter(property = PREFIX + "port")
protected int port = 2000;
@Parameter(property = PREFIX + "port", defaultValue = "2000")
protected int port;

@Parameter(property = PREFIX + "home", defaultValue = "index")
protected String home;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.asciidoctor.*;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Attributes;
import org.asciidoctor.AttributesBuilder;
import org.asciidoctor.Options;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.SafeMode;
import org.asciidoctor.jruby.AsciidoctorJRuby;
import org.asciidoctor.jruby.internal.JRubyRuntimeContext;
import org.asciidoctor.maven.commons.AsciidoctorHelper;
Expand All @@ -29,10 +34,18 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Logger;

import static org.asciidoctor.maven.commons.StringUtils.isBlank;
import static org.asciidoctor.maven.commons.StringUtils.isNotBlank;
import static org.asciidoctor.maven.process.SourceDirectoryFinder.DEFAULT_SOURCE_DIR;


Expand All @@ -52,10 +65,10 @@ public class AsciidoctorMojo extends AbstractMojo {
protected File outputFile;

@Parameter(property = AsciidoctorMaven.PREFIX + "preserveDirectories", defaultValue = "false")
protected boolean preserveDirectories = false;
protected boolean preserveDirectories;

@Parameter(property = AsciidoctorMaven.PREFIX + "relativeBaseDir", defaultValue = "false")
protected boolean relativeBaseDir = false;
protected boolean relativeBaseDir;

@Parameter(property = AsciidoctorMaven.PREFIX + "projectDirectory", defaultValue = "${basedir}")
protected File projectDirectory;
Expand All @@ -66,8 +79,8 @@ public class AsciidoctorMojo extends AbstractMojo {
@Parameter(property = AsciidoctorMaven.PREFIX + "baseDir")
protected File baseDir;

@Parameter(property = AsciidoctorMaven.PREFIX + "skip")
protected boolean skip = false;
@Parameter(property = AsciidoctorMaven.PREFIX + "skip", defaultValue = "false")
protected boolean skip;

@Parameter(property = AsciidoctorMaven.PREFIX + "gemPath")
protected String gemPath;
Expand All @@ -79,10 +92,10 @@ public class AsciidoctorMojo extends AbstractMojo {
protected Map<String, Object> attributes = new HashMap<>();

@Parameter(property = AsciidoctorMaven.PREFIX + Options.ATTRIBUTES)
protected String attributesChain = "";
protected String attributesChain;

@Parameter(property = AsciidoctorMaven.PREFIX + Options.BACKEND, defaultValue = "html5")
protected String backend = "html5";
protected String backend;

@Parameter(property = AsciidoctorMaven.PREFIX + Options.DOCTYPE)
protected String doctype;
Expand All @@ -91,41 +104,41 @@ public class AsciidoctorMojo extends AbstractMojo {
protected String eruby;

@Parameter(property = AsciidoctorMaven.PREFIX + "standalone", defaultValue = "true")
protected boolean standalone = true;
protected boolean standalone;

@Parameter(property = AsciidoctorMaven.PREFIX + "templateDirs")
protected List<File> templateDirs = new ArrayList<>();

@Parameter(property = AsciidoctorMaven.PREFIX + "templateEngine")
protected String templateEngine;

@Parameter(property = AsciidoctorMaven.PREFIX + "templateCache")
protected boolean templateCache = true;
@Parameter(property = AsciidoctorMaven.PREFIX + "templateCache", defaultValue = "true")
protected boolean templateCache;

@Parameter(property = AsciidoctorMaven.PREFIX + "sourceDocumentName")
protected String sourceDocumentName;

@Parameter(property = AsciidoctorMaven.PREFIX + "sourceDocumentExtensions")
protected List<String> sourceDocumentExtensions = new ArrayList<>();

@Parameter(property = AsciidoctorMaven.PREFIX + "sourcemap")
protected boolean sourcemap = false;
@Parameter(property = AsciidoctorMaven.PREFIX + "sourcemap", defaultValue = "false")
protected boolean sourcemap;

@Parameter(property = AsciidoctorMaven.PREFIX + "catalogAssets")
protected boolean catalogAssets = false;
@Parameter(property = AsciidoctorMaven.PREFIX + "catalogAssets", defaultValue = "false")
protected boolean catalogAssets;

@Parameter
protected List<ExtensionConfiguration> extensions = new ArrayList<>();

@Parameter(property = AsciidoctorMaven.PREFIX + "embedAssets", defaultValue = "false")
protected boolean embedAssets = false;
protected boolean embedAssets;

// List of resources to copy to the output directory (e.g., images, css). By default everything is copied
// List of resources to copy to the output directory (e.g., images, css). By default, everything is copied
@Parameter
protected List<Resource> resources;

@Parameter(property = AsciidoctorMaven.PREFIX + "verbose", defaultValue = "false")
protected boolean enableVerbose = false;
protected boolean enableVerbose;

@Parameter
private LogHandler logHandler = new LogHandler();
Expand Down Expand Up @@ -455,7 +468,7 @@ protected AttributesBuilder createAttributesBuilder(AsciidoctorMojo configuratio
AsciidoctorHelper.addProperties(mavenProject.getProperties(), attributesBuilder);
AsciidoctorHelper.addAttributes(configuration.getAttributes(), attributesBuilder);

if (!configuration.getAttributesChain().isEmpty()) {
if (isNotBlank(configuration.getAttributesChain())) {
getLog().info("Attributes: " + attributesChain);
attributesBuilder.arguments(attributesChain);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,32 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.asciidoctor.maven.refresh.*;
import org.asciidoctor.maven.refresh.AdditionalSourceFileAlterationListenerAdaptor;
import org.asciidoctor.maven.refresh.AsciidoctorConverterFileAlterationListenerAdaptor;
import org.asciidoctor.maven.refresh.ResourceCopyFileAlterationListenerAdaptor;
import org.asciidoctor.maven.refresh.ResourcesPatternBuilder;
import org.asciidoctor.maven.refresh.TimeCounter;

import java.io.File;
import java.io.FileFilter;
import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Scanner;
import java.util.StringJoiner;

import static org.asciidoctor.maven.commons.StringUtils.isNotBlank;
import static org.asciidoctor.maven.process.SourceDocumentFinder.*;
import static org.asciidoctor.maven.process.SourceDocumentFinder.CUSTOM_FILE_EXTENSIONS_PATTERN_PREFIX;
import static org.asciidoctor.maven.process.SourceDocumentFinder.CUSTOM_FILE_EXTENSIONS_PATTERN_SUFFIX;
import static org.asciidoctor.maven.process.SourceDocumentFinder.STANDARD_FILE_EXTENSIONS_PATTERN;

@Mojo(name = "auto-refresh")
public class AsciidoctorRefreshMojo extends AsciidoctorMojo {

public static final String PREFIX = AsciidoctorMaven.PREFIX + "refresher.";

@Parameter(property = PREFIX + "interval")
protected int interval = 2000; // 2s
@Parameter(property = PREFIX + "interval", defaultValue = "2000")
protected int interval;

@Parameter(property = PREFIX + "refreshOn")
protected String refreshOn;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package org.asciidoctor.maven;

import lombok.SneakyThrows;
import org.assertj.core.api.AbstractFileAssert;
import org.assertj.core.api.AbstractStringAssert;
import org.assertj.core.api.Assertions;

import java.io.File;
import java.nio.file.Files;

public class AsciidoctorAsserter {

private final AbstractFileAssert<?> fileAssert;
private final AbstractStringAssert<?> contentAssert;

@SneakyThrows
private AsciidoctorAsserter(File generatedFile) {
this.fileAssert = Assertions.assertThat(generatedFile);
this.contentAssert = Assertions.assertThat(TestUtils.readAsString(generatedFile));
this.contentAssert = Assertions.assertThat(Files.readString(generatedFile.toPath()));
}

public static AsciidoctorAsserter assertThat(File file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.net.URL;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.asciidoctor.maven.TestUtils.mockAsciidoctorHttpMojo;
import static org.asciidoctor.maven.test.TestUtils.mockAsciidoctorHttpMojo;
import static org.assertj.core.api.Assertions.assertThat;

class AsciidoctorHttpMojoTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import java.util.Map;

import static org.asciidoctor.maven.AsciidoctorAsserter.assertThat;
import static org.asciidoctor.maven.TestUtils.ResourceBuilder.excludeAll;
import static org.asciidoctor.maven.TestUtils.mockAsciidoctorMojo;
import static org.asciidoctor.maven.test.TestUtils.ResourceBuilder.excludeAll;
import static org.asciidoctor.maven.test.TestUtils.mockAsciidoctorMojo;
import static org.asciidoctor.maven.io.TestFilesHelper.newOutputTestDirectory;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Map;

import static java.util.Collections.singletonList;
import static org.asciidoctor.maven.TestUtils.mockAsciidoctorMojo;
import static org.asciidoctor.maven.test.TestUtils.mockAsciidoctorMojo;
import static org.asciidoctor.maven.io.TestFilesHelper.newOutputTestDirectory;
import static org.assertj.core.api.Assertions.assertThat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import static org.asciidoctor.log.Severity.ERROR;
import static org.asciidoctor.log.Severity.WARN;
import static org.asciidoctor.maven.TestUtils.mockAsciidoctorMojo;
import static org.asciidoctor.maven.test.TestUtils.mockAsciidoctorMojo;
import static org.asciidoctor.maven.io.TestFilesHelper.newOutputTestDirectory;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,28 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import static java.nio.file.Files.writeString;
import static java.util.Collections.singletonList;
import static org.asciidoctor.maven.AsciidoctorAsserter.assertThat;
import static org.asciidoctor.maven.TestUtils.*;
import static org.asciidoctor.maven.TestUtils.ResourceBuilder.excludeAll;
import static org.asciidoctor.maven.io.TestFilesHelper.newOutputTestDirectory;
import static org.asciidoctor.maven.test.TestUtils.ResourceBuilder;
import static org.asciidoctor.maven.test.TestUtils.ResourceBuilder.excludeAll;
import static org.asciidoctor.maven.test.TestUtils.assertEqualsStructure;
import static org.asciidoctor.maven.test.TestUtils.mockAsciidoctorMojo;


class AsciidoctorMojoTest {
Expand Down Expand Up @@ -156,7 +167,6 @@ void should_convert_to_html_with_attributes() throws MojoFailureException, MojoE
mojo.sourceDocumentName = "sample.asciidoc";
mojo.resources = excludeAll();
mojo.outputDirectory = outputDir;
mojo.standalone = true;
mojo.attributes = Map.of("toc", "",
"linkcss!", "",
"source-highlighter", "coderay");
Expand Down Expand Up @@ -283,15 +293,16 @@ void should_override_output_directory_with_output_file_with_absolute_path() thro
}

@Test
void should_set_file_extension() throws MojoFailureException, MojoExecutionException {
void should_set_file_extension() throws MojoFailureException, MojoExecutionException, IOException {
// given
File outputDir = newOutputTestDirectory();
Assertions.assertThat(outputDir).doesNotExist();

File srcDir = new File(DEFAULT_SOURCE_DIRECTORY);
outputDir.mkdirs();
writeToFile(srcDir, "sample1.foo", "= Document Title\n\nfoo");
writeToFile(srcDir, "sample2.bar", "= Document Title\n\nbar");

writeString(Path.of(DEFAULT_SOURCE_DIRECTORY, "sample1.foo"), "= Document Title\n\nfoo");
writeString(Path.of(DEFAULT_SOURCE_DIRECTORY, "sample2.bar"), "= Document Title\n\nbar");

// when
AsciidoctorMojo mojo = mockAsciidoctorMojo();
Expand Down Expand Up @@ -795,7 +806,6 @@ void should_embed_resources() throws MojoFailureException, MojoExecutionExceptio
.contains("i class=\"fa icon-tip\"");
}


// issue-78
@Test
void should_embed_image_in_included_adoc() throws MojoFailureException, MojoExecutionException {
Expand All @@ -817,7 +827,7 @@ void should_embed_image_in_included_adoc() throws MojoFailureException, MojoExec
assertThat(outputDir, "main.html")
.contains("<p>Here&#8217;s an image:</p>")
.contains("<img src=\"data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4gzESUNDX1BST0ZJTEUAAQEAAA");
assertThat(new File(outputDir, "halliburton_lab.jpg")).isNotEmpty();
Assertions.assertThat(new File(outputDir, "halliburton_lab.jpg")).isNotEmpty();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.asciidoctor.maven.TestUtils.ResourceBuilder;
import org.asciidoctor.maven.test.TestUtils.ResourceBuilder;
import org.asciidoctor.maven.io.ConsoleHolder;
import org.asciidoctor.maven.model.Resource;
import org.junit.jupiter.api.Test;
Expand All @@ -19,7 +19,7 @@
import java.util.stream.Collectors;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.asciidoctor.maven.TestUtils.newFakeRefreshMojo;
import static org.asciidoctor.maven.test.TestUtils.newFakeRefreshMojo;
import static org.asciidoctor.maven.io.TestFilesHelper.createFileWithContent;
import static org.asciidoctor.maven.io.TestFilesHelper.newOutputTestDirectory;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down
Loading

0 comments on commit 92f7120

Please sign in to comment.