| 1 |
package com.fasterxml.jackson.dataformat.smile; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
|
| 5 |
import com.fasterxml.jackson.core.*; |
| 6 |
import com.fasterxml.jackson.core.format.DataFormatDetector; |
| 7 |
import com.fasterxml.jackson.core.format.DataFormatMatcher; |
| 8 |
import com.fasterxml.jackson.core.format.MatchStrength; |
| 9 |
|
| 10 |
import com.fasterxml.jackson.dataformat.smile.SmileFactory; |
| 11 |
import com.fasterxml.jackson.dataformat.smile.SmileParser; |
| 12 |
|
| 13 |
public class TestSmileDetection extends SmileTestBase |
| 14 |
{ |
| 15 |
|
| 16 |
public void testSimpleObjectWithHeader() throws IOException |
| 17 |
{ |
| 18 |
SmileFactory f = new SmileFactory(); |
| 19 |
DataFormatDetector detector = new DataFormatDetector(f); |
| 20 |
byte[] doc = _smileDoc("{\"a\":3}", true); |
| 21 |
DataFormatMatcher matcher = detector.findFormat(doc); |
| 22 |
// should have match |
| 23 |
assertTrue(matcher.hasMatch()); |
| 24 |
assertEquals("Smile", matcher.getMatchedFormatName()); |
| 25 |
assertSame(f, matcher.getMatch()); |
| 26 |
// with header, should be full match |
| 27 |
assertEquals(MatchStrength.FULL_MATCH, matcher.getMatchStrength()); |
| 28 |
// and so: |
| 29 |
JsonParser jp = matcher.createParserWithMatch(); |
| 30 |
assertToken(JsonToken.START_OBJECT, jp.nextToken()); |
| 31 |
assertToken(JsonToken.FIELD_NAME, jp.nextToken()); |
| 32 |
assertEquals("a", jp.getCurrentName()); |
| 33 |
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken()); |
| 34 |
assertEquals(3, jp.getIntValue()); |
| 35 |
assertToken(JsonToken.END_OBJECT, jp.nextToken()); |
| 36 |
assertNull(jp.nextToken()); |
| 37 |
jp.close(); |
| 38 |
} |
| 39 |
|
| 40 |
public void testSimpleObjectWithoutHeader() throws IOException |
| 41 |
{ |
| 42 |
SmileFactory f = new SmileFactory(); |
| 43 |
DataFormatDetector detector = new DataFormatDetector(f); |
| 44 |
f.disable(SmileParser.Feature.REQUIRE_HEADER); |
| 45 |
byte[] doc = _smileDoc("{\"abc\":false}", false); |
| 46 |
DataFormatMatcher matcher = detector.findFormat(doc); |
| 47 |
assertTrue(matcher.hasMatch()); |
| 48 |
assertEquals("Smile", matcher.getMatchedFormatName()); |
| 49 |
assertSame(f, matcher.getMatch()); |
| 50 |
assertEquals(MatchStrength.SOLID_MATCH, matcher.getMatchStrength()); |
| 51 |
JsonParser jp = matcher.createParserWithMatch(); |
| 52 |
assertToken(JsonToken.START_OBJECT, jp.nextToken()); |
| 53 |
assertToken(JsonToken.FIELD_NAME, jp.nextToken()); |
| 54 |
assertEquals("abc", jp.getCurrentName()); |
| 55 |
assertToken(JsonToken.VALUE_FALSE, jp.nextToken()); |
| 56 |
assertToken(JsonToken.END_OBJECT, jp.nextToken()); |
| 57 |
assertNull(jp.nextToken()); |
| 58 |
jp.close(); |
| 59 |
} |
| 60 |
|
| 61 |
public void testSimpleArrayWithHeader() throws IOException |
| 62 |
{ |
| 63 |
SmileFactory f = new SmileFactory(); |
| 64 |
DataFormatDetector detector = new DataFormatDetector(f); |
| 65 |
byte[] doc = _smileDoc("[ true, 7 ]", true); |
| 66 |
DataFormatMatcher matcher = detector.findFormat(doc); |
| 67 |
// should have match |
| 68 |
assertTrue(matcher.hasMatch()); |
| 69 |
assertEquals("Smile", matcher.getMatchedFormatName()); |
| 70 |
assertSame(f, matcher.getMatch()); |
| 71 |
// with header, should be full match |
| 72 |
assertEquals(MatchStrength.FULL_MATCH, matcher.getMatchStrength()); |
| 73 |
JsonParser jp = matcher.createParserWithMatch(); |
| 74 |
assertToken(JsonToken.START_ARRAY, jp.nextToken()); |
| 75 |
assertToken(JsonToken.VALUE_TRUE, jp.nextToken()); |
| 76 |
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken()); |
| 77 |
assertEquals(7, jp.getIntValue()); |
| 78 |
assertToken(JsonToken.END_ARRAY, jp.nextToken()); |
| 79 |
assertNull(jp.nextToken()); |
| 80 |
jp.close(); |
| 81 |
} |
| 82 |
|
| 83 |
public void testSimpleArrayWithoutHeader() throws IOException |
| 84 |
{ |
| 85 |
SmileFactory f = new SmileFactory(); |
| 86 |
f.disable(SmileParser.Feature.REQUIRE_HEADER); |
| 87 |
DataFormatDetector detector = new DataFormatDetector(f); |
| 88 |
byte[] doc = _smileDoc("[ -13 ]", false); |
| 89 |
DataFormatMatcher matcher = detector.findFormat(doc); |
| 90 |
assertTrue(matcher.hasMatch()); |
| 91 |
assertEquals("Smile", matcher.getMatchedFormatName()); |
| 92 |
assertSame(f, matcher.getMatch()); |
| 93 |
assertEquals(MatchStrength.SOLID_MATCH, matcher.getMatchStrength()); |
| 94 |
JsonParser jp = matcher.createParserWithMatch(); |
| 95 |
assertToken(JsonToken.START_ARRAY, jp.nextToken()); |
| 96 |
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken()); |
| 97 |
assertToken(JsonToken.END_ARRAY, jp.nextToken()); |
| 98 |
assertNull(jp.nextToken()); |
| 99 |
jp.close(); |
| 100 |
} |
| 101 |
|
| 102 |
/* |
| 103 |
/********************************************************** |
| 104 |
/* Simple negative tests |
| 105 |
/********************************************************** |
| 106 |
*/ |
| 107 |
|
| 108 |
/* |
| 109 |
* Also let's ensure no match is found if data doesn't support it... |
| 110 |
* Let's use 0xFD since it can not be included (except in raw binary; |
| 111 |
* use of which requires header to be present) |
| 112 |
*/ |
| 113 |
public void testSimpleInvalid() throws Exception |
| 114 |
{ |
| 115 |
DataFormatDetector detector = new DataFormatDetector(new SmileFactory()); |
| 116 |
byte FD = (byte) 0xFD; |
| 117 |
byte[] DOC = new byte[] { FD, FD, FD, FD }; |
| 118 |
DataFormatMatcher matcher = detector.findFormat(new ByteArrayInputStream(DOC)); |
| 119 |
assertFalse(matcher.hasMatch()); |
| 120 |
assertEquals(MatchStrength.INCONCLUSIVE, matcher.getMatchStrength()); |
| 121 |
assertNull(matcher.createParserWithMatch()); |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
/********************************************************** |
| 126 |
/* Fallback tests to ensure Smile is found even against JSON |
| 127 |
/********************************************************** |
| 128 |
*/ |
| 129 |
|
| 130 |
public void testSmileVsJson() throws IOException |
| 131 |
{ |
| 132 |
SmileFactory f = new SmileFactory(); |
| 133 |
f.disable(SmileParser.Feature.REQUIRE_HEADER); |
| 134 |
DataFormatDetector detector = new DataFormatDetector(new JsonFactory(), f); |
| 135 |
// to make it bit trickier, leave out header |
| 136 |
byte[] doc = _smileDoc("[ \"abc\" ]", false); |
| 137 |
DataFormatMatcher matcher = detector.findFormat(doc); |
| 138 |
assertTrue(matcher.hasMatch()); |
| 139 |
assertEquals("Smile", matcher.getMatchedFormatName()); |
| 140 |
assertSame(f, matcher.getMatch()); |
| 141 |
// without header, just solid |
| 142 |
assertEquals(MatchStrength.SOLID_MATCH, matcher.getMatchStrength()); |
| 143 |
JsonParser jp = matcher.createParserWithMatch(); |
| 144 |
assertToken(JsonToken.START_ARRAY, jp.nextToken()); |
| 145 |
assertToken(JsonToken.VALUE_STRING, jp.nextToken()); |
| 146 |
assertToken(JsonToken.END_ARRAY, jp.nextToken()); |
| 147 |
assertNull(jp.nextToken()); |
| 148 |
jp.close(); |
| 149 |
} |
| 150 |
|
| 151 |
} |