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