Skip to content

Commit 4979d8f

Browse files
devdilsonsbrannen
authored andcommitted
Increase code coverage in spring-core serializer package
Closes gh-30744
1 parent f9b6aed commit 4979d8f

File tree

3 files changed

+142
-11
lines changed

3 files changed

+142
-11
lines changed

spring-core/src/main/java/org/springframework/core/serializer/DefaultDeserializer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public DefaultDeserializer(@Nullable ClassLoader classLoader) {
6767
@Override
6868
@SuppressWarnings("resource")
6969
public Object deserialize(InputStream inputStream) throws IOException {
70-
ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, this.classLoader);
71-
try {
70+
try (ConfigurableObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, this.classLoader)){
7271
return objectInputStream.readObject();
7372
}
7473
catch (ClassNotFoundException ex) {

spring-core/src/test/java/org/springframework/core/serializer/SerializationConverterTests.java

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,17 +16,24 @@
1616

1717
package org.springframework.core.serializer;
1818

19+
import java.io.ByteArrayInputStream;
1920
import java.io.NotSerializableException;
2021
import java.io.Serializable;
2122

2223
import org.junit.jupiter.api.Test;
24+
import org.mockito.MockedConstruction;
25+
import org.mockito.Mockito;
2326

27+
import org.springframework.core.ConfigurableObjectInputStream;
2428
import org.springframework.core.serializer.support.DeserializingConverter;
2529
import org.springframework.core.serializer.support.SerializationFailedException;
2630
import org.springframework.core.serializer.support.SerializingConverter;
2731

2832
import static org.assertj.core.api.Assertions.assertThat;
2933
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
34+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
35+
import static org.mockito.BDDMockito.given;
36+
3037

3138
/**
3239
* @author Gary Russell
@@ -43,27 +50,64 @@ void serializeAndDeserializeString() {
4350
assertThat(fromBytes.convert(bytes)).isEqualTo("Testing");
4451
}
4552

53+
@Test
54+
void serializeAndDeserializeStringWithCustomSerializer() {
55+
SerializingConverter toBytes = new SerializingConverter(new DefaultSerializer());
56+
byte[] bytes = toBytes.convert("Testing");
57+
DeserializingConverter fromBytes = new DeserializingConverter();
58+
assertThat(fromBytes.convert(bytes)).isEqualTo("Testing");
59+
}
60+
4661
@Test
4762
void nonSerializableObject() {
4863
SerializingConverter toBytes = new SerializingConverter();
49-
assertThatExceptionOfType(SerializationFailedException.class).isThrownBy(() ->
50-
toBytes.convert(new Object()))
51-
.withCauseInstanceOf(IllegalArgumentException.class);
64+
assertThatExceptionOfType(SerializationFailedException.class)
65+
.isThrownBy(() -> toBytes.convert(new Object()))
66+
.withCauseInstanceOf(IllegalArgumentException.class);
5267
}
5368

5469
@Test
5570
void nonSerializableField() {
5671
SerializingConverter toBytes = new SerializingConverter();
57-
assertThatExceptionOfType(SerializationFailedException.class).isThrownBy(() ->
58-
toBytes.convert(new UnSerializable()))
59-
.withCauseInstanceOf(NotSerializableException.class);
72+
assertThatExceptionOfType(SerializationFailedException.class)
73+
.isThrownBy(() -> toBytes.convert(new UnSerializable()))
74+
.withCauseInstanceOf(NotSerializableException.class);
6075
}
6176

6277
@Test
6378
void deserializationFailure() {
6479
DeserializingConverter fromBytes = new DeserializingConverter();
65-
assertThatExceptionOfType(SerializationFailedException.class).isThrownBy(() ->
66-
fromBytes.convert("Junk".getBytes()));
80+
assertThatExceptionOfType(SerializationFailedException.class)
81+
.isThrownBy(() -> fromBytes.convert("Junk".getBytes()));
82+
}
83+
84+
@Test
85+
void deserializationWithClassLoader() {
86+
DeserializingConverter fromBytes = new DeserializingConverter(this.getClass().getClassLoader());
87+
SerializingConverter toBytes = new SerializingConverter();
88+
String expected = "SPRING FRAMEWORK";
89+
assertThat(fromBytes.convert(toBytes.convert(expected))).isEqualTo(expected);
90+
}
91+
92+
@Test
93+
void deserializationWithDeserializer() {
94+
DeserializingConverter fromBytes = new DeserializingConverter(new DefaultDeserializer());
95+
SerializingConverter toBytes = new SerializingConverter();
96+
String expected = "SPRING FRAMEWORK";
97+
assertThat(fromBytes.convert(toBytes.convert(expected))).isEqualTo(expected);
98+
}
99+
100+
@Test
101+
void deserializationIOException() {
102+
try (MockedConstruction<ConfigurableObjectInputStream> mocked = Mockito.mockConstruction(
103+
ConfigurableObjectInputStream.class, (mock, context) -> given(mock.readObject())
104+
.willThrow(new ClassNotFoundException()))) {
105+
DefaultDeserializer defaultSerializer = new DefaultDeserializer(this.getClass().getClassLoader());
106+
assertThat(mocked).isNotNull();
107+
assertThatThrownBy(() -> defaultSerializer.deserialize(
108+
new ByteArrayInputStream("test".getBytes())))
109+
.hasMessage("Failed to deserialize object type");
110+
}
67111
}
68112

69113

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.serializer;
18+
19+
import java.io.ByteArrayInputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.OutputStream;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.core.serializer.support.SerializationDelegate;
27+
28+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
29+
30+
31+
class SerializerTests {
32+
33+
private static final String SPRING_FRAMEWORK = "Spring Framework";
34+
35+
36+
@Test
37+
void serializeToByteArray() throws IOException {
38+
SpyStringSerializer serializer = new SpyStringSerializer<String>();
39+
serializer.serializeToByteArray(SPRING_FRAMEWORK);
40+
assertThat(serializer.expectedObject).isEqualTo(SPRING_FRAMEWORK);
41+
assertThat(serializer.expectedOs).isNotNull();
42+
}
43+
44+
@Test
45+
void deserializeToByteArray() throws IOException {
46+
SpyStringDeserializer deserializer = new SpyStringDeserializer();
47+
deserializer.deserializeFromByteArray(SPRING_FRAMEWORK.getBytes());
48+
assertThat(deserializer.expectedObject).isEqualTo(SPRING_FRAMEWORK);
49+
}
50+
51+
@Test
52+
void serializationDelegate() throws IOException {
53+
SerializationDelegate delegate = new SerializationDelegate(new DefaultSerializer(), new DefaultDeserializer());
54+
byte[] serializedObj = delegate.serializeToByteArray(SPRING_FRAMEWORK);
55+
Object deserializedObj = delegate.deserialize(new ByteArrayInputStream(serializedObj));
56+
assertThat(deserializedObj).isEqualTo(SPRING_FRAMEWORK);
57+
}
58+
59+
@Test
60+
void serializationDelegateWithClassLoader() throws IOException {
61+
SerializationDelegate delegate = new SerializationDelegate(this.getClass().getClassLoader());
62+
byte[] serializedObj = delegate.serializeToByteArray(SPRING_FRAMEWORK);
63+
Object deserializedObj = delegate.deserialize(new ByteArrayInputStream(serializedObj));
64+
assertThat(deserializedObj).isEqualTo(SPRING_FRAMEWORK);
65+
}
66+
67+
static class SpyStringSerializer<T> implements Serializer<T> {
68+
T expectedObject;
69+
OutputStream expectedOs;
70+
71+
@Override
72+
public void serialize(T object, OutputStream outputStream) {
73+
expectedObject = object;
74+
expectedOs = outputStream;
75+
}
76+
}
77+
78+
static class SpyStringDeserializer implements Deserializer<Object> {
79+
Object expectedObject;
80+
81+
82+
@Override
83+
public String deserialize(InputStream inputStream) {
84+
expectedObject = SPRING_FRAMEWORK;
85+
return SPRING_FRAMEWORK;
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)