Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(v3.0.x) Formatting and Java 11 refactors #657

Merged
merged 1 commit into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept

private MemoryLogHandler asciidoctorLoggingSetup(Asciidoctor asciidoctor, LogHandler logHandler, File siteDirectory) {

final MemoryLogHandler memoryLogHandler = new MemoryLogHandler(logHandler.getOutputToConsole(), siteDirectory,
final MemoryLogHandler memoryLogHandler = new MemoryLogHandler(logHandler.getOutputToConsole(),
logRecord -> getLog().info(LogRecordFormatter.format(logRecord, siteDirectory)));
asciidoctor.registerLogHandler(memoryLogHandler);
// disable default console output of AsciidoctorJ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

public class AsciidoctorConverterDoxiaParserTest {
class AsciidoctorConverterDoxiaParserTest {

private static final String TEST_DOCS_PATH = "src/test/resources/";

@Test
public void should_convert_html_without_any_configuration() throws FileNotFoundException, ParseException {
void should_convert_html_without_any_configuration() throws FileNotFoundException, ParseException {
final File srcAsciidoc = new File(TEST_DOCS_PATH, "sample.asciidoc");
final Sink sink = createSinkMock();

Expand All @@ -39,7 +39,7 @@ public void should_convert_html_without_any_configuration() throws FileNotFoundE
}

@Test
public void should_convert_html_with_an_attribute() throws FileNotFoundException, ParseException {
void should_convert_html_with_an_attribute() throws FileNotFoundException, ParseException {
final File srcAsciidoc = new File(TEST_DOCS_PATH, "sample.asciidoc");
Reader reader = new FileReader(srcAsciidoc);

Expand All @@ -60,7 +60,7 @@ public void should_convert_html_with_an_attribute() throws FileNotFoundException
}

@Test
public void should_convert_html_with_baseDir_option() throws FileNotFoundException, ParseException {
void should_convert_html_with_baseDir_option() throws FileNotFoundException, ParseException {
final File srcAsciidoc = new File(TEST_DOCS_PATH, "main-document.adoc");
final Sink sink = createSinkMock();

Expand All @@ -80,7 +80,7 @@ public void should_convert_html_with_baseDir_option() throws FileNotFoundExcepti
}

@Test
public void should_convert_html_with_relative_baseDir_option() throws FileNotFoundException, ParseException {
void should_convert_html_with_relative_baseDir_option() throws FileNotFoundException, ParseException {
final File srcAsciidoc = new File(TEST_DOCS_PATH, "main-document.adoc");
final Sink sink = createSinkMock();

Expand All @@ -100,7 +100,7 @@ public void should_convert_html_with_relative_baseDir_option() throws FileNotFou
}

@Test
public void should_convert_html_with_templateDir_option() throws FileNotFoundException, ParseException {
void should_convert_html_with_templateDir_option() throws FileNotFoundException, ParseException {
final File srcAsciidoc = new File(TEST_DOCS_PATH, "sample.asciidoc");
final Sink sink = createSinkMock();

Expand All @@ -121,7 +121,7 @@ public void should_convert_html_with_templateDir_option() throws FileNotFoundExc
}

@Test
public void should_convert_html_with_attributes_and_baseDir_option() throws FileNotFoundException, ParseException {
void should_convert_html_with_attributes_and_baseDir_option() throws FileNotFoundException, ParseException {
final File srcAsciidoc = new File(TEST_DOCS_PATH, "main-document.adoc");
final Sink sink = createSinkMock();

Expand Down Expand Up @@ -149,7 +149,7 @@ public void should_convert_html_with_attributes_and_baseDir_option() throws File
}

@Test
public void should_process_empty_selfclosing_XML_attributes() throws FileNotFoundException, ParseException {
void should_process_empty_selfclosing_XML_attributes() throws FileNotFoundException, ParseException {
final File srcAsciidoc = new File(TEST_DOCS_PATH, "sample.asciidoc");
final Sink sink = createSinkMock();

Expand All @@ -170,7 +170,7 @@ public void should_process_empty_selfclosing_XML_attributes() throws FileNotFoun
}

@Test
public void should_process_empty_value_XML_attributes() throws FileNotFoundException, ParseException {
void should_process_empty_value_XML_attributes() throws FileNotFoundException, ParseException {
final File srcAsciidoc = new File(TEST_DOCS_PATH, "sample.asciidoc");
final Sink sink = createSinkMock();

Expand All @@ -191,7 +191,7 @@ public void should_process_empty_value_XML_attributes() throws FileNotFoundExcep
}

@Test
public void should_fail_when_logHandler_failIf_is_WARNING() {
void should_fail_when_logHandler_failIf_is_WARNING() {
final File srcAsciidoc = new File(TEST_DOCS_PATH, "errors/document-with-missing-include.adoc");
final Sink sink = createSinkMock();

Expand Down Expand Up @@ -243,7 +243,7 @@ private Sink createSinkMock() {
}

class TextProviderSink extends AbstractTextSink {
public String text;
String text;

@Override
public void rawText(String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ else if (value instanceof Boolean) {
attributesBuilder.attribute(attribute, value);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import org.asciidoctor.log.LogRecord;
import org.asciidoctor.log.Severity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;


/**
Expand All @@ -20,12 +20,10 @@ public class MemoryLogHandler implements LogHandler {
final List<LogRecord> records = new ArrayList<>();

private final Boolean outputToConsole;
private final File sourceDirectory;
private final Consumer<LogRecord> recordConsumer;

public MemoryLogHandler(Boolean outputToConsole, File sourceDirectory, Consumer<LogRecord> recordConsumer) {
public MemoryLogHandler(Boolean outputToConsole, Consumer<LogRecord> recordConsumer) {
this.outputToConsole = outputToConsole == null ? Boolean.FALSE : outputToConsole;
this.sourceDirectory = sourceDirectory;
this.recordConsumer = recordConsumer;
}

Expand All @@ -43,17 +41,13 @@ public void clear() {
/**
* Returns LogRecords that are equal or above the severity level.
*
* @param severity Asciidoctor severity level
* @param severity Asciidoctor's severity level
* @return list of filtered logRecords
*/
public List<LogRecord> filter(Severity severity) {
// FIXME: find better name or replace with stream
final List<LogRecord> records = new ArrayList<>();
for (LogRecord record : this.records) {
if (record.getSeverity().ordinal() >= severity.ordinal())
records.add(record);
}
return records;
return this.records.stream()
.filter(record -> severityIsHigher(record, severity))
.collect(Collectors.toList());
}

/**
Expand All @@ -63,28 +57,29 @@ public List<LogRecord> filter(Severity severity) {
* @return list of filtered logRecords
*/
public List<LogRecord> filter(String text) {
final List<LogRecord> records = new ArrayList<>();
for (LogRecord record : this.records) {
if (record.getMessage().contains(text))
records.add(record);
}
return records;
return this.records.stream()
.filter(record -> messageContains(record, text))
.collect(Collectors.toList());
}

/**
* Returns LogRecords that are equal or above the severity level and whose message contains text.
*
* @param severity Asciidoctor severity level
* @param severity Asciidoctor's severity level
* @param text text to search for in the LogRecords
* @return list of filtered logRecords
*/
public List<LogRecord> filter(Severity severity, String text) {
final List<LogRecord> records = new ArrayList<>();
for (LogRecord record : this.records) {
if (record.getSeverity().ordinal() >= severity.ordinal() && record.getMessage().contains(text))
records.add(record);
}
return records;
return this.records.stream()
.filter(record -> severityIsHigher(record, severity) && messageContains(record, text))
.collect(Collectors.toList());
}

private static boolean severityIsHigher(LogRecord record, Severity severity) {
return record.getSeverity().ordinal() >= severity.ordinal();
}

private static boolean messageContains(LogRecord record, String text) {
return record.getMessage().contains(text);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.asciidoctor.maven.commons;

import org.asciidoctor.Attributes;
import org.asciidoctor.AttributesBuilder;
import org.assertj.core.data.MapEntry;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import static org.asciidoctor.maven.commons.AsciidoctorHelper.addAttributes;
import static org.assertj.core.api.Assertions.assertThat;

public class AsciidoctorHelperTest {

@ParameterizedTest
@MethodSource("specialAttributes")
void should_add_attributes_with_special_values(Object actual, String expected) {
final Map<String, Object> attributes = new HashMap<>();
attributes.put("toc", actual);
final AttributesBuilder attributesBuilder = Attributes.builder();

addAttributes(attributes, attributesBuilder);

var attributesAsMap = attributesBuilder.build().map();
assertThat(attributesAsMap)
.containsExactly(MapEntry.entry("toc", expected));
}

private static Stream<Arguments> specialAttributes() {
return Stream.of(
Arguments.of(null, ""),
Arguments.of("", ""),
Arguments.of("true", ""),
Arguments.of("false", null),
Arguments.of(true, ""),
Arguments.of(false, null)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import static org.asciidoctor.maven.commons.StringUtils.isBlank;
import static org.assertj.core.api.Assertions.assertThat;

public class StringUtilsTest {
class StringUtilsTest {

@Test
void should_detect_null_as_blank_string() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;

public class LogRecordFormatterTest {
class LogRecordFormatterTest {

private static final String PROJECT_NAME = "asciidoctor-maven-commons";

@Test
public void should_apply_full_format_logRecord_with_all_data() {
void should_apply_full_format_logRecord_with_all_data() {
// given
final Cursor cursor = new TestCursor(new File("file.adoc").getAbsolutePath(), 3, "path", "dir");
final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message");
Expand All @@ -29,7 +29,7 @@ public void should_apply_full_format_logRecord_with_all_data() {
}

@Test
public void should_apply_simple_format_when_cursor_is_null() {
void should_apply_simple_format_when_cursor_is_null() {
// given
final LogRecord logRecord = new LogRecord(Severity.INFO, null, "a message");
// when
Expand All @@ -39,7 +39,7 @@ public void should_apply_simple_format_when_cursor_is_null() {
}

@Test
public void should_apply_simple_format_when_cursor_is_empty() {
void should_apply_simple_format_when_cursor_is_empty() {
// given
final Cursor cursor = new TestCursor(null, 0, null, null);
final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message");
Expand All @@ -50,7 +50,7 @@ public void should_apply_simple_format_when_cursor_is_empty() {
}

@Test
public void should_format_full_logRecord_with_file_absolute_path_when_sourceDir_is_not_valid() throws IOException {
void should_format_full_logRecord_with_file_absolute_path_when_sourceDir_is_not_valid() throws IOException {
// given
final Cursor cursor = new TestCursor(new File("file.adoc").getAbsolutePath(), 3, "path", "dir");
final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message");
Expand All @@ -63,7 +63,7 @@ public void should_format_full_logRecord_with_file_absolute_path_when_sourceDir_
}

@Test
public void should_format_logRecords_with_empty_lineNumber_absolute_path_when_sourceDir_is_not_valid() throws IOException {
void should_format_logRecords_with_empty_lineNumber_absolute_path_when_sourceDir_is_not_valid() throws IOException {
// given
final Cursor cursor = new TestCursor(new File("file.adoc").getAbsolutePath(), 0, "path", "dir");
final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message");
Expand All @@ -76,7 +76,7 @@ public void should_format_logRecords_with_empty_lineNumber_absolute_path_when_so
}

@Test
public void should_format_logRecords_when_source_is_not_under_sourceDir() {
void should_format_logRecords_when_source_is_not_under_sourceDir() {
// given
final Cursor cursor = new TestCursor(new File("..", "../file.adoc").toString(), 2, "path", "dir");
final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message");
Expand All @@ -88,7 +88,7 @@ public void should_format_logRecords_when_source_is_not_under_sourceDir() {
}

@Test
public void should_format_full_logRecord_when_cursor_is_http_source() {
void should_format_full_logRecord_when_cursor_is_http_source() {
// given
final TestCursor cursor = new TestCursor("http://something/source.adoc", 3, "path", "dir");
final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message");
Expand All @@ -99,7 +99,7 @@ public void should_format_full_logRecord_when_cursor_is_http_source() {
}

@Test
public void should_format_full_logRecord_when_cursor_is_https_source() {
void should_format_full_logRecord_when_cursor_is_https_source() {
// given
final TestCursor cursor = new TestCursor("https://something/source.adoc", 3, "path", "dir");
final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message");
Expand Down
Loading