Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Jun 18, 2023
1 parent 25e0feb commit dea8488
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,27 @@ public void testNestingDepthWithSmallLimit() throws Exception
String expected = "Document nesting depth (2) exceeds the maximum allowed (1, from `StreamWriteConstraints.getMaxNestingDepth()`)";
assertEquals(expected, sce.getMessage());
}
}

public void testNestingDepthWithSmallLimitNestedObject() throws Exception
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
IOContext ioc = new IOContext(null,
StreamWriteConstraints.builder().maxNestingDepth(1).build(),
new BufferRecycler(),
ContentReference.rawReference(bytes), true);
try (JsonGenerator gen = new UTF8JsonGenerator(ioc, 0, null, bytes, '"')) {
gen.writeStartObject();
gen.writeFieldName("object");
gen.writeStartObject();
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException sce) {
String expected = "Document nesting depth (2) exceeds the maximum allowed (1, from `StreamWriteConstraints.getMaxNestingDepth()`)";
assertEquals(expected, sce.getMessage());
}
}


// for [core#115]
public void testSurrogatesWithRaw() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.fasterxml.jackson.core.write;

import com.fasterxml.jackson.core.BaseTest;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.StreamWriteConstraints;
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
import com.fasterxml.jackson.core.io.ContentReference;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.json.WriterBasedJsonGenerator;
import com.fasterxml.jackson.core.util.BufferRecycler;

import java.io.StringWriter;

public class WriterBasedJsonGeneratorTest extends BaseTest
{
private final JsonFactory JSON_F = new JsonFactory();

public void testNestingDepthWithSmallLimit() throws Exception
{
StringWriter sw = new StringWriter();
IOContext ioc = new IOContext(null,
StreamWriteConstraints.builder().maxNestingDepth(1).build(),
new BufferRecycler(),
ContentReference.rawReference(sw), true);
try (JsonGenerator gen = new WriterBasedJsonGenerator(ioc, 0, null, sw, '"')) {
gen.writeStartObject();
gen.writeFieldName("array");
gen.writeStartArray();
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException sce) {
String expected = "Document nesting depth (2) exceeds the maximum allowed (1, from `StreamWriteConstraints.getMaxNestingDepth()`)";
assertEquals(expected, sce.getMessage());
}
}

public void testNestingDepthWithSmallLimitNestedObject() throws Exception
{
StringWriter sw = new StringWriter();
IOContext ioc = new IOContext(null,
StreamWriteConstraints.builder().maxNestingDepth(1).build(),
new BufferRecycler(),
ContentReference.rawReference(sw), true);
try (JsonGenerator gen = new WriterBasedJsonGenerator(ioc, 0, null, sw, '"')) {
gen.writeStartObject();
gen.writeFieldName("object");
gen.writeStartObject();
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException sce) {
String expected = "Document nesting depth (2) exceeds the maximum allowed (1, from `StreamWriteConstraints.getMaxNestingDepth()`)";
assertEquals(expected, sce.getMessage());
}
}

}

0 comments on commit dea8488

Please sign in to comment.