Skip to content

Commit abf2a70

Browse files
authored
test big strings (#3704)
1 parent 1c3c3d8 commit abf2a70

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.fasterxml.jackson.databind.deser;
2+
3+
import com.fasterxml.jackson.core.JsonFactory;
4+
import com.fasterxml.jackson.core.StreamReadConstraints;
5+
import com.fasterxml.jackson.databind.BaseMapTest;
6+
import com.fasterxml.jackson.databind.JsonMappingException;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.json.JsonMapper;
9+
10+
public class TestBigStrings extends BaseMapTest
11+
{
12+
final static class StringWrapper
13+
{
14+
String string;
15+
16+
StringWrapper() { }
17+
18+
StringWrapper(String string) { this.string = string; }
19+
20+
void setString(String string) {
21+
this.string = string;
22+
}
23+
}
24+
25+
/*
26+
/**********************************************************
27+
/* Tests
28+
/**********************************************************
29+
*/
30+
31+
private final ObjectMapper MAPPER = newJsonMapper();
32+
33+
private ObjectMapper newJsonMapperWithUnlimitedStringSizeSupport() {
34+
JsonFactory jsonFactory = JsonFactory.builder()
35+
.streamReadConstraints(StreamReadConstraints.builder().maxStringLength(Integer.MAX_VALUE).build())
36+
.build();
37+
return JsonMapper.builder(jsonFactory).build();
38+
}
39+
40+
public void testBigString() throws Exception
41+
{
42+
try {
43+
MAPPER.readValue(generateJson("string", 1001000), StringWrapper.class);
44+
fail("expected JsonMappingException");
45+
} catch (JsonMappingException jsonMappingException) {
46+
assertTrue("unexpected exception message: " + jsonMappingException.getMessage(),
47+
jsonMappingException.getMessage().startsWith("String length (1001000) exceeds the maximum length (1000000)"));
48+
}
49+
}
50+
51+
public void testBiggerString() throws Exception
52+
{
53+
try {
54+
MAPPER.readValue(generateJson("string", 2000000), StringWrapper.class);
55+
fail("expected JsonMappingException");
56+
} catch (JsonMappingException jsonMappingException) {
57+
final String message = jsonMappingException.getMessage();
58+
// this test fails when the TextBuffer is being resized, so we don't yet know just how big the string is
59+
// so best not to assert that the String length value in the message is the full 2000000 value
60+
assertTrue("unexpected exception message: " + message, message.startsWith("String length"));
61+
assertTrue("unexpected exception message: " + message, message.contains("exceeds the maximum length (1000000)"));
62+
}
63+
}
64+
65+
public void testUnlimitedString() throws Exception
66+
{
67+
final int len = 1001000;
68+
StringWrapper sw = newJsonMapperWithUnlimitedStringSizeSupport()
69+
.readValue(generateJson("string", len), StringWrapper.class);
70+
assertEquals(len, sw.string.length());
71+
}
72+
73+
74+
private String generateJson(final String fieldName, final int len) {
75+
final StringBuilder sb = new StringBuilder();
76+
sb.append("{\"")
77+
.append(fieldName)
78+
.append("\": \"");
79+
for (int i = 0; i < len; i++) {
80+
sb.append('a');
81+
}
82+
sb.append("\"}");
83+
return sb.toString();
84+
}
85+
}

0 commit comments

Comments
 (0)