From 8a60f4db66bad9b13e5be243a1a1acfe979240ca Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 29 Dec 2024 22:35:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=9D=E5=AD=98=20CollectionTools#nullToEmpt?= =?UTF-8?q?yXXX=20=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collection/CollectionToolsTests.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/java/xyz/zhouxy/plusone/commons/collection/CollectionToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/collection/CollectionToolsTests.java index bcb53a6..97fdef5 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/collection/CollectionToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/collection/CollectionToolsTests.java @@ -16,16 +16,24 @@ package xyz.zhouxy.plusone.commons.collection; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import org.junit.jupiter.api.Test; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + public class CollectionToolsTests { @Test void testIsEmpty() { @@ -46,4 +54,19 @@ public class CollectionToolsTests { assertFalse(CollectionTools.isEmpty(map)); assertTrue(CollectionTools.isNotEmpty(map)); } + + @Test + void testNullToEmpty() { + List list = Lists.newArrayList("Java", "C", "C++", "C#"); + assertSame(list, CollectionTools.nullToEmptyList(list)); + assertEquals(Collections.emptyList(), CollectionTools.nullToEmptyList(null)); + + Set set = Sets.newHashSet("Java", "C", "C++", "C#"); + assertSame(set, CollectionTools.nullToEmptySet(set)); + assertEquals(Collections.emptySet(), CollectionTools.nullToEmptySet(null)); + + Map map = ImmutableMap.of("K1", 1, "K2", 2, "K3", 3); + assertSame(map, CollectionTools.nullToEmptyMap(map)); + assertEquals(Collections.emptyMap(), CollectionTools.nullToEmptyMap(null)); + } }