6
6
7
7
import tools .jackson .core .JsonParser ;
8
8
import tools .jackson .core .ObjectReadContext ;
9
+ import tools .jackson .core .async .ByteArrayFeeder ;
9
10
import tools .jackson .core .async .ByteBufferFeeder ;
10
11
11
12
import tools .jackson .databind .ObjectMapper ;
12
13
import tools .jackson .databind .json .JsonMapper ;
14
+ import tools .jackson .databind .testutil .DatabindTestUtil ;
13
15
14
16
import static org .junit .jupiter .api .Assertions .assertEquals ;
15
17
16
18
/**
17
19
* Tests for checking that test deserialization with non-blocking parsers
18
20
*/
19
- public class NonBlockingDeserTest
21
+ public class NonBlockingDeserTest extends DatabindTestUtil
20
22
{
21
23
record Foo (String bar ) {}
22
24
@@ -26,24 +28,66 @@ record Foo(String bar) {}
26
28
/**********************************************************
27
29
*/
28
30
31
+ private final static int TEST_ITEM_COUNT = 10 ;
32
+
33
+ private final ObjectMapper MAPPER = newJsonMapper ();
34
+
35
+ private final byte [] TEST_DOC = _testDoc (TEST_ITEM_COUNT );
36
+
29
37
@ Test
30
- public void testNonBlockingParser ()
38
+ public void testNonBlockingByteArrayParserViaMapper ()
31
39
{
32
- final ObjectMapper m = JsonMapper .builder ()
33
- //.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
34
- .build ();
35
- final int len = 10 ;
36
- Foo [] foos = new Foo [len ];
37
- for (int i = 0 ; i < len ; ++i ) {
38
- foos [i ] = new Foo ("bar-" + i );
40
+ try (final JsonParser parser =
41
+ MAPPER .createNonBlockingByteArrayParser ()) {
42
+ ((ByteArrayFeeder ) parser ).feedInput (TEST_DOC , 0 , TEST_DOC .length );
43
+ ((ByteArrayFeeder ) parser ).endOfInput ();
44
+ Foo [] result = MAPPER .readValue (parser , Foo [].class );
45
+ assertEquals (TEST_ITEM_COUNT , result .length );
39
46
}
40
- final Foo [] result ;
47
+ }
48
+
49
+ @ Test
50
+ public void testNonBlockingByteArrayParserViaReader ()
51
+ {
41
52
try (final JsonParser parser =
42
- m .tokenStreamFactory ().createNonBlockingByteBufferParser (ObjectReadContext .empty ())) {
43
- ((ByteBufferFeeder ) parser ).feedInput (ByteBuffer .wrap (m .writeValueAsBytes (foos )));
53
+ MAPPER .reader ().createNonBlockingByteArrayParser ()) {
54
+ ((ByteArrayFeeder ) parser ).feedInput (TEST_DOC , 0 , TEST_DOC .length );
55
+ ((ByteArrayFeeder ) parser ).endOfInput ();
56
+ Foo [] result = MAPPER .readValue (parser , Foo [].class );
57
+ assertEquals (TEST_ITEM_COUNT , result .length );
58
+ }
59
+ }
60
+
61
+ @ Test
62
+ public void testNonBlockingByteBufferParserViaMapper ()
63
+ {
64
+ try (final JsonParser parser =
65
+ MAPPER .createNonBlockingByteBufferParser ()) {
66
+ ((ByteBufferFeeder ) parser ).feedInput (ByteBuffer .wrap (TEST_DOC ));
67
+ ((ByteBufferFeeder ) parser ).endOfInput ();
68
+ Foo [] result = MAPPER .readValue (parser , Foo [].class );
69
+ assertEquals (TEST_ITEM_COUNT , result .length );
70
+ }
71
+ }
72
+
73
+ @ Test
74
+ public void testNonBlockingByteBufferParserViaReader ()
75
+ {
76
+ try (final JsonParser parser =
77
+ MAPPER .reader ().createNonBlockingByteBufferParser ()) {
78
+ ((ByteBufferFeeder ) parser ).feedInput (ByteBuffer .wrap (TEST_DOC ));
44
79
((ByteBufferFeeder ) parser ).endOfInput ();
45
- result = m .readValue (parser , Foo [].class );
80
+ Foo [] result = MAPPER .readValue (parser , Foo [].class );
81
+ assertEquals (TEST_ITEM_COUNT , result .length );
82
+ }
83
+ }
84
+
85
+ private byte [] _testDoc (int count ) {
86
+ Foo [] foos = new Foo [count ];
87
+ for (int i = 0 ; i < count ; ++i ) {
88
+ foos [i ] = new Foo ("bar-" + i );
46
89
}
47
- assertEquals ( len , result . length );
90
+ return MAPPER . writeValueAsBytes ( foos );
48
91
}
92
+
49
93
}
0 commit comments