|
| 1 | +package com.fasterxml.jackson.dataformat.protobuf.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
| 4 | +import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper; |
| 5 | +import com.fasterxml.jackson.dataformat.protobuf.ProtobufTestBase; |
| 6 | +import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema; |
| 7 | + |
| 8 | +public class NestedWrite67Test extends ProtobufTestBase |
| 9 | +{ |
| 10 | + @JsonPropertyOrder({ "value", "level2" }) |
| 11 | + public static class Level1 { |
| 12 | + public int value; |
| 13 | + public Level2 level2; |
| 14 | + } |
| 15 | + |
| 16 | + @JsonPropertyOrder({ "value", "level3s" }) |
| 17 | + public static class Level2 { |
| 18 | + public int value; |
| 19 | + |
| 20 | + public Level3[] level3s; |
| 21 | + } |
| 22 | + |
| 23 | + public static class Level3 { |
| 24 | + public int value; |
| 25 | + } |
| 26 | + |
| 27 | + final ProtobufMapper MAPPER = new ProtobufMapper(); |
| 28 | + |
| 29 | + /* |
| 30 | + /********************************************************** |
| 31 | + /* Test methods |
| 32 | + /********************************************************** |
| 33 | + */ |
| 34 | + |
| 35 | + public void testIssue67() throws Exception |
| 36 | + { |
| 37 | + ProtobufSchema schema = MAPPER.generateSchemaFor(Level1.class); |
| 38 | + |
| 39 | +// System.out.println(schema.getSource()); |
| 40 | + |
| 41 | + Level1 level1 = new Level1(); |
| 42 | + Level2 level2 = new Level2(); |
| 43 | + Level3 level3a = new Level3(); |
| 44 | + Level3 level3b = new Level3(); |
| 45 | + |
| 46 | + level1.value = 1; |
| 47 | + level2.value = 2; |
| 48 | + level3a.value = 3; |
| 49 | + level3b.value = 4; |
| 50 | + Level3[] level3s = new Level3[] { level3a, level3b }; |
| 51 | + |
| 52 | + level1.level2 = level2; |
| 53 | + level2.level3s = level3s; |
| 54 | + |
| 55 | + byte[] bytes = MAPPER.writer(schema).writeValueAsBytes(level1); |
| 56 | + |
| 57 | + showBytes(bytes); |
| 58 | + |
| 59 | + Level1 gotLevel1 = MAPPER.readerFor(Level1.class).with(schema).readValue(bytes); |
| 60 | + |
| 61 | +// byte[] correct = new byte[]{0x08, 0x01, 0x12, 0x0a, 0x08, 0x02, 0x12, 0x02, 0x08, 0x03, 0x12, 0x02, 0x08, 0x04}; |
| 62 | +// Level1 gotLevel1 = mapper.readerFor(Level1.class).with(schema).readValue(new ByteArrayInputStream(correct)); |
| 63 | + |
| 64 | + assertEquals(level1.value, gotLevel1.value); |
| 65 | + assertEquals(level2.value, gotLevel1.level2.value); |
| 66 | + assertEquals(level3s.length, gotLevel1.level2.level3s.length); |
| 67 | + assertEquals(level3a.value, gotLevel1.level2.level3s[0].value); |
| 68 | + assertEquals(level3b.value, gotLevel1.level2.level3s[1].value); |
| 69 | + } |
| 70 | + |
| 71 | + private void showBytes(byte[] bytes) { |
| 72 | + for (byte b : bytes) { |
| 73 | + System.out.print(String.format("%8s", Integer.toHexString(b)).substring(6, 8).replaceAll(" ", "0") + " "); |
| 74 | + } |
| 75 | + System.out.println(); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments