BeanPath在空元素时默认加入map,修改根据下标类型赋值List or map

This commit is contained in:
Looly
2022-06-20 19:49:07 +08:00
parent d32e513191
commit 33e95b23c1
6 changed files with 183 additions and 74 deletions

View File

@@ -1,22 +1,20 @@
package cn.hutool.core.bean;
import cn.hutool.core.lang.test.bean.ExamInfoDict;
import cn.hutool.core.lang.test.bean.UserInfoDict;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import cn.hutool.core.lang.test.bean.ExamInfoDict;
import cn.hutool.core.lang.test.bean.UserInfoDict;
/**
* {@link BeanPath} 单元测试
*
* @author looly
*
*/
public class BeanPathTest {
@@ -25,28 +23,28 @@ public class BeanPathTest {
@Before
public void init() {
// ------------------------------------------------- 考试信息列表
ExamInfoDict examInfoDict = new ExamInfoDict();
final ExamInfoDict examInfoDict = new ExamInfoDict();
examInfoDict.setId(1);
examInfoDict.setExamType(0);
examInfoDict.setAnswerIs(1);
ExamInfoDict examInfoDict1 = new ExamInfoDict();
final ExamInfoDict examInfoDict1 = new ExamInfoDict();
examInfoDict1.setId(2);
examInfoDict1.setExamType(0);
examInfoDict1.setAnswerIs(0);
ExamInfoDict examInfoDict2 = new ExamInfoDict();
final ExamInfoDict examInfoDict2 = new ExamInfoDict();
examInfoDict2.setId(3);
examInfoDict2.setExamType(1);
examInfoDict2.setAnswerIs(0);
List<ExamInfoDict> examInfoDicts = new ArrayList<>();
final List<ExamInfoDict> examInfoDicts = new ArrayList<>();
examInfoDicts.add(examInfoDict);
examInfoDicts.add(examInfoDict1);
examInfoDicts.add(examInfoDict2);
// ------------------------------------------------- 用户信息
UserInfoDict userInfoDict = new UserInfoDict();
final UserInfoDict userInfoDict = new UserInfoDict();
userInfoDict.setId(1);
userInfoDict.setPhotoPath("yx.mm.com");
userInfoDict.setRealName("张三");
@@ -59,7 +57,7 @@ public class BeanPathTest {
@Test
public void beanPathTest1() {
BeanPath pattern = new BeanPath("userInfo.examInfoDict[0].id");
final BeanPath pattern = new BeanPath("userInfo.examInfoDict[0].id");
Assert.assertEquals("userInfo", pattern.patternParts.get(0));
Assert.assertEquals("examInfoDict", pattern.patternParts.get(1));
Assert.assertEquals("0", pattern.patternParts.get(2));
@@ -69,7 +67,7 @@ public class BeanPathTest {
@Test
public void beanPathTest2() {
BeanPath pattern = new BeanPath("[userInfo][examInfoDict][0][id]");
final BeanPath pattern = new BeanPath("[userInfo][examInfoDict][0][id]");
Assert.assertEquals("userInfo", pattern.patternParts.get(0));
Assert.assertEquals("examInfoDict", pattern.patternParts.get(1));
Assert.assertEquals("0", pattern.patternParts.get(2));
@@ -78,7 +76,7 @@ public class BeanPathTest {
@Test
public void beanPathTest3() {
BeanPath pattern = new BeanPath("['userInfo']['examInfoDict'][0]['id']");
final BeanPath pattern = new BeanPath("['userInfo']['examInfoDict'][0]['id']");
Assert.assertEquals("userInfo", pattern.patternParts.get(0));
Assert.assertEquals("examInfoDict", pattern.patternParts.get(1));
Assert.assertEquals("0", pattern.patternParts.get(2));
@@ -87,25 +85,43 @@ public class BeanPathTest {
@Test
public void getTest() {
BeanPath pattern = BeanPath.create("userInfo.examInfoDict[0].id");
Object result = pattern.get(tempMap);
final BeanPath pattern = BeanPath.create("userInfo.examInfoDict[0].id");
final Object result = pattern.get(tempMap);
Assert.assertEquals(1, result);
}
@Test
public void setTest() {
BeanPath pattern = BeanPath.create("userInfo.examInfoDict[0].id");
final BeanPath pattern = BeanPath.create("userInfo.examInfoDict[0].id");
pattern.set(tempMap, 2);
Object result = pattern.get(tempMap);
final Object result = pattern.get(tempMap);
Assert.assertEquals(2, result);
}
@Test
public void getMapTest () {
BeanPath pattern = BeanPath.create("userInfo[id, photoPath]");
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>)pattern.get(tempMap);
public void getMapTest() {
final BeanPath pattern = BeanPath.create("userInfo[id, photoPath]");
@SuppressWarnings("unchecked") final Map<String, Object> result = (Map<String, Object>) pattern.get(tempMap);
Assert.assertEquals(1, result.get("id"));
Assert.assertEquals("yx.mm.com", result.get("photoPath"));
}
@Test
public void issue2362Test() {
final Map<String, Object> map = new HashMap<>();
BeanPath beanPath = BeanPath.create("list[0].name");
beanPath.set(map, "张三");
Assert.assertEquals("{list=[{name=张三}]}", map.toString());
map.clear();
beanPath = BeanPath.create("list[1].name");
beanPath.set(map, "张三");
Assert.assertEquals("{list=[null, {name=张三}]}", map.toString());
map.clear();
beanPath = BeanPath.create("list[0].1.name");
beanPath.set(map, "张三");
Assert.assertEquals("{list=[[null, {name=张三}]]}", map.toString());
}
}

View File

@@ -38,20 +38,20 @@ public class ListUtilTest {
@Test
@Ignore
public void splitBenchTest() {
List<String> list = new ArrayList<>();
final List<String> list = new ArrayList<>();
CollUtil.padRight(list, RandomUtil.randomInt(1000_0000, 1_0000_0000), "test");
int size = RandomUtil.randomInt(10, 1000);
final int size = RandomUtil.randomInt(10, 1000);
Console.log("\nlist size: {}", list.size());
Console.log("partition size: {}\n", size);
StopWatch stopWatch = new StopWatch();
final StopWatch stopWatch = new StopWatch();
stopWatch.start("CollUtil#split");
List<List<String>> CollSplitResult = CollUtil.split(list, size);
final List<List<String>> CollSplitResult = CollUtil.split(list, size);
stopWatch.stop();
stopWatch.start("ListUtil#split");
List<List<String>> ListSplitResult = ListUtil.split(list, size);
final List<List<String>> ListSplitResult = ListUtil.split(list, size);
stopWatch.stop();
Assert.assertEquals(CollSplitResult, ListSplitResult);
@@ -87,7 +87,7 @@ public class ListUtilTest {
@Test
public void editTest() {
List<String> a = ListUtil.toLinkedList("1", "2", "3");
final List<String> a = ListUtil.toLinkedList("1", "2", "3");
final List<String> filter = (List<String>) CollUtil.edit(a, str -> "edit" + str);
Assert.assertEquals("edit1", filter.get(0));
Assert.assertEquals("edit2", filter.get(1));
@@ -96,7 +96,7 @@ public class ListUtilTest {
@Test
public void indexOfAll() {
List<String> a = ListUtil.toLinkedList("1", "2", "3", "4", "3", "2", "1");
final List<String> a = ListUtil.toLinkedList("1", "2", "3", "4", "3", "2", "1");
final int[] indexArray = ListUtil.indexOfAll(a, "2"::equals);
Assert.assertArrayEquals(new int[]{1,5}, indexArray);
final int[] indexArray2 = ListUtil.indexOfAll(a, "1"::equals);
@@ -105,14 +105,14 @@ public class ListUtilTest {
@Test
public void pageTest() {
List<Integer> a = ListUtil.toLinkedList(1, 2, 3,4,5);
final List<Integer> a = ListUtil.toLinkedList(1, 2, 3,4,5);
PageUtil.setFirstPageNo(1);
int[] a_1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] a1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] a2 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] a3 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] a4 = ListUtil.page(4,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] a_1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] a1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] a2 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] a3 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] a4 = ListUtil.page(4,2,a).stream().mapToInt(Integer::valueOf).toArray();
Assert.assertArrayEquals(new int[]{1,2},a_1);
Assert.assertArrayEquals(new int[]{1,2},a1);
Assert.assertArrayEquals(new int[]{3,4},a2);
@@ -121,11 +121,11 @@ public class ListUtilTest {
PageUtil.setFirstPageNo(2);
int[] b_1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] b1 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] b2 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] b3 = ListUtil.page(4,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] b4 = ListUtil.page(5,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] b_1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] b1 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] b2 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] b3 = ListUtil.page(4,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] b4 = ListUtil.page(5,2,a).stream().mapToInt(Integer::valueOf).toArray();
Assert.assertArrayEquals(new int[]{1,2},b_1);
Assert.assertArrayEquals(new int[]{1,2},b1);
Assert.assertArrayEquals(new int[]{3,4},b2);
@@ -133,11 +133,11 @@ public class ListUtilTest {
Assert.assertArrayEquals(new int[]{},b4);
PageUtil.setFirstPageNo(0);
int[] c_1 = ListUtil.page(-1,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] c1 = ListUtil.page(0,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] c2 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] c3 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray();
int[] c4 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] c_1 = ListUtil.page(-1,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] c1 = ListUtil.page(0,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] c2 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] c3 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] c4 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray();
Assert.assertArrayEquals(new int[]{1,2},c_1);
Assert.assertArrayEquals(new int[]{1,2},c1);
Assert.assertArrayEquals(new int[]{3,4},c2);
@@ -146,11 +146,11 @@ public class ListUtilTest {
PageUtil.setFirstPageNo(1);
int[] d1 = ListUtil.page(0,8,a).stream().mapToInt(Integer::valueOf).toArray();
final int[] d1 = ListUtil.page(0,8,a).stream().mapToInt(Integer::valueOf).toArray();
Assert.assertArrayEquals(new int[]{1,2,3,4,5},d1);
// page with consumer
List<List<Integer>> pageListData = new ArrayList<>();
final List<List<Integer>> pageListData = new ArrayList<>();
ListUtil.page(a, 2, pageListData::add);
Assert.assertArrayEquals(new int[]{1, 2}, pageListData.get(0).stream().mapToInt(Integer::valueOf).toArray());
Assert.assertArrayEquals(new int[]{3, 4}, pageListData.get(1).stream().mapToInt(Integer::valueOf).toArray());
@@ -210,20 +210,20 @@ public class ListUtilTest {
@Test
public void swapIndex() {
List<Integer> list = Arrays.asList(7, 2, 8, 9);
final List<Integer> list = Arrays.asList(7, 2, 8, 9);
ListUtil.swapTo(list, 8, 1);
Assert.assertEquals(8, (int) list.get(1));
}
@Test
public void swapElement() {
Map<String, String> map1 = new HashMap<>();
final Map<String, String> map1 = new HashMap<>();
map1.put("1", "张三");
Map<String, String> map2 = new HashMap<>();
final Map<String, String> map2 = new HashMap<>();
map2.put("2", "李四");
Map<String, String> map3 = new HashMap<>();
final Map<String, String> map3 = new HashMap<>();
map3.put("3", "王五");
List<Map<String, String>> list = Arrays.asList(map1, map2, map3);
final List<Map<String, String>> list = Arrays.asList(map1, map2, map3);
ListUtil.swapElement(list, map2, map3);
Map<String, String> map = list.get(2);
Assert.assertEquals("李四", map.get("2"));
@@ -232,4 +232,22 @@ public class ListUtilTest {
map = list.get(0);
Assert.assertEquals("李四", map.get("2"));
}
@Test
public void setOrPaddingNullTest(){
final List<String> list = new ArrayList<>();
list.add("1");
// 替换原值
ListUtil.setOrPadding(list, 0, "a");
Assert.assertEquals("[a]", list.toString());
//append值
ListUtil.setOrPadding(list, 1, "a");
Assert.assertEquals("[a, a]", list.toString());
// padding null 后加入值
ListUtil.setOrPadding(list, 3, "a");
Assert.assertEquals(4, list.size());
}
}