From 0455f0c2220d7663006c4335ef3312b857ba03c3 Mon Sep 17 00:00:00 2001 From: gong-dx <15922402762@163.com> Date: Mon, 29 Jun 2026 19:12:17 +0800 Subject: [PATCH 1/2] add test: unit tests for MessageUtils utility class - Add comprehensive tests for getShardingKeyIndex() with various key types - Add tests for getShardingKeyIndexByMsg() including null sharding key - Add tests for getShardingKeyIndexes() with empty and multiple messages - Add tests for deleteProperty() covering edge cases: * Delete first/last/middle keys * Null input handling * Non-existent key handling * Substring key matching (mykey vs key) * Value containing key name Addresses the gap in test coverage for MessageUtils, aligning with Apache RocketMQ's contribution guidelines requiring 80%+ test coverage. --- .../common/utils/MessageUtilsTest.java | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 common/src/test/java/org/apache/rocketmq/common/utils/MessageUtilsTest.java diff --git a/common/src/test/java/org/apache/rocketmq/common/utils/MessageUtilsTest.java b/common/src/test/java/org/apache/rocketmq/common/utils/MessageUtilsTest.java new file mode 100644 index 00000000000..50438a67df1 --- /dev/null +++ b/common/src/test/java/org/apache/rocketmq/common/utils/MessageUtilsTest.java @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.common.utils; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Set; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.common.message.MessageConst; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MessageUtilsTest { + + @Test + public void testGetShardingKeyIndex() { + String shardingKey = "test-key"; + int indexSize = 10; + int index = MessageUtils.getShardingKeyIndex(shardingKey, indexSize); + assertThat(index).isBetween(0, indexSize - 1); + } + + @Test + public void testGetShardingKeyIndex_EmptyKey() { + String shardingKey = ""; + int indexSize = 5; + int index = MessageUtils.getShardingKeyIndex(shardingKey, indexSize); + assertThat(index).isBetween(0, indexSize - 1); + } + + @Test + public void testGetShardingKeyIndex_ChineseKey() { + String shardingKey = "测试分片键"; + int indexSize = 100; + int index = MessageUtils.getShardingKeyIndex(shardingKey, indexSize); + assertThat(index).isBetween(0, indexSize - 1); + } + + @Test + public void testGetShardingKeyIndex_DifferentKeys() { + int indexSize = 10; + int index1 = MessageUtils.getShardingKeyIndex("user-1", indexSize); + int index2 = MessageUtils.getShardingKeyIndex("user-2", indexSize); + assertThat(index1).isBetween(0, indexSize - 1); + assertThat(index2).isBetween(0, indexSize - 1); + } + + @Test + public void testGetShardingKeyIndexByMsg() { + MessageExt msg = new MessageExt(); + msg.putUserProperty(MessageConst.PROPERTY_SHARDING_KEY, "sharding-123"); + int index = MessageUtils.getShardingKeyIndexByMsg(msg, 10); + assertThat(index).isBetween(0, 9); + } + + @Test + public void testGetShardingKeyIndexByMsg_NullShardingKey() { + MessageExt msg = new MessageExt(); + int index = MessageUtils.getShardingKeyIndexByMsg(msg, 10); + assertThat(index).isBetween(0, 9); + } + + @Test + public void testGetShardingKeyIndexes() { + MessageExt msg1 = new MessageExt(); + msg1.putUserProperty(MessageConst.PROPERTY_SHARDING_KEY, "key-1"); + MessageExt msg2 = new MessageExt(); + msg2.putUserProperty(MessageConst.PROPERTY_SHARDING_KEY, "key-2"); + MessageExt msg3 = new MessageExt(); + msg3.putUserProperty(MessageConst.PROPERTY_SHARDING_KEY, "key-1"); + Set indexes = MessageUtils.getShardingKeyIndexes(Arrays.asList(msg1, msg2, msg3), 10); + assertThat(indexes).isNotEmpty(); + assertThat(indexes).allMatch(i -> i >= 0 && i < 10); + } + + @Test + public void testGetShardingKeyIndexes_EmptyCollection() { + Set indexes = MessageUtils.getShardingKeyIndexes(Arrays.asList(), 10); + assertThat(indexes).isEmpty(); + } + + @Test + public void testDeleteProperty_FromSimpleString() { + String properties = "key1=value1;key2=value2;key3=value3"; + String result = MessageUtils.deleteProperty(properties, "key2"); + assertThat(result).isEqualTo("key1=value1;key3=value3"); + assertThat(result).doesNotContain("key2"); + } + + @Test + public void testDeleteProperty_FromNullString() { + String result = MessageUtils.deleteProperty(null, "key1"); + assertThat(result).isNull(); + } + + @Test + public void testDeleteProperty_KeyNotFound() { + String properties = "key1=value1;key2=value2"; + String result = MessageUtils.deleteProperty(properties, "key3"); + assertThat(result).isEqualTo("key1=value1;key2=value2"); + } + + @Test + public void testDeleteProperty_DeleteFirstKey() { + String properties = "first=value1;second=value2"; + String result = MessageUtils.deleteProperty(properties, "first"); + assertThat(result).isEqualTo("second=value2"); + assertThat(result).doesNotContain("first"); + } + + @Test + public void testDeleteProperty_DeleteLastKey() { + String properties = "first=value1;last=value2"; + String result = MessageUtils.deleteProperty(properties, "last"); + assertThat(result).isEqualTo("first=value1"); + assertThat(result).doesNotContain("last"); + } + + @Test + public void testDeleteProperty_MultipleSameKeys() { + String properties = "key=value1;other=value2;key=value3"; + String result = MessageUtils.deleteProperty(properties, "key"); + assertThat(result).isEqualTo("other=value2"); + assertThat(result).doesNotContain("key="); + } + + @Test + public void testDeleteProperty_SubstringKeyMatch() { + String properties = "mykey=value1;key=value2;other=value3"; + String result = MessageUtils.deleteProperty(properties, "key"); + assertThat(result).contains("mykey=value1"); + assertThat(result).doesNotContain("key=value2"); + } + + @Test + public void testDeleteProperty_ValueContainsKeyName() { + String properties = "topic=my-topic;msgId=abc123"; + String result = MessageUtils.deleteProperty(properties, "msgId"); + assertThat(result).isEqualTo("topic=my-topic"); + assertThat(result).doesNotContain("msgId"); + } +} From 86265c3eb9d6bc0491d1709d9a5e8ae1cb544f10 Mon Sep 17 00:00:00 2001 From: gong-dx <15922402762@163.com> Date: Tue, 30 Jun 2026 18:50:57 +0800 Subject: [PATCH 2/2] fix: address PR review feedback from RockteMQ-AI - Remove unused import StandardCharsets - Rename test methods to camelCase (remove underscores) per project convention - Add determinism check: same input produces same output - Add test for MessageExt with no sharding key property - Clarify test method names (WithEmptyKey, WithChineseKey, etc.) --- .../common/utils/MessageUtilsTest.java | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/common/src/test/java/org/apache/rocketmq/common/utils/MessageUtilsTest.java b/common/src/test/java/org/apache/rocketmq/common/utils/MessageUtilsTest.java index 50438a67df1..292f8d59024 100644 --- a/common/src/test/java/org/apache/rocketmq/common/utils/MessageUtilsTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/utils/MessageUtilsTest.java @@ -17,7 +17,6 @@ package org.apache.rocketmq.common.utils; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Set; import org.apache.rocketmq.common.message.MessageExt; @@ -37,7 +36,7 @@ public void testGetShardingKeyIndex() { } @Test - public void testGetShardingKeyIndex_EmptyKey() { + public void testGetShardingKeyIndexWithEmptyKey() { String shardingKey = ""; int indexSize = 5; int index = MessageUtils.getShardingKeyIndex(shardingKey, indexSize); @@ -45,7 +44,7 @@ public void testGetShardingKeyIndex_EmptyKey() { } @Test - public void testGetShardingKeyIndex_ChineseKey() { + public void testGetShardingKeyIndexWithChineseKey() { String shardingKey = "测试分片键"; int indexSize = 100; int index = MessageUtils.getShardingKeyIndex(shardingKey, indexSize); @@ -53,12 +52,13 @@ public void testGetShardingKeyIndex_ChineseKey() { } @Test - public void testGetShardingKeyIndex_DifferentKeys() { + public void testGetShardingKeyIndexConsistency() { + // Verify same input produces same output (deterministic) + String shardingKey = "test-key"; int indexSize = 10; - int index1 = MessageUtils.getShardingKeyIndex("user-1", indexSize); - int index2 = MessageUtils.getShardingKeyIndex("user-2", indexSize); - assertThat(index1).isBetween(0, indexSize - 1); - assertThat(index2).isBetween(0, indexSize - 1); + int index1 = MessageUtils.getShardingKeyIndex(shardingKey, indexSize); + int index2 = MessageUtils.getShardingKeyIndex(shardingKey, indexSize); + assertThat(index1).isEqualTo(index2); } @Test @@ -70,7 +70,8 @@ public void testGetShardingKeyIndexByMsg() { } @Test - public void testGetShardingKeyIndexByMsg_NullShardingKey() { + public void testGetShardingKeyIndexByMsgWithNoShardingKey() { + // Verify fallback to empty string when no sharding key property is set MessageExt msg = new MessageExt(); int index = MessageUtils.getShardingKeyIndexByMsg(msg, 10); assertThat(index).isBetween(0, 9); @@ -90,13 +91,13 @@ public void testGetShardingKeyIndexes() { } @Test - public void testGetShardingKeyIndexes_EmptyCollection() { + public void testGetShardingKeyIndexesWithEmptyCollection() { Set indexes = MessageUtils.getShardingKeyIndexes(Arrays.asList(), 10); assertThat(indexes).isEmpty(); } @Test - public void testDeleteProperty_FromSimpleString() { + public void testDeletePropertyFromSimpleString() { String properties = "key1=value1;key2=value2;key3=value3"; String result = MessageUtils.deleteProperty(properties, "key2"); assertThat(result).isEqualTo("key1=value1;key3=value3"); @@ -104,20 +105,20 @@ public void testDeleteProperty_FromSimpleString() { } @Test - public void testDeleteProperty_FromNullString() { + public void testDeletePropertyFromNullString() { String result = MessageUtils.deleteProperty(null, "key1"); assertThat(result).isNull(); } @Test - public void testDeleteProperty_KeyNotFound() { + public void testDeletePropertyKeyNotFound() { String properties = "key1=value1;key2=value2"; String result = MessageUtils.deleteProperty(properties, "key3"); assertThat(result).isEqualTo("key1=value1;key2=value2"); } @Test - public void testDeleteProperty_DeleteFirstKey() { + public void testDeletePropertyDeleteFirstKey() { String properties = "first=value1;second=value2"; String result = MessageUtils.deleteProperty(properties, "first"); assertThat(result).isEqualTo("second=value2"); @@ -125,7 +126,7 @@ public void testDeleteProperty_DeleteFirstKey() { } @Test - public void testDeleteProperty_DeleteLastKey() { + public void testDeletePropertyDeleteLastKey() { String properties = "first=value1;last=value2"; String result = MessageUtils.deleteProperty(properties, "last"); assertThat(result).isEqualTo("first=value1"); @@ -133,7 +134,7 @@ public void testDeleteProperty_DeleteLastKey() { } @Test - public void testDeleteProperty_MultipleSameKeys() { + public void testDeletePropertyMultipleSameKeys() { String properties = "key=value1;other=value2;key=value3"; String result = MessageUtils.deleteProperty(properties, "key"); assertThat(result).isEqualTo("other=value2"); @@ -141,7 +142,8 @@ public void testDeleteProperty_MultipleSameKeys() { } @Test - public void testDeleteProperty_SubstringKeyMatch() { + public void testDeletePropertySubstringKeyMatch() { + // Deleting "key" should not affect "mykey" String properties = "mykey=value1;key=value2;other=value3"; String result = MessageUtils.deleteProperty(properties, "key"); assertThat(result).contains("mykey=value1"); @@ -149,7 +151,7 @@ public void testDeleteProperty_SubstringKeyMatch() { } @Test - public void testDeleteProperty_ValueContainsKeyName() { + public void testDeletePropertyValueContainsKeyName() { String properties = "topic=my-topic;msgId=abc123"; String result = MessageUtils.deleteProperty(properties, "msgId"); assertThat(result).isEqualTo("topic=my-topic");