|
| 1 | +package com.fasterxml.jackson.dataformat.avro; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.core.*; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.*; |
| 10 | + |
| 11 | +public class ArrayTest extends AvroTestBase |
| 12 | +{ |
| 13 | + private final AvroMapper MAPPER = getMapper(); |
| 14 | + |
| 15 | + // Simple test for a single array |
| 16 | + public void testRootStringArray() throws Exception |
| 17 | + { |
| 18 | + AvroSchema schema = getStringArraySchema(); |
| 19 | + List<String> input = Arrays.asList("foo", "bar"); |
| 20 | + |
| 21 | + byte[] b = MAPPER.writer(schema).writeValueAsBytes(input); |
| 22 | + |
| 23 | + // writing's good (probably), let's read. First as List: |
| 24 | + List<String> result = MAPPER.readerFor(List.class) |
| 25 | + .with(schema) |
| 26 | + .readValue(b); |
| 27 | + assertNotNull(result); |
| 28 | + assertEquals(2, result.size()); |
| 29 | + assertEquals(input.get(0), result.get(0)); |
| 30 | + assertEquals(input.get(1), result.get(1)); |
| 31 | + |
| 32 | + // then as String array |
| 33 | + String[] array = MAPPER.readerFor(String[].class) |
| 34 | + .with(schema) |
| 35 | + .readValue(b); |
| 36 | + assertNotNull(array); |
| 37 | + assertEquals(2, array.length); |
| 38 | + assertEquals(input.get(0), array[0]); |
| 39 | + assertEquals(input.get(1), array[1]); |
| 40 | + } |
| 41 | + |
| 42 | + // And more complex: sequence of (String) arrays |
| 43 | + public void testStringArraySequence() throws Exception |
| 44 | + { |
| 45 | + AvroSchema schema = getStringArraySchema(); |
| 46 | + List<String> input1 = Arrays.asList("foo", "bar"); |
| 47 | + List<String> input2 = Arrays.asList("foobar"); |
| 48 | + String[] input3 = new String[] { "a", "b", "c"}; |
| 49 | + |
| 50 | + // First: write a sequence of 3 root-level Employee Objects |
| 51 | + |
| 52 | + ByteArrayOutputStream b = new ByteArrayOutputStream(); |
| 53 | + SequenceWriter sw = MAPPER.writer(schema) |
| 54 | + .writeValues(b); |
| 55 | + sw.write(input1); |
| 56 | + int curr = b.size(); |
| 57 | + sw.write(input2); |
| 58 | + int diff = b.size() - curr; |
| 59 | + if (diff == 0) { |
| 60 | + fail("Should have output more bytes for second entry, did not, total: "+curr); |
| 61 | + } |
| 62 | + sw.write(input3); |
| 63 | + sw.close(); |
| 64 | + |
| 65 | + // 18-Jan-2017, tatu: This get bit tricky just because `readValues()` doesn't |
| 66 | + // quite know whether to advance cursor to START_ARRAY or not, and we must |
| 67 | + // instead prepare things... and use direct bind |
| 68 | + |
| 69 | + JsonParser p = MAPPER.getFactory().createParser(b.toByteArray()); |
| 70 | + p.setSchema(schema); |
| 71 | + |
| 72 | + assertToken(JsonToken.START_ARRAY, p.nextToken()); |
| 73 | + |
| 74 | + List<?> result1 = MAPPER.readValue(p, List.class); |
| 75 | + _compare(input1, result1); |
| 76 | + |
| 77 | + assertToken(JsonToken.START_ARRAY, p.nextToken()); |
| 78 | + List<?> result2 = MAPPER.readValue(p, List.class); |
| 79 | + _compare(input2, result2); |
| 80 | + |
| 81 | + assertToken(JsonToken.START_ARRAY, p.nextToken()); |
| 82 | + List<?> result3 = MAPPER.readValue(p, List.class); |
| 83 | + _compare(Arrays.asList(input3), result3); |
| 84 | + |
| 85 | + assertNull(p.nextToken()); |
| 86 | + p.close(); |
| 87 | + } |
| 88 | + |
| 89 | + // And the ultimate case of sequence of arrays of records |
| 90 | + public void testEmployeeArraySequence() throws Exception |
| 91 | + { |
| 92 | + AvroSchema schema = MAPPER.schemaFrom(EMPLOYEE_ARRAY_SCHEMA_JSON); |
| 93 | + |
| 94 | + Employee boss = new Employee( "Bossman", 55, new String[] { "[email protected]" }, null); |
| 95 | + Employee peon1 = new Employee( "Worker#1", 24, new String[] { "[email protected]" }, boss); |
| 96 | + Employee peon2 = new Employee( "Worker#2", 43, new String[] { "[email protected]" }, boss); |
| 97 | + |
| 98 | + // First: write a sequence of 3 root-level Employee Objects |
| 99 | + |
| 100 | + ByteArrayOutputStream b = new ByteArrayOutputStream(); |
| 101 | + SequenceWriter sw = MAPPER.writer(schema) |
| 102 | + .writeValues(b); |
| 103 | + sw.write(new Employee[] { boss, peon1, peon2 }); |
| 104 | + int curr = b.size(); |
| 105 | + sw.write(new Employee[] { peon2, boss }); |
| 106 | + int diff = b.size() - curr; |
| 107 | + if (diff == 0) { |
| 108 | + fail("Should have output more bytes for second entry, did not, total: "+curr); |
| 109 | + } |
| 110 | + sw.close(); |
| 111 | + |
| 112 | + // 18-Jan-2017, tatu: This get bit tricky just because `readValues()` doesn't |
| 113 | + // quite know whether to advance cursor to START_ARRAY or not, and we must |
| 114 | + // instead prepare things... and use direct bind |
| 115 | + |
| 116 | + JsonParser p = MAPPER.getFactory().createParser(b.toByteArray()); |
| 117 | + p.setSchema(schema); |
| 118 | + |
| 119 | + assertToken(JsonToken.START_ARRAY, p.nextToken()); |
| 120 | + |
| 121 | + Employee[] result1 = MAPPER.readValue(p, Employee[].class); |
| 122 | + assertEquals(3, result1.length); |
| 123 | + assertEquals("Bossman", result1[0].name); |
| 124 | + assertEquals("Worker#2", result1[2].name); |
| 125 | + |
| 126 | + assertToken(JsonToken.START_ARRAY, p.nextToken()); |
| 127 | + Employee[] result2 = MAPPER.readValue(p, Employee[].class); |
| 128 | + assertEquals(2, result2.length); |
| 129 | + assertEquals("Bossman", result2[1].name); |
| 130 | + |
| 131 | + assertNull(p.nextToken()); |
| 132 | + p.close(); |
| 133 | + } |
| 134 | + |
| 135 | + private void _compare(List<String> input, List<?> result) { |
| 136 | + assertEquals(input, result); |
| 137 | + } |
| 138 | +} |
0 commit comments