This is an automated email from the ASF dual-hosted git repository.

iemejia pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git

commit 326950f40c1172f7564c757b0e51c39883721083
Author: Ismaël Mejía <[email protected]>
AuthorDate: Mon Aug 17 16:37:02 2026 +0200

    AVRO-4332: [java] Enable and fix the dormant IdlUtils tests
    
    The IdlUtils test class was never executed: Surefire only includes
    classes matching **/Test** (name starting with "Test"), but the class
    was named IdlUtilsTest, so its tests silently rotted since AVRO-3677.
    
    Rename IdlUtilsTest to TestIdlUtils so the suite runs, and fix the
    problems this uncovers:
    
    - byte[] values serialized to an empty string because the byte[]
      serializer discarded MAPPER.writeValueAsString(...) instead of
      writing to the generator; write the value to the generator.
    - The callToJson test helper had the same discard bug, so every
      *ToJson assertion previously compared against an empty string.
    - The happy-flow fixtures lived under org/apache/avro/util and were
      unreachable from the org.apache.avro.idl package; move them beside
      the test and regenerate them from the current writer output.
    - getMainSchema() now returns the record directly, so drop the
      obsolete union unwrapping in validateHappyFlowForSingleSchema.
    - Map/collection JSON expectations now include the ", " separator the
      MAPPER emits.
---
 .../main/java/org/apache/avro/idl/IdlUtils.java    |  2 +-
 .../idl/{IdlUtilsTest.java => TestIdlUtils.java}   | 20 ++++++-------
 .../{util => idl}/idl_utils_test_protocol.avdl     |  6 ++--
 .../org/apache/avro/idl/idl_utils_test_schema.avdl | 35 ++++++++++++++++++++++
 .../apache/avro/util/idl_utils_test_schema.avdl    | 35 ----------------------
 5 files changed, 50 insertions(+), 48 deletions(-)

diff --git a/lang/java/idl/src/main/java/org/apache/avro/idl/IdlUtils.java 
b/lang/java/idl/src/main/java/org/apache/avro/idl/IdlUtils.java
index 80499a2709..39614f5868 100644
--- a/lang/java/idl/src/main/java/org/apache/avro/idl/IdlUtils.java
+++ b/lang/java/idl/src/main/java/org/apache/avro/idl/IdlUtils.java
@@ -84,7 +84,7 @@ public final class IdlUtils {
     module.addSerializer(new StdSerializer<byte[]>(byte[].class) {
       @Override
       public void serialize(byte[] value, JsonGenerator gen, 
SerializerProvider provider) throws IOException {
-        MAPPER.writeValueAsString(new String(value, 
StandardCharsets.ISO_8859_1));
+        gen.writeString(new String(value, StandardCharsets.ISO_8859_1));
       }
     });
 
diff --git a/lang/java/idl/src/test/java/org/apache/avro/idl/IdlUtilsTest.java 
b/lang/java/idl/src/test/java/org/apache/avro/idl/TestIdlUtils.java
similarity index 93%
rename from lang/java/idl/src/test/java/org/apache/avro/idl/IdlUtilsTest.java
rename to lang/java/idl/src/test/java/org/apache/avro/idl/TestIdlUtils.java
index 7b3f2fd91d..6e026c1d54 100644
--- a/lang/java/idl/src/test/java/org/apache/avro/idl/IdlUtilsTest.java
+++ b/lang/java/idl/src/test/java/org/apache/avro/idl/TestIdlUtils.java
@@ -27,7 +27,7 @@ import java.util.Arrays;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
-import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonProcessingException;
 import org.apache.avro.AvroRuntimeException;
 import org.apache.avro.JsonProperties;
 import org.apache.avro.Protocol;
@@ -44,7 +44,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
-public class IdlUtilsTest {
+public class TestIdlUtils {
   @Test
   public void idlUtilsUtilitiesThrowRuntimeExceptionsOnProgrammerError() {
     assertThrows(IllegalStateException.class, () -> 
IdlUtils.getField(Object.class, "noSuchField"), "Programmer error");
@@ -97,7 +97,7 @@ public class IdlUtilsTest {
     Schema mainSchema = idlFile.getMainSchema();
 
     StringWriter buffer = new StringWriter();
-    IdlUtils.writeIdlSchema(buffer, mainSchema.getTypes().iterator().next());
+    IdlUtils.writeIdlSchema(buffer, mainSchema);
 
     assertEquals(getResourceAsString("idl_utils_test_schema.avdl"), 
buffer.toString());
   }
@@ -181,12 +181,12 @@ public class IdlUtilsTest {
     Map<String, Object> data = new LinkedHashMap<>();
     data.put("key", "name");
     data.put("value", 81763);
-    assertEquals("{\"key\":\"name\",\"value\":81763}", callToJson(data));
+    assertEquals("{\"key\":\"name\", \"value\":81763}", callToJson(data));
   }
 
   @Test
   public void validateCollectionToJson() throws IOException {
-    assertEquals("[123,\"abc\"]", callToJson(Arrays.asList(123, "abc")));
+    assertEquals("[123, \"abc\"]", callToJson(Arrays.asList(123, "abc")));
   }
 
   @Test
@@ -234,12 +234,12 @@ public class IdlUtilsTest {
     assertThrows(AvroRuntimeException.class, () -> callToJson(new Object()));
   }
 
-  private String callToJson(Object datum) throws IOException {
-    StringWriter buffer = new StringWriter();
-    try (JsonGenerator generator = IdlUtils.MAPPER.createGenerator(buffer)) {
-      IdlUtils.MAPPER.writeValueAsString(datum);
+  private String callToJson(Object datum) {
+    try {
+      return IdlUtils.MAPPER.writeValueAsString(datum);
+    } catch (JsonProcessingException e) {
+      throw new AvroRuntimeException(e);
     }
-    return buffer.toString();
   }
 
   private enum SingleValue {
diff --git 
a/lang/java/idl/src/test/resources/org/apache/avro/util/idl_utils_test_protocol.avdl
 
b/lang/java/idl/src/test/resources/org/apache/avro/idl/idl_utils_test_protocol.avdl
similarity index 90%
rename from 
lang/java/idl/src/test/resources/org/apache/avro/util/idl_utils_test_protocol.avdl
rename to 
lang/java/idl/src/test/resources/org/apache/avro/idl/idl_utils_test_protocol.avdl
index fa59f5b356..54b572d1f6 100644
--- 
a/lang/java/idl/src/test/resources/org/apache/avro/util/idl_utils_test_protocol.avdl
+++ 
b/lang/java/idl/src/test/resources/org/apache/avro/idl/idl_utils_test_protocol.avdl
@@ -6,7 +6,7 @@ protocol HappyFlow {
     @aliases(["naming.OldMessage"])
     record NewMessage {
         string @generator("uuid-type1") id;
-        @my-key("my-value") string? @aliases(["text","msg"]) message = null;
+        @my-key("my-value") string? @aliases(["text", "msg"]) message = null;
         @my-key("my-value") map<common.Flag> @order("DESCENDING") flags;
         Counter mainCounter;
         /** A list of counters. */
@@ -20,7 +20,9 @@ protocol HappyFlow {
     }
 
     @namespace("common")
-    enum Flag {ON, OFF, CANARY}
+    enum Flag {
+        ON, OFF, CANARY
+    }
 
     record Counter {
         string name;
diff --git 
a/lang/java/idl/src/test/resources/org/apache/avro/idl/idl_utils_test_schema.avdl
 
b/lang/java/idl/src/test/resources/org/apache/avro/idl/idl_utils_test_schema.avdl
new file mode 100644
index 0000000000..97fa466510
--- /dev/null
+++ 
b/lang/java/idl/src/test/resources/org/apache/avro/idl/idl_utils_test_schema.avdl
@@ -0,0 +1,35 @@
+namespace naming;
+
+schema NewMessage;
+
+/** A sample record type. */
+@version(2)
+@aliases(["naming.OldMessage"])
+record NewMessage {
+    string @generator("uuid-type1") id;
+    @my-key("my-value") string? @aliases(["text", "msg"]) message = null;
+    @my-key("my-value") map<common.Flag> @order("DESCENDING") flags;
+    Counter mainCounter;
+    /** A list of counters. */
+    union{null, @my-key("my-value") array<Counter>} otherCounters = null;
+    Nonce nonce;
+    date my_date;
+    time_ms my_time;
+    timestamp_ms my_timestamp;
+    decimal(12,3) my_number;
+    @logicalType("time-micros") long my_dummy;
+}
+
+@namespace("common")
+enum Flag {
+    ON, OFF, CANARY
+}
+
+record Counter {
+    string name;
+    int count;
+    /** Because the Flag field is defined earlier in NewMessage, it's already 
defined and does not need repeating below. */
+    common.Flag flag;
+}
+
+fixed Nonce(8);
diff --git 
a/lang/java/idl/src/test/resources/org/apache/avro/util/idl_utils_test_schema.avdl
 
b/lang/java/idl/src/test/resources/org/apache/avro/util/idl_utils_test_schema.avdl
deleted file mode 100644
index b500bde004..0000000000
--- 
a/lang/java/idl/src/test/resources/org/apache/avro/util/idl_utils_test_schema.avdl
+++ /dev/null
@@ -1,35 +0,0 @@
-namespace naming;
-
-schema NewMessage;
-
-/** A sample record type. */
-@version(2)
-@aliases(["naming.OldMessage"])
-record NewMessage {
-  string @generator("uuid-type1") id;
-  @my-key("my-value") string? @aliases(["text", "msg"]) message = null;
-  @my-key("my-value") map<common.Flag> @order("DESCENDING") flags;
-  Counter mainCounter;
-  /** A list of counters. */
-  union{null, @my-key("my-value") array<Counter>} otherCounters = null;
-  Nonce nonce;
-  date my_date;
-  time_ms my_time;
-  timestamp_ms my_timestamp;
-  decimal(12,3) my_number;
-  @logicalType("time-micros") long my_dummy;
-}
-
-@namespace("common")
-enum Flag {
-  ON, OFF, CANARY
-}
-
-record Counter {
-  string name;
-  int count;
-  /** Because the Flag field is defined earlier in NewMessage, it's already 
defined and does not need repeating below. */
-  common.Flag flag;
-}
-
-fixed Nonce(8);

Reply via email to