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 29c787c9e27..0a8b1eb419c 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 void serialize(JsonProperties.Null value, JsonGenerator gen, SerializerPr module.addSerializer(new StdSerializer(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/IdlSchemaFormatterFactoryTest.java b/lang/java/idl/src/test/java/org/apache/avro/idl/TestIdlSchemaFormatterFactory.java similarity index 85% rename from lang/java/idl/src/test/java/org/apache/avro/idl/IdlSchemaFormatterFactoryTest.java rename to lang/java/idl/src/test/java/org/apache/avro/idl/TestIdlSchemaFormatterFactory.java index 6332f4b078a..f4f195ceb0f 100644 --- a/lang/java/idl/src/test/java/org/apache/avro/idl/IdlSchemaFormatterFactoryTest.java +++ b/lang/java/idl/src/test/java/org/apache/avro/idl/TestIdlSchemaFormatterFactory.java @@ -30,20 +30,20 @@ import static java.util.Objects.requireNonNull; import static org.junit.jupiter.api.Assertions.assertEquals; -class IdlSchemaFormatterFactoryTest { +class TestIdlSchemaFormatterFactory { @Test void verifyIdlFormatting() throws IOException { SchemaFormatter idlFormatter = SchemaFormatter.getInstance("idl"); assertEquals(IdlSchemaFormatter.class, idlFormatter.getClass()); - String formattedHappyFlowSchema = getResourceAsString("../util/idl_utils_test_schema.avdl"); + String schemaResourceName = "../idl/idl_utils_test_schema.avdl"; + String formattedHappyFlowSchema = getResourceAsString(schemaResourceName); - String schemaResourceName = "../util/idl_utils_test_schema.avdl"; try (InputStream stream = getClass().getResourceAsStream(schemaResourceName)) { Schema happyFlowSchema = new SchemaParser().parse(formattedHappyFlowSchema).mainSchema(); // The Avro project indents .avdl files less than common - String formatted = idlFormatter.format(happyFlowSchema).replaceAll(" ", "\t").replaceAll("\t", " "); - assertEquals(formattedHappyFlowSchema, formatted); + String formatted = idlFormatter.format(happyFlowSchema); + assertEquals(formatted.replace("\r", ""), formattedHappyFlowSchema.replace("\r", "")); } } 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 89% 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 ef2a81d3ffd..aee544effc8 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 @@ -26,7 +26,7 @@ import java.util.LinkedHashMap; import java.util.Map; -import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JacksonException; import org.apache.avro.AvroRuntimeException; import org.apache.avro.JsonProperties; import org.apache.avro.Protocol; @@ -39,7 +39,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -public class IdlUtilsTest { +public class TestIdlUtils { @Test public void idlUtilsUtilitiesThrowRuntimeExceptionsOnProgrammerError() { assertThrows(IllegalStateException.class, () -> IdlUtils.getField(Object.class, "noSuchField"), "Programmer error"); @@ -62,7 +62,8 @@ public void validateHappyFlowForProtocol() throws IOException { StringWriter buffer = new StringWriter(); IdlUtils.writeIdlProtocol(buffer, protocol); - assertEquals(getResourceAsString("idl_utils_test_protocol.avdl"), buffer.toString()); + assertEquals(getResourceAsString("idl_utils_test_protocol.avdl").replace("\r", ""), + buffer.toString().replace("\r", "")); } private IdlFile parseIdlResource(String name) throws IOException { @@ -92,9 +93,10 @@ public void validateHappyFlowForSingleSchema() throws IOException { 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()); + assertEquals(getResourceAsString("idl_utils_test_schema.avdl").replace("\r", ""), + buffer.toString().replace("\r", "")); } @Test @@ -126,12 +128,12 @@ public void validateMapToJson() throws IOException { Map 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 @@ -174,17 +176,18 @@ public void validateBooleanToJson() throws IOException { assertEquals("true", callToJson(true)); } + @Test + public void validateByteArrayToJson() throws IOException { + assertEquals("\"123.456\"", callToJson("123.456".getBytes(StandardCharsets.ISO_8859_1))); + } + @Test public void validateUnknownCannotBeWrittenAsJson() { - assertThrows(AvroRuntimeException.class, () -> callToJson(new Object())); + assertThrows(JacksonException.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); - } - return buffer.toString(); + return IdlUtils.MAPPER.writeValueAsString(datum); } 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 fa59f5b3568..54b572d1f62 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 @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 00000000000..b653a4d3527 --- /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 @order("DESCENDING") flags; + Counter mainCounter; + /** A list of counters. */ + union{null, @my-key("my-value") array} otherCounters = null; + Nonce nonce; + date my_date; + time_ms my_time; + timestamp_ms my_timestamp; + decimal(12,3) my_number = "123.456"; + @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 b500bde004f..00000000000 --- 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 @order("DESCENDING") flags; - Counter mainCounter; - /** A list of counters. */ - union{null, @my-key("my-value") array} 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);