This is an automated email from the ASF dual-hosted git repository.
chia7712 pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git
The following commit(s) were added to refs/heads/trunk by this push:
new f749af557a4 MINOR: add some unit test for common Utils (#16549)
f749af557a4 is described below
commit f749af557a4d9ac3493aa329583260b8b16e2979
Author: xijiu <[email protected]>
AuthorDate: Mon Jul 15 21:18:51 2024 +0800
MINOR: add some unit test for common Utils (#16549)
Reviewers: Ken Huang <[email protected]>, Chia-Ping Tsai
<[email protected]>
---
.../org/apache/kafka/common/utils/UtilsTest.java | 60 ++++++++++++++++++++++
1 file changed, 60 insertions(+)
diff --git a/clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java
b/clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java
index 974c5b0fde2..1905b459037 100755
--- a/clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java
+++ b/clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java
@@ -57,6 +57,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
@@ -448,6 +449,65 @@ public class UtilsTest {
assertEquals(1, Utils.min(2, 3, 1));
}
+ @Test
+ public void testMax() {
+ assertEquals(1, Utils.max(1));
+ assertEquals(3, Utils.max(1, 2, 3));
+ assertEquals(3, Utils.max(2, 1, 3, 3));
+ assertEquals(100, Utils.max(0, 2, 2, 100));
+ assertEquals(-1, Utils.max(-1, -2, -2, -10, -100, -1000));
+ assertEquals(0, Utils.max(-1, -2, -2, -10, -150, -1800, 0));
+ }
+
+ @Test
+ public void mkStringTest() {
+ Map<String, String> map = new LinkedHashMap<>();
+ map.put("key1", "val1");
+ map.put("key2", "val2");
+ map.put("key3", "val3");
+ String result = Utils.mkString(map, "__begin__", "__end__", "=", ",");
+ assertEquals("__begin__key1=val1,key2=val2,key3=val3__end__", result);
+
+ String result2 = Utils.mkString(Collections.emptyMap(), "__begin__",
"__end__", "=", ",");
+ assertEquals("__begin____end__", result2);
+ }
+
+ @Test
+ public void parseMapTest() {
+ Map<String, String> map1 = Utils.parseMap("k1=v1,k2=v2,k3=v3", "=",
",");
+ assertEquals(3, map1.size());
+ assertEquals("v1", map1.get("k1"));
+ assertEquals("v2", map1.get("k2"));
+ assertEquals("v3", map1.get("k3"));
+
+ Map<String, String> map3 = Utils.parseMap("k4=v4,k5=v5=vv5=vvv5", "=",
",");
+ assertEquals(2, map3.size());
+ assertEquals("v4", map3.get("k4"));
+ assertEquals("v5=vv5=vvv5", map3.get("k5"));
+ }
+
+ @Test
+ public void ensureCapacityTest() {
+ ByteBuffer byteBuffer = ByteBuffer.allocate(10);
+ ByteBuffer newByteBuffer = Utils.ensureCapacity(byteBuffer, 5);
+ assertEquals(10, newByteBuffer.capacity());
+
+ ByteBuffer byteBuffer2 = ByteBuffer.allocate(10);
+ ByteBuffer newByteBuffer2 = Utils.ensureCapacity(byteBuffer2, 15);
+ assertEquals(15, newByteBuffer2.capacity());
+
+ ByteBuffer byteBuffer3 = ByteBuffer.allocate(10);
+ for (int i = 1; i <= 10; i++) {
+ byteBuffer3.put((byte) i);
+ }
+ ByteBuffer newByteBuffer3 = Utils.ensureCapacity(byteBuffer3, 15);
+ newByteBuffer3.flip();
+ assertEquals(15, newByteBuffer3.capacity());
+ assertEquals(1, newByteBuffer3.get());
+ assertEquals(2, newByteBuffer3.get());
+ assertEquals(3, newByteBuffer3.get());
+ }
+
@Test
public void testCloseAll() {
TestCloseable[] closeablesWithoutException =
TestCloseable.createCloseables(false, false, false);