|
15 | 15 | */
|
16 | 16 | package org.springframework.data.redis.serializer;
|
17 | 17 |
|
18 |
| -import static org.assertj.core.api.Assertions.*; |
| 18 | +import org.junit.jupiter.api.Test; |
19 | 19 |
|
20 | 20 | import java.nio.ByteBuffer;
|
| 21 | +import java.nio.charset.StandardCharsets; |
21 | 22 |
|
22 |
| -import org.junit.jupiter.api.Test; |
| 23 | +import static org.assertj.core.api.Assertions.*; |
23 | 24 |
|
24 | 25 | /**
|
25 | 26 | * Unit tests for {@link RedisSerializationContext}.
|
26 | 27 | *
|
27 | 28 | * @author Mark Paluch
|
28 | 29 | * @author Christoph Strobl
|
| 30 | + * @author Zhou KQ |
29 | 31 | */
|
30 | 32 | class RedisSerializationContextUnitTests {
|
31 | 33 |
|
@@ -137,6 +139,48 @@ void shouldEncodeAndDecodeByteArrayValue() {
|
137 | 139 | assertThat(deserialized).isEqualTo("hello".getBytes());
|
138 | 140 | }
|
139 | 141 |
|
| 142 | + @Test |
| 143 | + void shouldEncodeAndDecodeUtf8StringValue() { |
| 144 | + RedisSerializationContext<String, String> serializationContext = RedisSerializationContext |
| 145 | + .<String, String>newSerializationContext(StringRedisSerializer.UTF_8) |
| 146 | + .string(StringRedisSerializer.UTF_8) |
| 147 | + .build(); |
| 148 | + RedisSerializationContext.SerializationPair<String> serializationPair = serializationContext.getStringSerializationPair(); |
| 149 | + |
| 150 | + assertThat(serializationPair.write("üߨ")) |
| 151 | + .isEqualTo(ByteBuffer.wrap("üߨ".getBytes(StandardCharsets.UTF_8))); |
| 152 | + assertThat(serializationPair.read(ByteBuffer.wrap("üߨ".getBytes(StandardCharsets.UTF_8)))) |
| 153 | + .isEqualTo("üߨ"); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void shouldEncodeAndDecodeAsciiStringValue() { |
| 158 | + RedisSerializationContext<String, String> serializationContext = RedisSerializationContext |
| 159 | + .<String, String>newSerializationContext(StringRedisSerializer.US_ASCII) |
| 160 | + .string(StringRedisSerializer.US_ASCII) |
| 161 | + .build(); |
| 162 | + RedisSerializationContext.SerializationPair<String> serializationPair = serializationContext.getStringSerializationPair(); |
| 163 | + |
| 164 | + assertThat(serializationPair.write("üߨ")) |
| 165 | + .isEqualTo(ByteBuffer.wrap("???".getBytes(StandardCharsets.US_ASCII))); |
| 166 | + assertThat(serializationPair.read(ByteBuffer.wrap("üߨ".getBytes(StandardCharsets.US_ASCII)))) |
| 167 | + .isEqualTo("???"); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void shouldEncodeAndDecodeIso88591StringValue() { |
| 172 | + RedisSerializationContext<String, String> serializationContext = RedisSerializationContext |
| 173 | + .<String, String>newSerializationContext(StringRedisSerializer.ISO_8859_1) |
| 174 | + .string(StringRedisSerializer.ISO_8859_1) |
| 175 | + .build(); |
| 176 | + RedisSerializationContext.SerializationPair<String> serializationPair = serializationContext.getStringSerializationPair(); |
| 177 | + |
| 178 | + assertThat(serializationPair.write("üߨ")) |
| 179 | + .isEqualTo(ByteBuffer.wrap("üߨ".getBytes(StandardCharsets.ISO_8859_1))); |
| 180 | + assertThat(serializationPair.read(ByteBuffer.wrap("üߨ".getBytes(StandardCharsets.ISO_8859_1)))) |
| 181 | + .isEqualTo("üߨ"); |
| 182 | + } |
| 183 | + |
140 | 184 | private RedisSerializationContext<String, Long> createSerializationContext() {
|
141 | 185 |
|
142 | 186 | return RedisSerializationContext.<String, Long> newSerializationContext() //
|
|
0 commit comments