提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.common.util.collection; |
H |
2 |
|
|
3 |
import lombok.AllArgsConstructor; |
|
4 |
import lombok.Data; |
|
5 |
import org.junit.jupiter.api.Test; |
|
6 |
|
|
7 |
import java.util.Arrays; |
|
8 |
import java.util.Collection; |
|
9 |
import java.util.List; |
|
10 |
import java.util.function.BiFunction; |
|
11 |
|
|
12 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
13 |
|
|
14 |
/** |
|
15 |
* {@link CollectionUtils} 的单元测试 |
|
16 |
*/ |
|
17 |
public class CollectionUtilsTest { |
|
18 |
|
|
19 |
@Data |
|
20 |
@AllArgsConstructor |
|
21 |
private static class Dog { |
|
22 |
|
|
23 |
private Integer id; |
|
24 |
private String name; |
|
25 |
private String code; |
|
26 |
|
|
27 |
} |
|
28 |
|
|
29 |
@Test |
|
30 |
public void testDiffList() { |
|
31 |
// 准备参数 |
|
32 |
Collection<Dog> oldList = Arrays.asList( |
|
33 |
new Dog(1, "花花", "hh"), |
|
34 |
new Dog(2, "旺财", "wc") |
|
35 |
); |
|
36 |
Collection<Dog> newList = Arrays.asList( |
|
37 |
new Dog(null, "花花2", "hh"), |
|
38 |
new Dog(null, "小白", "xb") |
|
39 |
); |
|
40 |
BiFunction<Dog, Dog, Boolean> sameFunc = (oldObj, newObj) -> { |
|
41 |
boolean same = oldObj.getCode().equals(newObj.getCode()); |
|
42 |
// 如果相等的情况下,需要设置下 id,后续好更新 |
|
43 |
if (same) { |
|
44 |
newObj.setId(oldObj.getId()); |
|
45 |
} |
|
46 |
return same; |
|
47 |
}; |
|
48 |
|
|
49 |
// 调用 |
|
50 |
List<List<Dog>> result = CollectionUtils.diffList(oldList, newList, sameFunc); |
|
51 |
// 断言 |
|
52 |
assertEquals(result.size(), 3); |
|
53 |
// 断言 create |
|
54 |
assertEquals(result.get(0).size(), 1); |
|
55 |
assertEquals(result.get(0).get(0), new Dog(null, "小白", "xb")); |
|
56 |
// 断言 update |
|
57 |
assertEquals(result.get(1).size(), 1); |
|
58 |
assertEquals(result.get(1).get(0), new Dog(1, "花花2", "hh")); |
|
59 |
// 断言 delete |
|
60 |
assertEquals(result.get(2).size(), 1); |
|
61 |
assertEquals(result.get(2).get(0), new Dog(2, "旺财", "wc")); |
|
62 |
} |
|
63 |
|
|
64 |
} |