1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-11-22 04:55:10 +00:00

Fix issue with garbage after the diagram and created better tests

This commit is contained in:
Josep Mones Teixidor 2021-07-29 06:02:06 +02:00
parent 6849c96144
commit 0bef01372f
2 changed files with 238 additions and 200 deletions

View File

@ -35,11 +35,15 @@
*/
package net.sourceforge.plantuml;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import net.sourceforge.plantuml.core.Diagram;
@ -51,89 +55,97 @@ import net.sourceforge.plantuml.security.SFile;
public class Pipe {
private final Option option;
private final InputStream is;
private final BufferedReader br;
private final PrintStream ps;
private boolean closed = false;
private final String charset;
private final Stdrpt stdrpt;
public Pipe(Option option, PrintStream ps, InputStream is, String charset) {
this.option = option;
this.is = is;
try {
this.br = new BufferedReader(
new InputStreamReader(is, (charset != null) ? charset : Charset.defaultCharset().name()));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Invalid charset provided", e);
}
this.ps = ps;
this.charset = charset;
this.stdrpt = option.getStdrpt();
}
public void managePipe(ErrorStatus error) throws IOException {
final boolean noStdErr = option.isPipeNoStdErr();
int nb = 0;
do {
final String source = readOneDiagram();
if (source == null) {
ps.flush();
if (nb == 0) {
// error.goNoData();
}
return;
}
nb++;
for(String source = readFirstDiagram(); source != null; source = readSubsequentDiagram()) {
final Defines defines = option.getDefaultDefines();
final SFile newCurrentDir = option.getFileDir() == null ? null : new SFile(option.getFileDir());
final SourceStringReader sourceStringReader = new SourceStringReader(defines, source, "UTF-8",
option.getConfig(), newCurrentDir);
if (option.isComputeurl()) {
for (BlockUml s : sourceStringReader.getBlocks()) {
ps.println(s.getEncodedUrl());
}
computeUrlForDiagram(sourceStringReader);
} else if (option.isSyntax()) {
final Diagram system = sourceStringReader.getBlocks().get(0).getDiagram();
if (system instanceof UmlDiagram) {
error.goOk();
ps.println(((UmlDiagram) system).getUmlDiagramType().name());
ps.println(system.getDescription());
} else if (system instanceof PSystemError) {
error.goWithError();
stdrpt.printInfo(ps, (PSystemError) system);
} else {
error.goOk();
ps.println("OTHER");
ps.println(system.getDescription());
}
syntaxCheckDiagram(sourceStringReader, error);
} else if (option.isPipeMap()) {
final String result = sourceStringReader.getCMapData(option.getImageIndex(),
option.getFileFormatOption());
// https://forum.plantuml.net/10049/2019-pipemap-diagrams-containing-links-give-zero-exit-code
// We don't check errors
error.goOk();
if (result == null) {
ps.println();
} else {
ps.println(result);
}
createPipeMapForDiagram(sourceStringReader, error);
} else {
final OutputStream os = noStdErr ? new ByteArrayOutputStream() : ps;
final DiagramDescription result = sourceStringReader.outputImage(os, option.getImageIndex(),
option.getFileFormatOption());
printInfo(noStdErr ? ps : System.err, sourceStringReader);
if (result != null && "(error)".equalsIgnoreCase(result.getDescription())) {
error.goWithError();
} else {
error.goOk();
if (noStdErr) {
final ByteArrayOutputStream baos = (ByteArrayOutputStream) os;
baos.close();
ps.write(baos.toByteArray());
}
}
if (option.getPipeDelimitor() != null) {
ps.println(option.getPipeDelimitor());
}
generateDiagram(sourceStringReader, error, noStdErr);
}
ps.flush();
} while (closed == false);
if (nb == 0) {
// error.goNoData();
}
}
private void generateDiagram(SourceStringReader sourceStringReader, ErrorStatus error, boolean noStdErr) throws IOException {
final OutputStream os = noStdErr ? new ByteArrayOutputStream() : ps;
final DiagramDescription result = sourceStringReader.outputImage(os, option.getImageIndex(),
option.getFileFormatOption());
printInfo(noStdErr ? ps : System.err, sourceStringReader);
if (result != null && "(error)".equalsIgnoreCase(result.getDescription())) {
error.goWithError();
} else {
error.goOk();
if (noStdErr) {
final ByteArrayOutputStream baos = (ByteArrayOutputStream) os;
baos.close();
ps.write(baos.toByteArray());
}
}
if (option.getPipeDelimitor() != null) {
ps.println(option.getPipeDelimitor());
}
}
private void createPipeMapForDiagram(SourceStringReader sourceStringReader, ErrorStatus error) throws IOException {
final String result = sourceStringReader.getCMapData(option.getImageIndex(),
option.getFileFormatOption());
// https://forum.plantuml.net/10049/2019-pipemap-diagrams-containing-links-give-zero-exit-code
// We don't check errors
error.goOk();
if (result == null) {
ps.println();
} else {
ps.println(result);
}
}
private void computeUrlForDiagram(SourceStringReader sourceStringReader) throws IOException {
for (BlockUml s : sourceStringReader.getBlocks()) {
ps.println(s.getEncodedUrl());
}
}
private void syntaxCheckDiagram(SourceStringReader sourceStringReader, ErrorStatus error) {
final Diagram system = sourceStringReader.getBlocks().get(0).getDiagram();
if (system instanceof UmlDiagram) {
error.goOk();
ps.println(((UmlDiagram) system).getUmlDiagramType().name());
ps.println(system.getDescription());
} else if (system instanceof PSystemError) {
error.goWithError();
stdrpt.printInfo(ps, system);
} else {
error.goOk();
ps.println("OTHER");
ps.println(system.getDescription());
}
}
@ -146,34 +158,55 @@ public class Pipe {
}
}
private boolean isFinished(String s) {
return s == null || s.startsWith("@end");
String readFirstDiagram() throws IOException {
return readSingleDiagram(true);
}
String readOneDiagram() throws IOException {
String readSubsequentDiagram() throws IOException {
return readSingleDiagram(false);
}
String readSingleDiagram(boolean unmarkedAllowed) throws IOException {
State state = State.NO_CONTENT;
String expectedEnd = null;
final StringBuilder sb = new StringBuilder();
while (true) {
final String s = readOneLine();
if (s == null) {
closed = true;
} else if (s.startsWith("@@@format ")) {
manageFormat(s);
String line;
while(state != State.COMPLETE && (line = br.readLine()) != null) {
if(line.startsWith("@@@format ")) {
manageFormat(line);
} else {
sb.append(s);
sb.append(BackSlash.NEWLINE);
}
if (isFinished(s)) {
break;
if (state == State.NO_CONTENT && line.trim().length() > 0) {
state = State.START_MARK_NOT_FOUND;
}
if (state == State.START_MARK_NOT_FOUND && line.startsWith("@start")) {
sb.setLength(0); // discard any previous input
state = State.START_MARK_FOUND;
expectedEnd = "@end" + line.substring(6).split("^[A-Za-z]")[0];
} else if (state == State.START_MARK_FOUND && line.startsWith(expectedEnd)) {
state = State.COMPLETE;
}
if (state != State.NO_CONTENT) {
sb.append(line).append("\n");
}
}
}
String source = sb.toString().trim();
if (sb.length() == 0) {
return null;
switch(state) {
case NO_CONTENT:
return null;
case START_MARK_NOT_FOUND:
return (unmarkedAllowed) ? "@startuml\n" + sb.toString() + "@enduml\n" : null;
case START_MARK_FOUND:
return sb.toString() + expectedEnd;
case COMPLETE:
return sb.toString();
default:
throw new IllegalStateException("Unexpected value: " + state);
}
if (source.startsWith("@start") == false) {
source = "@startuml\n" + source + "\n@enduml";
}
return source;
}
void manageFormat(String s) {
@ -184,27 +217,10 @@ public class Pipe {
}
}
String readOneLine() throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (true) {
final int read = is.read();
if (read == -1) {
if (baos.size() == 0) {
return null;
}
break;
}
if (read != '\r' && read != '\n') {
baos.write(read);
}
if (read == '\n') {
break;
}
}
if (charset == null) {
return new String(baos.toByteArray());
}
return new String(baos.toByteArray(), charset);
enum State {
NO_CONTENT,
START_MARK_NOT_FOUND,
START_MARK_FOUND,
COMPLETE
}
}

View File

@ -3,6 +3,7 @@ package net.sourceforge.plantuml;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -154,43 +155,109 @@ class PipeTest {
}
@Test
void should_readOneDiagram_return_null_for_empty_input() throws IOException {
void should_readFirstDiagram_return_null_for_empty_input() throws IOException {
pipe = new Pipe(option, null, new ByteArrayInputStream(new byte[0]), UTF_8.name());
String actual = pipe.readOneDiagram();
String actual = pipe.readFirstDiagram();
assertThat(actual).isNull();
}
@Test
void should_readOneDiagram_remove_carriage_returns() throws IOException {
pipe = new Pipe(option, null, new ByteArrayInputStream("@startuml\na\rb\r\nc\rd\re\n@enduml".getBytes(UTF_8)), UTF_8.name());
void should_readFirstDiagram_decode_a_special_unicode_character_when_provided_charset_is_utf8() throws IOException {
pipe = new Pipe(option, null, new ByteArrayInputStream("\u2620\n".getBytes(UTF_8)), UTF_8.name());
String actual = pipe.readOneDiagram();
String actual = pipe.readFirstDiagram();
assertThat(actual).isEqualTo("@startuml\nab\ncde\n@enduml");
assertThat(actual).isEqualTo("@startuml\n\u2620\n@enduml\n");
}
static List<InputExpected> startAndEndMarks() {
LinkedList<InputExpected> linkedList = new LinkedList<>();
linkedList.add(InputExpected.of("\na\rb\r\nc\rd\re\n@enduml", "@startuml\nab\ncde\n@enduml\n@enduml"));
linkedList.add(InputExpected.of("\na\rb\r\nc\rd\re\n@endwhatever", "@startuml\nab\ncde\n@endwhatever\n@enduml"));
linkedList.add(InputExpected.of("\na\rb\r\nc\rd\re\n@enduml\n", "@startuml\nab\ncde\n@enduml\n@enduml"));
linkedList.add(InputExpected.of("@startuml\na\rb\r\nc\rd\re\n@enduml", "@startuml\nab\ncde\n@enduml"));
linkedList.add(InputExpected.of("@startuml\na\rb\r\nc\rd\re\n@enduml\n", "@startuml\nab\ncde\n@enduml"));
linkedList.add(InputExpected.of("@startuml\na\rb\r\nc\rd\re\n@enduml\n\n", "@startuml\nab\ncde\n@enduml"));
linkedList.add(InputExpected.of("@startuml\na\rb\r\nc\rd\re\n@enduml\r\n\r\n", "@startuml\nab\ncde\n@enduml"));
linkedList.add(InputExpected.of("this-is-garbage\n@startuml\na\rb\r\nc\rd\re\n@enduml\nthis-is-garbage\n", "@startuml\nab\ncde\n@enduml"));
linkedList.add(InputExpected.of("@startwhatever\na\rb\r\nc\rd\re\n@endwhatever", "@startwhatever\nab\ncde\n@endwhatever"));
return linkedList;
// The testing is relevant only if the testing VM is configured in non-UTF-8 defaultCharset()
@Test
void should_readFirstDiagram_uses_current_charset_if_not_provided() throws IOException {
String input = "\u00C1"; // A acute accented. (HTML &Aacute;). Multibyte in UTF-8.
if(Charset.defaultCharset().newEncoder().canEncode(input)) {
pipe = new Pipe(option, null, new ByteArrayInputStream(input.getBytes(Charset.defaultCharset())), null);
String actual = pipe.readFirstDiagram();
assertThat(actual).isEqualTo("@startuml\n\u00C1\n@enduml\n");
} else {
// default charset can't encode &Aacute;. Ignore the test.
assertTrue(true);
}
}
@ParameterizedTest
@MethodSource("startAndEndMarks")
void should_readOneDiagram_handle_correctly_start_and_end_marks(InputExpected inputExpected) throws IOException {
@ValueSource(strings = {
"ab\nc", // *nix, macOsX
"ab\rc", // pre-macOsX macs
"ab\r\nc", // Windows
// the case \n\r is handled as 2 new lines, thus not added
"ab\nc\n",
"ab\nc\r",
"ab\nc\r\n"
})
void should_readFirstDiagram_decode_correctly_different_line_endings(String input) throws IOException {
pipe = new Pipe(option, null, new ByteArrayInputStream(input.getBytes(UTF_8)), UTF_8.name());
String first = pipe.readFirstDiagram();
String second = pipe.readFirstDiagram();
assertThat(first).isEqualTo("@startuml\nab\nc\n@enduml\n");
assertThat(second).isEqualTo(null); // no spurious diagram afterwards
}
static List<InputExpected> firstStartAndEndMarks() {
List<InputExpected> l = new LinkedList<>();
l.add(InputExpected.of("\nab\r\ncde", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("\nab\r\ncde\n", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("\nab\r\ncde\n@enduml", "@startuml\nab\ncde\n@enduml\n@enduml\n"));
l.add(InputExpected.of("\nab\r\ncde\n@endwhatever", "@startuml\nab\ncde\n@endwhatever\n@enduml\n"));
l.add(InputExpected.of("\nab\r\ncde\n@enduml\n", "@startuml\nab\ncde\n@enduml\n@enduml\n"));
l.add(InputExpected.of("@startuml\nab\r\ncde\n@enduml", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("@startuml\nab\r\ncde\n@enduml\n", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("@startuml\nab\r\ncde\n@enduml\n\n", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("@startuml\nab\r\ncde\n@enduml\r\n\r\n", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("this-is-garbage\n@startuml\nab\rcde\n@enduml\nthis-is-garbage\n", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("@startwhatever\nab\rcde\n@endwhatever", "@startwhatever\nab\ncde\n@endwhatever\n"));
return l;
}
@ParameterizedTest
@MethodSource("firstStartAndEndMarks")
void should_readFirstDiagram_handle_correctly_start_and_end_marks(InputExpected inputExpected) throws IOException {
pipe = new Pipe(option, null, new ByteArrayInputStream(inputExpected.getInput().getBytes(UTF_8)), UTF_8.name());
String actual = pipe.readOneDiagram();
String actual = pipe.readFirstDiagram();
assertThat(actual).isEqualTo(inputExpected.getExpected());
}
static List<InputExpected> subsequentStartAndEndMarks() {
List<InputExpected> l = new LinkedList<>();
l.add(InputExpected.of("\nab\r\ncde", null));
l.add(InputExpected.of("\nab\r\ncde\n", null));
l.add(InputExpected.of("\nab\r\ncde\n@enduml", null));
l.add(InputExpected.of("\nab\r\ncde\n@endwhatever", null));
l.add(InputExpected.of("\nab\r\ncde\n@enduml\n", null));
l.add(InputExpected.of("@startuml\nab\r\ncde\n@enduml", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("@startuml\nab\r\ncde\n@enduml\n", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("@startuml\nab\r\ncde\n@enduml\n\n", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("@startuml\nab\r\ncde\n@enduml\r\n\r\n", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("this-is-garbage\n@startuml\nab\rcde\n@enduml\nthis-is-garbage\n", "@startuml\nab\ncde\n@enduml\n"));
l.add(InputExpected.of("@startwhatever\nab\rcde\n@endwhatever", "@startwhatever\nab\ncde\n@endwhatever\n"));
return l;
}
@ParameterizedTest
@MethodSource("subsequentStartAndEndMarks")
void should_readSubsequentDiagram_handle_correctly_start_and_end_marks(InputExpected inputExpected) throws IOException {
pipe = new Pipe(option, null, new ByteArrayInputStream(inputExpected.getInput().getBytes(UTF_8)), UTF_8.name());
String actual = pipe.readSubsequentDiagram();
assertThat(actual).isEqualTo(inputExpected.getExpected());
}
@ -219,81 +286,8 @@ class PipeTest {
assertThat(option.getFileFormatOption().getFileFormat()).isEqualTo(FileFormat.PNG);
}
@Test
void should_readOneLine_returns_null_if_no_input() throws IOException {
pipe = new Pipe(option, null, new ByteArrayInputStream(new byte[0]), UTF_8.toString());
String result = pipe.readOneLine();
assertThat(result).isNull();
}
@ParameterizedTest
@ValueSource(strings = {"\rab\nc\n", "\ra\rb\r\nc\n", "ab\n\rc"})
void should_readOneLine_ignore_carriage_return_and_stop_at_line_feed(String input) throws IOException {
pipe = new Pipe(option, null, new ByteArrayInputStream(input.getBytes(UTF_8)), UTF_8.name());
String actual = pipe.readOneLine();
assertThat(actual).isEqualTo("ab");
}
@Test
void should_readOneLine_return_empty_string_if_line_feed_is_the_first_character() throws IOException {
pipe = new Pipe(option, null, new ByteArrayInputStream("\n".getBytes(UTF_8)), UTF_8.name());
String actual = pipe.readOneLine();
assertThat(actual).isEqualTo("");
}
@Test
void should_readOneLine_use_the_default_charset_if_not_provided() throws IOException {
pipe = new Pipe(option, null, new ByteArrayInputStream("ab\n".getBytes(Charset.defaultCharset())), null);
String actual = pipe.readOneLine();
assertThat(actual).isEqualTo("ab");
}
@Test
void should_readOneLine_use_provided_charset() throws IOException {
pipe = new Pipe(option, null, new ByteArrayInputStream("\u2620\n".getBytes(UTF_8)), UTF_8.name());
String actual = pipe.readOneLine();
assertThat(actual).isEqualTo("\u2620");
}
static class InputExpected {
private final String input;
private final String expected;
public InputExpected(String input, String expected) {
this.input = input;
this.expected = expected;
}
public static InputExpected of(String input, String expected) {
return new InputExpected(input, expected);
}
public String getInput() {
return input;
}
public String getExpected() {
return expected;
}
@Override
public String toString() {
return "i:'" + input + "', e='" + expected + "'";
}
}
static class TestCase {
private final String options;
private final String input;
private final String expectedOut;
@ -346,6 +340,34 @@ class PipeTest {
}
static class InputExpected {
private final String input;
private final String expected;
public InputExpected(String input, String expected) {
this.input = input;
this.expected = expected;
}
public static InputExpected of(String input, String expected) {
return new InputExpected(input, expected);
}
public String getInput() {
return input;
}
public String getExpected() {
return expected;
}
@Override
public String toString() {
return "i:'" + input + "', e='" + expected + "'";
}
}
enum Verification {
EXACT,
REGEX