前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >stream-query的BeanHelper拷贝支持Converter

stream-query的BeanHelper拷贝支持Converter

作者头像
阿超
发布2024-01-23 08:24:05
1080
发布2024-01-23 08:24:05
举报
文章被收录于专栏:快乐阿超快乐阿超

锲而舍之,朽木不折;锲而不舍,金石可镂。——荀子

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

代码语言:javascript
复制
<dependency>
   <groupId>org.dromara.stream-query</groupId>
   <artifactId>stream-plugin-mybatis-plus</artifactId>
   <version>2.1.0-alpha</version>
</dependency>

然后对应的单元测试:

代码语言:javascript
复制
/*
 * 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()是带的

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-01-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档