-
-
Notifications
You must be signed in to change notification settings - Fork 138
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
…values (which now work)
- Loading branch information
1 parent
af1e155
commit 156d20f
Showing
5 changed files
with
216 additions
and
28 deletions.
There are no files selected for viewing
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
138 changes: 138 additions & 0 deletions
138
avro/src/test/java/com/fasterxml/jackson/dataformat/avro/ArrayTest.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,138 @@ | ||
package com.fasterxml.jackson.dataformat.avro; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.core.*; | ||
|
||
import com.fasterxml.jackson.databind.*; | ||
|
||
public class ArrayTest extends AvroTestBase | ||
{ | ||
private final AvroMapper MAPPER = getMapper(); | ||
|
||
// Simple test for a single array | ||
public void testRootStringArray() throws Exception | ||
{ | ||
AvroSchema schema = getStringArraySchema(); | ||
List<String> input = Arrays.asList("foo", "bar"); | ||
|
||
byte[] b = MAPPER.writer(schema).writeValueAsBytes(input); | ||
|
||
// writing's good (probably), let's read. First as List: | ||
List<String> result = MAPPER.readerFor(List.class) | ||
.with(schema) | ||
.readValue(b); | ||
assertNotNull(result); | ||
assertEquals(2, result.size()); | ||
assertEquals(input.get(0), result.get(0)); | ||
assertEquals(input.get(1), result.get(1)); | ||
|
||
// then as String array | ||
String[] array = MAPPER.readerFor(String[].class) | ||
.with(schema) | ||
.readValue(b); | ||
assertNotNull(array); | ||
assertEquals(2, array.length); | ||
assertEquals(input.get(0), array[0]); | ||
assertEquals(input.get(1), array[1]); | ||
} | ||
|
||
// And more complex: sequence of (String) arrays | ||
public void testStringArraySequence() throws Exception | ||
{ | ||
AvroSchema schema = getStringArraySchema(); | ||
List<String> input1 = Arrays.asList("foo", "bar"); | ||
List<String> input2 = Arrays.asList("foobar"); | ||
String[] input3 = new String[] { "a", "b", "c"}; | ||
|
||
// First: write a sequence of 3 root-level Employee Objects | ||
|
||
ByteArrayOutputStream b = new ByteArrayOutputStream(); | ||
SequenceWriter sw = MAPPER.writer(schema) | ||
.writeValues(b); | ||
sw.write(input1); | ||
int curr = b.size(); | ||
sw.write(input2); | ||
int diff = b.size() - curr; | ||
if (diff == 0) { | ||
fail("Should have output more bytes for second entry, did not, total: "+curr); | ||
} | ||
sw.write(input3); | ||
sw.close(); | ||
|
||
// 18-Jan-2017, tatu: This get bit tricky just because `readValues()` doesn't | ||
// quite know whether to advance cursor to START_ARRAY or not, and we must | ||
// instead prepare things... and use direct bind | ||
|
||
JsonParser p = MAPPER.getFactory().createParser(b.toByteArray()); | ||
p.setSchema(schema); | ||
|
||
assertToken(JsonToken.START_ARRAY, p.nextToken()); | ||
|
||
List<?> result1 = MAPPER.readValue(p, List.class); | ||
_compare(input1, result1); | ||
|
||
assertToken(JsonToken.START_ARRAY, p.nextToken()); | ||
List<?> result2 = MAPPER.readValue(p, List.class); | ||
_compare(input2, result2); | ||
|
||
assertToken(JsonToken.START_ARRAY, p.nextToken()); | ||
List<?> result3 = MAPPER.readValue(p, List.class); | ||
_compare(Arrays.asList(input3), result3); | ||
|
||
assertNull(p.nextToken()); | ||
p.close(); | ||
} | ||
|
||
// And the ultimate case of sequence of arrays of records | ||
public void testEmployeeArraySequence() throws Exception | ||
{ | ||
AvroSchema schema = MAPPER.schemaFrom(EMPLOYEE_ARRAY_SCHEMA_JSON); | ||
|
||
Employee boss = new Employee("Bossman", 55, new String[] { "[email protected]" }, null); | ||
Employee peon1 = new Employee("Worker#1", 24, new String[] { "[email protected]" }, boss); | ||
Employee peon2 = new Employee("Worker#2", 43, new String[] { "[email protected]" }, boss); | ||
|
||
// First: write a sequence of 3 root-level Employee Objects | ||
|
||
ByteArrayOutputStream b = new ByteArrayOutputStream(); | ||
SequenceWriter sw = MAPPER.writer(schema) | ||
.writeValues(b); | ||
sw.write(new Employee[] { boss, peon1, peon2 }); | ||
int curr = b.size(); | ||
sw.write(new Employee[] { peon2, boss }); | ||
int diff = b.size() - curr; | ||
if (diff == 0) { | ||
fail("Should have output more bytes for second entry, did not, total: "+curr); | ||
} | ||
sw.close(); | ||
|
||
// 18-Jan-2017, tatu: This get bit tricky just because `readValues()` doesn't | ||
// quite know whether to advance cursor to START_ARRAY or not, and we must | ||
// instead prepare things... and use direct bind | ||
|
||
JsonParser p = MAPPER.getFactory().createParser(b.toByteArray()); | ||
p.setSchema(schema); | ||
|
||
assertToken(JsonToken.START_ARRAY, p.nextToken()); | ||
|
||
Employee[] result1 = MAPPER.readValue(p, Employee[].class); | ||
assertEquals(3, result1.length); | ||
assertEquals("Bossman", result1[0].name); | ||
assertEquals("Worker#2", result1[2].name); | ||
|
||
assertToken(JsonToken.START_ARRAY, p.nextToken()); | ||
Employee[] result2 = MAPPER.readValue(p, Employee[].class); | ||
assertEquals(2, result2.length); | ||
assertEquals("Bossman", result2[1].name); | ||
|
||
assertNull(p.nextToken()); | ||
p.close(); | ||
} | ||
|
||
private void _compare(List<String> input, List<?> result) { | ||
assertEquals(input, result); | ||
} | ||
} |
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
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
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