forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBigStrings.java
More file actions
85 lines (72 loc) · 3.13 KB
/
Copy pathTestBigStrings.java
File metadata and controls
85 lines (72 loc) · 3.13 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
package com.fasterxml.jackson.databind.deser;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
public class TestBigStrings extends BaseMapTest
{
final static class StringWrapper
{
String string;
StringWrapper() { }
StringWrapper(String string) { this.string = string; }
void setString(String string) {
this.string = string;
}
}
/*
/**********************************************************
/* Tests
/**********************************************************
*/
private final ObjectMapper MAPPER = newJsonMapper();
private ObjectMapper newJsonMapperWithUnlimitedStringSizeSupport() {
JsonFactory jsonFactory = JsonFactory.builder()
.streamReadConstraints(StreamReadConstraints.builder().maxStringLength(Integer.MAX_VALUE).build())
.build();
return JsonMapper.builder(jsonFactory).build();
}
public void testBigString() throws Exception
{
try {
MAPPER.readValue(generateJson("string", 1001000), StringWrapper.class);
fail("expected JsonMappingException");
} catch (JsonMappingException jsonMappingException) {
assertTrue("unexpected exception message: " + jsonMappingException.getMessage(),
jsonMappingException.getMessage().startsWith("String length (1001000) exceeds the maximum length (1000000)"));
}
}
public void testBiggerString() throws Exception
{
try {
MAPPER.readValue(generateJson("string", 2000000), StringWrapper.class);
fail("expected JsonMappingException");
} catch (JsonMappingException jsonMappingException) {
final String message = jsonMappingException.getMessage();
// this test fails when the TextBuffer is being resized, so we don't yet know just how big the string is
// so best not to assert that the String length value in the message is the full 2000000 value
assertTrue("unexpected exception message: " + message, message.startsWith("String length"));
assertTrue("unexpected exception message: " + message, message.contains("exceeds the maximum length (1000000)"));
}
}
public void testUnlimitedString() throws Exception
{
final int len = 1001000;
StringWrapper sw = newJsonMapperWithUnlimitedStringSizeSupport()
.readValue(generateJson("string", len), StringWrapper.class);
assertEquals(len, sw.string.length());
}
private String generateJson(final String fieldName, final int len) {
final StringBuilder sb = new StringBuilder();
sb.append("{\"")
.append(fieldName)
.append("\": \"");
for (int i = 0; i < len; i++) {
sb.append('a');
}
sb.append("\"}");
return sb.toString();
}
}