Skip to content

Instantly share code, notes, and snippets.

@cykl
Last active July 18, 2016 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cykl/bf8936fca0410d0cea61402bf5b1e5f9 to your computer and use it in GitHub Desktop.
Save cykl/bf8936fca0410d0cea61402bf5b1e5f9 to your computer and use it in GitHub Desktop.
Does Avro Reflect need a public no-arg ctor ?
import java.io.File;
import java.io.IOException;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.reflect.ReflectData;
import org.apache.avro.reflect.ReflectDatumReader;
import org.apache.avro.reflect.ReflectDatumWriter;
import org.assertj.core.api.WithAssertions;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class AvroReflectVsPrivateCtorTest implements WithAssertions {
@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();
@Test
public void should_ctor_be_public() throws IOException {
File file = new File(tmpFolder.getRoot(), "avro");
DatumWriter<Pojo> datumWriter = new ReflectDatumWriter<>(Pojo.class);
try (DataFileWriter<Pojo> writer = new DataFileWriter<>(datumWriter)) {
writer.create(ReflectData.get().getSchema(Pojo.class), file);
writer.append(new Pojo("John"));
}
DatumReader<Pojo> datumReader = new ReflectDatumReader<>(Pojo.class);
try (DataFileReader<Pojo> reader = new DataFileReader<>(file, datumReader)) {
Pojo pojo = reader.next();
assertThat(pojo.getName()).isEqualTo("John");
}
}
}
public class Pojo {
private String name;
private Pojo() {
// Mandatory no-arg ctor
}
public Pojo(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment