mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add BeanPath
This commit is contained in:
@@ -1,210 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* https://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.core.bean;
|
||||
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.lang.test.bean.ExamInfoDict;
|
||||
import org.dromara.hutool.core.lang.test.bean.UserInfoDict;
|
||||
import org.dromara.hutool.core.map.Dict;
|
||||
import lombok.Data;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@link BeanPathOld} 单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class BeanPathTest {
|
||||
|
||||
Map<String, Object> tempMap;
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
// ------------------------------------------------- 考试信息列表
|
||||
final ExamInfoDict examInfoDict = new ExamInfoDict();
|
||||
examInfoDict.setId(1);
|
||||
examInfoDict.setExamType(0);
|
||||
examInfoDict.setAnswerIs(1);
|
||||
|
||||
final ExamInfoDict examInfoDict1 = new ExamInfoDict();
|
||||
examInfoDict1.setId(2);
|
||||
examInfoDict1.setExamType(0);
|
||||
examInfoDict1.setAnswerIs(0);
|
||||
|
||||
final ExamInfoDict examInfoDict2 = new ExamInfoDict();
|
||||
examInfoDict2.setId(3);
|
||||
examInfoDict2.setExamType(1);
|
||||
examInfoDict2.setAnswerIs(0);
|
||||
|
||||
final List<ExamInfoDict> examInfoDicts = new ArrayList<>();
|
||||
examInfoDicts.add(examInfoDict);
|
||||
examInfoDicts.add(examInfoDict1);
|
||||
examInfoDicts.add(examInfoDict2);
|
||||
|
||||
// ------------------------------------------------- 用户信息
|
||||
final UserInfoDict userInfoDict = new UserInfoDict();
|
||||
userInfoDict.setId(1);
|
||||
userInfoDict.setPhotoPath("yx.mm.com");
|
||||
userInfoDict.setRealName("张三");
|
||||
userInfoDict.setExamInfoDict(examInfoDicts);
|
||||
|
||||
tempMap = new HashMap<>();
|
||||
tempMap.put("userInfo", userInfoDict);
|
||||
tempMap.put("flag", 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanPathTest1() {
|
||||
final BeanPathOld pattern = new BeanPathOld("userInfo.examInfoDict[0].id");
|
||||
Assertions.assertEquals("userInfo", pattern.patternParts.get(0));
|
||||
Assertions.assertEquals("examInfoDict", pattern.patternParts.get(1));
|
||||
Assertions.assertEquals("0", pattern.patternParts.get(2));
|
||||
Assertions.assertEquals("id", pattern.patternParts.get(3));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanPathTest2() {
|
||||
final BeanPathOld pattern = new BeanPathOld("[userInfo][examInfoDict][0][id]");
|
||||
Assertions.assertEquals("userInfo", pattern.patternParts.get(0));
|
||||
Assertions.assertEquals("examInfoDict", pattern.patternParts.get(1));
|
||||
Assertions.assertEquals("0", pattern.patternParts.get(2));
|
||||
Assertions.assertEquals("id", pattern.patternParts.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanPathTest3() {
|
||||
final BeanPathOld pattern = new BeanPathOld("['userInfo']['examInfoDict'][0]['id']");
|
||||
Assertions.assertEquals("userInfo", pattern.patternParts.get(0));
|
||||
Assertions.assertEquals("examInfoDict", pattern.patternParts.get(1));
|
||||
Assertions.assertEquals("0", pattern.patternParts.get(2));
|
||||
Assertions.assertEquals("id", pattern.patternParts.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTest() {
|
||||
final BeanPathOld pattern = BeanPathOld.of("userInfo.examInfoDict[0].id");
|
||||
final Object result = pattern.get(tempMap);
|
||||
Assertions.assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setTest() {
|
||||
final BeanPathOld pattern = BeanPathOld.of("userInfo.examInfoDict[0].id");
|
||||
pattern.set(tempMap, 2);
|
||||
final Object result = pattern.get(tempMap);
|
||||
Assertions.assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMapTest () {
|
||||
final BeanPathOld pattern = BeanPathOld.of("userInfo[id, photoPath]");
|
||||
@SuppressWarnings("unchecked")
|
||||
final Map<String, Object> result = (Map<String, Object>)pattern.get(tempMap);
|
||||
Assertions.assertEquals(1, result.get("id"));
|
||||
Assertions.assertEquals("yx.mm.com", result.get("photoPath"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getKeyWithDotTest () {
|
||||
final Map<String, Object> dataMap = new HashMap<>(16);
|
||||
dataMap.put("aa", "value0");
|
||||
dataMap.put("aa.bb.cc", "value111111");// key 是类名 格式 带 ' . '
|
||||
|
||||
final BeanPathOld pattern = BeanPathOld.of("'aa.bb.cc'");
|
||||
Assertions.assertEquals("value111111", pattern.get(dataMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compileTest(){
|
||||
final BeanPathOld of = BeanPathOld.of("'abc.dd'.ee.ff'.'");
|
||||
Assertions.assertEquals("abc.dd", of.getPatternParts().get(0));
|
||||
Assertions.assertEquals("ee", of.getPatternParts().get(1));
|
||||
Assertions.assertEquals("ff.", of.getPatternParts().get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void issue2362Test() {
|
||||
final Map<String, Object> map = new HashMap<>();
|
||||
|
||||
BeanPathOld beanPath = BeanPathOld.of("list[0].name");
|
||||
beanPath.set(map, "张三");
|
||||
Assertions.assertEquals("{list=[{name=张三}]}", map.toString());
|
||||
|
||||
map.clear();
|
||||
beanPath = BeanPathOld.of("list[1].name");
|
||||
beanPath.set(map, "张三");
|
||||
Assertions.assertEquals("{list=[null, {name=张三}]}", map.toString());
|
||||
|
||||
map.clear();
|
||||
beanPath = BeanPathOld.of("list[0].1.name");
|
||||
beanPath.set(map, "张三");
|
||||
Assertions.assertEquals("{list=[[null, {name=张三}]]}", map.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putTest() {
|
||||
final Map<String, Object> map = new HashMap<>();
|
||||
|
||||
final BeanPathOld beanPath = BeanPathOld.of("list[1].name");
|
||||
beanPath.set(map, "张三");
|
||||
Assertions.assertEquals("{list=[null, {name=张三}]}", map.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putByPathTest() {
|
||||
final Dict dict = new Dict();
|
||||
BeanPathOld.of("aa.bb").set(dict, "BB");
|
||||
Assertions.assertEquals("{aa={bb=BB}}", dict.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void appendArrayTest(){
|
||||
// issue#3008@Github
|
||||
final MyUser myUser = new MyUser();
|
||||
BeanPathOld.of("hobby[0]").set(myUser, "LOL");
|
||||
BeanPathOld.of("hobby[1]").set(myUser, "KFC");
|
||||
BeanPathOld.of("hobby[2]").set(myUser, "COFFE");
|
||||
|
||||
Assertions.assertEquals("[LOL, KFC, COFFE]", ArrayUtil.toString(myUser.getHobby()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void appendArrayTest2(){
|
||||
// issue#3008@Github
|
||||
final MyUser2 myUser = new MyUser2();
|
||||
BeanPathOld.of("myUser.hobby[0]").set(myUser, "LOL");
|
||||
BeanPathOld.of("myUser.hobby[1]").set(myUser, "KFC");
|
||||
BeanPathOld.of("myUser.hobby[2]").set(myUser, "COFFE");
|
||||
|
||||
Assertions.assertEquals("[LOL, KFC, COFFE]", ArrayUtil.toString(myUser.getMyUser().getHobby()));
|
||||
}
|
||||
|
||||
@Data
|
||||
static class MyUser {
|
||||
private String[] hobby;
|
||||
}
|
||||
|
||||
@Data
|
||||
static class MyUser2 {
|
||||
private MyUser myUser;
|
||||
}
|
||||
}
|
@@ -12,10 +12,16 @@
|
||||
|
||||
package org.dromara.hutool.core.bean;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.dromara.hutool.core.annotation.Alias;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.bean.copier.CopyOptions;
|
||||
import org.dromara.hutool.core.bean.copier.ValueProvider;
|
||||
import org.dromara.hutool.core.bean.path.BeanPath;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.collection.set.SetUtil;
|
||||
import org.dromara.hutool.core.map.MapBuilder;
|
||||
@@ -23,11 +29,6 @@ import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.thread.ThreadUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -767,8 +768,8 @@ public class BeanUtilTest {
|
||||
|
||||
testPojo.setTestPojo2List(new TestPojo2[]{testPojo2, testPojo3});
|
||||
|
||||
final BeanPathOld beanPath = BeanPathOld.of("testPojo2List.age");
|
||||
final Object o = beanPath.get(testPojo);
|
||||
final BeanPath beanPath = BeanPath.of("testPojo2List.age");
|
||||
final Object o = beanPath.getValue(testPojo);
|
||||
|
||||
Assertions.assertEquals(Integer.valueOf(2), ArrayUtil.get(o, 0));
|
||||
Assertions.assertEquals(Integer.valueOf(3), ArrayUtil.get(o, 1));
|
||||
|
@@ -12,8 +12,11 @@
|
||||
|
||||
package org.dromara.hutool.core.bean.path;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.lang.test.bean.ExamInfoDict;
|
||||
import org.dromara.hutool.core.lang.test.bean.UserInfoDict;
|
||||
import org.dromara.hutool.core.map.Dict;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -113,4 +116,52 @@ public class BeanPathGetOrSetValueTest {
|
||||
beanPath.setValue(map, "张三");
|
||||
Assertions.assertEquals("{list=[[null, {name=张三}]]}", map.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putTest() {
|
||||
final Map<String, Object> map = new HashMap<>();
|
||||
|
||||
final BeanPath beanPath = BeanPath.of("list[1].name");
|
||||
beanPath.setValue(map, "张三");
|
||||
Assertions.assertEquals("{list=[null, {name=张三}]}", map.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putByPathTest() {
|
||||
final Dict dict = new Dict();
|
||||
BeanPath.of("aa.bb").setValue(dict, "BB");
|
||||
Assertions.assertEquals("{aa={bb=BB}}", dict.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void appendArrayTest(){
|
||||
// issue#3008@Github
|
||||
final MyUser myUser = new MyUser();
|
||||
BeanPath.of("hobby[0]").setValue(myUser, "LOL");
|
||||
BeanPath.of("hobby[1]").setValue(myUser, "KFC");
|
||||
BeanPath.of("hobby[2]").setValue(myUser, "COFFE");
|
||||
|
||||
Assertions.assertEquals("[LOL, KFC, COFFE]", ArrayUtil.toString(myUser.getHobby()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void appendArrayTest2(){
|
||||
// issue#3008@Github
|
||||
final MyUser2 myUser = new MyUser2();
|
||||
BeanPath.of("myUser.hobby[0]").setValue(myUser, "LOL");
|
||||
BeanPath.of("myUser.hobby[1]").setValue(myUser, "KFC");
|
||||
BeanPath.of("myUser.hobby[2]").setValue(myUser, "COFFE");
|
||||
|
||||
Assertions.assertEquals("[LOL, KFC, COFFE]", ArrayUtil.toString(myUser.getMyUser().getHobby()));
|
||||
}
|
||||
|
||||
@Data
|
||||
static class MyUser {
|
||||
private String[] hobby;
|
||||
}
|
||||
|
||||
@Data
|
||||
static class MyUser2 {
|
||||
private MyUser myUser;
|
||||
}
|
||||
}
|
||||
|
@@ -13,11 +13,6 @@
|
||||
package org.dromara.hutool.core.reflect;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.SneakyThrows;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 反射工具类单元测试
|
||||
|
Reference in New Issue
Block a user