-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
104 additions
and
49 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
...st/java/tools/jackson/dataformat/javaprop/constraints/DeeplyNestedPropsReadWriteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package tools.jackson.dataformat.javaprop.constraints; | ||
|
||
import tools.jackson.core.*; | ||
import tools.jackson.core.exc.StreamConstraintsException; | ||
|
||
import tools.jackson.databind.JsonNode; | ||
import tools.jackson.databind.ObjectMapper; | ||
import tools.jackson.databind.node.ObjectNode; | ||
import tools.jackson.dataformat.javaprop.JavaPropsFactory; | ||
import tools.jackson.dataformat.javaprop.JavaPropsMapper; | ||
import tools.jackson.dataformat.javaprop.ModuleTestBase; | ||
|
||
public class DeeplyNestedPropsReadWriteTest extends ModuleTestBase | ||
{ | ||
private final JavaPropsMapper PROPS_MAPPER_VANILLA = newPropertiesMapper(); | ||
|
||
private final JavaPropsMapper PROPS_MAPPER_CONSTR = new JavaPropsMapper( | ||
JavaPropsFactory.builder() | ||
// Use higher limit for writing to simplify testing setup | ||
.streamReadConstraints(StreamReadConstraints.builder() | ||
.maxNestingDepth(10).build()) | ||
.streamWriteConstraints(StreamWriteConstraints.builder() | ||
.maxNestingDepth(12).build()) | ||
.build() | ||
); | ||
|
||
public void testDeepNestingRead() throws Exception | ||
{ | ||
final String DOC = PROPS_MAPPER_CONSTR.writeValueAsString(createDeepNestedDoc(11)); | ||
try (JsonParser p = PROPS_MAPPER_CONSTR.createParser(DOC)) { | ||
_testDeepNestingRead(p); | ||
} | ||
} | ||
|
||
private void _testDeepNestingRead(JsonParser p) throws Exception | ||
{ | ||
try { | ||
while (p.nextToken() != null) { } | ||
fail("expected StreamConstraintsException"); | ||
} catch (StreamConstraintsException e) { | ||
assertEquals("Document nesting depth (11) exceeds the maximum allowed (10, from `StreamReadConstraints.getMaxNestingDepth()`)", | ||
e.getMessage()); | ||
} | ||
} | ||
|
||
public void testDeepNestingWrite() throws Exception | ||
{ | ||
final JsonNode docRoot = createDeepNestedDoc(13); | ||
try { | ||
PROPS_MAPPER_CONSTR.writeValueAsString(docRoot); | ||
fail("Should not pass"); | ||
} catch (StreamConstraintsException e) { | ||
assertEquals("Document nesting depth (13) exceeds the maximum allowed (12, from `StreamWriteConstraints.getMaxNestingDepth()`)", | ||
e.getMessage()); | ||
} | ||
} | ||
|
||
public void testDeeplyNestedReadVanilla() throws Exception { | ||
final int depth = 1500; | ||
final String doc = createDeepNestedString(depth); | ||
try (JsonParser p = PROPS_MAPPER_VANILLA.createParser(doc)) { | ||
while (p.nextToken() != null) { } | ||
fail("expected StreamConstraintsException"); | ||
} catch (StreamConstraintsException e) { | ||
String exceptionPrefix = String.format("Document nesting depth (%d) exceeds the maximum allowed", | ||
StreamReadConstraints.DEFAULT_MAX_DEPTH + 1); | ||
assertTrue("unexpected exception message: " + e.getMessage(), | ||
e.getMessage().startsWith(exceptionPrefix)); | ||
} | ||
} | ||
|
||
public void testDeeplyNestedReadWithUnconstrainedMapper() throws Exception { | ||
final int depth = 1500; | ||
final String doc = createDeepNestedString(depth); | ||
final JavaPropsFactory factory = JavaPropsFactory.builder() | ||
.streamReadConstraints(StreamReadConstraints.builder().maxNestingDepth(Integer.MAX_VALUE).build()) | ||
.build(); | ||
final ObjectMapper mapper = propertiesMapperBuilder(factory).build(); | ||
try (JsonParser p = mapper.createParser(doc)) { | ||
while (p.nextToken() != null) { } | ||
} | ||
} | ||
|
||
private JsonNode createDeepNestedDoc(final int depth) throws Exception | ||
{ | ||
final ObjectNode root = PROPS_MAPPER_VANILLA.createObjectNode(); | ||
ObjectNode curr = root; | ||
for (int i = 0; i < depth; ++i) { | ||
curr = curr.putObject("nested"+i); | ||
} | ||
curr.put("value", 42); | ||
return root; | ||
} | ||
|
||
private String createDeepNestedString(final int depth) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < depth; i++) { | ||
if (i > 1) sb.append('.'); | ||
sb.append('a'); | ||
} | ||
sb.append("=val"); | ||
return sb.toString(); | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
properties/src/test/java/tools/jackson/dataformat/javaprop/dos/DeepNestParserTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters