Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add serialization test #1275

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
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));
}
}