stream-query的BeanHelper拷贝支持Converter

简介: stream-query的BeanHelper拷贝支持Converter

这还是个实验性功能,首先引入:

<dependency>
   <groupId>org.dromara.stream-query</groupId>
   <artifactId>stream-plugin-mybatis-plus</artifactId>
   <version>2.1.0-alpha</version>
</dependency>

然后对应的单元测试:

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dromara.streamquery.stream.core.bean;

import lombok.Data;
import lombok.experimental.Accessors;
import lombok.val;
import org.junit.jupiter.api.Test;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;

/**
 * BeanHelperTest
 *
 * @author VampireAchao
 * @since 2023/3/15
 */
class BeanHelperTest {

  @Test
  void testGetSetterName() {
    assertEquals("setName", BeanHelper.getSetterName("name"));
    assertEquals("setLambda", BeanHelper.getSetterName("lambda"));
  }

  @Test
  void testGetGetterName() {
    assertEquals("getName", BeanHelper.getGetterName("name"));
    assertEquals("getLambda", BeanHelper.getGetterName("lambda"));
  }

  @Data
  public static class User {
    private String name;
  }

  @Data
  @Accessors(chain = true)
  public static class Person {
    private String name;
  }

  @Data
  public static class Artist implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
  }

  @Test
  void testCopyProperties() {
    User source =
        new User() {
          {
            setName("test");
          }
        };
    Person person = BeanHelper.copyProperties(source, Person.class);
    assertEquals(source.getName(), person.getName());

    Artist artist =
        new Artist() {
          private static final long serialVersionUID = 6276191330280044345L;

          {
            setName("test");
          }
        };
    User user = BeanHelper.copyProperties(source, User.class);
    assertEquals(artist.getName(), user.getName());
  }

  @Data
  public static class EntityWithStringId {
    private String id;
  }

  @Data
  public static class EntityWithIntegerId {
    private Integer id;
  }

  @Test
  void testCopyPropertiesWithConverter() {
    val source = new EntityWithStringId();
    source.setId("1");
    EntityWithIntegerId target = BeanHelper.copyProperties(source, EntityWithIntegerId.class);
    assertEquals(source.getId(), target.getId().toString());
    assertEquals(
        source.getId(), BeanHelper.copyProperties(target, EntityWithStringId.class).getId());
    assertEquals(
        target.getId(),
        BeanHelper.copyProperties(
                source,
                EntityWithIntegerId.class,
                new CopyOption().addConverter(String.class, Integer.class, Integer::new))
            .getId());
    assertEquals(
        target.getId(),
        BeanHelper.copyProperties(
                source,
                EntityWithIntegerId.class,
                new CopyOption()
                    .addConverter(
                        EntityWithStringId.class,
                        EntityWithIntegerId.class,
                        s -> {
                          val t = new EntityWithIntegerId();
                          t.setId(Integer.valueOf(s.getId()));
                          return t;
                        }))
            .getId());

    assertNull(BeanHelper.copyProperties(null, Object.class));
    assertEquals("1", BeanHelper.copyProperties(1, String.class));
    assertEquals(Integer.valueOf(1), BeanHelper.copyProperties(1L, Integer.class));
    assertEquals(Integer.valueOf(1), BeanHelper.copyProperties("1", Integer.class));
    assertEquals(Long.valueOf(1L), BeanHelper.copyProperties("1", Long.class));
    assertEquals(Long.valueOf(1L), BeanHelper.copyProperties(1, Long.class));
    assertEquals(Float.valueOf(1.0f), BeanHelper.copyProperties(1.0, Float.class));
    assertEquals(Double.valueOf(1.0), BeanHelper.copyProperties("1.0", Double.class));
    assertEquals(Float.valueOf(1.0f), BeanHelper.copyProperties("1.0", Float.class));
    assertEquals(Double.valueOf(1.0), BeanHelper.copyProperties(1.0f, Double.class));
    assertEquals(Double.valueOf(1.0), BeanHelper.copyProperties(1, Double.class));
    assertEquals(Float.valueOf(1.0f), BeanHelper.copyProperties(1, Float.class));
    assertEquals(Float.valueOf(1.0f), BeanHelper.copyProperties(1L, Float.class));
    assertEquals(Double.valueOf(1.0), BeanHelper.copyProperties(1L, Double.class));
    assertEquals(Boolean.TRUE, BeanHelper.copyProperties("true", Boolean.class));
    Date now = new Date();
    assertEquals(now.getTime(), BeanHelper.copyProperties(now, Long.class));
    assertEquals(new Date(now.getTime()), BeanHelper.copyProperties(now.getTime(), Date.class));
    UUID uuid = UUID.randomUUID();
    assertEquals(uuid, BeanHelper.copyProperties(uuid.toString(), UUID.class));
    LocalDateTime ldt = LocalDateTime.now();
    assertEquals(
        Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()),
        BeanHelper.copyProperties(ldt, Date.class));
    LocalDate ld = LocalDate.now();
    assertEquals(
        Date.from(ld.atStartOfDay(ZoneId.systemDefault()).toInstant()),
        BeanHelper.copyProperties(ld, Date.class));
    assertEquals(ld, BeanHelper.copyProperties(ldt, LocalDate.class));
    assertEquals(ld.atStartOfDay(), BeanHelper.copyProperties(ld, LocalDateTime.class));
  }

  @Test
  void testIgnoreError() {
    val source = new EntityWithStringId();
    source.setId("1");
    assertThrows(
        ConvertFailException.class,
        () -> BeanHelper.copyProperties(source, EntityWithIntegerId.class, new CopyOption()));
    assertDoesNotThrow(
        () ->
            BeanHelper.copyProperties(
                source, EntityWithIntegerId.class, new CopyOption().setIgnoreError(true)));
  }
}

这里可以自定义Converter,此处new CopyOption()是不带默认内置的Converter的,但是CopyOption.of()是带的

相关文章
|
数据库
mybatisplus返回指定字段的两种方式
mybatisplus返回指定字段的两种方式
1379 1
|
XML 前端开发 Android开发
Android:UI:Drawable:View/ImageView与Drawable
通过本文的介绍,我们详细探讨了Android中Drawable、View和ImageView的使用方法及其相互关系。Drawable作为图像和图形的抽象表示,提供了丰富的子类和自定义能力,使得开发者能够灵活地实现各种UI效果。View和ImageView则通过使用Drawable实现了各种图像和图形的显示需求。希望本文能为您在Android开发中使用Drawable提供有价值的参考和指导。
702 2
|
编译器
方法重载与抽象类3-13
方法重载与抽象类3-13
|
机器学习/深度学习 人工智能 Java
人工智能平台PAI产品使用合集之如何复制arn
阿里云人工智能平台PAI是一个功能强大、易于使用的AI开发平台,旨在降低AI开发门槛,加速创新,助力企业和开发者高效构建、部署和管理人工智能应用。其中包含了一系列相互协同的产品与服务,共同构成一个完整的人工智能开发与应用生态系统。以下是对PAI产品使用合集的概述,涵盖数据处理、模型开发、训练加速、模型部署及管理等多个环节。
|
存储 Python
解密虚拟机的执行环境:栈帧对象
解密虚拟机的执行环境:栈帧对象
313 0
|
Java Serverless Go
Golang 开发函数计算问题之在 Golang 中避免 "concurrent map writes" 异常如何解决
Golang 开发函数计算问题之在 Golang 中避免 "concurrent map writes" 异常如何解决
395 0
|
存储 搜索推荐 API
ES深度分页查询详解
ES深度分页查询详解
1766 0
ES深度分页查询详解
|
移动开发 算法 编译器
OAUTH之钉钉第三方授权 | GO主题月
hello,我是小魔童哪吒,欢迎点击关注,有更新,将第一时间呈现到你的面前 胖sir:小魔童,我今天收到了一个需求,期望我们做一个第三方登录的功能,用户可以通过第三方授权来登录我们的web
1064 0