From 3bb1792c966a775eae045c64b69a2d88da974823 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 29 Nov 2017 21:02:54 -0800 Subject: [PATCH] Fix #53 --- release-notes/VERSION | 6 +- .../dataformat/yaml/YAMLGenerator.java | 27 ++++-- .../jackson/dataformat/yaml/EventsTest.java | 52 ------------ .../jackson/dataformat/yaml/TypeIdTest.java | 1 - .../{ => deser}/DatabindAdvancedTest.java | 5 +- .../DatabindReadTest.java} | 5 +- .../yaml/{ => deser}/SimpleParseTest.java | 46 +++++++++- .../yaml/{ => deser}/UTF8ReaderTest.java | 4 +- .../dataformat/yaml/ser/BinaryWriteTest.java | 29 +++++++ .../yaml/ser/DatabindWriteTest.java | 77 +++++++++++++++++ .../{ => ser}/GeneratorFeature34Test.java | 7 +- .../yaml/{ => ser}/GeneratorFeatureTest.java | 6 +- .../GeneratorWithMinimizeTest.java} | 10 ++- .../GeneratorWithSplitLinesTest.java | 8 +- .../yaml/{ => ser}/SimpleGenerationTest.java | 85 +++---------------- 15 files changed, 222 insertions(+), 146 deletions(-) delete mode 100644 yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/EventsTest.java rename yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/{ => deser}/DatabindAdvancedTest.java (96%) rename yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/{SimpleDatabindTest.java => deser/DatabindReadTest.java} (96%) rename yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/{ => deser}/SimpleParseTest.java (89%) rename yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/{ => deser}/UTF8ReaderTest.java (91%) create mode 100644 yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/BinaryWriteTest.java create mode 100644 yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/DatabindWriteTest.java rename yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/{ => ser}/GeneratorFeature34Test.java (78%) rename yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/{ => ser}/GeneratorFeatureTest.java (86%) rename yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/{GeratorWithMinimizeTest.java => ser/GeneratorWithMinimizeTest.java} (90%) rename yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/{ => ser}/GeneratorWithSplitLinesTest.java (76%) rename yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/{ => ser}/SimpleGenerationTest.java (60%) diff --git a/release-notes/VERSION b/release-notes/VERSION index b98cf069..e52d4045 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -10,12 +10,14 @@ Modules: 2.9.3 (not yet released) +#39 (yamk): Binary data not recognized by YAML parser + (repoted by tmoschou@github) #42 (csv): Add support for escaping double quotes with the configured escape character (contributed by frankgrimes97@github) #51 (csv): Set of custom objects with `IGNORE_UNKNOWN` brokes silently csv (reported by Simone L) -#39 (yamk): Binary data not recognized by YAML parser - (repoted by tmoschou@github) +#53: (yaml) Binary values written without type tag + (reported by arulrajnet@github) 2.9.2 (14-Oct-2017) diff --git a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java index 6b266f3f..d8fb6b9b 100644 --- a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java +++ b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java @@ -12,6 +12,8 @@ import org.yaml.snakeyaml.DumperOptions.FlowStyle; import org.yaml.snakeyaml.emitter.Emitter; import org.yaml.snakeyaml.events.*; +import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; +import org.yaml.snakeyaml.nodes.Tag; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.core.base.GeneratorBase; @@ -155,6 +157,7 @@ private Feature(boolean defaultState) { protected final static long MIN_INT_AS_LONG = (long) Integer.MIN_VALUE; protected final static long MAX_INT_AS_LONG = (long) Integer.MAX_VALUE; protected final static Pattern PLAIN_NUMBER_P = Pattern.compile("[0-9]*(\\.[0-9]*)?"); + protected final static String TAG_BINARY = Tag.BINARY.toString(); /* /********************************************************** @@ -186,7 +189,8 @@ private Feature(boolean defaultState) { private final static Character STYLE_LITERAL = Character.valueOf('|'); // Which flow style to use for Base64? Maybe basic quoted? - private final static Character STYLE_BASE64 = Character.valueOf('"'); + // 29-Nov-2017, tatu: Actually SnakeYAML uses block style so: + private final static Character STYLE_BASE64 = STYLE_LITERAL; private final static Character STYLE_PLAIN = null; @@ -616,12 +620,10 @@ public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int l return; } _verifyValueWrite("write Binary value"); - // ok, better just Base64 encode as a String... if (offset > 0 || (offset+len) != data.length) { data = Arrays.copyOfRange(data, offset, offset+len); } - String encoded = b64variant.encode(data); - _writeScalar(encoded, "byte[]", STYLE_BASE64); + _writeScalarBinary(data); } /* @@ -785,13 +787,24 @@ protected void _releaseBuffers() { */ // Implicit means that (type) tags won't be shown, right? - private final static ImplicitTuple DEFAULT_IMPLICIT = new ImplicitTuple(true, true); + private final static ImplicitTuple NO_TAGS = new ImplicitTuple(true, true); + + // ... and sometimes we specifically DO want explicit tag: + private final static ImplicitTuple EXPLICIT_TAGS = new ImplicitTuple(false, false); protected void _writeScalar(String value, String type, Character style) throws IOException { _emitter.emit(_scalarEvent(value, style)); } + private void _writeScalarBinary(byte[] data) throws IOException + { + // 29-Nov-2017, tatu: Use SnakeYAML encoder instead of Jackson's + String encoded = Base64Coder.encodeLines(data); + _emitter.emit(new ScalarEvent(null, TAG_BINARY, EXPLICIT_TAGS, encoded, + null, null, STYLE_BASE64)); + } + protected ScalarEvent _scalarEvent(String value, Character style) { String yamlTag = _typeId; @@ -802,7 +815,9 @@ protected ScalarEvent _scalarEvent(String value, Character style) if (anchor != null) { _objectId = null; } - return new ScalarEvent(anchor, yamlTag, DEFAULT_IMPLICIT, value, + // 29-Nov-2017, tatu: Not 100% sure why we don't force explicit tags for + // type id, but trying to do so seems to double up tag output... + return new ScalarEvent(anchor, yamlTag, NO_TAGS, value, null, null, style); } } diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/EventsTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/EventsTest.java deleted file mode 100644 index 55196059..00000000 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/EventsTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.fasterxml.jackson.dataformat.yaml; - -import com.fasterxml.jackson.core.*; - -/** - * Tests that test low-level handling of events from YAML source - */ -public class EventsTest extends ModuleTestBase -{ - public void testBasic() throws Exception - { - final String YAML = - "string: 'text'\n" -+"bool: true\n" -+"bool2: false\n" -+"null: null\n" -+"i: 123\n" -+"d: 1.25\n" -; - YAMLFactory f = new YAMLFactory(); - JsonParser p = f.createParser(YAML); - assertToken(JsonToken.START_OBJECT, p.nextToken()); - - assertToken(JsonToken.FIELD_NAME, p.nextToken()); - assertToken(JsonToken.VALUE_STRING, p.nextToken()); - assertEquals("text", p.getText()); - assertToken(JsonToken.FIELD_NAME, p.nextToken()); - assertToken(JsonToken.VALUE_TRUE, p.nextToken()); - assertEquals("true", p.getText()); - assertToken(JsonToken.FIELD_NAME, p.nextToken()); - assertToken(JsonToken.VALUE_FALSE, p.nextToken()); - assertEquals("false", p.getText()); - assertToken(JsonToken.FIELD_NAME, p.nextToken()); - assertToken(JsonToken.VALUE_NULL, p.nextToken()); - assertEquals("null", p.getText()); - assertToken(JsonToken.FIELD_NAME, p.nextToken()); - assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); - assertEquals("123", p.getText()); - assertEquals(123, p.getIntValue()); - assertToken(JsonToken.FIELD_NAME, p.nextToken()); - assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); - assertEquals("1.25", p.getText()); - assertEquals(1.25, p.getDoubleValue()); - assertEquals(1, p.getIntValue()); - - assertToken(JsonToken.END_OBJECT, p.nextToken()); - assertNull(p.nextToken()); - assertNull(p.nextToken()); - assertNull(p.nextToken()); - p.close(); - } -} diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/TypeIdTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/TypeIdTest.java index 088fbdad..8205797b 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/TypeIdTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/TypeIdTest.java @@ -39,7 +39,6 @@ public void testNativeSerialization() throws Exception assertEquals("--- !\na: 13", yaml); } - // [Issue#22] public void testNonNativeSerialization() throws Exception { YAMLMapper mapper = new YAMLMapper(); diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/DatabindAdvancedTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/DatabindAdvancedTest.java similarity index 96% rename from yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/DatabindAdvancedTest.java rename to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/DatabindAdvancedTest.java index c46af69f..8043722e 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/DatabindAdvancedTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/DatabindAdvancedTest.java @@ -1,9 +1,10 @@ -package com.fasterxml.jackson.dataformat.yaml; +package com.fasterxml.jackson.dataformat.yaml.deser; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; public class DatabindAdvancedTest extends ModuleTestBase { @@ -135,7 +136,7 @@ public Image(String uri, String title, int w, int h, Size s) /********************************************************** */ - public void testBasic() throws Exception + public void testReadComplexPojo() throws Exception { ObjectMapper mapper = newObjectMapper(); String YAML = diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleDatabindTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/DatabindReadTest.java similarity index 96% rename from yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleDatabindTest.java rename to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/DatabindReadTest.java index a1f3a232..1a6ae4c2 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleDatabindTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/DatabindReadTest.java @@ -1,4 +1,4 @@ -package com.fasterxml.jackson.dataformat.yaml; +package com.fasterxml.jackson.dataformat.yaml.deser; import java.io.ByteArrayInputStream; import java.util.Map; @@ -8,12 +8,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; /** * Unit tests for checking functioning of the databinding * on top of YAML layer. */ -public class SimpleDatabindTest extends ModuleTestBase +public class DatabindReadTest extends ModuleTestBase { static class EmptyBean { } diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleParseTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/SimpleParseTest.java similarity index 89% rename from yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleParseTest.java rename to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/SimpleParseTest.java index da8e6b67..1b4ce3df 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleParseTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/SimpleParseTest.java @@ -1,7 +1,10 @@ -package com.fasterxml.jackson.dataformat.yaml; +package com.fasterxml.jackson.dataformat.yaml.deser; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLParser; import java.io.StringWriter; import java.math.BigInteger; @@ -14,6 +17,47 @@ public class SimpleParseTest extends ModuleTestBase { final YAMLFactory YAML_F = new YAMLFactory(); + public void testBasic() throws Exception + { + final String YAML = + "string: 'text'\n" ++"bool: true\n" ++"bool2: false\n" ++"null: null\n" ++"i: 123\n" ++"d: 1.25\n" +; + JsonParser p = YAML_F.createParser(YAML); + assertToken(JsonToken.START_OBJECT, p.nextToken()); + + assertToken(JsonToken.FIELD_NAME, p.nextToken()); + assertToken(JsonToken.VALUE_STRING, p.nextToken()); + assertEquals("text", p.getText()); + assertToken(JsonToken.FIELD_NAME, p.nextToken()); + assertToken(JsonToken.VALUE_TRUE, p.nextToken()); + assertEquals("true", p.getText()); + assertToken(JsonToken.FIELD_NAME, p.nextToken()); + assertToken(JsonToken.VALUE_FALSE, p.nextToken()); + assertEquals("false", p.getText()); + assertToken(JsonToken.FIELD_NAME, p.nextToken()); + assertToken(JsonToken.VALUE_NULL, p.nextToken()); + assertEquals("null", p.getText()); + assertToken(JsonToken.FIELD_NAME, p.nextToken()); + assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); + assertEquals("123", p.getText()); + assertEquals(123, p.getIntValue()); + assertToken(JsonToken.FIELD_NAME, p.nextToken()); + assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); + assertEquals("1.25", p.getText()); + assertEquals(1.25, p.getDoubleValue()); + assertEquals(1, p.getIntValue()); + + assertToken(JsonToken.END_OBJECT, p.nextToken()); + assertNull(p.nextToken()); + assertNull(p.nextToken()); + assertNull(p.nextToken()); + p.close(); + } // Parsing large numbers around the transition from int->long and long->BigInteger public void testIntParsing() throws Exception { diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/UTF8ReaderTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/UTF8ReaderTest.java similarity index 91% rename from yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/UTF8ReaderTest.java rename to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/UTF8ReaderTest.java index eae40250..e43a0659 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/UTF8ReaderTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/UTF8ReaderTest.java @@ -1,7 +1,9 @@ -package com.fasterxml.jackson.dataformat.yaml; +package com.fasterxml.jackson.dataformat.yaml.deser; import org.junit.Test; +import com.fasterxml.jackson.dataformat.yaml.UTF8Reader; + import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/BinaryWriteTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/BinaryWriteTest.java new file mode 100644 index 00000000..da8b86fe --- /dev/null +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/BinaryWriteTest.java @@ -0,0 +1,29 @@ +package com.fasterxml.jackson.dataformat.yaml.ser; + +import org.junit.Assert; + +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.node.JsonNodeType; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; + +public class BinaryWriteTest extends ModuleTestBase +{ + private final ObjectMapper MAPPER = newObjectMapper(); + + public void testBinaryViaTree() throws Exception + { + byte[] srcPayload = new byte[] { 1, 2, 3, 4, 5 }; + ObjectNode root = MAPPER.createObjectNode(); + root.put("payload", srcPayload); + String doc = MAPPER.writeValueAsString(root); + + // and read back + final JsonNode bean = MAPPER.readTree(doc); + final JsonNode data = bean.get("payload"); + assertNotNull(data); + assertEquals(JsonNodeType.BINARY, data.getNodeType()); + final byte[] b = data.binaryValue(); + Assert.assertArrayEquals(srcPayload, b); + } +} diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/DatabindWriteTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/DatabindWriteTest.java new file mode 100644 index 00000000..d21479c7 --- /dev/null +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/DatabindWriteTest.java @@ -0,0 +1,77 @@ +package com.fasterxml.jackson.dataformat.yaml.ser; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeSet; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; + +public class DatabindWriteTest extends ModuleTestBase +{ + public void testBasicPOJO() throws Exception + { + ObjectMapper mapper = newObjectMapper(); + FiveMinuteUser user = new FiveMinuteUser("Bob", "Dabolito", false, + FiveMinuteUser.Gender.MALE, new byte[] { 1, 3, 13, 79 }); + String yaml = mapper.writeValueAsString(user).trim(); + String[] parts = yaml.split("\n"); + // unify ordering, need to use TreeSets to get alphabetic ordering + TreeSet exp = new TreeSet(); + for (String part : parts) { + exp.add(part.trim()); + } + Iterator it = exp.iterator(); + assertEquals("---", it.next()); + assertEquals("AQMNTw==", it.next()); + assertEquals("firstName: \"Bob\"", it.next()); + assertEquals("gender: \"MALE\"", it.next()); + assertEquals("lastName: \"Dabolito\"", it.next()); + assertEquals("userImage: !!binary |", it.next()); + assertEquals("verified: false", it.next()); + assertFalse(it.hasNext()); + } + + public void testWithFile() throws Exception + { + File f = File.createTempFile("test", ".yml"); + f.deleteOnExit(); + ObjectMapper mapper = newObjectMapper(); + Map map = new HashMap(); + map.put("a", 3); + mapper.writeValue(f, map); + assertTrue(f.canRead()); + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream( + f), "UTF-8")); + String doc = br.readLine(); + String str = br.readLine(); + if (str != null) { + doc += "\n" + str; + } + doc = trimDocMarker(doc); + assertEquals("a: 3", doc); + br.close(); + f.delete(); + } + + public void testWithFile2() throws Exception + { + File f = File.createTempFile("test", ".yml"); + f.deleteOnExit(); + ObjectMapper mapper = newObjectMapper(); + ObjectNode root = mapper.createObjectNode(); + root.put("name", "Foobar"); + mapper.writeValue(f, root); + + // and get it back + Map result = mapper.readValue(f, Map.class); + assertEquals(1, result.size()); + assertEquals("Foobar", result.get("name")); + } +} diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeneratorFeature34Test.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorFeature34Test.java similarity index 78% rename from yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeneratorFeature34Test.java rename to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorFeature34Test.java index 0baccd8c..8f86cdfd 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeneratorFeature34Test.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorFeature34Test.java @@ -1,7 +1,12 @@ -package com.fasterxml.jackson.dataformat.yaml; +package com.fasterxml.jackson.dataformat.yaml.ser; import java.util.Map; +import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; + public class GeneratorFeature34Test extends ModuleTestBase { /* diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeneratorFeatureTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorFeatureTest.java similarity index 86% rename from yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeneratorFeatureTest.java rename to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorFeatureTest.java index 6bb6a422..7e04023e 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeneratorFeatureTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorFeatureTest.java @@ -1,9 +1,13 @@ -package com.fasterxml.jackson.dataformat.yaml; +package com.fasterxml.jackson.dataformat.yaml.ser; import java.util.Arrays; import java.util.List; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; public class GeneratorFeatureTest extends ModuleTestBase { diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeratorWithMinimizeTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithMinimizeTest.java similarity index 90% rename from yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeratorWithMinimizeTest.java rename to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithMinimizeTest.java index 13a8e675..9279b2c1 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeratorWithMinimizeTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithMinimizeTest.java @@ -1,9 +1,15 @@ -package com.fasterxml.jackson.dataformat.yaml; +package com.fasterxml.jackson.dataformat.yaml.ser; import java.util.HashMap; import java.util.Map; -public class GeratorWithMinimizeTest extends ModuleTestBase +import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; + +public class GeneratorWithMinimizeTest extends ModuleTestBase { private final static YAMLMapper MINIM_MAPPER = new YAMLMapper(); static { diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeneratorWithSplitLinesTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithSplitLinesTest.java similarity index 76% rename from yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeneratorWithSplitLinesTest.java rename to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithSplitLinesTest.java index a9fbc6ea..ed301b5b 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/GeneratorWithSplitLinesTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithSplitLinesTest.java @@ -1,4 +1,10 @@ -package com.fasterxml.jackson.dataformat.yaml; +package com.fasterxml.jackson.dataformat.yaml.ser; + +import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; public class GeneratorWithSplitLinesTest extends ModuleTestBase { diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleGenerationTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/SimpleGenerationTest.java similarity index 60% rename from yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleGenerationTest.java rename to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/SimpleGenerationTest.java index bf9542e5..157710bb 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleGenerationTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/SimpleGenerationTest.java @@ -1,19 +1,23 @@ -package com.fasterxml.jackson.dataformat.yaml; +package com.fasterxml.jackson.dataformat.yaml.ser; import java.io.*; import java.util.*; import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; + +import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; public class SimpleGenerationTest extends ModuleTestBase { + private final YAMLFactory YAML_F = new YAMLFactory(); + public void testStreamingArray() throws Exception { - YAMLFactory f = new YAMLFactory(); StringWriter w = new StringWriter(); - JsonGenerator gen = f.createGenerator(w); + JsonGenerator gen = YAML_F.createGenerator(w); gen.writeStartArray(); gen.writeNumber(3); gen.writeString("foobar"); @@ -29,9 +33,8 @@ public void testStreamingArray() throws Exception public void testStreamingObject() throws Exception { - YAMLFactory f = new YAMLFactory(); StringWriter w = new StringWriter(); - JsonGenerator gen = f.createGenerator(w); + JsonGenerator gen = YAML_F.createGenerator(w); _writeBradDoc(gen); String yaml = w.toString(); @@ -43,9 +46,8 @@ public void testStreamingObject() throws Exception public void testStreamingNested() throws Exception { - YAMLFactory f = new YAMLFactory(); StringWriter w = new StringWriter(); - JsonGenerator gen = f.createGenerator(w); + JsonGenerator gen = YAML_F.createGenerator(w); gen.writeStartObject(); gen.writeFieldName("ob"); @@ -73,71 +75,6 @@ public void testStreamingNested() throws Exception br.close(); } - public void testBasicPOJO() throws Exception - { - ObjectMapper mapper = newObjectMapper(); - FiveMinuteUser user = new FiveMinuteUser("Bob", "Dabolito", false, - FiveMinuteUser.Gender.MALE, new byte[] { 1, 3, 13, 79 }); - String yaml = mapper.writeValueAsString(user).trim(); - String[] parts = yaml.split("\n"); - boolean gotHeader = (parts.length == 6); - if (!gotHeader) { - // 1.10 has 6 as it has header - assertEquals(5, parts.length); - } - // unify ordering, need to use TreeSets - TreeSet exp = new TreeSet(); - for (String part : parts) { - exp.add(part.trim()); - } - Iterator it = exp.iterator(); - if (gotHeader) { - assertEquals("---", it.next()); - } - assertEquals("firstName: \"Bob\"", it.next()); - assertEquals("gender: \"MALE\"", it.next()); - assertEquals("lastName: \"Dabolito\"", it.next()); - assertEquals("userImage: \"AQMNTw==\"", it.next()); - assertEquals("verified: false", it.next()); - } - - public void testWithFile() throws Exception - { - File f = File.createTempFile("test", ".yml"); - f.deleteOnExit(); - ObjectMapper mapper = newObjectMapper(); - Map map = new HashMap(); - map.put("a", 3); - mapper.writeValue(f, map); - assertTrue(f.canRead()); - BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream( - f), "UTF-8")); - String doc = br.readLine(); - String str = br.readLine(); - if (str != null) { - doc += "\n" + str; - } - doc = trimDocMarker(doc); - assertEquals("a: 3", doc); - br.close(); - f.delete(); - } - - public void testWithFile2() throws Exception - { - File f = File.createTempFile("test", ".yml"); - f.deleteOnExit(); - ObjectMapper mapper = newObjectMapper(); - ObjectNode root = mapper.createObjectNode(); - root.put("name", "Foobar"); - mapper.writeValue(f, root); - - // and get it back - Map result = mapper.readValue(f, Map.class); - assertEquals(1, result.size()); - assertEquals("Foobar", result.get("name")); - } - @SuppressWarnings("resource") public void testStartMarker() throws Exception {