content: package com.alibaba.fastjson2.support.spring6;
  
  import com.alibaba.fastjson2.support.spring6.data.redis.GenericFastJsonRedisSerializer;
  import org.hamcrest.core.Is;
  import org.hamcrest.core.IsNull;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.springframework.data.redis.serializer.SerializationException;
  
  import java.nio.charset.StandardCharsets;
  import java.util.Arrays;
  import java.util.List;
  
  import static org.hamcrest.MatcherAssert.assertThat;
  import static org.junit.jupiter.api.Assertions.*;
  
  public class GenericFastJsonRedisSerializerTest {
      private GenericFastJsonRedisSerializer serializer;
  
      @BeforeEach
      public void setUp() {
          this.serializer = new GenericFastJsonRedisSerializer();
      }
  
      @Test
      public void test_1() {
          User user = (User) serializer.deserialize(serializer.serialize(new User(1, "土豆", 25)));
          assertTrue(user.getId().equals(1));
          assertTrue(user.getName().equals("土豆"));
          assertTrue(user.getAge().equals(25));
      }
  
      @Test
      public void test_2() {
          assertThat(serializer.serialize(null), Is.is(new byte[0]));
      }
  
      @Test
      public void test_3() {
          assertThat(serializer.deserialize(new byte[0]), IsNull.nullValue());
      }
  
      @Test
      public void test_4() {
          assertThat(serializer.deserialize(null), IsNull.nullValue());
      }
  
      @Test
      public void test_5() {
          assertThrows(SerializationException.class, () -> {
              User user = new User(1, "土豆", 25);
              byte[] serializedValue = serializer.serialize(user);
              Arrays.sort(serializedValue); // corrupt serialization result
              serializer.deserialize(serializedValue);
          });
      }
  
      @Test
      public void test_6() {
          BaseResult<List<String>> baseResult = new BaseResult<List<String>>();
          baseResult.setCode("1000");
          baseResult.setMsg("success");
          baseResult.setData(Arrays.asList("测试1", "测试2", "测试3"));
  
          GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
          byte[] bytes = genericFastJsonRedisSerializer.serialize(baseResult);
          BaseResult<List<String>> baseResult2 = (BaseResult<List<String>>) genericFastJsonRedisSerializer.deserialize(bytes);
  
          assertEquals(baseResult2.getCode(), "1000");
          assertEquals(baseResult2.getData().size(), 3);
  
          String json = "{\n" +
                  "\"@type\": \"com.alibaba.fastjson2.support.spring6.GenericFastJsonRedisSerializerTest$BaseResult\",\n" +
                  "\"code\": \"1000\",\n" +
                  "\"data\": [\n" +
                  "\"按手动控制按钮\",\n" +
                  "\"不停机\",\n" +
                  "\"不转动\",\n" +
                  "\"传动轴振动大\",\n" +
                  "\"第一推进器\",\n" +
                  "\"电机不运行\",\n" +
                  "],\n" +
                  "\"msg\": \"success\"\n" +
                  "}";
  
          BaseResult<List<String>> baseResult3 = (BaseResult<List<String>>) genericFastJsonRedisSerializer.deserialize(json.getBytes(StandardCharsets.UTF_8));
          assertEquals(baseResult3.getCode(), "1000");
          assertEquals(baseResult3.getData().size(), 6);
      }
  
      static class User {
          private Integer id;
          private String name;
          private Integer age;
  
          public User() {
          }
  
          public User(Integer id, String name, Integer age) {
              this.id = id;
              this.name = name;
              this.age = age;
          }
  
          public Integer getId() {
              return id;
          }
  
          public void setId(Integer id) {
              this.id = id;
          }
  
          public String getName() {
              return name;
          }
  
          public void setName(String name) {
              this.name = name;
          }
  
          public Integer getAge() {
              return age;
          }
  
          public void setAge(Integer age) {
              this.age = age;
          }
      }
  
      static class BaseResult<T> {
          private String msg;
          private String code;
          private T data;
  
          public String getMsg() {
              return msg;
          }
  
          public void setMsg(String msg) {
              this.msg = msg;
          }
  
          public String getCode() {
              return code;
          }
  
          public void setCode(String code) {
              this.code = code;
          }
  
          public T getData() {
              return data;
          }
  
          public void setData(T data) {
              this.data = data;
          }
      }
  }
  