Skip to content

Commit

Permalink
Bit more testing on non-canonicalizing impl
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 13, 2021
1 parent c4ab92f commit fe2e029
Showing 1 changed file with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import java.lang.reflect.Field;
import java.util.Random;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer;

import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile;
Expand All @@ -15,6 +15,15 @@

public class SymbolTableTest extends BaseTestForSmile
{
static class Point {
public int x, y;
}

private final SmileMapper NO_CAN_MAPPER = SmileMapper.builder(SmileFactory.builder()
.disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES)
.build())
.build();

public void testSimpleDefault() throws Exception
{
final SmileMapper vanillaMapper = smileMapper();
Expand Down Expand Up @@ -72,6 +81,17 @@ public void testSimpleDefault() throws Exception

// [dataformats-binary#252]: should be able to prevent canonicalization
// Assumption: there is still non-null symbol table, but has "no canonicalization"
public void testNoCanonicalizeWithMapper() throws Exception
{
final byte[] doc = _smileDoc(a2q("{ 'x':13, 'y':-999}"));
try (JsonParser p = NO_CAN_MAPPER.createParser(doc)) {
Point point = NO_CAN_MAPPER.readValue(p, Point.class);
assertEquals(13, point.x);
assertEquals(-999, point.y);
}
}

// [dataformats-binary#252]: should be able to prevent canonicalization
public void testSimpleNoCanonicalize() throws Exception
{
final String[] fieldNames = new String[] {
Expand All @@ -89,12 +109,8 @@ public void testSimpleNoCanonicalize() throws Exception
"end", // just simple end marker
};
final byte[] doc = _smileDoc(jsonFrom(fieldNames));
final SmileMapper mapper = SmileMapper.builder(SmileFactory.builder()
.disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES)
.build())
.build();

try (JsonParser p = mapper.createParser(doc)) {
try (JsonParser p = NO_CAN_MAPPER.createParser(doc)) {
ByteQuadsCanonicalizer syms = _findSymbols(p);
assertFalse(syms.isCanonicalizing()); // added in 2.13
assertEquals(-1, syms.size());
Expand All @@ -121,6 +137,50 @@ public void testSimpleNoCanonicalize() throws Exception

assertEquals(-1, syms.size());
}

// But let's also try other accessors: first, nextName()
try (JsonParser p = NO_CAN_MAPPER.createParser(doc)) {
assertToken(JsonToken.START_OBJECT, p.nextToken());
assertEquals(fieldNames[0], p.nextFieldName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(0, p.getIntValue());

// and from thereon...
for (int i = 1; i < fieldNames.length; ++i) {
assertEquals(fieldNames[i], p.nextFieldName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(i, p.getIntValue());
}
assertToken(JsonToken.END_OBJECT, p.nextToken());
assertNull(p.nextToken());

assertEquals(-1, _findSymbols(p).size());
}

// and then nextName(match)
try (JsonParser p = NO_CAN_MAPPER.createParser(doc)) {
assertToken(JsonToken.START_OBJECT, p.nextToken());
assertTrue(p.nextFieldName(new SerializedString(fieldNames[0])));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(0, p.getIntValue());

// and then negative match
assertFalse(p.nextFieldName(new SerializedString("bogus")));
assertEquals(fieldNames[1], p.currentName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(1, p.getIntValue());

// and from thereon...
for (int i = 2; i < fieldNames.length; ++i) {
assertEquals(fieldNames[i], p.nextFieldName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(i, p.getIntValue());
}
assertToken(JsonToken.END_OBJECT, p.nextToken());
assertNull(p.nextToken());

assertEquals(-1, _findSymbols(p).size());
}
}

private String jsonFrom(String... fields) {
Expand Down

0 comments on commit fe2e029

Please sign in to comment.