From 721c294250b1b66d7088b797538f35743185740d Mon Sep 17 00:00:00 2001 From: ashley-taylor Date: Thu, 3 Jul 2025 15:58:25 +1200 Subject: [PATCH 1/3] AVRO-4163: add java 17 test module. --- lang/java/java17-test/pom.xml | 68 +++++++++++ .../apache/avro/reflect/TestJavaRecords.java | 108 ++++++++++++++++++ lang/java/pom.xml | 3 + 3 files changed, 179 insertions(+) create mode 100644 lang/java/java17-test/pom.xml create mode 100644 lang/java/java17-test/src/test/java/org/apache/avro/reflect/TestJavaRecords.java diff --git a/lang/java/java17-test/pom.xml b/lang/java/java17-test/pom.xml new file mode 100644 index 00000000000..c8dc0dbc7cf --- /dev/null +++ b/lang/java/java17-test/pom.xml @@ -0,0 +1,68 @@ + + + + 4.0.0 + + + avro-parent + org.apache.avro + 1.13.0-SNAPSHOT + ../ + + + java17-test + Avro Java 17 Tests + Unit tests that require java 17 language support. + https://avro.apache.org/ + + + 17 + 17 + 17 + ${project.parent.parent.basedir} + + + + + ${project.groupId} + avro + ${project.version} + + + + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + org.apache.maven.plugins + maven-compiler-plugin> + + + org.apache.maven.plugins + maven-surefire-plugin + + + + + \ No newline at end of file diff --git a/lang/java/java17-test/src/test/java/org/apache/avro/reflect/TestJavaRecords.java b/lang/java/java17-test/src/test/java/org/apache/avro/reflect/TestJavaRecords.java new file mode 100644 index 00000000000..4b10dba8d1b --- /dev/null +++ b/lang/java/java17-test/src/test/java/org/apache/avro/reflect/TestJavaRecords.java @@ -0,0 +1,108 @@ +/* + * 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 + * + * https://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.avro.reflect; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Arrays; + +import org.apache.avro.AvroTypeException; +import org.apache.avro.Schema; +import org.apache.avro.io.Decoder; +import org.apache.avro.io.DecoderFactory; +import org.apache.avro.io.Encoder; +import org.apache.avro.io.EncoderFactory; +import org.junit.jupiter.api.Test; + +public class TestJavaRecords { + + EncoderFactory factory = new EncoderFactory(); + + @Test + void testRecordWithEncoder() throws IOException { + + var wrapper = new Wrapper(new R1("test")); + + var read = readWrite(wrapper); + + assertEquals("test", wrapper.getR1().value()); + assertEquals("test used this", read.getR1().value()); + } + + public static class Wrapper { + + @AvroEncode(using = R1Encoding.class) + private R1 r1; + + public Wrapper() { + } + + public Wrapper(R1 r1) { + this.r1 = r1; + } + + public R1 getR1() { + return r1; + } + + public void setR1(R1 r1) { + this.r1 = r1; + } + + } + + public static record R1(String value) { + } + + public static class R1Encoding extends CustomEncoding { + + { + + schema = Schema.createRecord("R1", null, null, false, + Arrays.asList(new Schema.Field("value", Schema.create(Schema.Type.STRING), null, null))); + } + + @Override + protected void write(Object datum, Encoder out) throws IOException { + if (datum instanceof R1 r1) { + out.writeString(r1.value()); + + } else { + throw new AvroTypeException("Expected R1, got " + datum.getClass()); + } + + } + + @Override + protected R1 read(Object reuse, Decoder in) throws IOException { + return new R1(in.readString() + " used this"); + } + } + + T readWrite(T object) throws IOException { + var schema = ReflectData.get().getSchema(object.getClass()); + ReflectDatumWriter writer = new ReflectDatumWriter<>(schema); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + writer.write(object, factory.directBinaryEncoder(out, null)); + ReflectDatumReader reader = new ReflectDatumReader<>(schema); + return reader.read(null, DecoderFactory.get().binaryDecoder(out.toByteArray(), null)); + } + +} diff --git a/lang/java/pom.xml b/lang/java/pom.xml index 46f659ea9b1..4e889ec96e0 100644 --- a/lang/java/pom.xml +++ b/lang/java/pom.xml @@ -91,6 +91,7 @@ integration-test perf interop-data-test + java17-test @@ -393,6 +394,7 @@ com.diffplug.spotless spotless-maven-plugin + [0,) check @@ -405,6 +407,7 @@ org.apache.maven.plugins maven-plugin-plugin + [0,) helpmojo descriptor From 357a1bc5d1b511364e76ef0742ded9539cb37eb8 Mon Sep 17 00:00:00 2001 From: ashley-taylor Date: Fri, 4 Jul 2025 22:31:46 +1200 Subject: [PATCH 2/3] mis click --- lang/java/java17-test/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/java/java17-test/pom.xml b/lang/java/java17-test/pom.xml index c8dc0dbc7cf..d494dd16faa 100644 --- a/lang/java/java17-test/pom.xml +++ b/lang/java/java17-test/pom.xml @@ -56,7 +56,7 @@ org.apache.maven.plugins - maven-compiler-plugin> + maven-compiler-plugin org.apache.maven.plugins From 4f9ed113e2fe6265621c393e8436aaf6162095d6 Mon Sep 17 00:00:00 2001 From: Ashley Taylor <7232476+ashley-taylor@users.noreply.github.com> Date: Fri, 11 Jul 2025 13:32:41 +1200 Subject: [PATCH 3/3] remove changes from pom.xml PR feedback --- lang/java/pom.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/lang/java/pom.xml b/lang/java/pom.xml index 4e889ec96e0..c0274a3c44d 100644 --- a/lang/java/pom.xml +++ b/lang/java/pom.xml @@ -394,7 +394,6 @@ com.diffplug.spotless spotless-maven-plugin - [0,) check @@ -407,7 +406,6 @@ org.apache.maven.plugins maven-plugin-plugin - [0,) helpmojo descriptor