Skip to content
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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

<!-- JUnit versions -->
<version.jupiter.junit>5.14.1</version.jupiter.junit>
<version.mockito>5.24.0</version.mockito>
<version.plugin.jacoco>0.8.16</version.plugin.jacoco>
<version.plugin.jacoco-console-reporter>1.3.2</version.plugin.jacoco-console-reporter>

Expand Down Expand Up @@ -73,6 +74,11 @@
</dependency>

<!-- YAML writing -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
Expand All @@ -86,6 +92,12 @@
<version>${version.jupiter.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${version.mockito}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/rickwporter/prettytable/CsvTableRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@
import org.apache.commons.csv.CSVPrinter;

class CsvTableRender implements TableRenderInterface {
private final Appendable output;

CsvTableRender() {
this(null);
}

CsvTableRender(Appendable destination) {
this.output = destination;
}

@Override
public String render(PrettyTable table, boolean removedRedundant) {
StringBuilder result = new StringBuilder();
try {
StringBuilder result = new StringBuilder();
Appendable target = this.output == null ? result : this.output;
CSVFormat format = CSVFormat.DEFAULT.builder().setRecordSeparator("\n").get();
CSVPrinter printer = new CSVPrinter(result, format);
CSVPrinter printer = new CSVPrinter(target, format);
if (!table.getHeaders().isEmpty()) {
printer.printRecord(table.getHeaders());
}
Expand All @@ -25,8 +35,7 @@ public String render(PrettyTable table, boolean removedRedundant) {
printer.close();
return result.toString();
} catch (IOException ex) {
// nothing to do here
throw new IllegalStateException("Unable to render table as CSV", ex);
}
return null;
}
}
16 changes: 12 additions & 4 deletions src/main/java/rickwporter/prettytable/YamlTableRender.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package rickwporter.prettytable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;

class YamlTableRender implements TableRenderInterface {
private static final YAMLMapper MAPPER = yamlMapper();
private YAMLMapper yamlMapper;

YamlTableRender() {
this(null);
}

YamlTableRender(YAMLMapper mapper) {
this.yamlMapper = (mapper == null ? yamlMapper() : mapper);
}

private static YAMLMapper yamlMapper() {
YAMLMapper mapper = new YAMLMapper();
Expand Down Expand Up @@ -66,8 +74,8 @@ private Object cellValue(Object cell) {
@Override
public String render(PrettyTable table, boolean removeRedundant) {
try {
return MAPPER.writeValueAsString(this.toData(table));
} catch (IOException ex) {
return this.yamlMapper.writeValueAsString(this.toData(table));
} catch (JsonProcessingException ex) {
throw new IllegalStateException("Unable to render table as YAML", ex);
}
}
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/rickwporter/prettytable/CsvTableRenderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package rickwporter.prettytable;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyChar;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.when;

import java.io.IOException;

import org.apache.commons.csv.CSVFormat;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;

public class CsvTableRenderTest {
@Test
public void testIoException() throws IOException {
IOException failure = new IOException("csv failed");
Appendable out = mock(Appendable.class);
when(out.append(any(CharSequence.class))).thenThrow(failure);
when(out.append(any(CharSequence.class), anyInt(), anyInt())).thenThrow(failure);
when(out.append(anyChar())).thenThrow(failure);

// A header makes CSVPrinter's constructor write, so the failing Appendable throws there.
CSVFormat withHeader = CSVFormat.DEFAULT.builder()
.setRecordSeparator("\n")
.setHeader("Name")
.get();
try (MockedConstruction<CSVFormat.Builder> ignored = mockConstruction(CSVFormat.Builder.class,
(mock, context) -> {
when(mock.setRecordSeparator(anyString())).thenReturn(mock);
when(mock.get()).thenReturn(withHeader);
})) {
PrettyTable table = new PrettyTable("Name");
table.addRow("Ada");
CsvTableRender uut = new CsvTableRender(out);

IllegalStateException ex = Assertions.assertThrows(
IllegalStateException.class,
() -> uut.render(table, false)
);
Assertions.assertEquals("Unable to render table as CSV", ex.getMessage());
Assertions.assertSame(failure, ex.getCause());
}
}
}
36 changes: 36 additions & 0 deletions src/test/java/rickwporter/prettytable/YamlTableRenderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package rickwporter.prettytable;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;


public class YamlTableRenderTest {
@Test
public void testJsonProcessingException() throws IOException {
JsonProcessingException failure = new JsonGenerationException("this is it", (JsonGenerator)null);
YAMLMapper mapper = mock(YAMLMapper.class);
YamlTableRender uut = new YamlTableRender(mapper);
when(mapper.writeValueAsString(any())).thenThrow(failure);

PrettyTable table = new PrettyTable("Name");
table.addRow("Ada");

IllegalStateException ex = Assertions.assertThrows(
IllegalStateException.class,
() -> uut.render(table, false)
);
Assertions.assertEquals("Unable to render table as YAML", ex.getMessage());
Assertions.assertSame(failure, ex.getCause());
}
}
Loading