Skip to content

Commit

Permalink
non blocking test
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Aug 23, 2023
1 parent 954a2d0 commit 6f3a9dc
Showing 1 changed file with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
import com.fasterxml.jackson.core.json.async.NonBlockingJsonParser;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class LargeNameReadTest extends BaseTest {

Expand All @@ -14,10 +18,7 @@ public void testLargeName() throws Exception
final String doc = generateJSON(1000);
final JsonFactory jsonFactory = JsonFactory.builder().build();
try (JsonParser jp = createParserUsingStream(jsonFactory, doc, "UTF-8")) {
JsonToken jsonToken;
while ((jsonToken = jp.nextToken()) != null) {

}
consumeTokens(jp);
}
}

Expand All @@ -28,10 +29,24 @@ public void testLargeNameWithSmallLimit() throws Exception
.streamReadConstraints(StreamReadConstraints.builder().maxNameLength(100).build())
.build();
try (JsonParser jp = createParserUsingStream(jsonFactory, doc, "UTF-8")) {
JsonToken jsonToken;
while ((jsonToken = jp.nextToken()) != null) {
consumeTokens(jp);
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException e) {
assertTrue("Unexpected exception message: " + e.getMessage(),
e.getMessage().contains("Name value length"));
}
}

}
public void testAsyncLargeNameWithSmallLimit() throws Exception
{
final byte[] doc = generateJSON(1000).getBytes(StandardCharsets.UTF_8);
final JsonFactory jsonFactory = JsonFactory.builder()
.streamReadConstraints(StreamReadConstraints.builder().maxNameLength(100).build())
.build();

try (NonBlockingJsonParser jp = (NonBlockingJsonParser) jsonFactory.createNonBlockingByteArrayParser()) {
jp.feedInput(doc, 0, doc.length);
consumeTokens(jp);
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException e) {
assertTrue("Unexpected exception message: " + e.getMessage(),
Expand All @@ -46,17 +61,21 @@ public void testReaderLargeNameWithSmallLimit() throws Exception
.streamReadConstraints(StreamReadConstraints.builder().maxNameLength(100).build())
.build();
try (JsonParser jp = createParserUsingReader(jsonFactory, doc)) {
JsonToken jsonToken;
while ((jsonToken = jp.nextToken()) != null) {
System.out.println(jsonToken);
}
consumeTokens(jp);
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException e) {
assertTrue("Unexpected exception message: " + e.getMessage(),
e.getMessage().contains("Name value length"));
}
}

private void consumeTokens(JsonParser jp) throws IOException {
JsonToken jsonToken;
while ((jsonToken = jp.nextToken()) != null) {

}
}

private String generateJSON(final int nameLen) {
final StringBuilder sb = new StringBuilder();
sb.append("{\"");
Expand Down

0 comments on commit 6f3a9dc

Please sign in to comment.