|
| 1 | +package com.fasterxml.jackson.dataformat.avro; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | + |
| 7 | +public class JDKSerializabilityTest extends AvroTestBase |
| 8 | +{ |
| 9 | + public void testNativeMapperWithModule() throws Exception { |
| 10 | + _testModule(newMapper()); |
| 11 | + } |
| 12 | + |
| 13 | + public void testApacheMapperWithModule() throws Exception { |
| 14 | + _testModule(newApacheMapper()); |
| 15 | + } |
| 16 | + |
| 17 | + private void _testModule(ObjectMapper mapper) throws Exception |
| 18 | + { |
| 19 | + // very simple validation: should still work wrt serialization |
| 20 | + ObjectMapper unfrozenMapper = serializeAndDeserialize(mapper); |
| 21 | + |
| 22 | + // and then simple verification that write+read still works |
| 23 | + |
| 24 | + Employee empl = _simpleEmployee(); |
| 25 | + byte[] avro = toAvro(empl, unfrozenMapper); |
| 26 | + Employee result = unfrozenMapper.readerFor(Employee.class) |
| 27 | + .with(getEmployeeSchema()) |
| 28 | + .readValue(avro); |
| 29 | + assertEquals(empl.name, result.name); |
| 30 | + } |
| 31 | + |
| 32 | + private ObjectMapper serializeAndDeserialize(ObjectMapper mapper) throws Exception { |
| 33 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 34 | + ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream); |
| 35 | + |
| 36 | + outputStream.writeObject(mapper); |
| 37 | + byte[] serializedBytes = byteArrayOutputStream.toByteArray(); |
| 38 | + |
| 39 | + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serializedBytes); |
| 40 | + ObjectInputStream inputStream = new ObjectInputStream(byteArrayInputStream); |
| 41 | + Object deserializedObject = inputStream.readObject(); |
| 42 | + assertTrue("Deserialized object should be an instance of ObjectMapper", |
| 43 | + deserializedObject instanceof ObjectMapper); |
| 44 | + return (ObjectMapper) deserializedObject; |
| 45 | + } |
| 46 | + |
| 47 | + private Employee _simpleEmployee() { |
| 48 | + Employee boss = new Employee(); |
| 49 | + boss.name = "Pointy"; |
| 50 | + boss.age = 25; |
| 51 | + boss. emails = new String[] { "[email protected]" }; |
| 52 | + boss.boss = null; |
| 53 | + |
| 54 | + Employee empl = new Employee(); |
| 55 | + empl.name = "Bob"; |
| 56 | + empl.age = 39; |
| 57 | + empl. emails = new String[] { "[email protected]", "[email protected]" }; |
| 58 | + |
| 59 | + // NOTE: currently problematic (gives us a union...) |
| 60 | + empl.boss = boss; |
| 61 | + return empl; |
| 62 | + } |
| 63 | +} |
0 commit comments