1
1
package com .jsoniter ;
2
2
3
+ import com .jsoniter .any .Any ;
4
+ import com .jsoniter .spi .JsonException ;
5
+ import java .io .ByteArrayInputStream ;
6
+ import java .io .IOException ;
7
+ import java .io .InputStream ;
3
8
import junit .framework .TestCase ;
9
+ import org .junit .experimental .categories .Category ;
10
+ import sun .reflect .generics .reflectiveObjects .NotImplementedException ;
4
11
5
12
public class IterImplForStreamingTest extends TestCase {
6
13
@@ -11,4 +18,76 @@ public void testReadMaxDouble() throws Exception {
11
18
String number = new String (numberChars .chars , 0 , numberChars .charsLength );
12
19
assertEquals (maxDouble , number );
13
20
}
14
- }
21
+
22
+ @ Category (StreamingCategory .class )
23
+ public void testLoadMore () throws IOException {
24
+ final String originalContent = "1234567890" ;
25
+ final byte [] src = ("{\" a\" :\" " + originalContent + "\" }" ).getBytes ();
26
+
27
+ int initialBufferSize ;
28
+ Any parsedString ;
29
+ // Case #1: Data fits into initial buffer, autoresizing on
30
+ // Input must definitely fit into such large buffer
31
+ initialBufferSize = src .length * 2 ;
32
+ JsonIterator jsonIterator = JsonIterator .parse (getSluggishInputStream (src ), initialBufferSize , 512 );
33
+ jsonIterator .readObject ();
34
+ parsedString = jsonIterator .readAny ();
35
+ assertEquals (originalContent , parsedString .toString ());
36
+ // Check buffer was not expanded
37
+ assertEquals (initialBufferSize , jsonIterator .buf .length );
38
+
39
+ // Case #2: Data does not fit into initial buffer, autoresizing off
40
+ initialBufferSize = originalContent .length () / 2 ;
41
+ jsonIterator = JsonIterator .parse (getSluggishInputStream (src ), initialBufferSize , 0 );
42
+ jsonIterator .readObject ();
43
+ try {
44
+ jsonIterator .readAny ();
45
+ fail ("Expect to fail because buffer is too small." );
46
+ } catch (JsonException e ) {
47
+ if (!e .getMessage ().startsWith ("loadMore" )) {
48
+ throw e ;
49
+ }
50
+ }
51
+ // Check buffer was not expanded
52
+ assertEquals (initialBufferSize , jsonIterator .buf .length );
53
+
54
+ // Case #3: Data does fit into initial buffer, autoresizing on
55
+ initialBufferSize = originalContent .length () / 2 ;
56
+ int autoExpandBufferStep = initialBufferSize * 3 ;
57
+ jsonIterator = JsonIterator .parse (getSluggishInputStream (src ), initialBufferSize , autoExpandBufferStep );
58
+ jsonIterator .readObject ();
59
+ parsedString = jsonIterator .readAny ();
60
+ assertEquals (originalContent , parsedString .toString ());
61
+ // Check buffer was expanded exactly once
62
+ assertEquals (initialBufferSize + autoExpandBufferStep , jsonIterator .buf .length );
63
+
64
+ // Case #4: Data does not fit (but largest string does) into initial buffer, autoresizing on
65
+ initialBufferSize = originalContent .length () + 2 ;
66
+ jsonIterator = JsonIterator .parse (new ByteArrayInputStream (src ), initialBufferSize , 0 );
67
+ jsonIterator .readObject ();
68
+ parsedString = jsonIterator .readAny ();
69
+ assertEquals (originalContent , parsedString .toString ());
70
+ // Check buffer was expanded exactly once
71
+ assertEquals (initialBufferSize , jsonIterator .buf .length );
72
+ }
73
+
74
+ private static InputStream getSluggishInputStream (final byte [] src ) {
75
+ return new InputStream () {
76
+ int position = 0 ;
77
+
78
+ @ Override
79
+ public int read () throws IOException {
80
+ throw new NotImplementedException ();
81
+ }
82
+
83
+ @ Override
84
+ public int read (byte [] b , int off , int len ) throws IOException {
85
+ if (position < src .length ) {
86
+ b [off ] = src [position ++];
87
+ return 1 ;
88
+ }
89
+ return -1 ;
90
+ }
91
+ };
92
+ }
93
+ }
0 commit comments