WriterBasedJsonGeneratorTest.java
package tools.jackson.core.unittest.write;
import java.io.StringWriter;
import org.junit.jupiter.api.Test;
import tools.jackson.core.JsonGenerator;
import tools.jackson.core.ObjectWriteContext;
import tools.jackson.core.StreamWriteConstraints;
import tools.jackson.core.exc.StreamConstraintsException;
import tools.jackson.core.json.JsonFactory;
import tools.jackson.core.unittest.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
class WriterBasedJsonGeneratorTest extends JacksonCoreTestBase
{
private final JsonFactory JSON_MAX_NESTING_1 = JsonFactory.builder()
.streamWriteConstraints(StreamWriteConstraints.builder().maxNestingDepth(1).build())
.build();
@Test
void nestingDepthWithSmallLimit() throws Exception
{
StringWriter sw = new StringWriter();
try (JsonGenerator gen = JSON_MAX_NESTING_1.createGenerator(ObjectWriteContext.empty(), sw)) {
gen.writeStartObject();
gen.writeName("array");
gen.writeStartArray();
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException e) {
assertThat(e.getMessage())
.startsWith("Document nesting depth (2) exceeds the maximum allowed (1, from `StreamWriteConstraints.getMaxNestingDepth()`)");
}
}
@Test
void nestingDepthWithSmallLimitNestedObject() throws Exception
{
StringWriter sw = new StringWriter();
try (JsonGenerator gen = JSON_MAX_NESTING_1.createGenerator(ObjectWriteContext.empty(), sw)) {
gen.writeStartObject();
gen.writeName("object");
gen.writeStartObject();
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException e) {
assertThat(e.getMessage())
.startsWith("Document nesting depth (2) exceeds the maximum allowed (1, from `StreamWriteConstraints.getMaxNestingDepth()`)");
}
}
}