Polish contribution
See gh-30744
This commit is contained in:
@@ -31,11 +31,12 @@ import org.springframework.core.serializer.support.SerializingConverter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assertions.assertThatIOException;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
|
||||
/**
|
||||
* Unit tests for {@link SerializingConverter} and {@link DeserializingConverter}.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @author Mark Fisher
|
||||
* @since 3.0.5
|
||||
@@ -43,7 +44,7 @@ import static org.mockito.BDDMockito.given;
|
||||
class SerializationConverterTests {
|
||||
|
||||
@Test
|
||||
void serializeAndDeserializeString() {
|
||||
void serializeAndDeserializeStringWithDefaultSerializer() {
|
||||
SerializingConverter toBytes = new SerializingConverter();
|
||||
byte[] bytes = toBytes.convert("Testing");
|
||||
DeserializingConverter fromBytes = new DeserializingConverter();
|
||||
@@ -51,7 +52,7 @@ class SerializationConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializeAndDeserializeStringWithCustomSerializer() {
|
||||
void serializeAndDeserializeStringWithExplicitSerializer() {
|
||||
SerializingConverter toBytes = new SerializingConverter(new DefaultSerializer());
|
||||
byte[] bytes = toBytes.convert("Testing");
|
||||
DeserializingConverter fromBytes = new DeserializingConverter();
|
||||
@@ -63,7 +64,9 @@ class SerializationConverterTests {
|
||||
SerializingConverter toBytes = new SerializingConverter();
|
||||
assertThatExceptionOfType(SerializationFailedException.class)
|
||||
.isThrownBy(() -> toBytes.convert(new Object()))
|
||||
.withCauseInstanceOf(IllegalArgumentException.class);
|
||||
.havingCause()
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.withMessageContaining("requires a Serializable payload");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -82,15 +85,15 @@ class SerializationConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void deserializationWithClassLoader() {
|
||||
DeserializingConverter fromBytes = new DeserializingConverter(this.getClass().getClassLoader());
|
||||
void deserializationWithExplicitClassLoader() {
|
||||
DeserializingConverter fromBytes = new DeserializingConverter(getClass().getClassLoader());
|
||||
SerializingConverter toBytes = new SerializingConverter();
|
||||
String expected = "SPRING FRAMEWORK";
|
||||
assertThat(fromBytes.convert(toBytes.convert(expected))).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deserializationWithDeserializer() {
|
||||
void deserializationWithExplicitDeserializer() {
|
||||
DeserializingConverter fromBytes = new DeserializingConverter(new DefaultDeserializer());
|
||||
SerializingConverter toBytes = new SerializingConverter();
|
||||
String expected = "SPRING FRAMEWORK";
|
||||
@@ -99,24 +102,26 @@ class SerializationConverterTests {
|
||||
|
||||
@Test
|
||||
void deserializationIOException() {
|
||||
try (MockedConstruction<ConfigurableObjectInputStream> mocked = Mockito.mockConstruction(
|
||||
ConfigurableObjectInputStream.class, (mock, context) -> given(mock.readObject())
|
||||
.willThrow(new ClassNotFoundException()))) {
|
||||
DefaultDeserializer defaultSerializer = new DefaultDeserializer(this.getClass().getClassLoader());
|
||||
ClassNotFoundException classNotFoundException = new ClassNotFoundException();
|
||||
try (MockedConstruction<ConfigurableObjectInputStream> mocked =
|
||||
Mockito.mockConstruction(ConfigurableObjectInputStream.class,
|
||||
(mock, context) -> given(mock.readObject()).willThrow(classNotFoundException))) {
|
||||
DefaultDeserializer defaultSerializer = new DefaultDeserializer(getClass().getClassLoader());
|
||||
assertThat(mocked).isNotNull();
|
||||
assertThatThrownBy(() -> defaultSerializer.deserialize(
|
||||
new ByteArrayInputStream("test".getBytes())))
|
||||
.hasMessage("Failed to deserialize object type");
|
||||
assertThatIOException()
|
||||
.isThrownBy(() -> defaultSerializer.deserialize(new ByteArrayInputStream("test".getBytes())))
|
||||
.withMessage("Failed to deserialize object type")
|
||||
.havingCause().isSameAs(classNotFoundException);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class UnSerializable implements Serializable {
|
||||
static class UnSerializable implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@SuppressWarnings({"unused", "serial"})
|
||||
private Object object;
|
||||
private Object object = new Object();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,7 +27,11 @@ import org.springframework.core.serializer.support.SerializationDelegate;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
|
||||
/**
|
||||
* Unit tests for {@link Serializer}, {@link Deserializer}, and {@link SerializationDelegate}.
|
||||
*
|
||||
* @since 6.1
|
||||
*/
|
||||
class SerializerTests {
|
||||
|
||||
private static final String SPRING_FRAMEWORK = "Spring Framework";
|
||||
@@ -35,21 +39,47 @@ class SerializerTests {
|
||||
|
||||
@Test
|
||||
void serializeToByteArray() throws IOException {
|
||||
SpyStringSerializer serializer = new SpyStringSerializer<String>();
|
||||
|
||||
class SpyStringSerializer implements Serializer<String> {
|
||||
|
||||
String expectedObject;
|
||||
OutputStream expectedOutputStream;
|
||||
|
||||
@Override
|
||||
public void serialize(String object, OutputStream outputStream) {
|
||||
this.expectedObject = object;
|
||||
this.expectedOutputStream = outputStream;
|
||||
}
|
||||
}
|
||||
|
||||
SpyStringSerializer serializer = new SpyStringSerializer();
|
||||
serializer.serializeToByteArray(SPRING_FRAMEWORK);
|
||||
assertThat(serializer.expectedObject).isEqualTo(SPRING_FRAMEWORK);
|
||||
assertThat(serializer.expectedOs).isNotNull();
|
||||
assertThat(serializer.expectedOutputStream).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void deserializeToByteArray() throws IOException {
|
||||
|
||||
class SpyStringDeserializer implements Deserializer<String> {
|
||||
|
||||
InputStream expectedInputStream;
|
||||
|
||||
@Override
|
||||
public String deserialize(InputStream inputStream) {
|
||||
this.expectedInputStream = inputStream;
|
||||
return SPRING_FRAMEWORK;
|
||||
}
|
||||
}
|
||||
|
||||
SpyStringDeserializer deserializer = new SpyStringDeserializer();
|
||||
deserializer.deserializeFromByteArray(SPRING_FRAMEWORK.getBytes());
|
||||
assertThat(deserializer.expectedObject).isEqualTo(SPRING_FRAMEWORK);
|
||||
Object deserializedObj = deserializer.deserializeFromByteArray(SPRING_FRAMEWORK.getBytes());
|
||||
assertThat(deserializedObj).isEqualTo(SPRING_FRAMEWORK);
|
||||
assertThat(deserializer.expectedInputStream).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializationDelegate() throws IOException {
|
||||
void serializationDelegateWithExplicitSerializerAndDeserializer() throws IOException {
|
||||
SerializationDelegate delegate = new SerializationDelegate(new DefaultSerializer(), new DefaultDeserializer());
|
||||
byte[] serializedObj = delegate.serializeToByteArray(SPRING_FRAMEWORK);
|
||||
Object deserializedObj = delegate.deserialize(new ByteArrayInputStream(serializedObj));
|
||||
@@ -57,32 +87,11 @@ class SerializerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializationDelegateWithClassLoader() throws IOException {
|
||||
SerializationDelegate delegate = new SerializationDelegate(this.getClass().getClassLoader());
|
||||
void serializationDelegateWithExplicitClassLoader() throws IOException {
|
||||
SerializationDelegate delegate = new SerializationDelegate(getClass().getClassLoader());
|
||||
byte[] serializedObj = delegate.serializeToByteArray(SPRING_FRAMEWORK);
|
||||
Object deserializedObj = delegate.deserialize(new ByteArrayInputStream(serializedObj));
|
||||
assertThat(deserializedObj).isEqualTo(SPRING_FRAMEWORK);
|
||||
}
|
||||
|
||||
static class SpyStringSerializer<T> implements Serializer<T> {
|
||||
T expectedObject;
|
||||
OutputStream expectedOs;
|
||||
|
||||
@Override
|
||||
public void serialize(T object, OutputStream outputStream) {
|
||||
expectedObject = object;
|
||||
expectedOs = outputStream;
|
||||
}
|
||||
}
|
||||
|
||||
static class SpyStringDeserializer implements Deserializer<Object> {
|
||||
Object expectedObject;
|
||||
|
||||
|
||||
@Override
|
||||
public String deserialize(InputStream inputStream) {
|
||||
expectedObject = SPRING_FRAMEWORK;
|
||||
return SPRING_FRAMEWORK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user