Randomize XContentType for requests

The used XContentType is static and should be randomized per test.

Closes #5515
This commit is contained in:
Simon Willnauer 2014-03-24 12:27:16 +01:00
parent c2e6aa273d
commit 8709427b4a
11 changed files with 30 additions and 42 deletions

View File

@ -33,7 +33,6 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.util.Arrays;
@ -47,8 +46,6 @@ import java.util.Map;
*/
public class ValidateQueryRequest extends BroadcastOperationRequest<ValidateQueryRequest> {
private static final XContentType contentType = Requests.CONTENT_TYPE;
private BytesReference source;
private boolean sourceUnsafe;
@ -93,7 +90,7 @@ public class ValidateQueryRequest extends BroadcastOperationRequest<ValidateQuer
}
public ValidateQueryRequest source(QuerySourceBuilder sourceBuilder) {
this.source = sourceBuilder.buildAsBytes(contentType);
this.source = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
this.sourceUnsafe = false;
return this;
}
@ -103,7 +100,7 @@ public class ValidateQueryRequest extends BroadcastOperationRequest<ValidateQuer
*/
public ValidateQueryRequest source(Map source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
builder.map(source);
return source(builder);
} catch (IOException e) {

View File

@ -33,7 +33,6 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.util.Arrays;
@ -52,8 +51,6 @@ import java.util.Map;
*/
public class CountRequest extends BroadcastOperationRequest<CountRequest> {
private static final XContentType contentType = Requests.CONTENT_TYPE;
public static final float DEFAULT_MIN_SCORE = -1f;
private float minScore = DEFAULT_MIN_SCORE;
@ -123,7 +120,7 @@ public class CountRequest extends BroadcastOperationRequest<CountRequest> {
* The source to execute.
*/
public CountRequest source(QuerySourceBuilder sourceBuilder) {
this.source = sourceBuilder.buildAsBytes(contentType);
this.source = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
this.sourceUnsafe = false;
return this;
}
@ -133,7 +130,7 @@ public class CountRequest extends BroadcastOperationRequest<CountRequest> {
*/
public CountRequest source(Map querySource) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
builder.map(querySource);
return source(builder);
} catch (IOException e) {

View File

@ -34,7 +34,6 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.util.Arrays;
@ -55,8 +54,6 @@ import static org.elasticsearch.action.ValidateActions.addValidationError;
*/
public class DeleteByQueryRequest extends IndicesReplicationOperationRequest<DeleteByQueryRequest> {
private static final XContentType contentType = Requests.CONTENT_TYPE;
private BytesReference source;
private boolean sourceUnsafe;
@ -98,7 +95,7 @@ public class DeleteByQueryRequest extends IndicesReplicationOperationRequest<Del
* The source to execute.
*/
public DeleteByQueryRequest source(QuerySourceBuilder sourceBuilder) {
this.source = sourceBuilder.buildAsBytes(contentType);
this.source = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
this.sourceUnsafe = false;
return this;
}
@ -118,7 +115,7 @@ public class DeleteByQueryRequest extends IndicesReplicationOperationRequest<Del
*/
public DeleteByQueryRequest source(Map source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
builder.map(source);
return source(builder);
} catch (IOException e) {

View File

@ -28,7 +28,6 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.fetch.source.FetchSourceContext;
import java.io.IOException;
@ -38,8 +37,6 @@ import java.io.IOException;
*/
public class ExplainRequest extends SingleShardOperationRequest<ExplainRequest> {
private static final XContentType contentType = Requests.CONTENT_TYPE;
private String type = "_all";
private String id;
private String routing;
@ -115,7 +112,7 @@ public class ExplainRequest extends SingleShardOperationRequest<ExplainRequest>
}
public ExplainRequest source(QuerySourceBuilder sourceBuilder) {
this.source = sourceBuilder.buildAsBytes(contentType);
this.source = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
this.sourceUnsafe = false;
return this;
}

View File

@ -33,7 +33,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.builder.SearchSourceBuilder;
@ -54,8 +53,6 @@ import static org.elasticsearch.search.Scroll.readScroll;
*/
public class MoreLikeThisRequest extends ActionRequest<MoreLikeThisRequest> {
private static final XContentType contentType = Requests.CONTENT_TYPE;
private String index;
private String type;
@ -345,7 +342,7 @@ public class MoreLikeThisRequest extends ActionRequest<MoreLikeThisRequest> {
public MoreLikeThisRequest searchSource(Map searchSource) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
builder.map(searchSource);
return searchSource(builder);
} catch (IOException e) {

View File

@ -41,8 +41,6 @@ import static org.elasticsearch.action.ValidateActions.addValidationError;
*/
public class PercolateRequest extends BroadcastOperationRequest<PercolateRequest> {
public static final XContentType contentType = Requests.CONTENT_TYPE;
private String documentType;
private String routing;
private String preference;
@ -123,7 +121,7 @@ public class PercolateRequest extends BroadcastOperationRequest<PercolateRequest
}
public PercolateRequest source(Map document) throws ElasticsearchGenerationException {
return source(document, contentType);
return source(document, Requests.CONTENT_TYPE);
}
public PercolateRequest source(Map document, XContentType contentType) throws ElasticsearchGenerationException {
@ -167,7 +165,7 @@ public class PercolateRequest extends BroadcastOperationRequest<PercolateRequest
}
public PercolateRequest source(PercolateSourceBuilder sourceBuilder) {
this.source = sourceBuilder.buildAsBytes(contentType);
this.source = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
this.unsafe = false;
return this;
}

View File

@ -21,6 +21,7 @@ package org.elasticsearch.action.percolate;
import com.google.common.collect.Lists;
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.*;
@ -262,7 +263,7 @@ public class PercolateSourceBuilder implements ToXContent {
}
public DocBuilder setDoc(Map doc) {
return setDoc(doc, PercolateRequest.contentType);
return setDoc(doc, Requests.CONTENT_TYPE);
}
public DocBuilder setDoc(Map doc, XContentType contentType) {

View File

@ -35,7 +35,6 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.builder.SearchSourceBuilder;
@ -60,8 +59,6 @@ import static org.elasticsearch.search.Scroll.readScroll;
*/
public class SearchRequest extends ActionRequest<SearchRequest> {
private static final XContentType contentType = Requests.CONTENT_TYPE;
private SearchType searchType = SearchType.DEFAULT;
private String[] indices;
@ -266,7 +263,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> {
* The source of the search request.
*/
public SearchRequest source(SearchSourceBuilder sourceBuilder) {
this.source = sourceBuilder.buildAsBytes(contentType);
this.source = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
this.sourceUnsafe = false;
return this;
}
@ -286,7 +283,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> {
*/
public SearchRequest source(Map source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
builder.map(source);
return source(builder);
} catch (IOException e) {
@ -353,14 +350,14 @@ public class SearchRequest extends ActionRequest<SearchRequest> {
extraSource = null;
return this;
}
this.extraSource = sourceBuilder.buildAsBytes(contentType);
this.extraSource = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
this.extraSourceUnsafe = false;
return this;
}
public SearchRequest extraSource(Map extraSource) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
builder.map(extraSource);
return extraSource(builder);
} catch (IOException e) {

View File

@ -47,8 +47,6 @@ import org.elasticsearch.common.xcontent.XContentType;
*/
public final class SuggestRequest extends BroadcastOperationRequest<SuggestRequest> {
static final XContentType contentType = Requests.CONTENT_TYPE;
@Nullable
private String routing;

View File

@ -23,6 +23,7 @@ import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.internal.InternalClient;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -86,7 +87,7 @@ public class SuggestRequestBuilder extends BroadcastOperationRequestBuilder<Sugg
@Override
protected void doExecute(ActionListener<SuggestResponse> listener) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(SuggestRequest.contentType);
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
suggest.toXContent(builder, ToXContent.EMPTY_PARAMS);
request.suggest(builder.bytes());
} catch (IOException e) {

View File

@ -18,8 +18,11 @@
*/
package org.elasticsearch.test;
import com.carrotsearch.randomizedtesting.annotations.*;
import com.carrotsearch.randomizedtesting.annotations.Listeners;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope.Scope;
import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import org.apache.lucene.store.MockDirectoryWrapper;
@ -28,11 +31,13 @@ import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TimeUnits;
import org.elasticsearch.Version;
import org.elasticsearch.cache.recycler.MockPageCacheRecycler;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.util.concurrent.EsAbortPolicy;
import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.cache.recycler.MockBigArrays;
import org.elasticsearch.test.engine.MockInternalEngine;
import org.elasticsearch.test.junit.listeners.LoggingListener;
@ -220,14 +225,13 @@ public abstract class ElasticsearchTestCase extends AbstractRandomizedTest {
}
@BeforeClass
public static void registerMockDirectoryHooks() throws Exception {
public static void setBeforeClass() throws Exception {
closeAfterSuite(new Closeable() {
@Override
public void close() throws IOException {
ensureAllFilesClosed();
}
});
closeAfterSuite(new Closeable() {
@Override
public void close() throws IOException {
@ -236,11 +240,15 @@ public abstract class ElasticsearchTestCase extends AbstractRandomizedTest {
});
defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new ElasticsearchUncaughtExceptionHandler(defaultHandler));
Requests.CONTENT_TYPE = randomFrom(XContentType.values());
Requests.INDEX_CONTENT_TYPE = randomFrom(XContentType.values());
}
@AfterClass
public static void resetUncaughtExceptionHandler() {
public static void resetAfterClass() {
Thread.setDefaultUncaughtExceptionHandler(defaultHandler);
Requests.CONTENT_TYPE = XContentType.SMILE;
Requests.INDEX_CONTENT_TYPE = XContentType.JSON;
}
public static boolean maybeDocValues() {