mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add BeanPath
This commit is contained in:
@@ -27,7 +27,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@link BeanPath} 单元测试
|
||||
* {@link BeanPathOld} 单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
@@ -73,7 +73,7 @@ public class BeanPathTest {
|
||||
|
||||
@Test
|
||||
public void beanPathTest1() {
|
||||
final BeanPath pattern = new BeanPath("userInfo.examInfoDict[0].id");
|
||||
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));
|
||||
@@ -83,7 +83,7 @@ public class BeanPathTest {
|
||||
|
||||
@Test
|
||||
public void beanPathTest2() {
|
||||
final BeanPath pattern = new BeanPath("[userInfo][examInfoDict][0][id]");
|
||||
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));
|
||||
@@ -92,7 +92,7 @@ public class BeanPathTest {
|
||||
|
||||
@Test
|
||||
public void beanPathTest3() {
|
||||
final BeanPath pattern = new BeanPath("['userInfo']['examInfoDict'][0]['id']");
|
||||
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));
|
||||
@@ -101,14 +101,14 @@ public class BeanPathTest {
|
||||
|
||||
@Test
|
||||
public void getTest() {
|
||||
final BeanPath pattern = BeanPath.of("userInfo.examInfoDict[0].id");
|
||||
final BeanPathOld pattern = BeanPathOld.of("userInfo.examInfoDict[0].id");
|
||||
final Object result = pattern.get(tempMap);
|
||||
Assertions.assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setTest() {
|
||||
final BeanPath pattern = BeanPath.of("userInfo.examInfoDict[0].id");
|
||||
final BeanPathOld pattern = BeanPathOld.of("userInfo.examInfoDict[0].id");
|
||||
pattern.set(tempMap, 2);
|
||||
final Object result = pattern.get(tempMap);
|
||||
Assertions.assertEquals(2, result);
|
||||
@@ -116,7 +116,7 @@ public class BeanPathTest {
|
||||
|
||||
@Test
|
||||
public void getMapTest () {
|
||||
final BeanPath pattern = BeanPath.of("userInfo[id, photoPath]");
|
||||
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"));
|
||||
@@ -129,13 +129,13 @@ public class BeanPathTest {
|
||||
dataMap.put("aa", "value0");
|
||||
dataMap.put("aa.bb.cc", "value111111");// key 是类名 格式 带 ' . '
|
||||
|
||||
final BeanPath pattern = BeanPath.of("'aa.bb.cc'");
|
||||
final BeanPathOld pattern = BeanPathOld.of("'aa.bb.cc'");
|
||||
Assertions.assertEquals("value111111", pattern.get(dataMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compileTest(){
|
||||
final BeanPath of = BeanPath.of("'abc.dd'.ee.ff'.'");
|
||||
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));
|
||||
@@ -145,17 +145,17 @@ public class BeanPathTest {
|
||||
public void issue2362Test() {
|
||||
final Map<String, Object> map = new HashMap<>();
|
||||
|
||||
BeanPath beanPath = BeanPath.of("list[0].name");
|
||||
BeanPathOld beanPath = BeanPathOld.of("list[0].name");
|
||||
beanPath.set(map, "张三");
|
||||
Assertions.assertEquals("{list=[{name=张三}]}", map.toString());
|
||||
|
||||
map.clear();
|
||||
beanPath = BeanPath.of("list[1].name");
|
||||
beanPath = BeanPathOld.of("list[1].name");
|
||||
beanPath.set(map, "张三");
|
||||
Assertions.assertEquals("{list=[null, {name=张三}]}", map.toString());
|
||||
|
||||
map.clear();
|
||||
beanPath = BeanPath.of("list[0].1.name");
|
||||
beanPath = BeanPathOld.of("list[0].1.name");
|
||||
beanPath.set(map, "张三");
|
||||
Assertions.assertEquals("{list=[[null, {name=张三}]]}", map.toString());
|
||||
}
|
||||
@@ -164,7 +164,7 @@ public class BeanPathTest {
|
||||
public void putTest() {
|
||||
final Map<String, Object> map = new HashMap<>();
|
||||
|
||||
BeanPath beanPath = BeanPath.of("list[1].name");
|
||||
final BeanPathOld beanPath = BeanPathOld.of("list[1].name");
|
||||
beanPath.set(map, "张三");
|
||||
Assertions.assertEquals("{list=[null, {name=张三}]}", map.toString());
|
||||
}
|
||||
@@ -172,7 +172,7 @@ public class BeanPathTest {
|
||||
@Test
|
||||
public void putByPathTest() {
|
||||
final Dict dict = new Dict();
|
||||
BeanPath.of("aa.bb").set(dict, "BB");
|
||||
BeanPathOld.of("aa.bb").set(dict, "BB");
|
||||
Assertions.assertEquals("{aa={bb=BB}}", dict.toString());
|
||||
}
|
||||
|
||||
@@ -180,9 +180,9 @@ public class BeanPathTest {
|
||||
public void appendArrayTest(){
|
||||
// issue#3008@Github
|
||||
final MyUser myUser = new MyUser();
|
||||
BeanPath.of("hobby[0]").set(myUser, "LOL");
|
||||
BeanPath.of("hobby[1]").set(myUser, "KFC");
|
||||
BeanPath.of("hobby[2]").set(myUser, "COFFE");
|
||||
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()));
|
||||
}
|
||||
@@ -191,9 +191,9 @@ public class BeanPathTest {
|
||||
public void appendArrayTest2(){
|
||||
// issue#3008@Github
|
||||
final MyUser2 myUser = new MyUser2();
|
||||
BeanPath.of("myUser.hobby[0]").set(myUser, "LOL");
|
||||
BeanPath.of("myUser.hobby[1]").set(myUser, "KFC");
|
||||
BeanPath.of("myUser.hobby[2]").set(myUser, "COFFE");
|
||||
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()));
|
||||
}
|
||||
|
@@ -767,7 +767,7 @@ public class BeanUtilTest {
|
||||
|
||||
testPojo.setTestPojo2List(new TestPojo2[]{testPojo2, testPojo3});
|
||||
|
||||
final BeanPath beanPath = BeanPath.of("testPojo2List.age");
|
||||
final BeanPathOld beanPath = BeanPathOld.of("testPojo2List.age");
|
||||
final Object o = beanPath.get(testPojo);
|
||||
|
||||
Assertions.assertEquals(Integer.valueOf(2), ArrayUtil.get(o, 0));
|
||||
|
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* 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.path;
|
||||
|
||||
import org.dromara.hutool.core.bean.BeanPathOld;
|
||||
import org.dromara.hutool.core.lang.test.bean.ExamInfoDict;
|
||||
import org.dromara.hutool.core.lang.test.bean.UserInfoDict;
|
||||
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;
|
||||
|
||||
public class BeanPathGetOrSetValueTest {
|
||||
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 getValueTest() {
|
||||
final BeanPath pattern = new BeanPath("$.userInfo.examInfoDict[0].id");
|
||||
final Object result = pattern.getValue(tempMap);
|
||||
Assertions.assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setValueTest() {
|
||||
final BeanPath pattern = new BeanPath("userInfo.examInfoDict[0].id");
|
||||
pattern.setValue(tempMap, 2);
|
||||
final Object result = pattern.getValue(tempMap);
|
||||
Assertions.assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMapTest () {
|
||||
final BeanPath pattern = new BeanPath("userInfo[id, photoPath]");
|
||||
@SuppressWarnings("unchecked")
|
||||
final Map<String, Object> result = (Map<String, Object>)pattern.getValue(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 BeanPath pattern = new BeanPath("'aa.bb.cc'");
|
||||
Assertions.assertEquals("value111111", pattern.getValue(dataMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void issue2362Test() {
|
||||
final Map<String, Object> map = new HashMap<>();
|
||||
|
||||
BeanPath beanPath = BeanPath.of("list[0].name");
|
||||
beanPath.setValue(map, "张三");
|
||||
Assertions.assertEquals("{list=[{name=张三}]}", map.toString());
|
||||
|
||||
map.clear();
|
||||
beanPath = BeanPath.of("list[1].name");
|
||||
beanPath.setValue(map, "张三");
|
||||
Assertions.assertEquals("{list=[null, {name=张三}]}", map.toString());
|
||||
|
||||
map.clear();
|
||||
beanPath = BeanPath.of("list[0].1.name");
|
||||
beanPath.setValue(map, "张三");
|
||||
Assertions.assertEquals("{list=[[null, {name=张三}]]}", map.toString());
|
||||
}
|
||||
}
|
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* 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.path;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BeanPathTest {
|
||||
|
||||
@Test
|
||||
void parseDotTest() {
|
||||
BeanPath beanPath = new BeanPath("userInfo.examInfoDict[0].id");
|
||||
Assertions.assertEquals("userInfo", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("examInfoDict[0].id", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("examInfoDict", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[0].id", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("0", beanPath.getNode().toString());
|
||||
Assertions.assertEquals(".id", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("id", beanPath.getNode().toString());
|
||||
Assertions.assertNull(beanPath.getChild());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseDotWithQuoteTest() {
|
||||
BeanPath beanPath = new BeanPath("'userInfo'.examInfoDict[0].'id'");
|
||||
Assertions.assertEquals("userInfo", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("examInfoDict[0].'id'", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("examInfoDict", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[0].'id'", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("0", beanPath.getNode().toString());
|
||||
Assertions.assertEquals(".'id'", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("id", beanPath.getNode().toString());
|
||||
Assertions.assertNull(beanPath.getChild());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseDotWithQuoteTest2() {
|
||||
BeanPath beanPath = new BeanPath("userInfo.'examInfoDict'[0].id");
|
||||
Assertions.assertEquals("userInfo", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("'examInfoDict'[0].id", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("examInfoDict", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[0].id", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("0", beanPath.getNode().toString());
|
||||
Assertions.assertEquals(".id", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("id", beanPath.getNode().toString());
|
||||
Assertions.assertNull(beanPath.getChild());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseBucketTest() {
|
||||
BeanPath beanPath = new BeanPath("[userInfo][examInfoDict][0][id]");
|
||||
Assertions.assertEquals("userInfo", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[examInfoDict][0][id]", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("examInfoDict", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[0][id]", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("0", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[id]", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("id", beanPath.getNode().toString());
|
||||
Assertions.assertNull(beanPath.getChild());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseBucketWithQuoteTest() {
|
||||
BeanPath beanPath = new BeanPath("['userInfo']['examInfoDict'][0][id]");
|
||||
Assertions.assertEquals("userInfo", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("['examInfoDict'][0][id]", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("examInfoDict", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[0][id]", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("0", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[id]", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("id", beanPath.getNode().toString());
|
||||
Assertions.assertNull(beanPath.getChild());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseBucketWithQuoteTest2() {
|
||||
BeanPath beanPath = new BeanPath("[userInfo][examInfoDict][0]['id']");
|
||||
Assertions.assertEquals("userInfo", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[examInfoDict][0]['id']", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("examInfoDict", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[0]['id']", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("0", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("['id']", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("id", beanPath.getNode().toString());
|
||||
Assertions.assertNull(beanPath.getChild());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rangePathTest() {
|
||||
BeanPath beanPath = new BeanPath("[userInfo][2:3]");
|
||||
Assertions.assertEquals("userInfo", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[2:3]", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("[2:3:1]", beanPath.getNode().toString());
|
||||
Assertions.assertNull(beanPath.getChild());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPathTest() {
|
||||
BeanPath beanPath = new BeanPath("[userInfo][1,2,3]");
|
||||
Assertions.assertEquals("userInfo", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("[1,2,3]", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("[1, 2, 3]", beanPath.getNode().toString());
|
||||
Assertions.assertNull(beanPath.getChild());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listKeysPathTest() {
|
||||
BeanPath beanPath = new BeanPath("[userInfo]['a', 'b', 'c']");
|
||||
Assertions.assertEquals("userInfo", beanPath.getNode().toString());
|
||||
Assertions.assertEquals("['a', 'b', 'c']", beanPath.getChild());
|
||||
|
||||
beanPath = beanPath.next();
|
||||
Assertions.assertEquals("[a, b, c]", beanPath.getNode().toString());
|
||||
Assertions.assertNull(beanPath.getChild());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user