From c0f0cbd14a8c08b1075ffaabadba5ac4779d8229 Mon Sep 17 00:00:00 2001 From: baymax55 <35788491+baymax55@users.noreply.github.com> Date: Wed, 28 Sep 2022 14:46:26 +0800 Subject: [PATCH] Add test case for GroupKey.java (#744) --- .../cn/hippo4j/common/toolkit/GroupKey.java | 2 +- .../hippo4j/common/toolkit/GroupKeyTest.java | 66 ++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/GroupKey.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/GroupKey.java index b082690d..227bba06 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/GroupKey.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/GroupKey.java @@ -41,7 +41,7 @@ public class GroupKey { for (int i = 1; i < params.length - 1; i++) { groupKey.append(params[i]).append(GROUP_KEY_DELIMITER); } - groupKey.append(params[params.length]); + groupKey.append(params[params.length - 1]); return groupKey.toString(); } diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/GroupKeyTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/GroupKeyTest.java index c45d5919..b512704a 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/GroupKeyTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/GroupKeyTest.java @@ -17,5 +17,67 @@ package cn.hippo4j.common.toolkit; -public final class GroupKeyTest { -} +import org.checkerframework.checker.units.qual.A; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class GroupKeyTest { + + @Test + public void getKey() { + String dataId = "dataId"; + String group = "group"; + String datumStr = "datumStr"; + String expected = "dataId+group+datumStr"; + String key = GroupKey.getKey(dataId, group, datumStr); + Assert.isTrue(key.equals(expected)); + } + + @Test + public void testGetKey() { + String dataId = "dataId"; + String group = "group"; + String expected = "dataId+group"; + String key = GroupKey.getKey(dataId, group); + Assert.isTrue(key.equals(expected)); + } + + @Test + public void testGetKey1() { + String[] strings = {"dataId", "group", "datumStr"}; + String expected = "dataId+group+datumStr"; + String key = GroupKey.getKey(strings); + Assert.isTrue(key.equals(expected)); + } + + @Test + public void getKeyTenant() { + String dataId = "dataId"; + String group = "group"; + String datumStr = "datumStr"; + String expected = "dataId+group+datumStr"; + + String keyTenant = GroupKey.getKeyTenant(dataId, group, datumStr); + Assert.isTrue(keyTenant.equals(expected)); + } + + @Test + public void parseKey() { + String groupKey = "prescription+dynamic-threadpool-example+message-consume+12"; + String[] strings = GroupKey.parseKey(groupKey); + Assert.isTrue(strings.length == 4); + } + + @Test + public void urlEncode() { + String str = "hello+World%"; + String expected = "hello%2BWorld%25"; + StringBuilder stringBuilder = new StringBuilder(); + GroupKey.urlEncode(str, stringBuilder); + Assert.isTrue(stringBuilder.toString().contains(expected)); + } +} \ No newline at end of file