| 1 |
package com.fasterxml.jackson.dataformat.smile; |
| 2 |
|
| 3 |
import com.fasterxml.jackson.core.*; |
| 4 |
|
| 5 |
public class TestParserDupHandling extends SmileTestBase |
| 6 |
{ |
| 7 |
public void testSimpleDups() throws Exception |
| 8 |
{ |
| 9 |
for (String json : new String[] { |
| 10 |
"{ \"a\":1, \"a\":2 }", |
| 11 |
"[{ \"a\":1, \"a\":2 }]", |
| 12 |
"{ \"a\":1, \"b\":2, \"c\":3,\"a\":true,\"e\":false }", |
| 13 |
"{ \"foo\": { \"bar\": [ [ { \"x\":3, \"a\":1 } ]], \"x\":0, \"a\":\"y\", \"b\":3,\"a\":13 } }", |
| 14 |
}) { |
| 15 |
SmileFactory f = new SmileFactory(); |
| 16 |
byte[] doc = _smileDoc(json); |
| 17 |
assertFalse(f.isEnabled(JsonParser.Feature.STRICT_DUPLICATE_DETECTION)); |
| 18 |
_testSimpleDupsOk(doc, f); |
| 19 |
|
| 20 |
f.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION); |
| 21 |
_testSimpleDupsFail(doc, f, "a"); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
private void _testSimpleDupsOk(final byte[] doc, JsonFactory f) throws Exception |
| 26 |
{ |
| 27 |
JsonParser jp = f.createParser(doc); |
| 28 |
JsonToken t = jp.nextToken(); |
| 29 |
assertNotNull(t); |
| 30 |
assertTrue(t.isStructStart()); |
| 31 |
while (jp.nextToken() != null) { } |
| 32 |
jp.close(); |
| 33 |
} |
| 34 |
|
| 35 |
private void _testSimpleDupsFail(final byte[] doc, JsonFactory f, String name) throws Exception |
| 36 |
{ |
| 37 |
JsonParser jp = f.createParser(doc); |
| 38 |
JsonToken t = jp.nextToken(); |
| 39 |
assertNotNull(t); |
| 40 |
assertTrue(t.isStructStart()); |
| 41 |
try { |
| 42 |
while (jp.nextToken() != null) { } |
| 43 |
fail("Should have caught dups in document: "+doc); |
| 44 |
} catch (JsonParseException e) { |
| 45 |
verifyException(e, "duplicate field '"+name+"'"); |
| 46 |
} |
| 47 |
jp.close(); |
| 48 |
} |
| 49 |
|
| 50 |
} |