From d4b08c00c0d2ac9fc89d1e3657170ee9a9896bfd Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 27 Apr 2024 04:10:18 +0200 Subject: [PATCH] add serialization test (#1275) --- .../jackson/core/JDKSerializabilityTest.java | 25 ++------------ .../core/util/JdkSerializationTestUtils.java | 34 +++++++++++++++++++ .../core/util/TestSerializedString.java | 24 ++++++++++++- 3 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 src/test/java/com/fasterxml/jackson/core/util/JdkSerializationTestUtils.java diff --git a/src/test/java/com/fasterxml/jackson/core/JDKSerializabilityTest.java b/src/test/java/com/fasterxml/jackson/core/JDKSerializabilityTest.java index 4da19ea005..2710170cc0 100644 --- a/src/test/java/com/fasterxml/jackson/core/JDKSerializabilityTest.java +++ b/src/test/java/com/fasterxml/jackson/core/JDKSerializabilityTest.java @@ -9,6 +9,8 @@ import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.core.util.JsonRecyclerPools; +import static com.fasterxml.jackson.core.util.JdkSerializationTestUtils.jdkDeserialize; +import static com.fasterxml.jackson.core.util.JdkSerializationTestUtils.jdkSerialize; import static org.junit.jupiter.api.Assertions.*; /** @@ -259,29 +261,6 @@ void pointerSerializationEmpty() throws Exception /********************************************************** */ - protected byte[] jdkSerialize(Object o) throws IOException - { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000); - ObjectOutputStream obOut = new ObjectOutputStream(bytes); - obOut.writeObject(o); - obOut.close(); - return bytes.toByteArray(); - } - - @SuppressWarnings("unchecked") - protected T jdkDeserialize(byte[] raw) throws IOException - { - ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(raw)); - try { - return (T) objIn.readObject(); - } catch (ClassNotFoundException e) { - fail("Missing class: "+e.getMessage()); - return null; - } finally { - objIn.close(); - } - } - @SuppressWarnings("resource") protected String _copyJson(JsonFactory f, String json, boolean useBytes) throws IOException { diff --git a/src/test/java/com/fasterxml/jackson/core/util/JdkSerializationTestUtils.java b/src/test/java/com/fasterxml/jackson/core/util/JdkSerializationTestUtils.java new file mode 100644 index 0000000000..21bcccda7e --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/core/util/JdkSerializationTestUtils.java @@ -0,0 +1,34 @@ +package com.fasterxml.jackson.core.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import static org.junit.jupiter.api.Assertions.fail; + +public class JdkSerializationTestUtils { + public static byte[] jdkSerialize(Object o) throws IOException + { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000); + ObjectOutputStream obOut = new ObjectOutputStream(bytes); + obOut.writeObject(o); + obOut.close(); + return bytes.toByteArray(); + } + + @SuppressWarnings("unchecked") + public static T jdkDeserialize(byte[] raw) throws IOException + { + ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(raw)); + try { + return (T) objIn.readObject(); + } catch (ClassNotFoundException e) { + fail("Missing class: "+e.getMessage()); + return null; + } finally { + objIn.close(); + } + } +} diff --git a/src/test/java/com/fasterxml/jackson/core/util/TestSerializedString.java b/src/test/java/com/fasterxml/jackson/core/util/TestSerializedString.java index be7b6e4c30..8be473112c 100644 --- a/src/test/java/com/fasterxml/jackson/core/util/TestSerializedString.java +++ b/src/test/java/com/fasterxml/jackson/core/util/TestSerializedString.java @@ -9,6 +9,8 @@ import org.junit.jupiter.api.Test; import com.fasterxml.jackson.core.io.SerializedString; +import static com.fasterxml.jackson.core.util.JdkSerializationTestUtils.jdkDeserialize; +import static com.fasterxml.jackson.core.util.JdkSerializationTestUtils.jdkSerialize; import static org.junit.jupiter.api.Assertions.assertEquals; /** @@ -18,11 +20,12 @@ class TestSerializedString extends com.fasterxml.jackson.core.JUnit5TestBase { + private static final String QUOTED = "\\\"quo\\\\ted\\\""; + @Test void appending() throws IOException { final String INPUT = "\"quo\\ted\""; - final String QUOTED = "\\\"quo\\\\ted\\\""; SerializableString sstr = new SerializedString(INPUT); // sanity checks first: @@ -63,4 +66,23 @@ void failedAccess() throws IOException assertEquals(-1, sstr.appendUnquoted(ch, 0)); assertEquals(-1, sstr.putUnquotedUTF8(bbuf)); } + + @Test + void testAppendQuotedUTF8() throws IOException { + SerializedString sstr = new SerializedString(QUOTED); + assertEquals(QUOTED, sstr.getValue()); + final byte[] buffer = new byte[100]; + final int len = sstr.appendQuotedUTF8(buffer, 3); + assertEquals("\\\\\\\"quo\\\\\\\\ted\\\\\\\"", new String(buffer, 3, len)); + } + + @Test + void testJdkSerialize() throws IOException { + final byte[] bytes = jdkSerialize(new SerializedString(QUOTED)); + SerializedString sstr = jdkDeserialize(bytes); + assertEquals(QUOTED, sstr.getValue()); + final byte[] buffer = new byte[100]; + final int len = sstr.appendQuotedUTF8(buffer, 3); + assertEquals("\\\\\\\"quo\\\\\\\\ted\\\\\\\"", new String(buffer, 3, len)); + } }