From fc091b01a23271e02f3174c45942105048155c90 Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 27 Jun 2022 21:47:38 +0800 Subject: [PATCH] fix test --- .../cn/hutool/core/bean/BeanUtilTest.java | 191 +++++++++--------- 1 file changed, 95 insertions(+), 96 deletions(-) diff --git a/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java index 219780b430..66f6941869 100755 --- a/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java @@ -43,16 +43,16 @@ public class BeanUtilTest { public void isBeanTest() { // HashMap不包含setXXX方法,不是bean - boolean isBean = BeanUtil.isBean(HashMap.class); + final boolean isBean = BeanUtil.isBean(HashMap.class); Assert.assertFalse(isBean); } @Test public void fillBeanTest() { - Person person = BeanUtil.fillBean(new Person(), new ValueProvider() { + final Person person = BeanUtil.fillBean(new Person(), new ValueProvider() { @Override - public Object value(String key, Type valueType) { + public Object value(final String key, final Type valueType) { switch (key) { case "name": return "张三"; @@ -63,7 +63,7 @@ public Object value(String key, Type valueType) { } @Override - public boolean containsKey(String key) { + public boolean containsKey(final String key) { // 总是存在key return true; } @@ -76,12 +76,12 @@ public boolean containsKey(String key) { @Test public void fillBeanWithMapIgnoreCaseTest() { - Map map = MapBuilder.create() + final Map map = MapBuilder.create() .put("Name", "Joe") .put("aGe", 12) .put("openId", "DFDFSDFWERWER") .build(); - SubPerson person = BeanUtil.fillBeanWithMapIgnoreCase(map, new SubPerson(), false); + final SubPerson person = BeanUtil.fillBeanWithMapIgnoreCase(map, new SubPerson(), false); Assert.assertEquals("Joe", person.getName()); Assert.assertEquals(12, person.getAge()); Assert.assertEquals("DFDFSDFWERWER", person.getOpenid()); @@ -89,7 +89,7 @@ public void fillBeanWithMapIgnoreCaseTest() { @Test public void toBeanTest() { - SubPerson person = new SubPerson(); + final SubPerson person = new SubPerson(); person.setAge(14); person.setOpenid("11213232"); person.setName("测试A11"); @@ -108,12 +108,12 @@ public void toBeanTest() { */ @Test public void toBeanIgnoreErrorTest() { - HashMap map = MapUtil.newHashMap(); + final HashMap map = MapUtil.newHashMap(); map.put("name", "Joe"); // 错误的类型,此处忽略 map.put("age", "aaaaaa"); - Person person = BeanUtil.toBeanIgnoreError(map, Person.class); + final Person person = BeanUtil.toBeanIgnoreError(map, Person.class); Assert.assertEquals("Joe", person.getName()); // 错误的类型,不copy这个字段,使用对象创建的默认值 Assert.assertEquals(0, person.getAge()); @@ -121,27 +121,27 @@ public void toBeanIgnoreErrorTest() { @Test public void mapToBeanIgnoreCaseTest() { - HashMap map = MapUtil.newHashMap(); + final HashMap map = MapUtil.newHashMap(); map.put("Name", "Joe"); map.put("aGe", 12); - Person person = BeanUtil.toBeanIgnoreCase(map, Person.class, false); + final Person person = BeanUtil.toBeanIgnoreCase(map, Person.class, false); Assert.assertEquals("Joe", person.getName()); Assert.assertEquals(12, person.getAge()); } @Test public void mapToBeanTest() { - HashMap map = MapUtil.newHashMap(); + final HashMap map = MapUtil.newHashMap(); map.put("a_name", "Joe"); map.put("b_age", 12); // 别名,用于对应bean的字段名 - HashMap mapping = MapUtil.newHashMap(); + final HashMap mapping = MapUtil.newHashMap(); mapping.put("a_name", "name"); mapping.put("b_age", "age"); - Person person = BeanUtil.toBean(map, Person.class, CopyOptions.create().setFieldMapping(mapping)); + final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.create().setFieldMapping(mapping)); Assert.assertEquals("Joe", person.getName()); Assert.assertEquals(12, person.getAge()); } @@ -151,12 +151,12 @@ public void mapToBeanTest() { */ @Test public void mapToBeanTest2() { - HashMap map = MapUtil.newHashMap(); + final HashMap map = MapUtil.newHashMap(); map.put("name", "Joe"); map.put("age", 12); // 非空构造也可以实例化成功 - Person2 person = BeanUtil.toBean(map, Person2.class, CopyOptions.create()); + final Person2 person = BeanUtil.toBean(map, Person2.class, CopyOptions.create()); Assert.assertEquals("Joe", person.name); Assert.assertEquals(12, person.age); } @@ -166,20 +166,20 @@ public void mapToBeanTest2() { */ @Test(expected = NumberFormatException.class) public void mapToBeanWinErrorTest() { - Map map = new HashMap<>(); + final Map map = new HashMap<>(); map.put("age", "哈哈"); BeanUtil.toBean(map, Person.class); } @Test public void beanToMapTest() { - SubPerson person = new SubPerson(); + final SubPerson person = new SubPerson(); person.setAge(14); person.setOpenid("11213232"); person.setName("测试A11"); person.setSubName("sub名字"); - Map map = BeanUtil.beanToMap(person); + final Map map = BeanUtil.beanToMap(person); Assert.assertEquals("测试A11", map.get("name")); Assert.assertEquals(14, map.get("age")); @@ -190,13 +190,13 @@ public void beanToMapTest() { @Test public void beanToMapNullPropertiesTest() { - SubPerson person = new SubPerson(); + final SubPerson person = new SubPerson(); person.setAge(14); person.setOpenid("11213232"); person.setName("测试A11"); person.setSubName("sub名字"); - Map map = BeanUtil.beanToMap(person, null); + final Map map = BeanUtil.beanToMap(person, (String[]) null); Assert.assertEquals("测试A11", map.get("name")); Assert.assertEquals(14, map.get("age")); @@ -207,32 +207,32 @@ public void beanToMapNullPropertiesTest() { @Test public void beanToMapTest2() { - SubPerson person = new SubPerson(); + final SubPerson person = new SubPerson(); person.setAge(14); person.setOpenid("11213232"); person.setName("测试A11"); person.setSubName("sub名字"); - Map map = BeanUtil.beanToMap(person, true, true); + final Map map = BeanUtil.beanToMap(person, true, true); Assert.assertEquals("sub名字", map.get("sub_name")); } @Test public void beanToMapWithValueEditTest() { - SubPerson person = new SubPerson(); + final SubPerson person = new SubPerson(); person.setAge(14); person.setOpenid("11213232"); person.setName("测试A11"); person.setSubName("sub名字"); - Map map = BeanUtil.beanToMap(person, new LinkedHashMap<>(), + final Map map = BeanUtil.beanToMap(person, new LinkedHashMap<>(), CopyOptions.create().setFieldValueEditor((key, value) -> key + "_" + value)); Assert.assertEquals("subName_sub名字", map.get("subName")); } @Test public void beanToMapWithAliasTest() { - SubPersonWithAlias person = new SubPersonWithAlias(); + final SubPersonWithAlias person = new SubPersonWithAlias(); person.setAge(14); person.setOpenid("11213232"); person.setName("测试A11"); @@ -241,13 +241,13 @@ public void beanToMapWithAliasTest() { person.setBooleana(true); person.setBooleanb(true); - Map map = BeanUtil.beanToMap(person); + final Map map = BeanUtil.beanToMap(person); Assert.assertEquals("sub名字", map.get("aliasSubName")); } @Test public void mapToBeanWithAliasTest() { - Map map = MapUtil.newHashMap(); + final Map map = MapUtil.newHashMap(); map.put("aliasSubName", "sub名字"); map.put("slow", true); map.put("is_booleana", "1"); @@ -263,7 +263,7 @@ public void mapToBeanWithAliasTest() { public void beanToMapWithLocalDateTimeTest() { final LocalDateTime now = LocalDateTime.now(); - SubPerson person = new SubPerson(); + final SubPerson person = new SubPerson(); person.setAge(14); person.setOpenid("11213232"); person.setName("测试A11"); @@ -271,22 +271,22 @@ public void beanToMapWithLocalDateTimeTest() { person.setDate(now); person.setDate2(now.toLocalDate()); - Map map = BeanUtil.beanToMap(person, false, true); + final Map map = BeanUtil.beanToMap(person, false, true); Assert.assertEquals(now, map.get("date")); Assert.assertEquals(now.toLocalDate(), map.get("date2")); } @Test public void getPropertyTest() { - SubPerson person = new SubPerson(); + final SubPerson person = new SubPerson(); person.setAge(14); person.setOpenid("11213232"); person.setName("测试A11"); person.setSubName("sub名字"); - Object name = BeanUtil.getProperty(person, "name"); + final Object name = BeanUtil.getProperty(person, "name"); Assert.assertEquals("测试A11", name); - Object subName = BeanUtil.getProperty(person, "subName"); + final Object subName = BeanUtil.getProperty(person, "subName"); Assert.assertEquals("sub名字", subName); } @@ -299,9 +299,9 @@ public void getNullPropertyTest() { @Test public void getPropertyDescriptorsTest() { - HashSet set = CollUtil.newHashSet(); - PropertyDescriptor[] propertyDescriptors = BeanUtil.getPropertyDescriptors(SubPerson.class); - for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + final HashSet set = CollUtil.newHashSet(); + final PropertyDescriptor[] propertyDescriptors = BeanUtil.getPropertyDescriptors(SubPerson.class); + for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { set.add(propertyDescriptor.getName()); } Assert.assertTrue(set.contains("age")); @@ -314,13 +314,13 @@ public void getPropertyDescriptorsTest() { @Test public void copyPropertiesTest() { - SubPerson person = new SubPerson(); + final SubPerson person = new SubPerson(); person.setAge(14); person.setOpenid("11213232"); person.setName("测试A11"); person.setSubName("sub名字"); - SubPerson person1 = BeanUtil.copyProperties(person, SubPerson.class); + final SubPerson person1 = BeanUtil.copyProperties(person, SubPerson.class); Assert.assertEquals(14, person1.getAge()); Assert.assertEquals("11213232", person1.getOpenid()); Assert.assertEquals("测试A11", person1.getName()); @@ -330,23 +330,23 @@ public void copyPropertiesTest() { @Test @Ignore public void multiThreadTest(){ - Student student = new Student(); + final Student student = new Student(); student.setName("张三"); student.setAge(123); student.setNo(3158L); - Student student2 = new Student(); + final Student student2 = new Student(); student.setName("李四"); student.setAge(125); student.setNo(8848L); - List studentList = ListUtil.of(student, student2); + final List studentList = ListUtil.of(student, student2); for (int i=0;i<5000;i++){ new Thread(()->{ - List list = ObjectUtil.clone(studentList); - List listReps = list.stream().map(s1 -> { - Student s2 = new Student(); + final List list = ObjectUtil.clone(studentList); + final List listReps = list.stream().map(s1 -> { + final Student s2 = new Student(); BeanUtil.copyProperties(s1, s2); return s2; }).collect(Collectors.toList()); @@ -360,27 +360,27 @@ public void multiThreadTest(){ @Test public void copyPropertiesHasBooleanTest() { - SubPerson p1 = new SubPerson(); + final SubPerson p1 = new SubPerson(); p1.setSlow(true); // 测试boolean参数值isXXX形式 - SubPerson p2 = new SubPerson(); + final SubPerson p2 = new SubPerson(); BeanUtil.copyProperties(p1, p2); Assert.assertTrue(p2.getSlow()); // 测试boolean参数值非isXXX形式 - SubPerson2 p3 = new SubPerson2(); + final SubPerson2 p3 = new SubPerson2(); BeanUtil.copyProperties(p1, p3); Assert.assertTrue(p3.getSlow()); } @Test public void copyPropertiesIgnoreNullTest() { - SubPerson p1 = new SubPerson(); + final SubPerson p1 = new SubPerson(); p1.setSlow(true); p1.setName(null); - SubPerson2 p2 = new SubPerson2(); + final SubPerson2 p2 = new SubPerson2(); p2.setName("oldName"); // null值不覆盖目标属性 @@ -395,12 +395,12 @@ public void copyPropertiesIgnoreNullTest() { @Test public void copyPropertiesBeanToMapTest() { // 测试BeanToMap - SubPerson p1 = new SubPerson(); + final SubPerson p1 = new SubPerson(); p1.setSlow(true); p1.setName("测试"); p1.setSubName("sub测试"); - Map map = MapUtil.newHashMap(); + final Map map = MapUtil.newHashMap(); BeanUtil.copyProperties(p1, map); Assert.assertTrue((Boolean) map.get("slow")); Assert.assertEquals("测试", map.get("name")); @@ -410,12 +410,12 @@ public void copyPropertiesBeanToMapTest() { @Test public void copyPropertiesMapToMapTest() { // 测试MapToMap - Map p1 = new HashMap<>(); + final Map p1 = new HashMap<>(); p1.put("isSlow", true); p1.put("name", "测试"); p1.put("subName", "sub测试"); - Map map = MapUtil.newHashMap(); + final Map map = MapUtil.newHashMap(); BeanUtil.copyProperties(p1, map); Assert.assertTrue((Boolean) map.get("isSlow")); Assert.assertEquals("测试", map.get("name")); @@ -424,11 +424,11 @@ public void copyPropertiesMapToMapTest() { @Test public void trimBeanStrFieldsTest() { - Person person = new Person(); + final Person person = new Person(); person.setAge(1); person.setName(" 张三 "); person.setOpenid(null); - Person person2 = BeanUtil.trimStrFields(person); + final Person person2 = BeanUtil.trimStrFields(person); // 是否改变原对象 Assert.assertEquals("张三", person.getName()); @@ -494,7 +494,7 @@ public static class PersonWithTransientField { public static class Person2 { - public Person2(String name, int age, String openid) { + public Person2(final String name, final int age, final String openid) { this.name = name; this.age = age; this.openid = openid; @@ -510,11 +510,11 @@ public Person2(String name, int age, String openid) { */ @Test public void beanToBeanOverlayFieldTest() { - SubPersonWithOverlayTransientField source = new SubPersonWithOverlayTransientField(); + final SubPersonWithOverlayTransientField source = new SubPersonWithOverlayTransientField(); source.setName("zhangsan"); source.setAge(20); source.setOpenid("1"); - SubPersonWithOverlayTransientField dest = new SubPersonWithOverlayTransientField(); + final SubPersonWithOverlayTransientField dest = new SubPersonWithOverlayTransientField(); BeanUtil.copyProperties(source, dest); Assert.assertEquals(source.getName(), dest.getName()); @@ -525,7 +525,7 @@ public void beanToBeanOverlayFieldTest() { @Test public void beanToBeanTest() { // 修复对象无getter方法导致报错的问题 - Page page1 = new Page(); + final Page page1 = new Page(); BeanUtil.toBean(page1, Page.class); } @@ -536,7 +536,7 @@ public boolean optimizeCountSql() { return optimizeCountSql; } - public Page setOptimizeCountSql(boolean optimizeCountSql) { + public Page setOptimizeCountSql(final boolean optimizeCountSql) { this.optimizeCountSql = optimizeCountSql; return this; } @@ -545,10 +545,10 @@ public Page setOptimizeCountSql(boolean optimizeCountSql) { @Test public void copyBeanToBeanTest() { // 测试在copyProperties方法中alias是否有效 - Food info = new Food(); + final Food info = new Food(); info.setBookID("0"); info.setCode("123"); - HllFoodEntity entity = new HllFoodEntity(); + final HllFoodEntity entity = new HllFoodEntity(); BeanUtil.copyProperties(info, entity); Assert.assertEquals(info.getBookID(), entity.getBookId()); Assert.assertEquals(info.getCode(), entity.getCode2()); @@ -556,10 +556,10 @@ public void copyBeanToBeanTest() { @Test public void copyBeanTest() { - Food info = new Food(); + final Food info = new Food(); info.setBookID("0"); info.setCode("123"); - Food newFood = BeanUtil.copyProperties(info, Food.class, "code"); + final Food newFood = BeanUtil.copyProperties(info, Food.class, "code"); Assert.assertEquals(info.getBookID(), newFood.getBookID()); Assert.assertNull(newFood.getCode()); } @@ -571,11 +571,11 @@ public void copyNullTest() { @Test public void copyBeanPropertiesFilterTest() { - Food info = new Food(); + final Food info = new Food(); info.setBookID("0"); info.setCode(""); - Food newFood = new Food(); - CopyOptions copyOptions = CopyOptions.create().setPropertiesFilter((f, v) -> !(v instanceof CharSequence) || StrUtil.isNotBlank(v.toString())); + final Food newFood = new Food(); + final CopyOptions copyOptions = CopyOptions.create().setPropertiesFilter((f, v) -> !(v instanceof CharSequence) || StrUtil.isNotBlank(v.toString())); BeanUtil.copyProperties(info, newFood, copyOptions); Assert.assertEquals(info.getBookID(), newFood.getBookID()); Assert.assertNull(newFood.getCode()); @@ -584,14 +584,13 @@ public void copyBeanPropertiesFilterTest() { @Test public void copyBeanPropertiesFunctionFilterTest() { //https://gitee.com/dromara/hutool/pulls/590 - Person o = new Person(); + final Person o = new Person(); o.setName("asd"); o.setAge(123); o.setOpenid("asd"); - @SuppressWarnings("unchecked") - CopyOptions copyOptions = CopyOptions.create().setIgnoreProperties(Person::getAge,Person::getOpenid); - Person n = new Person(); + @SuppressWarnings("unchecked") final CopyOptions copyOptions = CopyOptions.create().setIgnoreProperties(Person::getAge,Person::getOpenid); + final Person n = new Person(); BeanUtil.copyProperties(o, n, copyOptions); // 是否忽略拷贝属性 @@ -617,9 +616,9 @@ public static class HllFoodEntity implements Serializable { @Test public void setPropertiesTest() { - Map resultMap = MapUtil.newHashMap(); + final Map resultMap = MapUtil.newHashMap(); BeanUtil.setProperty(resultMap, "codeList[0].name", "张三"); - Assert.assertEquals("{codeList={0={name=张三}}}", resultMap.toString()); + Assert.assertEquals("{codeList=[{name=张三}]}", resultMap.toString()); } @Test @@ -643,18 +642,18 @@ public static class Entity { @Test public void copyListTest() { - Student student = new Student(); + final Student student = new Student(); student.setName("张三"); student.setAge(123); student.setNo(3158L); - Student student2 = new Student(); + final Student student2 = new Student(); student.setName("李四"); student.setAge(125); student.setNo(8848L); - List studentList = ListUtil.of(student, student2); - List people = BeanUtil.copyToList(studentList, Person.class); + final List studentList = ListUtil.of(student, student2); + final List people = BeanUtil.copyToList(studentList, Person.class); Assert.assertEquals(studentList.size(), people.size()); for (int i = 0; i < studentList.size(); i++) { @@ -667,14 +666,14 @@ public void copyListTest() { @Test public void toMapTest() { // 测试转map的时候返回key - PrivilegeIClassification a = new PrivilegeIClassification(); + final PrivilegeIClassification a = new PrivilegeIClassification(); a.setId("1"); a.setName("2"); a.setCode("3"); a.setCreateTime(new Date()); a.setSortOrder(9L); - Map f = BeanUtil.beanToMap( + final Map f = BeanUtil.beanToMap( a, new LinkedHashMap<>(), false, @@ -696,19 +695,19 @@ public static class PrivilegeIClassification implements Serializable { @Test public void getFieldValue() { - TestPojo testPojo = new TestPojo(); + final TestPojo testPojo = new TestPojo(); testPojo.setName("名字"); - TestPojo2 testPojo2 = new TestPojo2(); + final TestPojo2 testPojo2 = new TestPojo2(); testPojo2.setAge(2); - TestPojo2 testPojo3 = new TestPojo2(); + final TestPojo2 testPojo3 = new TestPojo2(); testPojo3.setAge(3); testPojo.setTestPojo2List(new TestPojo2[]{testPojo2, testPojo3}); - BeanPath beanPath = BeanPath.create("testPojo2List.age"); - Object o = beanPath.get(testPojo); + final BeanPath beanPath = BeanPath.create("testPojo2List.age"); + final Object o = beanPath.get(testPojo); Assert.assertEquals(Integer.valueOf(2), ArrayUtil.get(o, 0)); Assert.assertEquals(Integer.valueOf(3), ArrayUtil.get(o, 1)); @@ -741,18 +740,18 @@ public static class Student implements Serializable{ */ @Test public void beanToBeanCopyOptionsTest() { - ChildVo1 childVo1 = new ChildVo1(); + final ChildVo1 childVo1 = new ChildVo1(); childVo1.setChild_address("中国北京五道口"); childVo1.setChild_name("张三"); childVo1.setChild_father_name("张无忌"); childVo1.setChild_mother_name("赵敏敏"); - CopyOptions copyOptions = CopyOptions.create(). + final CopyOptions copyOptions = CopyOptions.create(). //setIgnoreNullValue(true). //setIgnoreCase(false). setFieldNameEditor(StrUtil::toCamelCase); - ChildVo2 childVo2 = new ChildVo2(); + final ChildVo2 childVo2 = new ChildVo2(); BeanUtil.copyProperties(childVo1, childVo2, copyOptions); Assert.assertEquals(childVo1.getChild_address(), childVo2.getChildAddress()); @@ -779,8 +778,8 @@ public static class ChildVo2 { @Test public void issueI41WKPTest(){ - Test1 t1 = new Test1().setStrList(ListUtil.toList("list")); - Test2 t2_hu = new Test2(); + final Test1 t1 = new Test1().setStrList(ListUtil.toList("list")); + final Test2 t2_hu = new Test2(); BeanUtil.copyProperties(t1, t2_hu, CopyOptions.create().setIgnoreError(true)); Assert.assertNull(t2_hu.getStrList()); } @@ -799,10 +798,10 @@ public static class Test2 { @Test public void issuesI53O9JTest(){ - Map map = new HashMap<>(); + final Map map = new HashMap<>(); map.put("statusIdUpdateTime", ""); - WkCrmCustomer customer = new WkCrmCustomer(); + final WkCrmCustomer customer = new WkCrmCustomer(); BeanUtil.copyProperties(map, customer); Assert.assertNull(customer.getStatusIdUpdateTime()); @@ -816,22 +815,22 @@ public static class WkCrmCustomer{ @Test public void valueProviderToBeanTest(){ // https://gitee.com/dromara/hutool/issues/I5B4R7 - CopyOptions copyOptions = CopyOptions.create(); - Map filedMap= new HashMap<>(); + final CopyOptions copyOptions = CopyOptions.create(); + final Map filedMap= new HashMap<>(); filedMap.put("name", "sourceId"); copyOptions.setFieldMapping(filedMap); - TestPojo pojo = BeanUtil.toBean(TestPojo.class, new ValueProvider() { + final TestPojo pojo = BeanUtil.toBean(TestPojo.class, new ValueProvider() { final HashMap map = new HashMap<>(); { map.put("sourceId", "123"); } @Override - public Object value(String key, Type valueType) { + public Object value(final String key, final Type valueType) { return map.get(key); } @Override - public boolean containsKey(String key) { + public boolean containsKey(final String key) { return map.containsKey(key); } }, copyOptions);