Skip to content

Commit

Permalink
Rename assertThrows to prevent naming clash (#52582)
Browse files Browse the repository at this point in the history
This commit renames ElasticsearchAssertions#assertThrows to
assertRequestBuilderThrows and assertFutureThrows to avoid a
naming clash with JUnit 4.13+ and static imports of these methods.
Additionally, these methods have been updated to make use of
expectThrows internally to avoid duplicating the logic there.

Relates #51787
  • Loading branch information
jaymode authored Feb 21, 2020
1 parent ae68e4f commit f83d75d
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@
import static org.elasticsearch.common.unit.TimeValue.timeValueMillis;
import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
import static org.elasticsearch.http.HttpTransportSettings.SETTING_HTTP_MAX_HEADER_SIZE;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFutureThrows;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
Expand Down Expand Up @@ -786,7 +786,7 @@ public void testTaskStoringFailureResult() throws Exception {
request.setShouldBlock(false);

// Start non-blocking test task that should fail
assertThrows(
assertFutureThrows(
client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request),
IllegalStateException.class
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_WAIT_FOR_ACTIVE_SHARDS;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBlocked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand Down Expand Up @@ -303,7 +303,7 @@ public void testFailureToCreateIndexCleansUpIndicesService() {
.addAlias(new Alias("alias1").writeIndex(true))
.get());

assertThrows(client().admin().indices().prepareCreate("test-idx-2")
assertRequestBuilderThrows(client().admin().indices().prepareCreate("test-idx-2")
.setSettings(settings)
.addAlias(new Alias("alias1").writeIndex(true)),
IllegalStateException.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
Expand Down Expand Up @@ -368,7 +368,7 @@ public void testDuelESLucene() throws Exception {
for (TestConfig test : testConfigs) {
TermVectorsRequestBuilder request = getRequestForConfig(test);
if (test.expectedException != null) {
assertThrows(request, test.expectedException);
assertRequestBuilderThrows(request, test.expectedException);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertExists;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;

Expand Down Expand Up @@ -96,35 +96,35 @@ public void testNoMasterActions() throws Exception {
assertTrue(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID));
});

assertThrows(clientToMasterlessNode.prepareGet("test", "1"),
assertRequestBuilderThrows(clientToMasterlessNode.prepareGet("test", "1"),
ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE
);

assertThrows(clientToMasterlessNode.prepareGet("no_index", "1"),
assertRequestBuilderThrows(clientToMasterlessNode.prepareGet("no_index", "1"),
ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE
);

assertThrows(clientToMasterlessNode.prepareMultiGet().add("test", "1"),
assertRequestBuilderThrows(clientToMasterlessNode.prepareMultiGet().add("test", "1"),
ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE
);

assertThrows(clientToMasterlessNode.prepareMultiGet().add("no_index", "1"),
assertRequestBuilderThrows(clientToMasterlessNode.prepareMultiGet().add("no_index", "1"),
ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE
);

assertThrows(clientToMasterlessNode.admin().indices().prepareAnalyze("test", "this is a test"),
assertRequestBuilderThrows(clientToMasterlessNode.admin().indices().prepareAnalyze("test", "this is a test"),
ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE
);

assertThrows(clientToMasterlessNode.admin().indices().prepareAnalyze("no_index", "this is a test"),
assertRequestBuilderThrows(clientToMasterlessNode.admin().indices().prepareAnalyze("no_index", "this is a test"),
ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE
);

assertThrows(clientToMasterlessNode.prepareSearch("test").setSize(0),
assertRequestBuilderThrows(clientToMasterlessNode.prepareSearch("test").setSize(0),
ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE
);

assertThrows(clientToMasterlessNode.prepareSearch("no_index").setSize(0),
assertRequestBuilderThrows(clientToMasterlessNode.prepareSearch("no_index").setSize(0),
ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_READ_ONLY;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBlocked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
Expand Down Expand Up @@ -452,7 +452,8 @@ public void testEngineGCDeletesSetting() throws Exception {
client().prepareIndex("test").setId("1").setSource("f", 1).setVersionType(VersionType.EXTERNAL).setVersion(1).get();
client().prepareDelete("test", "1").setVersionType(VersionType.EXTERNAL).setVersion(2).get();
// delete is still in cache this should fail
assertThrows(client().prepareIndex("test").setId("1").setSource("f", 3).setVersionType(VersionType.EXTERNAL).setVersion(1),
assertRequestBuilderThrows(
client().prepareIndex("test").setId("1").setSource("f", 3).setVersionType(VersionType.EXTERNAL).setVersion(1),
VersionConflictEngineException.class);

assertAcked(client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put("index.gc_deletes", 0)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -109,7 +109,7 @@ public void testSimpleIndexTemplateTests() throws Exception {
.get();

// test create param
assertThrows(client().admin().indices().preparePutTemplate("template_2")
assertRequestBuilderThrows(client().admin().indices().preparePutTemplate("template_2")
.setPatterns(Collections.singletonList("test*"))
.setSettings(indexSettings())
.setCreate(true)
Expand Down Expand Up @@ -329,7 +329,7 @@ public void testThatInvalidGetIndexTemplatesFails() throws Exception {
}

private void testExpectActionRequestValidationException(String... names) {
assertThrows(client().admin().indices().prepareGetTemplates(names),
assertRequestBuilderThrows(client().admin().indices().prepareGetTemplates(names),
ActionRequestValidationException.class,
"get template with " + Arrays.toString(names));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.tasks.TaskInfo;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.persistent.PersistentTasksCustomMetaData.PersistentTask;
import org.elasticsearch.persistent.PersistentTasksService.WaitForPersistentTaskListener;
import org.elasticsearch.persistent.TestPersistentTasksPlugin.State;
import org.elasticsearch.persistent.TestPersistentTasksPlugin.TestPersistentTasksExecutor;
import org.elasticsearch.persistent.TestPersistentTasksPlugin.TestParams;
import org.elasticsearch.persistent.TestPersistentTasksPlugin.TestPersistentTasksExecutor;
import org.elasticsearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.tasks.TaskInfo;
import org.elasticsearch.test.ESIntegTestCase;
import org.junit.After;
import org.junit.Before;

Expand All @@ -43,7 +43,7 @@
import java.util.List;
import java.util.Objects;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFutureThrows;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
Expand Down Expand Up @@ -132,7 +132,7 @@ public void testPersistentActionCompletion() throws Exception {
//try sending completion request with incorrect allocation id
PlainActionFuture<PersistentTask<?>> failedCompletionNotificationFuture = new PlainActionFuture<>();
persistentTasksService.sendCompletionRequest(taskId, Long.MAX_VALUE, null, failedCompletionNotificationFuture);
assertThrows(failedCompletionNotificationFuture, ResourceNotFoundException.class);
assertFutureThrows(failedCompletionNotificationFuture, ResourceNotFoundException.class);
// Make sure that the task is still running
assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]")
.setDetailed(true).get().getTasks().size(), equalTo(1));
Expand Down Expand Up @@ -240,11 +240,11 @@ public void testPersistentActionStatusUpdate() throws Exception {
persistentTasksService.waitForPersistentTaskCondition(taskId,
task -> false, TimeValue.timeValueMillis(10), future1);

assertThrows(future1, IllegalStateException.class, "timed out after 10ms");
assertFutureThrows(future1, IllegalStateException.class, "timed out after 10ms");

PlainActionFuture<PersistentTask<?>> failedUpdateFuture = new PlainActionFuture<>();
persistentTasksService.sendUpdateStateRequest(taskId, -2, new State("should fail"), failedUpdateFuture);
assertThrows(failedUpdateFuture, ResourceNotFoundException.class, "the task with id " + taskId +
assertFutureThrows(failedUpdateFuture, ResourceNotFoundException.class, "the task with id " + taskId +
" and allocation id -2 doesn't exist");

// Wait for the task to disappear
Expand All @@ -268,7 +268,7 @@ public void testCreatePersistentTaskWithDuplicateId() throws Exception {

PlainActionFuture<PersistentTask<TestParams>> future2 = new PlainActionFuture<>();
persistentTasksService.sendStartRequest(taskId, TestPersistentTasksExecutor.NAME, new TestParams("Blah"), future2);
assertThrows(future2, ResourceAlreadyExistsException.class);
assertFutureThrows(future2, ResourceAlreadyExistsException.class);

assertBusy(() -> {
// Wait for the task to start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.concurrent.TimeUnit;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFutureThrows;
import static org.hamcrest.Matchers.is;

@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0)
Expand All @@ -59,7 +59,7 @@ public void testRepeatCleanupsDontRemove() throws Exception {
final String masterNode = startBlockedCleanup("test-repo");

logger.info("--> sending another cleanup");
assertThrows(client().admin().cluster().prepareCleanupRepository("test-repo").execute(), IllegalStateException.class);
assertFutureThrows(client().admin().cluster().prepareCleanupRepository("test-repo").execute(), IllegalStateException.class);

logger.info("--> ensure cleanup is still in progress");
final RepositoryCleanupInProgress cleanup =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -319,12 +319,12 @@ public void testNumericField() throws Exception {
assertHitCount(searchResponse, 1L);

// Explicit list of fields including numeric fields -> fail
assertThrows(client().prepareSearch().setQuery(
assertRequestBuilderThrows(client().prepareSearch().setQuery(
new MoreLikeThisQueryBuilder(new String[] {"string_value", "int_value"}, null,
new Item[] {new Item("test", "1")}).minTermFreq(1).minDocFreq(1)), SearchPhaseExecutionException.class);

// mlt query with no field -> exception because _all is not enabled)
assertThrows(client().prepareSearch()
assertRequestBuilderThrows(client().prepareSearch()
.setQuery(moreLikeThisQuery(new String[] {"index"}).minTermFreq(1).minDocFreq(1)),
SearchPhaseExecutionException.class);

Expand All @@ -334,12 +334,12 @@ public void testNumericField() throws Exception {
assertHitCount(searchResponse, 2L);

// mlt query with at least a numeric field -> fail by default
assertThrows(client().prepareSearch().setQuery(
assertRequestBuilderThrows(client().prepareSearch().setQuery(
moreLikeThisQuery(new String[] {"string_value", "int_value"}, new String[] {"index"}, null)),
SearchPhaseExecutionException.class);

// mlt query with at least a numeric field -> fail by command
assertThrows(client().prepareSearch().setQuery(
assertRequestBuilderThrows(client().prepareSearch().setQuery(
moreLikeThisQuery(new String[] {"string_value", "int_value"}, new String[] {"index"}, null).failOnUnsupportedField(true)),
SearchPhaseExecutionException.class);

Expand All @@ -351,12 +351,12 @@ public void testNumericField() throws Exception {
assertHitCount(searchResponse, 2L);

// mlt field query on a numeric field -> failure by default
assertThrows(client().prepareSearch().setQuery(
assertRequestBuilderThrows(client().prepareSearch().setQuery(
moreLikeThisQuery(new String[] {"int_value"}, new String[] {"42"}, null).minTermFreq(1).minDocFreq(1)),
SearchPhaseExecutionException.class);

// mlt field query on a numeric field -> failure by command
assertThrows(client().prepareSearch().setQuery(
assertRequestBuilderThrows(client().prepareSearch().setQuery(
moreLikeThisQuery(new String[] {"int_value"}, new String[] {"42"}, null).minTermFreq(1).minDocFreq(1)
.failOnUnsupportedField(true)),
SearchPhaseExecutionException.class);
Expand Down
Loading

0 comments on commit f83d75d

Please sign in to comment.