-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AVRO-4163: [java] add java 17 test module. #3424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| 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. | ||
| --> | ||
| <project | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
| xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <artifactId>avro-parent</artifactId> | ||
| <groupId>org.apache.avro</groupId> | ||
| <version>1.13.0-SNAPSHOT</version> | ||
| <relativePath>../</relativePath> | ||
| </parent> | ||
|
|
||
| <artifactId>java17-test</artifactId> | ||
| <name>Avro Java 17 Tests</name> | ||
| <description>Unit tests that require java 17 language support.</description> | ||
| <url>https://avro.apache.org/</url> | ||
|
|
||
| <properties> | ||
| <maven.compiler.target>17</maven.compiler.target> | ||
| <maven.compiler.source>17</maven.compiler.source> | ||
| <maven.compiler.release>17</maven.compiler.release> | ||
| <main.basedir>${project.parent.parent.basedir}</main.basedir> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>${project.groupId}</groupId> | ||
| <artifactId>avro</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-deploy-plugin</artifactId> | ||
| <configuration> | ||
| <skip>true</skip> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> |
108 changes: 108 additions & 0 deletions
108
lang/java/java17-test/src/test/java/org/apache/avro/reflect/TestJavaRecords.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<R1> { | ||
|
|
||
| { | ||
|
|
||
| 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> T readWrite(T object) throws IOException { | ||
| var schema = ReflectData.get().getSchema(object.getClass()); | ||
| ReflectDatumWriter<T> writer = new ReflectDatumWriter<>(schema); | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| writer.write(object, factory.directBinaryEncoder(out, null)); | ||
| ReflectDatumReader<T> reader = new ReflectDatumReader<>(schema); | ||
| return reader.read(null, DecoderFactory.get().binaryDecoder(out.toByteArray(), null)); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this class needed ?
Isn't it possible to put
@AvroEncode(using = R1Encoding.class)directly on therecordclass itself ?Disclaimer: I haven't used Java (in general, not only the Avro Java SDK) for several years now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see it in your other PR - https://github.com/apache/avro/pull/3425/files#diff-6f4c55d40c67ca00b539c805c113311a15f5bf8c14a85b56e58d1c8f57392a89R33