From fe597c7a183b2fbd011b496b8d4ba9a85a1ca0e2 Mon Sep 17 00:00:00 2001 From: Gdk666 <763366136@qq.com> Date: Sun, 18 Sep 2022 19:12:34 +0800 Subject: [PATCH] add BeanUtilTest test case (#706) * add BeanUtilTest test case * add BeanUtilTest test case * add BeanUtilTest test case --- .../hippo4j/config/toolkit/BeanUtilTest.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/hippo4j-config/src/test/java/cn/hippo4j/config/toolkit/BeanUtilTest.java b/hippo4j-config/src/test/java/cn/hippo4j/config/toolkit/BeanUtilTest.java index 9a090ae5..c0e1e066 100644 --- a/hippo4j-config/src/test/java/cn/hippo4j/config/toolkit/BeanUtilTest.java +++ b/hippo4j-config/src/test/java/cn/hippo4j/config/toolkit/BeanUtilTest.java @@ -17,8 +17,108 @@ package cn.hippo4j.config.toolkit; +import cn.hippo4j.common.toolkit.Assert; +import com.github.dozermapper.core.converters.ConversionException; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import lombok.*; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + /** * BeanUtil Test */ public class BeanUtilTest { + + @Test + public void beanToMapConvertTest() { + // 测试BeanToMap + final Person person = new Person(); + person.setName("Hippo4j"); + person.setAge(1); + person.setAddress("hippo4j.cn"); + person.setSize(999); + final Map convert = BeanUtil.convert(person, Map.class); + Assert.isTrue(Objects.equals("Hippo4j", convert.get("name"))); + Assert.isTrue(Objects.equals(1, convert.get("age"))); + Assert.isTrue(Objects.equals("hippo4j.cn", convert.get("address"))); + Assert.isTrue(Objects.equals(999, convert.get("size"))); + } + + @Test + public void mapToBeanConvertTest() { + // 测试MapToBean + final HashMap map = Maps.newHashMap(); + map.put("name", "Hippo4j"); + map.put("age", 1); + map.put("address", "hippo4j.cn"); + map.put("size", 999); + final Person person = BeanUtil.convert(map, Person.class); + Assert.isTrue(Objects.equals("Hippo4j", person.getName())); + Assert.isTrue(Objects.equals(1, person.getAge())); + Assert.isTrue(Objects.equals("hippo4j.cn", person.getAddress())); + Assert.isTrue(Objects.equals(999, person.getSize())); + } + + @Test + public void ListToListConvertTest() { + final List list = Lists.newArrayList(); + list.add(Person.builder().name("one").age(1).build()); + list.add(Person.builder().name("two").age(2).build()); + list.add(Person.builder().name("three").age(3).build()); + + final List persons = BeanUtil.convert(list, PersonVo.class); + Assert.isTrue(Objects.equals(list.size(), persons.size())); + } + + @Test + public void copyPropertiesBeanToMapTest() { + // 测试BeanToMap + final Person person = new Person(); + person.setName("Hippo4j"); + + final Map convert = BeanUtil.convert(person, Map.class); + Assert.isTrue(Objects.equals("Hippo4j", convert.get("name"))); + + // static属性应被忽略 + Assert.isTrue(!convert.containsKey("STATIC_NAME")); + } + + /** + * 测试在不忽略错误情况下,转换失败需要报错。 + */ + @Test(expected = ConversionException.class) + public void mapToBeanWinErrorTest() { + final Map map = new HashMap<>(); + map.put("age", "Hippo4j"); + BeanUtil.convert(map, Person.class); + } + + // ----------------------------------------------------------------------------------------------------------------- + + @Getter + @Setter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class Person { + + public static final String STATIC_NAME = "STATIC_NAME"; + + private String name; + private int age; + private String address; + private Integer size; + } + + @Setter + public static class PersonVo { + + private String name; + private int age; + } }