Skip to content

Commit 4504c2e

Browse files
committed
Added failing test of loadMore function simulating malfunction mentioned in previous commit.
1 parent d0b5fa8 commit 4504c2e

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/test/java/com/jsoniter/IterImplForStreamingTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.jsoniter;
22

3+
import com.jsoniter.any.Any;
4+
import java.io.IOException;
5+
import java.io.InputStream;
36
import junit.framework.TestCase;
7+
import org.junit.experimental.categories.Category;
48

59
public class IterImplForStreamingTest extends TestCase {
610

@@ -11,4 +15,37 @@ public void testReadMaxDouble() throws Exception {
1115
String number = new String(numberChars.chars, 0, numberChars.charsLength);
1216
assertEquals(maxDouble, number);
1317
}
14-
}
18+
19+
@Category(StreamingCategory.class)
20+
public void testLoadMore() throws IOException {
21+
final String originalContent = "1234";
22+
final byte[] src = ("{\"a\":\"" + originalContent + "\"}").getBytes();
23+
InputStream slowStream = new InputStream() {
24+
int position = 0;
25+
boolean pretendEmptyNextRead = false;
26+
27+
@Override
28+
public int read() throws IOException {
29+
if (position < src.length) {
30+
if (pretendEmptyNextRead) {
31+
pretendEmptyNextRead = false;
32+
return -1;
33+
} else {
34+
pretendEmptyNextRead = true;
35+
return src[position++];
36+
}
37+
}
38+
return -1;
39+
}
40+
};
41+
42+
// Input must definitely fit into such large buffer
43+
final int initialBufferSize = src.length * 2;
44+
JsonIterator jsonIterator = JsonIterator.parse(slowStream, initialBufferSize);
45+
jsonIterator.readObject();
46+
Any parsedString = jsonIterator.readAny();
47+
assertEquals(originalContent, parsedString.toString());
48+
// Check buffer was not expanded prematurely
49+
assertEquals(initialBufferSize, jsonIterator.buf.length);
50+
}
51+
}

src/test/java/com/jsoniter/suite/AllTestCases.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
TestGson.class,
5454
com.jsoniter.output.TestGson.class,
5555
TestStreamBuffer.class,
56+
IterImplForStreamingTest.class,
5657
TestCollection.class,
5758
TestList.class,
5859
TestAnnotationJsonObject.class,

0 commit comments

Comments
 (0)