File tree Expand file tree Collapse file tree 3 files changed +15
-11
lines changed
main/java/com/baeldung/avro
test/java/com/baeldung/avro Expand file tree Collapse file tree 3 files changed +15
-11
lines changed Original file line number Diff line number Diff line change 11package com .baeldung .avro ;
22
33import generated .avro .Car ;
4+
45import org .apache .avro .file .DataFileReader ;
56import org .apache .avro .file .DataFileWriter ;
67import org .apache .avro .io .DatumReader ;
1314
1415public class SerializationDeserializationLogic {
1516
16- static void serializeCar (Car car ) {
17+ static void serializeCar (Car car ) throws IOException {
1718
1819 DatumWriter <Car > userDatumWriter = new SpecificDatumWriter (Car .class );
1920
2021 try (DataFileWriter <Car > dataFileWriter = new DataFileWriter (userDatumWriter )) {
2122 dataFileWriter .create (car .getSchema (), new File ("cars.avro" ));
2223 dataFileWriter .append (car );
2324 } catch (IOException e ) {
24- e . printStackTrace () ;
25+ throw e ;
2526 }
2627 }
2728
28- static Car deserializeCar () {
29+ static Car deserializeCar () throws IOException {
2930
30- Car resultCar = null ;
3131 DatumReader <Car > userDatumReader = new SpecificDatumReader (Car .class );
3232
33- try (DataFileReader <Car > dataFileReader = new DataFileReader (new File ("cars.avro" ), userDatumReader )){
33+ try (DataFileReader <Car > dataFileReader = new DataFileReader (new File ("cars.avro" ), userDatumReader )) {
3434 return dataFileReader .next ();
3535 } catch (Exception e ) {
36- e . printStackTrace () ;
36+ throw e ;
3737 }
38- return resultCar ;
3938 }
4039}
Original file line number Diff line number Diff line change 1212 "default" : 4
1313 },
1414 { "name" : " color" ,
15- "type" : " string"
15+ "type" : [" null" , " string" ],
16+ "default" : null
1617 }
1718 ]
1819}
Original file line number Diff line number Diff line change 11package com .baeldung .avro ;
22
33import generated .avro .Car ;
4+
45import org .junit .jupiter .api .Test ;
56
7+ import java .io .IOException ;
8+
69import static org .junit .jupiter .api .Assertions .*;
710
811public class AvroDefaultValuesUnitTest {
912
1013 @ Test
11- public void givenCarJsonSchema_whenCarIsSerialized_thenCarIsSuccessfullyDeserialized () {
14+ public void givenCarJsonSchema_whenCarIsSerialized_thenCarIsSuccessfullyDeserialized () throws IOException {
1215
13- Car car = Car .newBuilder ().setColor ("blue" ).build ();
16+ Car car = Car .newBuilder ()
17+ .build ();
1418
1519 SerializationDeserializationLogic .serializeCar (car );
1620 Car deserializedCar = SerializationDeserializationLogic .deserializeCar ();
1721
1822 assertEquals ("Dacia" , deserializedCar .getBrand ());
1923 assertEquals (4 , deserializedCar .getNumberOfDoors ());
20- assertEquals ( "blue" , deserializedCar .getColor ());
24+ assertNull ( deserializedCar .getColor ());
2125 }
2226}
You can’t perform that action at this time.
0 commit comments