Skip to content

Commit b66ab0f

Browse files
committed
minor clean up wrt #16
1 parent 8465329 commit b66ab0f

File tree

3 files changed

+51
-33
lines changed

3 files changed

+51
-33
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroGenerator.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ public enum Feature
5353

5454
/**
5555
* Feature that tells Avro to write data in file format (i.e. including the schema with the data)
56-
* rather than the RPC format
56+
* rather than the RPC format which is otherwise default
57+
*<p>
58+
* NOTE: reader-side will have to be aware of distinction as well, since possible inclusion
59+
* of this header is not 100% reliably auto-detectable (while header has distinct marker,
60+
* "raw" Avro content has no limitations and could theoretically have same pre-amble from data).
61+
*
62+
* @since 2.9
5763
*/
5864
AVRO_FILE_OUTPUT(false)
5965
;
@@ -606,6 +612,8 @@ protected void _complete() throws IOException
606612
// do not want to hide the original problem...
607613
// First one sanity check, for a (relatively?) common case
608614
if (_rootContext != null) {
615+
// 21-Feb-2017, tatu: As per [dataformats-binary#15], need to ensure schema gets
616+
// written, if using "File" format (not raw "rpc" one)
609617
if (isEnabled(Feature.AVRO_FILE_OUTPUT)) {
610618
_rootContext.complete(_output);
611619
} else {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fasterxml.jackson.dataformat.avro;
2+
3+
import org.apache.avro.file.DataFileReader;
4+
import org.apache.avro.file.SeekableByteArrayInput;
5+
import org.apache.avro.generic.GenericDatumReader;
6+
import org.apache.avro.generic.GenericRecord;
7+
import org.apache.avro.io.DatumReader;
8+
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
11+
// for [dataformats-binary#16]
12+
public class FileFormatTest extends AvroTestBase
13+
{
14+
public void testFileFormatOutput() throws Exception
15+
{
16+
Employee empl = new Employee();
17+
empl.name = "Bobbee";
18+
empl.age = 39;
19+
empl.emails = new String[] { "bob@aol.com", "bobby@gmail.com" };
20+
empl.boss = null;
21+
22+
AvroFactory af = new AvroFactory();
23+
ObjectMapper mapper = new ObjectMapper(af);
24+
25+
af.enable(AvroGenerator.Feature.AVRO_FILE_OUTPUT);
26+
27+
AvroSchema schema = getEmployeeSchema();
28+
byte[] bytes = mapper.writer(schema).writeValueAsBytes(empl);
29+
30+
assertNotNull(bytes);
31+
assertEquals(301, bytes.length);
32+
33+
DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(schema.getAvroSchema());
34+
@SuppressWarnings("resource")
35+
DataFileReader<GenericRecord> dataFileReader = new DataFileReader<GenericRecord>(new SeekableByteArrayInput(bytes),
36+
datumReader);
37+
GenericRecord output = dataFileReader.next();
38+
39+
assertNotNull(output);
40+
assertEquals(output.get("name").toString(), empl.name);
41+
}
42+
}

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/SimpleGenerationTest.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
55

6-
import org.apache.avro.file.DataFileReader;
7-
import org.apache.avro.file.SeekableByteArrayInput;
8-
import org.apache.avro.generic.GenericDatumReader;
9-
import org.apache.avro.generic.GenericRecord;
10-
import org.apache.avro.io.DatumReader;
116
import org.junit.Assert;
127

138
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@@ -165,31 +160,4 @@ public void testIgnoringOfUnknownObject() throws Exception
165160
BinaryAndNumber output = mapper.reader(SCHEMA_WITH_BINARY_JSON).forType(BinaryAndNumber.class).readValue(bytes);
166161
assertEquals("Bob", output.name);
167162
}
168-
169-
public void testFileOutput() throws Exception
170-
{
171-
Employee empl = new Employee();
172-
empl.name = "Bobbee";
173-
empl.age = 39;
174-
empl.emails = new String[] { "bob@aol.com", "bobby@gmail.com" };
175-
empl.boss = null;
176-
177-
AvroFactory af = new AvroFactory();
178-
ObjectMapper mapper = new ObjectMapper(af);
179-
180-
af.enable(AvroGenerator.Feature.AVRO_FILE_OUTPUT);
181-
182-
AvroSchema schema = getEmployeeSchema();
183-
byte[] bytes = mapper.writer(schema).writeValueAsBytes(empl);
184-
185-
assertNotNull(bytes);
186-
assertEquals(301, bytes.length);
187-
188-
DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(schema.getAvroSchema());
189-
DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(new SeekableByteArrayInput(bytes), datumReader);
190-
GenericRecord output = dataFileReader.next();
191-
192-
assertNotNull(output);
193-
assertEquals(output.get("name").toString(), empl.name);
194-
}
195163
}

0 commit comments

Comments
 (0)