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 348e64c57d8 MINOR: Add unit tests for verifying --formatter-property 
in console tools. (#20560)
348e64c57d8 is described below

commit 348e64c57d80188334b441d1a06c4181c00a8afa
Author: Shivsundar R <s...@confluent.io>
AuthorDate: Wed Sep 24 12:15:05 2025 -0400

    MINOR: Add unit tests for verifying --formatter-property in console tools. 
(#20560)
    
    *What*
    In the implementation of KIP-1147 for console tools -
    
    
https://github.com/apache/kafka/pull/20479/files#diff-85b87c675a4b933e8e0e05c654d35d60e9cfd36cebe3331af825191b2cc688ee,
    we missed adding unit tests for verifying the new
    "`--formatter-property`" option.
    Thanks to @Yunyung for pointing this out.
    
    PR adds unit tests to both `ConsoleConsumerOptionsTest` and
    `ConsoleShareConsumerOptionsTest` to verify the same.
    
    Reviewers: Jhen-Yung Hsu <jhenyung...@gmail.com>, Chia-Ping Tsai
     <chia7...@gmail.com>
---
 .../tools/consumer/ConsoleConsumerOptionsTest.java | 77 +++++++++++++++++++++-
 .../consumer/ConsoleShareConsumerOptionsTest.java  | 54 ++++++++++++++-
 2 files changed, 126 insertions(+), 5 deletions(-)

diff --git 
a/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleConsumerOptionsTest.java
 
b/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleConsumerOptionsTest.java
index 4639ff63a85..843d9785cf8 100644
--- 
a/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleConsumerOptionsTest.java
+++ 
b/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleConsumerOptionsTest.java
@@ -151,7 +151,7 @@ public class ConsoleConsumerOptionsTest {
     }
 
     @Test
-    public void shouldParseValidSimpleConsumerValidConfigWithStringOffset() 
throws Exception {
+    public void 
shouldParseValidSimpleConsumerValidConfigWithStringOffsetDeprecated() throws 
Exception {
         String[] args = new String[]{
             "--bootstrap-server", "localhost:9092",
             "--topic", "test",
@@ -171,6 +171,27 @@ public class ConsoleConsumerOptionsTest {
         assertFalse(((DefaultMessageFormatter) 
config.formatter()).printValue());
     }
 
+    @Test
+    public void shouldParseValidSimpleConsumerValidConfigWithStringOffset() 
throws Exception {
+        String[] args = new String[]{
+            "--bootstrap-server", "localhost:9092",
+            "--topic", "test",
+            "--partition", "0",
+            "--offset", "LatEst",
+            "--formatter-property", "print.value=false"
+        };
+
+        ConsoleConsumerOptions config = new ConsoleConsumerOptions(args);
+
+        assertEquals("localhost:9092", config.bootstrapServer());
+        assertEquals("test", config.topicArg().orElse(""));
+        assertTrue(config.partitionArg().isPresent());
+        assertEquals(0, config.partitionArg().getAsInt());
+        assertEquals(-1, config.offsetArg());
+        assertFalse(config.fromBeginning());
+        assertFalse(((DefaultMessageFormatter) 
config.formatter()).printValue());
+    }
+
     @Test
     public void 
shouldParseValidConsumerConfigWithAutoOffsetResetLatestDeprecated() throws 
IOException {
         String[] args = new String[]{
@@ -355,7 +376,7 @@ public class ConsoleConsumerOptionsTest {
     }
 
     @Test
-    public void testCustomPropertyShouldBePassedToConfigureMethod() throws 
Exception {
+    public void testCustomPropertyShouldBePassedToConfigureMethodDeprecated() 
throws Exception {
         String[] args = new String[]{
             "--bootstrap-server", "localhost:9092",
             "--topic", "test",
@@ -378,7 +399,30 @@ public class ConsoleConsumerOptionsTest {
     }
 
     @Test
-    public void testCustomConfigShouldBePassedToConfigureMethod() throws 
Exception {
+    public void testCustomPropertyShouldBePassedToConfigureMethod() throws 
Exception {
+        String[] args = new String[]{
+            "--bootstrap-server", "localhost:9092",
+            "--topic", "test",
+            "--formatter-property", "print.key=true",
+            "--formatter-property", 
"key.deserializer=org.apache.kafka.test.MockDeserializer",
+            "--formatter-property", "key.deserializer.my-props=abc"
+        };
+
+        ConsoleConsumerOptions config = new ConsoleConsumerOptions(args);
+
+        assertInstanceOf(DefaultMessageFormatter.class, config.formatter());
+        
assertTrue(config.formatterArgs().containsKey("key.deserializer.my-props"));
+        DefaultMessageFormatter formatter = (DefaultMessageFormatter) 
config.formatter();
+        assertTrue(formatter.keyDeserializer().isPresent());
+        assertInstanceOf(MockDeserializer.class, 
formatter.keyDeserializer().get());
+        MockDeserializer keyDeserializer = (MockDeserializer) 
formatter.keyDeserializer().get();
+        assertEquals(1, keyDeserializer.configs.size());
+        assertEquals("abc", keyDeserializer.configs.get("my-props"));
+        assertTrue(keyDeserializer.isKey);
+    }
+
+    @Test
+    public void testCustomConfigShouldBePassedToConfigureMethodDeprecated() 
throws Exception {
         Map<String, String> configs = new HashMap<>();
         configs.put("key.deserializer.my-props", "abc");
         configs.put("print.key", "false");
@@ -404,6 +448,33 @@ public class ConsoleConsumerOptionsTest {
         assertTrue(keyDeserializer.isKey);
     }
 
+    @Test
+    public void testCustomConfigShouldBePassedToConfigureMethod() throws 
Exception {
+        Map<String, String> configs = new HashMap<>();
+        configs.put("key.deserializer.my-props", "abc");
+        configs.put("print.key", "false");
+        File propsFile = ToolsTestUtils.tempPropertiesFile(configs);
+        String[] args = new String[]{
+            "--bootstrap-server", "localhost:9092",
+            "--topic", "test",
+            "--formatter-property", "print.key=true",
+            "--formatter-property", 
"key.deserializer=org.apache.kafka.test.MockDeserializer",
+            "--formatter-config", propsFile.getAbsolutePath()
+        };
+
+        ConsoleConsumerOptions config = new ConsoleConsumerOptions(args);
+
+        assertInstanceOf(DefaultMessageFormatter.class, config.formatter());
+        
assertTrue(config.formatterArgs().containsKey("key.deserializer.my-props"));
+        DefaultMessageFormatter formatter = (DefaultMessageFormatter) 
config.formatter();
+        assertTrue(formatter.keyDeserializer().isPresent());
+        assertInstanceOf(MockDeserializer.class, 
formatter.keyDeserializer().get());
+        MockDeserializer keyDeserializer = (MockDeserializer) 
formatter.keyDeserializer().get();
+        assertEquals(1, keyDeserializer.configs.size());
+        assertEquals("abc", keyDeserializer.configs.get("my-props"));
+        assertTrue(keyDeserializer.isKey);
+    }
+
     @Test
     public void shouldParseGroupIdFromBeginningGivenTogether() throws 
IOException {
         // Start from earliest
diff --git 
a/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleShareConsumerOptionsTest.java
 
b/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleShareConsumerOptionsTest.java
index fecf53dbbec..a097a9bf536 100644
--- 
a/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleShareConsumerOptionsTest.java
+++ 
b/tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleShareConsumerOptionsTest.java
@@ -222,7 +222,7 @@ public class ConsoleShareConsumerOptionsTest {
     }
 
     @Test
-    public void testCustomPropertyShouldBePassedToConfigureMethod() throws 
Exception {
+    public void testCustomPropertyShouldBePassedToConfigureMethodDeprecated() 
throws Exception {
         String[] args = new String[]{
             "--bootstrap-server", "localhost:9092",
             "--topic", "test",
@@ -245,7 +245,30 @@ public class ConsoleShareConsumerOptionsTest {
     }
 
     @Test
-    public void testCustomConfigShouldBePassedToConfigureMethod() throws 
Exception {
+    public void testCustomPropertyShouldBePassedToConfigureMethod() throws 
Exception {
+        String[] args = new String[]{
+            "--bootstrap-server", "localhost:9092",
+            "--topic", "test",
+            "--formatter-property", "print.key=true",
+            "--formatter-property", 
"key.deserializer=org.apache.kafka.test.MockDeserializer",
+            "--formatter-property", "key.deserializer.my-props=abc"
+        };
+
+        ConsoleShareConsumerOptions config = new 
ConsoleShareConsumerOptions(args);
+
+        assertInstanceOf(DefaultMessageFormatter.class, config.formatter());
+        
assertTrue(config.formatterArgs().containsKey("key.deserializer.my-props"));
+        DefaultMessageFormatter formatter = (DefaultMessageFormatter) 
config.formatter();
+        assertTrue(formatter.keyDeserializer().isPresent());
+        assertInstanceOf(MockDeserializer.class, 
formatter.keyDeserializer().get());
+        MockDeserializer keyDeserializer = (MockDeserializer) 
formatter.keyDeserializer().get();
+        assertEquals(1, keyDeserializer.configs.size());
+        assertEquals("abc", keyDeserializer.configs.get("my-props"));
+        assertTrue(keyDeserializer.isKey);
+    }
+
+    @Test
+    public void testCustomConfigShouldBePassedToConfigureMethodDeprecated() 
throws Exception {
         Map<String, String> configs = new HashMap<>();
         configs.put("key.deserializer.my-props", "abc");
         configs.put("print.key", "false");
@@ -271,6 +294,33 @@ public class ConsoleShareConsumerOptionsTest {
         assertTrue(keyDeserializer.isKey);
     }
 
+    @Test
+    public void testCustomConfigShouldBePassedToConfigureMethod() throws 
Exception {
+        Map<String, String> configs = new HashMap<>();
+        configs.put("key.deserializer.my-props", "abc");
+        configs.put("print.key", "false");
+        File propsFile = ToolsTestUtils.tempPropertiesFile(configs);
+        String[] args = new String[]{
+            "--bootstrap-server", "localhost:9092",
+            "--topic", "test",
+            "--formatter-property", "print.key=true",
+            "--formatter-property", 
"key.deserializer=org.apache.kafka.test.MockDeserializer",
+            "--formatter-config", propsFile.getAbsolutePath()
+        };
+
+        ConsoleShareConsumerOptions config = new 
ConsoleShareConsumerOptions(args);
+
+        assertInstanceOf(DefaultMessageFormatter.class, config.formatter());
+        
assertTrue(config.formatterArgs().containsKey("key.deserializer.my-props"));
+        DefaultMessageFormatter formatter = (DefaultMessageFormatter) 
config.formatter();
+        assertTrue(formatter.keyDeserializer().isPresent());
+        assertInstanceOf(MockDeserializer.class, 
formatter.keyDeserializer().get());
+        MockDeserializer keyDeserializer = (MockDeserializer) 
formatter.keyDeserializer().get();
+        assertEquals(1, keyDeserializer.configs.size());
+        assertEquals("abc", keyDeserializer.configs.get("my-props"));
+        assertTrue(keyDeserializer.isKey);
+    }
+
     @Test
     public void testDefaultClientId() throws IOException {
         String[] args = new String[]{

Reply via email to