Skip to content

Commit 59502f4

Browse files
ashley-tayloropwvhk
authored andcommitted
AVRO-4163: [java] add java 17 test module. (apache#3424)
* AVRO-4163: add java 17 test module. * mis click * remove changes from pom.xml PR feedback
1 parent 63a1d86 commit 59502f4

3 files changed

Lines changed: 177 additions & 0 deletions

File tree

lang/java/java17-test/pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project
17+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
18+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
19+
<modelVersion>4.0.0</modelVersion>
20+
21+
<parent>
22+
<artifactId>avro-parent</artifactId>
23+
<groupId>org.apache.avro</groupId>
24+
<version>1.12.1</version>
25+
<relativePath>../</relativePath>
26+
</parent>
27+
28+
<artifactId>java17-test</artifactId>
29+
<name>Avro Java 17 Tests</name>
30+
<description>Unit tests that require java 17 language support.</description>
31+
<url>https://avro.apache.org/</url>
32+
33+
<properties>
34+
<maven.compiler.target>17</maven.compiler.target>
35+
<maven.compiler.source>17</maven.compiler.source>
36+
<maven.compiler.release>17</maven.compiler.release>
37+
<main.basedir>${project.parent.parent.basedir}</main.basedir>
38+
</properties>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>${project.groupId}</groupId>
43+
<artifactId>avro</artifactId>
44+
<version>${project.version}</version>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-deploy-plugin</artifactId>
53+
<configuration>
54+
<skip>true</skip>
55+
</configuration>
56+
</plugin>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-compiler-plugin</artifactId>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
</project>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.avro.reflect;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.util.Arrays;
25+
26+
import org.apache.avro.AvroTypeException;
27+
import org.apache.avro.Schema;
28+
import org.apache.avro.io.Decoder;
29+
import org.apache.avro.io.DecoderFactory;
30+
import org.apache.avro.io.Encoder;
31+
import org.apache.avro.io.EncoderFactory;
32+
import org.junit.jupiter.api.Test;
33+
34+
public class TestJavaRecords {
35+
36+
EncoderFactory factory = new EncoderFactory();
37+
38+
@Test
39+
void testRecordWithEncoder() throws IOException {
40+
41+
var wrapper = new Wrapper(new R1("test"));
42+
43+
var read = readWrite(wrapper);
44+
45+
assertEquals("test", wrapper.getR1().value());
46+
assertEquals("test used this", read.getR1().value());
47+
}
48+
49+
public static class Wrapper {
50+
51+
@AvroEncode(using = R1Encoding.class)
52+
private R1 r1;
53+
54+
public Wrapper() {
55+
}
56+
57+
public Wrapper(R1 r1) {
58+
this.r1 = r1;
59+
}
60+
61+
public R1 getR1() {
62+
return r1;
63+
}
64+
65+
public void setR1(R1 r1) {
66+
this.r1 = r1;
67+
}
68+
69+
}
70+
71+
public static record R1(String value) {
72+
}
73+
74+
public static class R1Encoding extends CustomEncoding<R1> {
75+
76+
{
77+
78+
schema = Schema.createRecord("R1", null, null, false,
79+
Arrays.asList(new Schema.Field("value", Schema.create(Schema.Type.STRING), null, null)));
80+
}
81+
82+
@Override
83+
protected void write(Object datum, Encoder out) throws IOException {
84+
if (datum instanceof R1 r1) {
85+
out.writeString(r1.value());
86+
87+
} else {
88+
throw new AvroTypeException("Expected R1, got " + datum.getClass());
89+
}
90+
91+
}
92+
93+
@Override
94+
protected R1 read(Object reuse, Decoder in) throws IOException {
95+
return new R1(in.readString() + " used this");
96+
}
97+
}
98+
99+
<T> T readWrite(T object) throws IOException {
100+
var schema = ReflectData.get().getSchema(object.getClass());
101+
ReflectDatumWriter<T> writer = new ReflectDatumWriter<>(schema);
102+
ByteArrayOutputStream out = new ByteArrayOutputStream();
103+
writer.write(object, factory.directBinaryEncoder(out, null));
104+
ReflectDatumReader<T> reader = new ReflectDatumReader<>(schema);
105+
return reader.read(null, DecoderFactory.get().binaryDecoder(out.toByteArray(), null));
106+
}
107+
108+
}

lang/java/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<module>integration-test</module>
9292
<module>perf</module>
9393
<module>interop-data-test</module>
94+
<module>java17-test</module>
9495
</modules>
9596

9697
<build>

0 commit comments

Comments
 (0)