-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
avro/src/test/java/com/fasterxml/jackson/dataformat/avro/failing/FileSerializationTest.java
This file contains 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,66 @@ | ||
package com.fasterxml.jackson.dataformat.avro.failing; | ||
|
||
import com.fasterxml.jackson.databind.SequenceWriter; | ||
import com.fasterxml.jackson.dataformat.avro.AvroFactory; | ||
import com.fasterxml.jackson.dataformat.avro.AvroGenerator; | ||
import com.fasterxml.jackson.dataformat.avro.AvroMapper; | ||
import com.fasterxml.jackson.dataformat.avro.AvroSchema; | ||
import com.fasterxml.jackson.dataformat.avro.AvroTestBase; | ||
import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator; | ||
import org.apache.avro.file.DataFileReader; | ||
import org.apache.avro.generic.GenericDatumReader; | ||
import org.apache.avro.generic.GenericRecord; | ||
import org.apache.avro.io.DatumReader; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.nio.file.Files; | ||
|
||
public class FileSerializationTest extends AvroTestBase { | ||
public void testFileSerialization() throws Exception { | ||
final Employee employee = new Employee(); | ||
employee.name = "Bobbee"; | ||
employee.age = 39; | ||
employee.emails = new String[]{"[email protected]", "[email protected]"}; | ||
employee.boss = null; | ||
|
||
final AvroFactory avroFactory = AvroFactory.builderWithApacheDecoder().enable(AvroGenerator.Feature.AVRO_FILE_OUTPUT).build(); | ||
final AvroSchemaGenerator generator = new AvroSchemaGenerator(); | ||
|
||
final AvroMapper mapper = AvroMapper.builder(avroFactory).build(); | ||
mapper.acceptJsonFormatVisitor(Employee.class, generator); | ||
|
||
final AvroSchema generatedSchema = generator.getGeneratedSchema(); | ||
|
||
final File file = Files.createTempFile("employees", ".avro").toFile(); | ||
file.deleteOnExit(); | ||
|
||
final ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
final SequenceWriter writer = mapper.writer(generatedSchema).writeValues(out); | ||
|
||
// Write multiple entries, this seems to be what makes it invalid. | ||
writer.write(employee); | ||
writer.write(employee); | ||
writer.close(); | ||
|
||
// Write the bytes to a file | ||
try (FileOutputStream outputStream = new FileOutputStream(file)) { | ||
out.writeTo(outputStream); | ||
} | ||
|
||
final DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(generatedSchema.getAvroSchema()); | ||
|
||
@SuppressWarnings("resource") final DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(file, datumReader); | ||
|
||
GenericRecord output = dataFileReader.next(); | ||
assertNotNull(output); | ||
assertEquals(output.get("name").toString(), employee.name); | ||
|
||
// This line currently throws the following exception: | ||
// org.apache.avro.AvroRuntimeException: java.io.IOException: Invalid sync! | ||
output = dataFileReader.next(); | ||
assertNotNull(output); | ||
assertEquals(output.get("name").toString(), employee.name); | ||
} | ||
} |