|
| 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 | +} |
0 commit comments