[java] Adding more tests for json reader to increase coverage before applying a PR
import java.io.StringReader;
import java.util.Map;
+import java.util.Random;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
@Category(UnitTests.class)
public class JsonInputTest {
@@ -80,6 +83,15 @@ public void shouldBeAbleToReadAString() {
assertThat(input.nextString()).isEqualTo("cheese");
}
+ @Test
+ public void shouldBeAbleToHandleAnUnterminatedString() {
+ JsonInput input = newInput("\"cheese");
+ assertThat(input.peek()).isEqualTo(STRING);
+ assertThatExceptionOfType(JsonException.class)
+ .isThrownBy(input::nextString)
+ .withMessageStartingWith("Unterminated string");
+ }
+
@Test
public void shouldBeAbleToReadTheEmptyString() {
JsonInput input = newInput("\"\"");
@@ -149,10 +161,12 @@ public void canReadAMapWithASingleEntry() {
@Test
public void canReadAMapWithManyEntries() {
- JsonInput input = newInput("{" +
- "\"cheese\": \"stilton\"," +
- "\"vegetable\": \"peas\"," +
- "\"random\": 42}");
+ JsonInput input = newInput(
+ "{"
+ + "\"cheese\": \"stilton\","
+ + "\"vegetable\": \"peas\","
+ + "\"random\": 42"
+ + "}");
assertThat(input.peek()).isEqualTo(START_MAP);
input.beginObject();
@@ -218,6 +232,32 @@ public void shouldCallFromJsonWithJsonInputParameter() {
}
}
+ @Test
+ public void shouldBeAbleToReadDataLongerThanReadBuffer() {
+ char[] chars = new char[]{'c', 'h', 'e', 's'};
+ Random r = new Random();
+ String raw = Stream.generate(() -> "" + chars[r.nextInt(4)])
+ .limit(150).collect(Collectors.joining());
+ JsonInput input = newInput("\"" + raw + "\"");
+ assertThat(input.peek()).isEqualTo(STRING);
+ assertThat(input.nextString()).isEqualTo(raw);
+ }
+
+ @Test
+ public void shouldBeAbleToReadNonWellFormedDataLongerThanReadBuffer() {
+ char[] chars = new char[]{'c', 'h', 'e', 's'};
+ Random r = new Random();
+ String raw = Stream.generate(() -> "" + chars[r.nextInt(4)])
+ .limit(150).collect(Collectors.joining());
+ JsonInput input = newInput("\"" + raw);
+ assertThat(input.peek()).isEqualTo(STRING);
+ assertThatExceptionOfType(JsonException.class)
+ .isThrownBy(input::nextString)
+ .withMessageStartingWith(String.format(
+ "Unterminated string: %s. Last 128 characters read: %s",
+ raw, raw.substring(raw.length() - 128)));
+ }
+
private JsonInput newInput(String raw) {
StringReader reader = new StringReader(raw);
return new JsonInput(reader, new JsonTypeCoercer(), BY_NAME);
