Skip to content

Commit

Permalink
add serialization test (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Apr 27, 2024
1 parent 9401ae3 commit d4b08c0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
Expand Up @@ -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.*;

/**
Expand Down Expand Up @@ -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> 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
{
Expand Down
@@ -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> 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();
}
}
}
Expand Up @@ -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;

/**
Expand All @@ -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:
Expand Down Expand Up @@ -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));
}
}

0 comments on commit d4b08c0

Please sign in to comment.