Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public void testCompareParsingTokens() throws IOException {
xsonGen.close();
jsonGen.close();

verifySameTokens(XContentFactory.xContent(XContentType.JSON).createParser(jsonOs.bytes()),
XContentFactory.xContent(XContentType.CBOR).createParser(xsonOs.bytes()));
verifySameTokens(createParser(jsonOs.bytes()), createParser(xsonOs.bytes()));
}

private void verifySameTokens(XContentParser parser1, XContentParser parser2) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public void testCompareParsingTokens() throws IOException {
xsonGen.close();
jsonGen.close();

verifySameTokens(XContentFactory.xContent(XContentType.JSON).createParser(jsonOs.bytes()),
XContentFactory.xContent(XContentType.SMILE).createParser(xsonOs.bytes()));
verifySameTokens(createParser(jsonOs.bytes()), createParser(xsonOs.bytes()));
}

private void verifySameTokens(XContentParser parser1, XContentParser parser2) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testFilter() throws Exception {
.endObject();

Map<String, Object> source;
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
source = parser.map();
}
Map<String, Object> filter = XContentMapValues.filter(source, new String[]{"test1"}, Strings.EMPTY_ARRAY);
Expand Down Expand Up @@ -81,7 +81,7 @@ public void testFilter() throws Exception {
.field("test1", "value1")
.endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
source = parser.map();
}
filter = XContentMapValues.filter(source, new String[]{"path1"}, Strings.EMPTY_ARRAY);
Expand All @@ -107,7 +107,7 @@ public void testExtractValue() throws Exception {
.endObject();

Map<String, Object> map;
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractValue("test", map).toString(), equalTo("value"));
Expand All @@ -118,7 +118,7 @@ public void testExtractValue() throws Exception {
.startObject("path1").startObject("path2").field("test", "value").endObject().endObject()
.endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractValue("path1.path2.test", map).toString(), equalTo("value"));
Expand All @@ -140,7 +140,7 @@ public void testExtractValue() throws Exception {
.startObject("path1").array("test", "value1", "value2").endObject()
.endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
map = parser.map();
}

Expand All @@ -159,7 +159,7 @@ public void testExtractValue() throws Exception {
.endObject()
.endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
map = parser.map();
}

Expand All @@ -175,7 +175,7 @@ public void testExtractValue() throws Exception {
builder = XContentFactory.jsonBuilder().startObject()
.field("xxx.yyy", "value")
.endObject();
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractValue("xxx.yyy", map).toString(), equalTo("value"));
Expand All @@ -184,7 +184,7 @@ public void testExtractValue() throws Exception {
.startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject()
.endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractValue("path1.xxx.path2.yyy.test", map).toString(), equalTo("value"));
Expand All @@ -196,7 +196,7 @@ public void testExtractRawValue() throws Exception {
.endObject();

Map<String, Object> map;
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractRawValues("test", map).get(0).toString(), equalTo("value"));
Expand All @@ -205,7 +205,7 @@ public void testExtractRawValue() throws Exception {
.field("test.me", "value")
.endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractRawValues("test.me", map).get(0).toString(), equalTo("value"));
Expand All @@ -214,7 +214,7 @@ public void testExtractRawValue() throws Exception {
.startObject("path1").startObject("path2").field("test", "value").endObject().endObject()
.endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractRawValues("path1.path2.test", map).get(0).toString(), equalTo("value"));
Expand All @@ -223,7 +223,7 @@ public void testExtractRawValue() throws Exception {
.startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject()
.endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
map = parser.map();
}
assertThat(XContentMapValues.extractRawValues("path1.xxx.path2.yyy.test", map).get(0).toString(), equalTo("value"));
Expand Down Expand Up @@ -475,7 +475,7 @@ public void testEmptyList() throws IOException {
.startArray("some_array")
.endArray().endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("some_array", parser.currentName());
Expand All @@ -495,7 +495,7 @@ public void testSimpleList() throws IOException {
.value(0)
.endArray().endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("some_array", parser.currentName());
Expand All @@ -515,7 +515,7 @@ public void testNestedList() throws IOException {
.startArray().value(2).endArray()
.endArray().endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("some_array", parser.currentName());
Expand All @@ -536,7 +536,7 @@ public void testNestedMapInList() throws IOException {
.startObject().endObject()
.endArray().endObject();

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {
try (XContentParser parser = createParser(builder.string())) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("some_array", parser.currentName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,12 +740,12 @@ public void testMalformedQueryMultipleQueryElements() throws IOException {
expectParsingException(json, "[query] is already defined.");
}

private static void expectParsingException(String json, Matcher<String> messageMatcher) {
private void expectParsingException(String json, Matcher<String> messageMatcher) {
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json));
assertThat(e.getMessage(), messageMatcher);
}

private static void expectParsingException(String json, String message) {
private void expectParsingException(String json, String message) {
expectParsingException(json, equalTo("failed to parse [function_score] query. " + message));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public final void testQueryWrappedInArray() throws IOException {
}
}

private static void queryWrappedInArrayTest(String queryName, String validQuery) throws IOException {
private void queryWrappedInArrayTest(String queryName, String validQuery) throws IOException {
int i = validQuery.indexOf("\"" + queryName + "\"");
assertThat(i, greaterThan(0));

Expand Down Expand Up @@ -544,11 +544,11 @@ protected Map<String, QB> getAlternateVersions() {
/**
* Parses the query provided as string argument and compares it with the expected result provided as argument as a {@link QueryBuilder}
*/
protected static void assertParsedQuery(String queryAsString, QueryBuilder expectedQuery) throws IOException {
protected void assertParsedQuery(String queryAsString, QueryBuilder expectedQuery) throws IOException {
assertParsedQuery(queryAsString, expectedQuery, ParseFieldMatcher.STRICT);
}

protected static void assertParsedQuery(String queryAsString, QueryBuilder expectedQuery, ParseFieldMatcher matcher)
protected void assertParsedQuery(String queryAsString, QueryBuilder expectedQuery, ParseFieldMatcher matcher)
throws IOException {
QueryBuilder newQuery = parseQuery(queryAsString, matcher);
assertNotSame(newQuery, expectedQuery);
Expand All @@ -559,37 +559,37 @@ protected static void assertParsedQuery(String queryAsString, QueryBuilder expec
/**
* Parses the query provided as bytes argument and compares it with the expected result provided as argument as a {@link QueryBuilder}
*/
private static void assertParsedQuery(BytesReference queryAsBytes, QueryBuilder expectedQuery) throws IOException {
private void assertParsedQuery(BytesReference queryAsBytes, QueryBuilder expectedQuery) throws IOException {
assertParsedQuery(queryAsBytes, expectedQuery, ParseFieldMatcher.STRICT);
}

private static void assertParsedQuery(BytesReference queryAsBytes, QueryBuilder expectedQuery, ParseFieldMatcher matcher)
private void assertParsedQuery(BytesReference queryAsBytes, QueryBuilder expectedQuery, ParseFieldMatcher matcher)
throws IOException {
QueryBuilder newQuery = parseQuery(queryAsBytes, matcher);
assertNotSame(newQuery, expectedQuery);
assertEquals(expectedQuery, newQuery);
assertEquals(expectedQuery.hashCode(), newQuery.hashCode());
}

protected static QueryBuilder parseQuery(String queryAsString) throws IOException {
protected QueryBuilder parseQuery(String queryAsString) throws IOException {
return parseQuery(queryAsString, ParseFieldMatcher.STRICT);
}

protected static QueryBuilder parseQuery(String queryAsString, ParseFieldMatcher matcher) throws IOException {
XContentParser parser = XContentFactory.xContent(queryAsString).createParser(queryAsString);
protected QueryBuilder parseQuery(String queryAsString, ParseFieldMatcher matcher) throws IOException {
XContentParser parser = createParser(queryAsString);
return parseQuery(parser, matcher);
}

protected static QueryBuilder parseQuery(BytesReference queryAsBytes) throws IOException {
protected QueryBuilder parseQuery(BytesReference queryAsBytes) throws IOException {
return parseQuery(queryAsBytes, ParseFieldMatcher.STRICT);
}

protected static QueryBuilder parseQuery(BytesReference queryAsBytes, ParseFieldMatcher matcher) throws IOException {
XContentParser parser = XContentFactory.xContent(queryAsBytes).createParser(queryAsBytes);
protected QueryBuilder parseQuery(BytesReference queryAsBytes, ParseFieldMatcher matcher) throws IOException {
XContentParser parser = createParser(queryAsBytes);
return parseQuery(parser, matcher);
}

private static QueryBuilder parseQuery(XContentParser parser, ParseFieldMatcher matcher) throws IOException {
private QueryBuilder parseQuery(XContentParser parser, ParseFieldMatcher matcher) throws IOException {
QueryParseContext context = createParseContext(parser, matcher);
QueryBuilder parseInnerQueryBuilder = context.parseInnerQueryBuilder()
.orElseThrow(() -> new IllegalArgumentException("inner query body cannot be empty"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.elasticsearch.bootstrap.BootstrapForTesting;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.io.PathUtilsForTesting;
Expand All @@ -63,6 +64,7 @@
import org.elasticsearch.common.util.MockPageCacheRecycler;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.env.Environment;
Expand Down Expand Up @@ -878,6 +880,20 @@ public void assertPathHasBeenCleared(Path path) {
assertThat(count + " files exist that should have been cleaned:\n" + sb.toString(), count, equalTo(0));
}

/**
* Create a new {@link XContentParser}.
*/
protected final XContentParser createParser(BytesReference data) throws IOException {
return XContentFactory.xContent(data).createParser(data);
}

/**
* Create a new {@link XContentParser}.
*/
protected final XContentParser createParser(String string) throws IOException {
return createParser(new BytesArray(string));
}

/** Returns the suite failure marker: internal use only! */
public static TestRuleMarkFailure getSuiteFailureMarker() {
return suiteFailureMarker;
Expand Down