Skip to content

Commit

Permalink
Port test to Junit5
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Apr 14, 2024
1 parent 65fbeab commit 8ef925b
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 142 deletions.
17 changes: 14 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@
<version>2.5.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -56,6 +55,18 @@
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
*/
package org.asciidoctor.asciidoclet;

import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.*;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

public class AsciidocletIntegrationTest {

Expand All @@ -33,7 +35,7 @@ public class AsciidocletIntegrationTest {
* --add-exports jdk.javadoc/jdk.javadoc.internal.tool=asciidoclet
*/
//@Test
public void testJavadocIntegration() throws Exception {
void testJavadocIntegration() throws Exception {
Method execute = Class.forName("jdk.javadoc.internal.tool.Main").getMethod("execute", String[].class);
execute.setAccessible(true);
String outputDirectory = "target/javadoc-output";
Expand All @@ -52,7 +54,7 @@ public void testJavadocIntegration() throws Exception {
"--base-dir", ".",
"org.asciidoctor.asciidoclet",
});
assertEquals(0, result);
assertThat(result).isEqualTo(0);
}

private void deleteRecursively(String outputDirectory) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,56 @@
*/
package org.asciidoctor.asciidoclet;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.asciidoctor.asciidoclet.AsciidoctorConverter.MARKER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

/**
* @author John Ericksen
*/
public class AsciidoctorConverterTest {
class AsciidoctorConverterTest {

private static final String LINEBREAK = "\r?\n";

private AsciidoctorConverter converter;
private StubReporter reporter = new StubReporter();

@Before
public void setup() {
@BeforeEach
void setup() {
DocletOptions options = new DocletOptions(reporter);
converter = new AsciidoctorConverter(options, reporter);
}

@Test
public void testAtLiteralRender() {
void testAtLiteralRender() {
String actual = converter.convert("{@literal @}Test");
assertThat(actual).matches(MARKER + "<p>\\{@literal @}Test</p>" + LINEBREAK);
}

@Test
public void testTagRender() {
void testTagRender() {
String actual = converter.convert("input\n@tagName tagText");
assertThat(actual).matches(MARKER + "<p>input</p>" + LINEBREAK + "@tagName tagText" + LINEBREAK);
}

@Test
public void testCleanInput() {
assertEquals("test1\ntest2", AsciidoctorConverter.cleanJavadocInput(" test1\n test2\n"));
assertEquals("/*\ntest\n*/", AsciidoctorConverter.cleanJavadocInput("/*\ntest\n*\\/"));
assertEquals("&#64;", AsciidoctorConverter.cleanJavadocInput("{at}"));
assertEquals("/", AsciidoctorConverter.cleanJavadocInput("{slash}"));
void testCleanInput() {
assertThat(AsciidoctorConverter.cleanJavadocInput(" test1\n test2\n")).isEqualTo("test1\ntest2");
assertThat(AsciidoctorConverter.cleanJavadocInput("/*\ntest\n*\\/")).isEqualTo("/*\ntest\n*/");
assertThat(AsciidoctorConverter.cleanJavadocInput("{at}")).isEqualTo("&#64;");
assertThat(AsciidoctorConverter.cleanJavadocInput("{slash}")).isEqualTo("/");
}

@Test
public void testComment() {
void testComment() {
assertThat(converter.convert("comment\n"))
.matches(MARKER + "<p>comment</p>" + LINEBREAK);
}

@Test
public void testParameterWithoutTypeTag() {
void testParameterWithoutTypeTag() {
assertThat(converter.convert("comment\n@param p description"))
.matches(MARKER + "<p>comment</p>" + LINEBREAK + "@param p description" + LINEBREAK);
assertThat(converter.convert("comment\n@param p"))
Expand All @@ -75,7 +74,7 @@ public void testParameterWithoutTypeTag() {
}

@Test
public void testParamTagWithTypeParameter() {
void testParamTagWithTypeParameter() {
String commentText = "comment";
String param1Name = "T";
String param1Text = "<" + param1Name + ">";
Expand Down
72 changes: 37 additions & 35 deletions src/test/java/org/asciidoctor/asciidoclet/AttributesLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,35 @@
package org.asciidoctor.asciidoclet;

import org.asciidoctor.Asciidoctor;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class AttributesLoaderTest {
class AttributesLoaderTest {

private final Asciidoctor asciidoctor = Asciidoctor.Factory.create();
private final StubReporter reporter = new StubReporter();

@Rule
public final TemporaryFolder tmpDir = new TemporaryFolder();
private Path tmpDir;

@BeforeEach
void before(@TempDir Path tmpDir) {
this.tmpDir = tmpDir;
}

@Test
public void testNoAttributes() {
void testNoAttributes() {
DocletOptions options = new DocletOptions(reporter);
AttributesLoader loader = new AttributesLoader(asciidoctor, options, reporter);

Expand All @@ -51,7 +55,7 @@ public void testNoAttributes() {
}

@Test
public void testOnlyCommandLineAttributes() {
void testOnlyCommandLineAttributes() {
DocletOptions options = new DocletOptions(reporter);
options.collect(AsciidocletOptions.ATTRIBUTE, List.of("foo=bar, foo2=foo-two, not!, override=override@"));
AttributesLoader loader = new AttributesLoader(asciidoctor, options, reporter);
Expand All @@ -69,7 +73,7 @@ public void testOnlyCommandLineAttributes() {
}

@Test
public void testOnlyCommandLineAttributesMulti() {
void testOnlyCommandLineAttributesMulti() {
DocletOptions options = new DocletOptions(reporter);
options.collect(AsciidocletOptions.ATTRIBUTE, List.of(
"foo=bar", "foo2=foo two", "not!", "override=override@"));
Expand All @@ -88,11 +92,11 @@ public void testOnlyCommandLineAttributesMulti() {
}

@Test
public void testOnlyAttributesFile() throws IOException {
File attrsFile = createTempFile("attrs.adoc", ATTRS);
void testOnlyAttributesFile() throws IOException {
Path attrsFile = createTempFile("attrs.adoc", ATTRS);

DocletOptions options = new DocletOptions(reporter);
options.collect(AsciidocletOptions.ATTRIBUTES_FILE, List.of(attrsFile.getAbsolutePath()));
options.collect(AsciidocletOptions.ATTRIBUTES_FILE, List.of(attrsFile.toAbsolutePath().toString()));
AttributesLoader loader = new AttributesLoader(asciidoctor, options, reporter);

Map<String, Object> attrs = loader.load();
Expand All @@ -106,12 +110,12 @@ public void testOnlyAttributesFile() throws IOException {
}

@Test
public void testCommandLineAndAttributesFile() throws IOException {
File attrsFile = createTempFile("attrs.adoc", ATTRS);
void testCommandLineAndAttributesFile() throws IOException {
Path attrsFile = createTempFile("attrs.adoc", ATTRS);

DocletOptions options = new DocletOptions(reporter);
options.collect(AsciidocletOptions.ATTRIBUTE, List.of("foo=bar, not!, override=override@"));
options.collect(AsciidocletOptions.ATTRIBUTES_FILE, List.of(attrsFile.getAbsolutePath()));
options.collect(AsciidocletOptions.ATTRIBUTES_FILE, List.of(attrsFile.toAbsolutePath().toString()));
AttributesLoader loader = new AttributesLoader(asciidoctor, options, reporter);

Map<String, Object> attrs = new HashMap<>(loader.load());
Expand All @@ -127,13 +131,13 @@ public void testCommandLineAndAttributesFile() throws IOException {
}

@Test
public void testAttributesFileIncludeFromBaseDir() throws IOException {
File attrsFile = createTempFile("attrs.adoc", "include::attrs-include.adoc[]");
void testAttributesFileIncludeFromBaseDir() throws IOException {
Path attrsFile = createTempFile("attrs.adoc", "include::attrs-include.adoc[]");
createTempFile("attrs-include.adoc", ATTRS);

DocletOptions options = new DocletOptions(reporter);
options.collect(AsciidocletOptions.ATTRIBUTES_FILE, List.of(attrsFile.getAbsolutePath()));
options.collect(AsciidocletOptions.BASEDIR, List.of(attrsFile.getParentFile().getAbsolutePath()));
options.collect(AsciidocletOptions.ATTRIBUTES_FILE, List.of(attrsFile.toAbsolutePath().toString()));
options.collect(AsciidocletOptions.BASEDIR, List.of(attrsFile.getParent().toAbsolutePath().toString()));
AttributesLoader loader = new AttributesLoader(asciidoctor, options, reporter);

Map<String, Object> attrs = loader.load();
Expand All @@ -148,13 +152,13 @@ public void testAttributesFileIncludeFromBaseDir() throws IOException {
}

@Test
public void testAttributesFileIncludeFromOtherDir() throws IOException {
File attrsFile = createTempFile("attrs.adoc", "include::{includedir}/attrs-include.adoc[]");
void testAttributesFileIncludeFromOtherDir() throws IOException {
Path attrsFile = createTempFile("attrs.adoc", "include::{includedir}/attrs-include.adoc[]");
createTempFile("foo", "attrs-include.adoc", ATTRS);

DocletOptions options = new DocletOptions(reporter);
options.collect(AsciidocletOptions.ATTRIBUTES_FILE, List.of(attrsFile.getAbsolutePath()));
options.collect(AsciidocletOptions.BASEDIR, List.of(attrsFile.getParentFile().getAbsolutePath()));
options.collect(AsciidocletOptions.ATTRIBUTES_FILE, List.of(attrsFile.toAbsolutePath().toString()));
options.collect(AsciidocletOptions.BASEDIR, List.of(attrsFile.getParent().toAbsolutePath().toString()));
options.collect(AsciidocletOptions.ATTRIBUTE, List.of("includedir=foo"));
AttributesLoader loader = new AttributesLoader(asciidoctor, options, reporter);

Expand All @@ -169,22 +173,20 @@ public void testAttributesFileIncludeFromOtherDir() throws IOException {
reporter.assertNoMoreInteractions();
}

private File createTempFile(String name, String content) throws IOException {
File file = tmpDir.newFile(name);
writeFile(content, file);
return file;
private Path createTempFile(String name, String content) throws IOException {
return writeFile(content, tmpDir.resolve(name));
}

private File createTempFile(String dir, String name, String content) throws IOException {
File directory = tmpDir.newFolder(dir);
File file = new File(directory, name);
writeFile(content, file);
return file;
private Path createTempFile(String dir, String name, String content) throws IOException {
Path directory = tmpDir.resolve(dir);
Files.createDirectories(directory);
return writeFile(content, directory.resolve(name));
}

private void writeFile(String content, File file) throws IOException {
private Path writeFile(String content, Path file) throws IOException {
byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
Files.write(file.toPath(), bytes, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
Files.write(file, bytes, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
return file;
}

static final String ATTRS =
Expand Down
Loading

0 comments on commit 8ef925b

Please sign in to comment.