This is an automated email from the ASF dual-hosted git repository.
ShannonDing pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git
The following commit(s) were added to refs/heads/develop by this push:
new cb0dfc2eae [ISSUE #8262] Add unit tests for common utils (#11506)
cb0dfc2eae is described below
commit cb0dfc2eae8960e847804498e475bb80b92864de
Author: FinberA0A <[email protected]>
AuthorDate: Tue Nov 17 13:56:08 2026 +0800
[ISSUE #8262] Add unit tests for common utils (#11506)
Add direct unit tests for three previously untested utility classes in
the common module to improve coverage:
- MessageUtilsTest (14 tests): sharding key index determinism, range and
deduplication, plus deleteProperty boundary cases (first/middle/last
entry, repeated keys, name prefix collisions, missing value separator).
- QueueTypeUtilsTest (6 tests): CQ type resolution from topic attributes,
including absent config, null/empty attributes and explicit values.
- BinaryUtilTest (7 tests): MD5 generation for strings/bytes and ASCII
validation for null, control characters and non-ASCII input.
Co-authored-by: Finber <[email protected]>
---
.../rocketmq/common/utils/BinaryUtilTest.java | 68 ++++++++++
.../rocketmq/common/utils/MessageUtilsTest.java | 150 +++++++++++++++++++++
.../rocketmq/common/utils/QueueTypeUtilsTest.java | 87 ++++++++++++
3 files changed, 305 insertions(+)
diff --git
a/common/src/test/java/org/apache/rocketmq/common/utils/BinaryUtilTest.java
b/common/src/test/java/org/apache/rocketmq/common/utils/BinaryUtilTest.java
new file mode 100644
index 0000000000..3db37cbd7a
--- /dev/null
+++ b/common/src/test/java/org/apache/rocketmq/common/utils/BinaryUtilTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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 org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class BinaryUtilTest {
+
+ @Test
+ public void testGenerateMd5FromString() {
+
assertThat(BinaryUtil.generateMd5("hello")).isEqualTo("5D41402ABC4B2A76B9719D911017C592");
+
assertThat(BinaryUtil.generateMd5("")).isEqualTo("D41D8CD98F00B204E9800998ECF8427E");
+ }
+
+ @Test
+ public void testGenerateMd5FromBytesMatchesStringVersion() {
+ byte[] content = "RocketMQ".getBytes(StandardCharsets.UTF_8);
+
+ assertThat(BinaryUtil.generateMd5(content))
+ .isEqualTo(BinaryUtil.generateMd5("RocketMQ"));
+ }
+
+ @Test
+ public void testCalculateMd5ReturnsSixteenBytes() {
+
assertThat(BinaryUtil.calculateMd5("hello".getBytes(StandardCharsets.UTF_8))).hasSize(16);
+ }
+
+ @Test
+ public void testIsAsciiWithNullSubject() {
+ assertThat(BinaryUtil.isAscii(null)).isFalse();
+ }
+
+ @Test
+ public void testIsAsciiWithPrintableCharacters() {
+ assertThat(BinaryUtil.isAscii("Hello RocketMQ
~!".getBytes(StandardCharsets.US_ASCII))).isTrue();
+ assertThat(BinaryUtil.isAscii(new byte[0])).isTrue();
+ }
+
+ @Test
+ public void testIsAsciiRejectsControlCharacters() {
+ assertThat(BinaryUtil.isAscii(new byte[] {'a', '\t'})).isFalse();
+ assertThat(BinaryUtil.isAscii(new byte[] {'a', '\n'})).isFalse();
+ }
+
+ @Test
+ public void testIsAsciiRejectsNonAsciiBytes() {
+
assertThat(BinaryUtil.isAscii("\u4e2d\u6587".getBytes(StandardCharsets.UTF_8))).isFalse();
+ assertThat(BinaryUtil.isAscii(new byte[] {127})).isFalse();
+ }
+}
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 0000000000..baf211e461
--- /dev/null
+++
b/common/src/test/java/org/apache/rocketmq/common/utils/MessageUtilsTest.java
@@ -0,0 +1,150 @@
+/*
+ * 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.util.Arrays;
+import java.util.Set;
+import org.apache.rocketmq.common.message.MessageAccessor;
+import org.apache.rocketmq.common.message.MessageConst;
+import org.apache.rocketmq.common.message.MessageExt;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class MessageUtilsTest {
+
+ private static final String NAME_VALUE_SEPARATOR = "\u0001";
+ private static final String PROPERTY_SEPARATOR = "\u0002";
+
+ @Test
+ public void testGetShardingKeyIndexIsDeterministicAndInRange() {
+ for (int i = 0; i < 100; i++) {
+ String shardingKey = "sharding-key-" + i;
+ int index = MessageUtils.getShardingKeyIndex(shardingKey, 8);
+
+ assertThat(index).isBetween(0, 7);
+ assertThat(MessageUtils.getShardingKeyIndex(shardingKey,
8)).isEqualTo(index);
+ }
+ }
+
+ @Test
+ public void testGetShardingKeyIndexWithEmptyKey() {
+ assertThat(MessageUtils.getShardingKeyIndex("", 4)).isBetween(0, 3);
+ }
+
+ @Test
+ public void testGetShardingKeyIndexByMsgUsesShardingKeyProperty() {
+ MessageExt message = new MessageExt();
+ MessageAccessor.putProperty(message,
MessageConst.PROPERTY_SHARDING_KEY, "order-123");
+
+ assertThat(MessageUtils.getShardingKeyIndexByMsg(message, 8))
+ .isEqualTo(MessageUtils.getShardingKeyIndex("order-123", 8));
+ }
+
+ @Test
+ public void testGetShardingKeyIndexByMsgFallsBackToEmptyKey() {
+ MessageExt message = new MessageExt();
+
+ assertThat(MessageUtils.getShardingKeyIndexByMsg(message, 8))
+ .isEqualTo(MessageUtils.getShardingKeyIndex("", 8));
+ }
+
+ @Test
+ public void testGetShardingKeyIndexesDeduplicatesSameKey() {
+ MessageExt first = new MessageExt();
+ MessageAccessor.putProperty(first, MessageConst.PROPERTY_SHARDING_KEY,
"same-key");
+ MessageExt second = new MessageExt();
+ MessageAccessor.putProperty(second,
MessageConst.PROPERTY_SHARDING_KEY, "same-key");
+
+ Set<Integer> indexes =
MessageUtils.getShardingKeyIndexes(Arrays.asList(first, second), 8);
+
+ assertThat(indexes).hasSize(1);
+
assertThat(indexes).contains(MessageUtils.getShardingKeyIndex("same-key", 8));
+ }
+
+ @Test
+ public void testDeletePropertyWithNullProperties() {
+ assertThat(MessageUtils.deleteProperty(null, "KeyA")).isNull();
+ }
+
+ @Test
+ public void testDeletePropertyKeepsUnrelatedProperties() {
+ String properties = property("KeyA", "ValueA") + PROPERTY_SEPARATOR +
property("KeyB", "ValueB");
+
+ assertThat(MessageUtils.deleteProperty(properties,
"KeyC")).isEqualTo(properties);
+ }
+
+ @Test
+ public void testDeletePropertyAtTheBeginning() {
+ String properties = property("KeyA", "ValueA") + PROPERTY_SEPARATOR +
property("KeyB", "ValueB");
+
+ assertThat(MessageUtils.deleteProperty(properties, "KeyA"))
+ .isEqualTo(property("KeyB", "ValueB"));
+ }
+
+ @Test
+ public void testDeletePropertyInTheMiddle() {
+ String properties = property("KeyA", "ValueA") + PROPERTY_SEPARATOR
+ + property("KeyB", "ValueB") + PROPERTY_SEPARATOR
+ + property("KeyC", "ValueC");
+
+ assertThat(MessageUtils.deleteProperty(properties, "KeyB"))
+ .isEqualTo(property("KeyA", "ValueA") + PROPERTY_SEPARATOR +
property("KeyC", "ValueC"));
+ }
+
+ @Test
+ public void testDeleteLastPropertyKeepsTrailingSeparator() {
+ String properties = property("KeyA", "ValueA") + PROPERTY_SEPARATOR +
property("KeyB", "ValueB");
+
+ // The trailing separator is preserved by design, consistent with the
broker's
+ // property layout where each entry is followed by a separator.
+ assertThat(MessageUtils.deleteProperty(properties, "KeyB"))
+ .isEqualTo(property("KeyA", "ValueA") + PROPERTY_SEPARATOR);
+ }
+
+ @Test
+ public void testDeleteOnlyProperty() {
+ assertThat(MessageUtils.deleteProperty(property("KeyA", "ValueA"),
"KeyA")).isEmpty();
+ }
+
+ @Test
+ public void testDeleteAllOccurrencesOfSameProperty() {
+ String properties = property("KeyA", "ValueA") + PROPERTY_SEPARATOR +
property("KeyA", "ValueA2");
+
+ assertThat(MessageUtils.deleteProperty(properties, "KeyA")).isEmpty();
+ }
+
+ @Test
+ public void testDeletePropertyDoesNotMatchNamePrefix() {
+ String properties = property("KeyA", "ValueA") + PROPERTY_SEPARATOR +
property("KeyAB", "ValueB");
+
+ assertThat(MessageUtils.deleteProperty(properties, "KeyA"))
+ .isEqualTo(property("KeyAB", "ValueB"));
+ }
+
+ @Test
+ public void testDeletePropertyRequiresNameValueSeparator() {
+ String properties = "KeyA" + PROPERTY_SEPARATOR + property("KeyB",
"ValueB");
+
+ assertThat(MessageUtils.deleteProperty(properties,
"KeyA")).isEqualTo(properties);
+ }
+
+ private static String property(String name, String value) {
+ return name + NAME_VALUE_SEPARATOR + value;
+ }
+}
diff --git
a/common/src/test/java/org/apache/rocketmq/common/utils/QueueTypeUtilsTest.java
b/common/src/test/java/org/apache/rocketmq/common/utils/QueueTypeUtilsTest.java
new file mode 100644
index 0000000000..7f17106168
--- /dev/null
+++
b/common/src/test/java/org/apache/rocketmq/common/utils/QueueTypeUtilsTest.java
@@ -0,0 +1,87 @@
+/*
+ * 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.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.rocketmq.common.TopicAttributes;
+import org.apache.rocketmq.common.TopicConfig;
+import org.apache.rocketmq.common.attribute.CQType;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class QueueTypeUtilsTest {
+
+ @Test
+ public void testGetCQTypeWithAbsentTopicConfig() {
+
assertThat(QueueTypeUtils.getCQType(Optional.empty())).isEqualTo(CQType.SimpleCQ);
+ assertThat(QueueTypeUtils.isBatchCq(Optional.empty())).isFalse();
+ }
+
+ @Test
+ public void testGetCQTypeWithNullAttributes() {
+ TopicConfig topicConfig = new TopicConfig("test-topic");
+ topicConfig.setAttributes(null);
+
+
assertThat(QueueTypeUtils.getCQType(Optional.of(topicConfig))).isEqualTo(CQType.SimpleCQ);
+ }
+
+ @Test
+ public void testGetCQTypeWithEmptyAttributes() {
+ TopicConfig topicConfig = new TopicConfig("test-topic");
+ topicConfig.setAttributes(new HashMap<>());
+
+
assertThat(QueueTypeUtils.getCQType(Optional.of(topicConfig))).isEqualTo(CQType.SimpleCQ);
+ }
+
+ @Test
+ public void testGetCQTypeWithUnrelatedAttributes() {
+ TopicConfig topicConfig = new TopicConfig("test-topic");
+ Map<String, String> attributes = new HashMap<>();
+ attributes.put("cleanup.policy", "DELETE");
+ topicConfig.setAttributes(attributes);
+
+
assertThat(QueueTypeUtils.getCQType(Optional.of(topicConfig))).isEqualTo(CQType.SimpleCQ);
+ }
+
+ @Test
+ public void testGetCQTypeWithExplicitSimpleCQ() {
+ TopicConfig topicConfig = topicConfigWithQueueType("SimpleCQ");
+
+
assertThat(QueueTypeUtils.getCQType(Optional.of(topicConfig))).isEqualTo(CQType.SimpleCQ);
+
assertThat(QueueTypeUtils.isBatchCq(Optional.of(topicConfig))).isFalse();
+ }
+
+ @Test
+ public void testGetCQTypeWithExplicitBatchCQ() {
+ TopicConfig topicConfig = topicConfigWithQueueType("BatchCQ");
+
+
assertThat(QueueTypeUtils.getCQType(Optional.of(topicConfig))).isEqualTo(CQType.BatchCQ);
+
assertThat(QueueTypeUtils.isBatchCq(Optional.of(topicConfig))).isTrue();
+ }
+
+ private static TopicConfig topicConfigWithQueueType(String queueType) {
+ TopicConfig topicConfig = new TopicConfig("test-topic");
+ Map<String, String> attributes = new HashMap<>();
+ attributes.put(TopicAttributes.QUEUE_TYPE_ATTRIBUTE.getName(),
queueType);
+ topicConfig.setAttributes(attributes);
+ return topicConfig;
+ }
+}