Skip to content

Commit b0f3336

Browse files
committed
Add tests for ESJsonFactory
1 parent e285349 commit b0f3336

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.xcontent.provider.json;
11+
12+
import com.fasterxml.jackson.core.JsonFactory;
13+
import com.fasterxml.jackson.core.JsonParseException;
14+
import com.fasterxml.jackson.core.JsonParser;
15+
import com.fasterxml.jackson.core.JsonToken;
16+
import com.fasterxml.jackson.core.json.ReaderBasedJsonParser;
17+
import com.fasterxml.jackson.core.json.UTF8StreamJsonParser;
18+
19+
import org.elasticsearch.test.ESTestCase;
20+
import org.hamcrest.Matchers;
21+
22+
import java.io.IOException;
23+
import java.nio.ByteBuffer;
24+
import java.nio.charset.Charset;
25+
import java.nio.charset.StandardCharsets;
26+
27+
public class ESJsonFactoryTests extends ESTestCase {
28+
29+
public void testCreateParser() throws IOException {
30+
JsonFactory factory = new ESJsonFactoryBuilder().build();
31+
assertThat(factory, Matchers.instanceOf(ESJsonFactory.class));
32+
33+
// \ufeff is the BOM
34+
String[] inputs = { "{\"foo\": \"bar\"}", "\ufeff{\"foo\": \"bar\"}" };
35+
Charset[] charsets = { StandardCharsets.UTF_8, StandardCharsets.UTF_16LE, StandardCharsets.UTF_16BE };
36+
Class<?>[] expectedParsers = { ESUTF8StreamJsonParser.class, ReaderBasedJsonParser.class, ReaderBasedJsonParser.class };
37+
38+
for (String input : inputs) {
39+
for (int i = 0; i < charsets.length; i++) {
40+
ByteBuffer encoded = charsets[i].encode(input);
41+
JsonParser parser = factory.createParser(encoded.array());
42+
assertThat(parser, Matchers.instanceOf(expectedParsers[i]));
43+
assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.START_OBJECT));
44+
assertThat(parser.nextFieldName(), Matchers.equalTo("foo"));
45+
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
46+
assertThat(parser.getValueAsString(), Matchers.equalTo("bar"));
47+
}
48+
}
49+
50+
// Valid BOM
51+
{
52+
JsonParser parser = factory.createParser(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF, '{', '}' });
53+
assertThat(parser, Matchers.instanceOf(ESUTF8StreamJsonParser.class));
54+
assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.START_OBJECT));
55+
}
56+
57+
// Invalid BOMs
58+
{
59+
JsonParser parser = factory.createParser(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBB, '{', '}' });
60+
assertThat(parser, Matchers.instanceOf(UTF8StreamJsonParser.class));
61+
assertThrows("Invalid UTF-8 start byte 0xbb", JsonParseException.class, parser::nextToken);
62+
}
63+
64+
{
65+
JsonParser parser = factory.createParser(new byte[] { (byte) 0xEF, '{', '}' });
66+
assertThat(parser, Matchers.instanceOf(UTF8StreamJsonParser.class));
67+
assertThrows("Invalid UTF-8 start byte 0x7b", JsonParseException.class, parser::nextToken);
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)