|
2 | 2 |
|
3 | 3 | import org.junit.jupiter.api.Test; |
4 | 4 |
|
5 | | -import tools.jackson.core.StreamReadConstraints; |
6 | 5 | import tools.jackson.databind.exc.InvalidDefinitionException; |
7 | 6 |
|
8 | 7 | import tools.jackson.dataformat.avro.*; |
9 | 8 |
|
10 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
11 | 9 | import static org.junit.jupiter.api.Assertions.fail; |
12 | 10 |
|
13 | 11 | /** |
14 | | - * Simple unit tests to verify that we fail gracefully if you attempt to serialize |
15 | | - * data that is cyclic (eg a list that contains itself). |
| 12 | + * Simple unit test to verify that we fail gracefully if you attempt to serialize |
| 13 | + * data that is directly cyclic (eg a list that contains itself). |
16 | 14 | */ |
17 | 15 | public class CyclicAvroDataSerTest extends AvroTestBase |
18 | 16 | { |
19 | | - |
20 | | - public static class Bean |
| 17 | + public static class LinkedBean |
21 | 18 | { |
22 | | - Bean _next; |
| 19 | + LinkedBean _next; |
23 | 20 | final String _name; |
24 | 21 |
|
25 | | - public Bean(Bean next, String name) { |
| 22 | + public LinkedBean(LinkedBean next, String name) { |
26 | 23 | _next = next; |
27 | 24 | _name = name; |
28 | 25 | } |
29 | 26 |
|
30 | | - public Bean getNext() { return _next; } |
| 27 | + public LinkedBean getNext() { return _next; } |
31 | 28 | public String getName() { return _name; } |
32 | 29 |
|
33 | | - public void assignNext(Bean n) { _next = n; } |
| 30 | + public void assignNext(LinkedBean n) { _next = n; } |
34 | 31 | } |
35 | 32 |
|
36 | 33 | private final AvroMapper MAPPER = getMapper(); |
37 | 34 |
|
38 | | - // Unlike default depth of 1000 for other formats, use lower (400) here |
39 | | - // because we cannot actually generate 1000 levels due to Avro codec's |
40 | | - // limitations |
41 | | - private final AvroMapper MAPPER_400; |
42 | | - { |
43 | | - AvroFactory f = AvroFactory.builder() |
44 | | - .streamReadConstraints(StreamReadConstraints.builder().maxNestingDepth(400).build()) |
45 | | - .build(); |
46 | | - MAPPER_400 = new AvroMapper(f); |
47 | | - } |
48 | | - |
49 | 35 | @Test |
50 | 36 | public void testDirectCyclic() throws Exception { |
51 | | - Bean bean = new Bean(null, "123"); |
| 37 | + LinkedBean bean = new LinkedBean(null, "123"); |
52 | 38 | bean.assignNext(bean); |
53 | 39 | try { |
54 | | - AvroSchema schema = MAPPER.schemaFor(Bean.class); |
| 40 | + AvroSchema schema = MAPPER.schemaFor(LinkedBean.class); |
55 | 41 | MAPPER.writer(schema).writeValueAsBytes(bean); |
56 | 42 | fail("expected InvalidDefinitionException"); |
57 | | - } catch (InvalidDefinitionException idex) { |
58 | | - assertTrue(idex.getMessage().startsWith("Direct self-reference leading to cycle"), |
59 | | - "InvalidDefinitionException message is as expected?"); |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - // With 2.16 also test looser loops, wrt new limits |
64 | | - @Test |
65 | | - public void testLooserCyclic() throws Exception |
66 | | - { |
67 | | - Bean beanRoot = new Bean(null, "123"); |
68 | | - Bean bean2 = new Bean(beanRoot, "456"); |
69 | | - beanRoot.assignNext(bean2); |
70 | | - |
71 | | - // 12-Jul-2023, tatu: Alas, won't work -- Avro serialization by-passes many |
72 | | - // checks. Needs more work in future |
73 | | - |
74 | | - if (false) { |
75 | | - try { |
76 | | - AvroSchema schema = MAPPER_400.schemaFor(Bean.class); |
77 | | - MAPPER_400.writer(schema).writeValueAsBytes(beanRoot); |
78 | | - fail("expected InvalidDefinitionException"); |
79 | | - } catch (InvalidDefinitionException idex) { |
80 | | - assertTrue(idex.getMessage().startsWith("Direct self-reference leading to cycle"), |
81 | | - "InvalidDefinitionException message is as expected?"); |
82 | | - } |
| 43 | + } catch (InvalidDefinitionException e) { |
| 44 | + verifyException(e, "Direct self-reference leading to cycle"); |
83 | 45 | } |
84 | 46 | } |
85 | 47 | } |
0 commit comments