From 2e3ee988501a732835b31d391af4f0cf61f484bf Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Fri, 12 Apr 2024 17:14:36 +0300 Subject: [PATCH] tests and fixes --- checkstyle.xml | 2 +- .../amihaiemil/eoyaml/ReadFlowMapping.java | 1 - .../com/amihaiemil/eoyaml/RtYamlInput.java | 4 +- .../amihaiemil/eoyaml/RtYamlInputTest.java | 241 +++++++++++++++++- src/test/resources/flowMapping.yml | 6 + src/test/resources/flowMapping_one_line.yml | 1 + src/test/resources/flowSequence.yml | 14 + src/test/resources/flowSequence_one_line.yml | 1 + 8 files changed, 262 insertions(+), 8 deletions(-) create mode 100644 src/test/resources/flowMapping.yml create mode 100644 src/test/resources/flowMapping_one_line.yml create mode 100644 src/test/resources/flowSequence.yml create mode 100644 src/test/resources/flowSequence_one_line.yml diff --git a/checkstyle.xml b/checkstyle.xml index 9113a7fc..93e1c932 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -49,7 +49,7 @@ Maximum number of lines in any .java file is limited. --> - + diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java b/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java index 49e69c8b..26fdd10d 100644 --- a/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java +++ b/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java @@ -97,7 +97,6 @@ final class ReadFlowMapping extends BaseYamlMapping { */ ReadFlowMapping(final YamlLine folded) { this.entries = new StringEntries(folded); - System.out.println("FLOW LINE: " + folded.value()); this.folded = folded; } diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java index 4fa3ecd2..8bd2c84b 100644 --- a/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java +++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java @@ -69,7 +69,7 @@ public YamlMapping readYamlMapping() throws IOException { ).iterator(); final YamlMapping read; if(iterator.hasNext()) { - if ("{".equals(iterator.next().trimmed())) { + if (iterator.next().trimmed().startsWith("{")) { read = new ReadFlowMapping(all); } else { read = new ReadYamlMapping(all); @@ -93,7 +93,7 @@ public YamlSequence readYamlSequence() throws IOException { ).iterator(); final YamlSequence read; if(iterator.hasNext()) { - if ("[".equals(iterator.next().trimmed())) { + if (iterator.next().trimmed().startsWith("[")) { read = new ReadFlowSequence(all); } else { read = new ReadYamlSequence(all); diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java index a3543c74..3a8e2f21 100644 --- a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java +++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java @@ -35,7 +35,6 @@ import org.junit.Test; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; @@ -1844,6 +1843,240 @@ public void shouldReadListOfMapProperly() ); } + /** + * We should be able to read a file containing only a flow mapping. + * @throws IOException If something is wrong. + */ + @Test + public void readsFlowMapping() throws IOException { + final String filename = "flowMapping.yml"; + final YamlMapping mapping = new RtYamlInput( + Files.newBufferedReader(Paths.get("src/test/resources/" + filename)) + ).readYamlMapping(); + System.out.println(mapping); + MatcherAssert.assertThat( + mapping.keys().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + mapping.values().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + mapping.string("name"), Matchers.equalTo("eo-yaml") + ); + MatcherAssert.assertThat( + mapping.string("architect"), Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + mapping.yamlSequence("developers").string(0), + Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + mapping.yamlSequence("developers").string(1), + Matchers.equalTo("sherif") + ); + MatcherAssert.assertThat( + mapping.yamlSequence("developers").string(2), + Matchers.equalTo("rultor") + ); + MatcherAssert.assertThat( + mapping.yamlMapping("tools").string("devops"), + Matchers.equalTo("rultor") + ); + MatcherAssert.assertThat( + mapping.yamlMapping("tools").string("pm"), + Matchers.equalTo("self-xdsd") + ); + } + + /** + * We should be able to read a file containing only a flow mapping + * on one line. + * @throws IOException If something is wrong. + */ + @Test + public void readsFlowMappingOneLine() throws IOException { + final String filename = "flowMapping_one_line.yml"; + final YamlMapping mapping = new RtYamlInput( + Files.newBufferedReader(Paths.get("src/test/resources/" + filename)) + ).readYamlMapping(); + System.out.println(mapping); + MatcherAssert.assertThat( + mapping.keys().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + mapping.values().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + mapping.string("name"), Matchers.equalTo("eo-yaml") + ); + MatcherAssert.assertThat( + mapping.string("architect"), Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + mapping.yamlSequence("developers").string(0), + Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + mapping.yamlSequence("developers").string(1), + Matchers.equalTo("sherif") + ); + MatcherAssert.assertThat( + mapping.yamlSequence("developers").string(2), + Matchers.equalTo("rultor") + ); + MatcherAssert.assertThat( + mapping.yamlMapping("tools").string("devops"), + Matchers.equalTo("rultor") + ); + MatcherAssert.assertThat( + mapping.yamlMapping("tools").string("pm"), + Matchers.equalTo("self-xdsd") + ); + } + + /** + * We should be able to read a file containing only a flow sequence. + * @throws IOException If something is wrong. + */ + @Test + public void readsFlowSequence() throws IOException { + final String filename = "flowSequence.yml"; + final YamlSequence sequence = new RtYamlInput( + Files.newBufferedReader(Paths.get("src/test/resources/" + filename)) + ).readYamlSequence(); + System.out.println(sequence); + final YamlMapping first = sequence.yamlMapping(0); + MatcherAssert.assertThat( + first.keys().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + first.values().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + first.string("name"), Matchers.equalTo("eo-yaml") + ); + MatcherAssert.assertThat( + first.string("architect"), Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + first.yamlSequence("developers").string(0), + Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + first.yamlSequence("developers").string(1), + Matchers.equalTo("sherif") + ); + MatcherAssert.assertThat( + first.yamlSequence("developers").string(2), + Matchers.equalTo("rultor") + ); + MatcherAssert.assertThat( + first.yamlMapping("tools").string("devops"), + Matchers.equalTo("rultor") + ); + MatcherAssert.assertThat( + first.yamlMapping("tools").string("pm"), + Matchers.equalTo("self-xdsd") + ); + final YamlMapping second = sequence.yamlMapping(1); + MatcherAssert.assertThat( + second.keys().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + second.values().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + second.string("name"), Matchers.equalTo("queenlang") + ); + MatcherAssert.assertThat( + second.string("architect"), Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + second.yamlSequence("developers").string(0), + Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + second.yamlMapping("tools").string("devops"), + Matchers.equalTo("rultor") + ); + MatcherAssert.assertThat( + second.yamlMapping("tools").string("pm"), + Matchers.equalTo("self-xdsd") + ); + } + + /** + * We should be able to read a file containing only a flow sequence on one + * line. + * @throws IOException If something is wrong. + */ + @Test + public void readsFlowSequenceOneLine() throws IOException { + final String filename = "flowSequence_one_line.yml"; + final YamlSequence sequence = new RtYamlInput( + Files.newBufferedReader(Paths.get("src/test/resources/" + filename)) + ).readYamlSequence(); + System.out.println(sequence); + final YamlMapping first = sequence.yamlMapping(0); + MatcherAssert.assertThat( + first.keys().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + first.values().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + first.string("name"), Matchers.equalTo("eo-yaml") + ); + MatcherAssert.assertThat( + first.string("architect"), Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + first.yamlSequence("developers").string(0), + Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + first.yamlSequence("developers").string(1), + Matchers.equalTo("sherif") + ); + MatcherAssert.assertThat( + first.yamlSequence("developers").string(2), + Matchers.equalTo("rultor") + ); + MatcherAssert.assertThat( + first.yamlMapping("tools").string("devops"), + Matchers.equalTo("rultor") + ); + MatcherAssert.assertThat( + first.yamlMapping("tools").string("pm"), + Matchers.equalTo("self-xdsd") + ); + final YamlMapping second = sequence.yamlMapping(1); + MatcherAssert.assertThat( + second.keys().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + second.values().size(), Matchers.equalTo(4) + ); + MatcherAssert.assertThat( + second.string("name"), Matchers.equalTo("queenlang") + ); + MatcherAssert.assertThat( + second.string("architect"), Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + second.yamlSequence("developers").string(0), + Matchers.equalTo("amihaiemil") + ); + MatcherAssert.assertThat( + second.yamlMapping("tools").string("devops"), + Matchers.equalTo("rultor") + ); + MatcherAssert.assertThat( + second.yamlMapping("tools").string("pm"), + Matchers.equalTo("self-xdsd") + ); + } + /** * Read a test resource file's contents. * @param fileName File to read. @@ -1852,11 +2085,11 @@ public void shouldReadListOfMapProperly() * @throws IOException If something is wrong. */ private String readTestResource(final String fileName) - throws FileNotFoundException, IOException { + throws IOException { return new String( IOUtils.toByteArray( - new FileInputStream( - new File("src/test/resources/" + fileName) + Files.newInputStream( + new File("src/test/resources/" + fileName).toPath() ) ) ); diff --git a/src/test/resources/flowMapping.yml b/src/test/resources/flowMapping.yml new file mode 100644 index 00000000..85335266 --- /dev/null +++ b/src/test/resources/flowMapping.yml @@ -0,0 +1,6 @@ +{ + name: eo-yaml, + architect: amihaiemil, + developers: [amihaiemil, sherif, rultor], + tools: {devops: rultor, pm: self-xdsd} +} \ No newline at end of file diff --git a/src/test/resources/flowMapping_one_line.yml b/src/test/resources/flowMapping_one_line.yml new file mode 100644 index 00000000..9aa13526 --- /dev/null +++ b/src/test/resources/flowMapping_one_line.yml @@ -0,0 +1 @@ +{name: eo-yaml,architect: amihaiemil,developers: [amihaiemil, sherif, rultor],tools: {devops: rultor, pm: self-xdsd}} \ No newline at end of file diff --git a/src/test/resources/flowSequence.yml b/src/test/resources/flowSequence.yml new file mode 100644 index 00000000..63a93b37 --- /dev/null +++ b/src/test/resources/flowSequence.yml @@ -0,0 +1,14 @@ +[ + { + name: eo-yaml, + architect: amihaiemil, + developers: [ amihaiemil, sherif, rultor ], + tools: { devops: rultor, pm: self-xdsd } + }, + { + name: queenlang, + architect: amihaiemil, + developers: [ amihaiemil], + tools: { devops: rultor, pm: self-xdsd } + }, +] \ No newline at end of file diff --git a/src/test/resources/flowSequence_one_line.yml b/src/test/resources/flowSequence_one_line.yml new file mode 100644 index 00000000..658383ab --- /dev/null +++ b/src/test/resources/flowSequence_one_line.yml @@ -0,0 +1 @@ +[{name: eo-yaml,architect: amihaiemil,developers: [ amihaiemil, sherif, rultor ],tools: { devops: rultor, pm: self-xdsd }},{name: queenlang,architect: amihaiemil,developers: [ amihaiemil], tools: { devops: rultor, pm: self-xdsd }},] \ No newline at end of file