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