Skip to content

Commit 872984d

Browse files
authored
Continue consolidating XContentParser construction in tests (#22145)
Consolidate more parser creation in tests Moves more parser creation in tests to the `createParser` methods in `ESTestCase`.
1 parent b7bcb5b commit 872984d

File tree

58 files changed

+681
-637
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+681
-637
lines changed

core/src/main/java/org/elasticsearch/common/xcontent/XContentFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
*/
4040
public class XContentFactory {
4141

42-
private static int GUESS_HEADER_LENGTH = 20;
42+
private static final int GUESS_HEADER_LENGTH = 20;
4343

4444
/**
4545
* Returns a content builder using JSON format ({@link org.elasticsearch.common.xcontent.XContentType#JSON}.

core/src/test/java/org/elasticsearch/action/DocWriteResponseTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void testToXContentDoesntIncludeForcedRefreshUnlessForced() throws IOExce
7272
builder.startObject();
7373
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
7474
builder.endObject();
75-
try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) {
75+
try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes())) {
7676
assertThat(parser.map(), not(hasKey("forced_refresh")));
7777
}
7878
}
@@ -81,7 +81,7 @@ public void testToXContentDoesntIncludeForcedRefreshUnlessForced() throws IOExce
8181
builder.startObject();
8282
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
8383
builder.endObject();
84-
try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) {
84+
try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes())) {
8585
assertThat(parser.map(), hasEntry("forced_refresh", true));
8686
}
8787
}

core/src/test/java/org/elasticsearch/action/admin/indices/shards/IndicesShardStoreResponseTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.common.xcontent.XContentFactory;
3232
import org.elasticsearch.common.xcontent.XContentParser;
3333
import org.elasticsearch.common.xcontent.XContentType;
34+
import org.elasticsearch.common.xcontent.json.JsonXContent;
3435
import org.elasticsearch.index.shard.ShardStateMetaData;
3536
import org.elasticsearch.test.ESTestCase;
3637
import org.elasticsearch.transport.NodeDisconnectedException;
@@ -73,7 +74,7 @@ public void testBasicSerialization() throws Exception {
7374
contentBuilder.endObject();
7475
BytesReference bytes = contentBuilder.bytes();
7576

76-
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(bytes)) {
77+
try (XContentParser parser = createParser(JsonXContent.jsonXContent, bytes)) {
7778
Map<String, Object> map = parser.map();
7879
List failureList = (List) map.get("failures");
7980
assertThat(failureList.size(), equalTo(1));

core/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@
4141
import org.elasticsearch.common.bytes.BytesReference;
4242
import org.elasticsearch.common.io.stream.InputStreamStreamInput;
4343
import org.elasticsearch.common.io.stream.OutputStreamStreamOutput;
44-
import org.elasticsearch.common.xcontent.XContentFactory;
4544
import org.elasticsearch.common.xcontent.XContentHelper;
4645
import org.elasticsearch.common.xcontent.XContentParser;
47-
import org.elasticsearch.common.xcontent.XContentType;
46+
import org.elasticsearch.common.xcontent.json.JsonXContent;
4847
import org.elasticsearch.index.mapper.AllFieldMapper;
4948
import org.elasticsearch.index.mapper.FieldMapper;
5049
import org.elasticsearch.index.mapper.MapperParsingException;
@@ -179,7 +178,7 @@ public void testRestRequestParsing() throws Exception {
179178
" {\"fields\" : [\"a\", \"b\",\"c\"], \"offsets\":false, \"positions\":false, \"payloads\":true}");
180179

181180
TermVectorsRequest tvr = new TermVectorsRequest(null, null, null);
182-
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(inputBytes);
181+
XContentParser parser = createParser(JsonXContent.jsonXContent, inputBytes);
183182
TermVectorsRequest.parseRequest(tvr, parser);
184183

185184
Set<String> fields = tvr.selectedFields();
@@ -200,7 +199,7 @@ public void testRestRequestParsing() throws Exception {
200199

201200
inputBytes = new BytesArray(" {\"offsets\":false, \"positions\":false, \"payloads\":true}");
202201
tvr = new TermVectorsRequest(null, null, null);
203-
parser = XContentFactory.xContent(XContentType.JSON).createParser(inputBytes);
202+
parser = createParser(JsonXContent.jsonXContent, inputBytes);
204203
TermVectorsRequest.parseRequest(tvr, parser);
205204
additionalFields = "";
206205
RestTermVectorsAction.addFieldStringsFromParameter(tvr, additionalFields);
@@ -217,7 +216,7 @@ public void testRequestParsingThrowsException() throws Exception {
217216
TermVectorsRequest tvr = new TermVectorsRequest(null, null, null);
218217
boolean threwException = false;
219218
try {
220-
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(inputBytes);
219+
XContentParser parser = createParser(JsonXContent.jsonXContent, inputBytes);
221220
TermVectorsRequest.parseRequest(tvr, parser);
222221
} catch (Exception e) {
223222
threwException = true;

core/src/test/java/org/elasticsearch/cluster/metadata/IndexGraveyardTests.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,16 @@
2020
package org.elasticsearch.cluster.metadata;
2121

2222
import org.elasticsearch.common.UUIDs;
23-
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
2423
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2524
import org.elasticsearch.common.settings.Settings;
2625
import org.elasticsearch.common.xcontent.ToXContent;
2726
import org.elasticsearch.common.xcontent.XContentBuilder;
2827
import org.elasticsearch.common.xcontent.XContentParser;
29-
import org.elasticsearch.common.xcontent.XContentType;
3028
import org.elasticsearch.common.xcontent.json.JsonXContent;
3129
import org.elasticsearch.index.Index;
3230
import org.elasticsearch.test.ESTestCase;
3331

3432
import java.io.IOException;
35-
import java.nio.ByteBuffer;
3633
import java.util.ArrayList;
3734
import java.util.Collections;
3835
import java.util.HashSet;
@@ -69,7 +66,7 @@ public void testXContent() throws IOException {
6966
builder.startObject();
7067
graveyard.toXContent(builder, ToXContent.EMPTY_PARAMS);
7168
builder.endObject();
72-
XContentParser parser = XContentType.JSON.xContent().createParser(builder.bytes());
69+
XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
7370
parser.nextToken(); // the beginning of the parser
7471
assertThat(IndexGraveyard.PROTO.fromXContent(parser), equalTo(graveyard));
7572
}

core/src/test/java/org/elasticsearch/cluster/metadata/IndexMetaDataTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.common.xcontent.ToXContent;
2626
import org.elasticsearch.common.xcontent.XContentBuilder;
2727
import org.elasticsearch.common.xcontent.XContentParser;
28-
import org.elasticsearch.common.xcontent.XContentType;
2928
import org.elasticsearch.common.xcontent.json.JsonXContent;
3029
import org.elasticsearch.index.shard.ShardId;
3130
import org.elasticsearch.test.ESTestCase;
@@ -53,7 +52,7 @@ public void testIndexMetaDataSerialization() throws IOException {
5352
builder.startObject();
5453
metaData.toXContent(builder, ToXContent.EMPTY_PARAMS);
5554
builder.endObject();
56-
XContentParser parser = XContentType.JSON.xContent().createParser(builder.bytes());
55+
XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
5756
final IndexMetaData fromXContentMeta = IndexMetaData.PROTO.fromXContent(parser, null);
5857
assertEquals(metaData, fromXContentMeta);
5958
assertEquals(metaData.hashCode(), fromXContentMeta.hashCode());

core/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.common.xcontent.ToXContent;
2828
import org.elasticsearch.common.xcontent.XContentBuilder;
2929
import org.elasticsearch.common.xcontent.XContentParser;
30-
import org.elasticsearch.common.xcontent.XContentType;
3130
import org.elasticsearch.common.xcontent.json.JsonXContent;
3231
import org.elasticsearch.index.Index;
3332
import org.elasticsearch.test.ESTestCase;
@@ -129,7 +128,7 @@ public void testUnknownFieldClusterMetaData() throws IOException {
129128
.field("random", "value")
130129
.endObject()
131130
.endObject().bytes();
132-
XContentParser parser = JsonXContent.jsonXContent.createParser(metadata);
131+
XContentParser parser = createParser(JsonXContent.jsonXContent, metadata);
133132
try {
134133
MetaData.Builder.fromXContent(parser);
135134
fail();
@@ -145,7 +144,7 @@ public void testUnknownFieldIndexMetaData() throws IOException {
145144
.field("random", "value")
146145
.endObject()
147146
.endObject().bytes();
148-
XContentParser parser = JsonXContent.jsonXContent.createParser(metadata);
147+
XContentParser parser = createParser(JsonXContent.jsonXContent, metadata);
149148
try {
150149
IndexMetaData.Builder.fromXContent(parser);
151150
fail();
@@ -173,7 +172,7 @@ public void testXContentWithIndexGraveyard() throws IOException {
173172
builder.startObject();
174173
originalMeta.toXContent(builder, ToXContent.EMPTY_PARAMS);
175174
builder.endObject();
176-
XContentParser parser = XContentType.JSON.xContent().createParser(builder.bytes());
175+
XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
177176
final MetaData fromXContentMeta = MetaData.PROTO.fromXContent(parser, null);
178177
assertThat(fromXContentMeta.indexGraveyard(), equalTo(originalMeta.indexGraveyard()));
179178
}

core/src/test/java/org/elasticsearch/cluster/metadata/ToAndFromJsonMetaDataTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
import org.elasticsearch.Version;
2323
import org.elasticsearch.common.settings.Settings;
24-
import org.elasticsearch.common.xcontent.XContentFactory;
25-
import org.elasticsearch.common.xcontent.XContentType;
24+
import org.elasticsearch.common.xcontent.json.JsonXContent;
2625
import org.elasticsearch.test.ESTestCase;
2726

2827
import java.io.IOException;
@@ -149,7 +148,7 @@ public void testSimpleJsonFromAndTo() throws IOException {
149148
String metaDataSource = MetaData.Builder.toXContent(metaData);
150149
// System.out.println("ToJson: " + metaDataSource);
151150

152-
MetaData parsedMetaData = MetaData.Builder.fromXContent(XContentFactory.xContent(XContentType.JSON).createParser(metaDataSource));
151+
MetaData parsedMetaData = MetaData.Builder.fromXContent(createParser(JsonXContent.jsonXContent, metaDataSource));
153152

154153
IndexMetaData indexMetaData = parsedMetaData.index("test1");
155154
assertThat(indexMetaData.primaryTerm(0), equalTo(1L));

core/src/test/java/org/elasticsearch/cluster/routing/AllocationIdTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.elasticsearch.common.bytes.BytesReference;
2424
import org.elasticsearch.common.xcontent.ToXContent;
2525
import org.elasticsearch.common.xcontent.XContentFactory;
26-
import org.elasticsearch.common.xcontent.XContentType;
26+
import org.elasticsearch.common.xcontent.json.JsonXContent;
2727
import org.elasticsearch.index.shard.ShardId;
2828
import org.elasticsearch.test.ESTestCase;
2929

@@ -129,7 +129,7 @@ public void testSerialization() throws IOException {
129129
allocationId = AllocationId.newRelocation(allocationId);
130130
}
131131
BytesReference bytes = allocationId.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS).bytes();
132-
AllocationId parsedAllocationId = AllocationId.fromXContent(XContentFactory.xContent(XContentType.JSON).createParser(bytes));
132+
AllocationId parsedAllocationId = AllocationId.fromXContent(createParser(JsonXContent.jsonXContent, bytes));
133133
assertEquals(allocationId, parsedAllocationId);
134134
}
135135
}

core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@
4646
import org.elasticsearch.common.logging.Loggers;
4747
import org.elasticsearch.common.network.NetworkModule;
4848
import org.elasticsearch.common.settings.Settings;
49-
import org.elasticsearch.common.xcontent.XContentFactory;
5049
import org.elasticsearch.common.xcontent.XContentParser;
51-
import org.elasticsearch.common.xcontent.XContentType;
50+
import org.elasticsearch.common.xcontent.json.JsonXContent;
5251
import org.elasticsearch.index.IndexNotFoundException;
5352
import org.elasticsearch.index.shard.ShardId;
5453
import org.elasticsearch.index.shard.ShardNotFoundException;
@@ -486,7 +485,7 @@ public void testXContent() throws Exception {
486485
" ,{\"cancel\" : {\"index\" : \"test\", \"shard\" : 4, \"node\" : \"node5\", \"allow_primary\" : true}} \n" +
487486
" ]\n" +
488487
"}\n";
489-
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(commands);
488+
XContentParser parser = createParser(JsonXContent.jsonXContent, commands);
490489
// move two tokens, parser expected to be "on" `commands` field
491490
parser.nextToken();
492491
parser.nextToken();

0 commit comments

Comments
 (0)