1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .core .serializer ;
18
18
19
+ import java .io .ByteArrayInputStream ;
19
20
import java .io .NotSerializableException ;
20
21
import java .io .Serializable ;
21
22
22
23
import org .junit .jupiter .api .Test ;
24
+ import org .mockito .MockedConstruction ;
25
+ import org .mockito .Mockito ;
23
26
27
+ import org .springframework .core .ConfigurableObjectInputStream ;
24
28
import org .springframework .core .serializer .support .DeserializingConverter ;
25
29
import org .springframework .core .serializer .support .SerializationFailedException ;
26
30
import org .springframework .core .serializer .support .SerializingConverter ;
27
31
28
32
import static org .assertj .core .api .Assertions .assertThat ;
29
33
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
+
30
37
31
38
/**
32
39
* @author Gary Russell
@@ -43,27 +50,64 @@ void serializeAndDeserializeString() {
43
50
assertThat (fromBytes .convert (bytes )).isEqualTo ("Testing" );
44
51
}
45
52
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
+
46
61
@ Test
47
62
void nonSerializableObject () {
48
63
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 );
52
67
}
53
68
54
69
@ Test
55
70
void nonSerializableField () {
56
71
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 );
60
75
}
61
76
62
77
@ Test
63
78
void deserializationFailure () {
64
79
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
+ }
67
111
}
68
112
69
113
0 commit comments