Skip to content

Commit

Permalink
Remove deprecated third-party methods from tests (#55255) (#55269)
Browse files Browse the repository at this point in the history
I've noticed that a lot of our tests are using deprecated static methods
from the Hamcrest matchers. While this is not a big deal in any
objective sense, it seems like a small good thing to reduce compilation
warnings and be ready for a new release of the matcher library if we
need to upgrade. I've also switched a few other methods in tests that
have drop-in replacements.
  • Loading branch information
williamrandolph committed Apr 15, 2020
1 parent 6ef1c64 commit 2ba3be9
Show file tree
Hide file tree
Showing 56 changed files with 217 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.isEmptyOrNullString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.not;

public class LicenseIT extends ESRestHighLevelClientTestCase {

Expand Down Expand Up @@ -168,9 +168,9 @@ public void testStartBasic() throws Exception {
private static void assertNotEmptyAcknowledgeMessages(Map<String, String[]> acknowledgeMessages) {
assertThat(acknowledgeMessages.entrySet(), not(empty()));
for (Map.Entry<String, String[]> entry : acknowledgeMessages.entrySet()) {
assertThat(entry.getKey(), not(isEmptyOrNullString()));
assertThat(entry.getKey(), not(emptyOrNullString()));
for (String message : entry.getValue()) {
assertThat(message, not(isEmptyOrNullString()));
assertThat(message, not(emptyOrNullString()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSecondHit;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.either;
Expand Down Expand Up @@ -1234,7 +1235,7 @@ public void testFieldCaps() throws IOException {
FieldCapabilitiesResponse response = execute(request,
highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync);

assertEquals(new String[] {"index1", "index2"}, response.getIndices());
assertThat(response.getIndices(), arrayContaining("index1", "index2"));

// Check the capabilities for the 'rating' field.
assertTrue(response.get().containsKey("rating"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.Matchers.in;

public class BroadcastResponseTests extends AbstractResponseTestCase<org.elasticsearch.action.support.broadcast.BroadcastResponse,
BroadcastResponse> {
Expand Down Expand Up @@ -80,7 +80,7 @@ protected void assertInstances(org.elasticsearch.action.support.broadcast.Broadc
if (clientInstance.shards().failed() > 0) {
final DefaultShardOperationFailedException groupedFailure = clientInstance.shards().failures().iterator().next();
assertThat(groupedFailure.index(), equalTo(index));
assertThat(groupedFailure.shardId(), isIn(shardIds));
assertThat(groupedFailure.shardId(), in(shardIds));
assertThat(groupedFailure.reason(), containsString("reason=retention lease with ID [" + id + "] not found"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.isOneOf;
import static org.hamcrest.Matchers.oneOf;

public class RollupDocumentationIT extends ESRestHighLevelClientTestCase {

Expand Down Expand Up @@ -429,7 +429,7 @@ public void testGetRollupCaps() throws Exception {
ClusterHealthRequest healthRequest = new ClusterHealthRequest(config.getRollupIndex()).waitForYellowStatus();
ClusterHealthResponse healthResponse = client.cluster().health(healthRequest, RequestOptions.DEFAULT);
assertFalse(healthResponse.isTimedOut());
assertThat(healthResponse.getStatus(), isOneOf(ClusterHealthStatus.YELLOW, ClusterHealthStatus.GREEN));
assertThat(healthResponse.getStatus(), oneOf(ClusterHealthStatus.YELLOW, ClusterHealthStatus.GREEN));

// Now that the job is created, we should have a rollup index with metadata.
// We can test out the caps API now.
Expand Down Expand Up @@ -546,7 +546,7 @@ public void testGetRollupIndexCaps() throws Exception {
ClusterHealthRequest healthRequest = new ClusterHealthRequest(config.getRollupIndex()).waitForYellowStatus();
ClusterHealthResponse healthResponse = client.cluster().health(healthRequest, RequestOptions.DEFAULT);
assertFalse(healthResponse.isTimedOut());
assertThat(healthResponse.getStatus(), isOneOf(ClusterHealthStatus.YELLOW, ClusterHealthStatus.GREEN));
assertThat(healthResponse.getStatus(), oneOf(ClusterHealthStatus.YELLOW, ClusterHealthStatus.GREEN));

// Now that the job is created, we should have a rollup index with metadata.
// We can test out the caps API now.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.in;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.Matchers.iterableWithSize;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -491,7 +491,7 @@ public void testGetRoleMappings() throws Exception {
assertThat(mappings.size(), is(2));
for (ExpressionRoleMapping roleMapping : mappings) {
assertThat(roleMapping.isEnabled(), is(true));
assertThat(roleMapping.getName(), isIn(new String[]{"mapping-example-1", "mapping-example-2"}));
assertThat(roleMapping.getName(), in(new String[]{"mapping-example-1", "mapping-example-2"}));
if (roleMapping.getName().equals("mapping-example-1")) {
assertThat(roleMapping.getMetadata(), equalTo(Collections.emptyMap()));
assertThat(roleMapping.getExpression(), equalTo(rules1));
Expand All @@ -516,7 +516,7 @@ public void testGetRoleMappings() throws Exception {
assertThat(mappings.size(), is(2));
for (ExpressionRoleMapping roleMapping : mappings) {
assertThat(roleMapping.isEnabled(), is(true));
assertThat(roleMapping.getName(), isIn(new String[]{"mapping-example-1", "mapping-example-2"}));
assertThat(roleMapping.getName(), in(new String[]{"mapping-example-1", "mapping-example-2"}));
if (roleMapping.getName().equals("mapping-example-1")) {
assertThat(roleMapping.getMetadata(), equalTo(Collections.emptyMap()));
assertThat(roleMapping.getExpression(), equalTo(rules1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.Matchers.in;

public class ReloadAnalyzersResponseTests
extends AbstractResponseTestCase<org.elasticsearch.xpack.core.action.ReloadAnalyzersResponse, ReloadAnalyzersResponse> {
Expand Down Expand Up @@ -94,7 +94,7 @@ protected void assertInstances(org.elasticsearch.xpack.core.action.ReloadAnalyze
if (clientInstance.shards().failed() > 0) {
final DefaultShardOperationFailedException groupedFailure = clientInstance.shards().failures().iterator().next();
assertThat(groupedFailure.index(), equalTo(index));
assertThat(groupedFailure.shardId(), isIn(shardIds));
assertThat(groupedFailure.shardId(), in(shardIds));
assertThat(groupedFailure.reason(), containsString("reason=retention lease with ID [" + id + "] not found"));
}
Map<String, ReloadDetails> serverDetails = serverTestInstance.getReloadDetails();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public long getContentLength() {
return contentLength.get();
}
};
contentLength.set(randomLong(bufferLimit));
contentLength.set(randomLongBetween(0L, bufferLimit));
consumer.onEntityEnclosed(entity, ContentType.APPLICATION_JSON);

contentLength.set(randomLongBetween(bufferLimit + 1, MAX_TEST_BUFFER_SIZE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;

public class ParseFieldTests extends ESTestCase {
public void testParse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.in;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage;

Expand Down Expand Up @@ -208,7 +208,7 @@ public void testReadBooleansFailsForLenientBooleans() throws IOException {
assertThat(token, equalTo(XContentParser.Token.FIELD_NAME));
assertThat(parser.currentName(), equalTo("foo"));
token = parser.nextToken();
assertThat(token, isIn(Arrays.asList(XContentParser.Token.VALUE_STRING, XContentParser.Token.VALUE_NUMBER)));
assertThat(token, in(Arrays.asList(XContentParser.Token.VALUE_STRING, XContentParser.Token.VALUE_NUMBER)));
assertFalse(parser.isBooleanValue());
if (token.equals(XContentParser.Token.VALUE_STRING)) {
expectThrows(IllegalArgumentException.class, parser::booleanValue);
Expand All @@ -220,7 +220,7 @@ public void testReadBooleansFailsForLenientBooleans() throws IOException {
assertThat(token, equalTo(XContentParser.Token.FIELD_NAME));
assertThat(parser.currentName(), equalTo("bar"));
token = parser.nextToken();
assertThat(token, isIn(Arrays.asList(XContentParser.Token.VALUE_STRING, XContentParser.Token.VALUE_NUMBER)));
assertThat(token, in(Arrays.asList(XContentParser.Token.VALUE_STRING, XContentParser.Token.VALUE_NUMBER)));
assertFalse(parser.isBooleanValue());
if (token.equals(XContentParser.Token.VALUE_STRING)) {
expectThrows(IllegalArgumentException.class, parser::booleanValue);
Expand All @@ -241,15 +241,15 @@ public void testReadBooleans() throws IOException {
assertThat(token, equalTo(XContentParser.Token.FIELD_NAME));
assertThat(parser.currentName(), equalTo("foo"));
token = parser.nextToken();
assertThat(token, isIn(Arrays.asList(XContentParser.Token.VALUE_STRING, XContentParser.Token.VALUE_BOOLEAN)));
assertThat(token, in(Arrays.asList(XContentParser.Token.VALUE_STRING, XContentParser.Token.VALUE_BOOLEAN)));
assertTrue(parser.isBooleanValue());
assertFalse(parser.booleanValue());

token = parser.nextToken();
assertThat(token, equalTo(XContentParser.Token.FIELD_NAME));
assertThat(parser.currentName(), equalTo("bar"));
token = parser.nextToken();
assertThat(token, isIn(Arrays.asList(XContentParser.Token.VALUE_STRING, XContentParser.Token.VALUE_BOOLEAN)));
assertThat(token, in(Arrays.asList(XContentParser.Token.VALUE_STRING, XContentParser.Token.VALUE_BOOLEAN)));
assertTrue(parser.isBooleanValue());
assertTrue(parser.booleanValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.isEmptyOrNullString;
import static org.hamcrest.Matchers.not;

public class MustacheTests extends ESTestCase {
Expand Down Expand Up @@ -384,7 +384,7 @@ private void assertScript(String script, Map<String, Object> vars, Matcher<Strin
}

private TemplateScript.Factory compile(String script) {
assertThat("cannot compile null or empty script", script, not(isEmptyOrNullString()));
assertThat("cannot compile null or empty script", script, not(emptyOrNullString()));
return engine.compile(null, script, TemplateScript.CONTEXT, Collections.emptyMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@

import static java.util.Arrays.asList;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasProperty;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder;
import static org.hamcrest.core.IsInstanceOf.instanceOf;

public class SearchAsYouTypeFieldMapperTests extends ESSingleNodeTestCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.IsCollectionContaining.hasItem;

public class AttachmentProcessorTests extends ESTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@

import static org.elasticsearch.repositories.azure.AzureStorageService.blobNameFromUri;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

Expand All @@ -58,8 +58,8 @@ public void testReadSecuredSettings() {
final Map<String, AzureStorageSettings> loadedSettings = AzureStorageSettings.load(settings);
assertThat(loadedSettings.keySet(), containsInAnyOrder("azure1","azure2","azure3","default"));

assertThat(loadedSettings.get("azure1").getEndpointSuffix(), isEmptyString());
assertThat(loadedSettings.get("azure2").getEndpointSuffix(), isEmptyString());
assertThat(loadedSettings.get("azure1").getEndpointSuffix(), is(emptyString()));
assertThat(loadedSettings.get("azure2").getEndpointSuffix(), is(emptyString()));
assertThat(loadedSettings.get("azure3").getEndpointSuffix(), equalTo("my_endpoint_suffix"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import java.util.Map;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.nullValue;

public class S3ClientSettingsTests extends ESTestCase {
Expand All @@ -41,12 +41,12 @@ public void testThereIsADefaultClientByDefault() {

final S3ClientSettings defaultSettings = settings.get("default");
assertThat(defaultSettings.credentials, nullValue());
assertThat(defaultSettings.endpoint, isEmptyString());
assertThat(defaultSettings.endpoint, is(emptyString()));
assertThat(defaultSettings.protocol, is(Protocol.HTTPS));
assertThat(defaultSettings.proxyHost, isEmptyString());
assertThat(defaultSettings.proxyHost, is(emptyString()));
assertThat(defaultSettings.proxyPort, is(80));
assertThat(defaultSettings.proxyUsername, isEmptyString());
assertThat(defaultSettings.proxyPassword, isEmptyString());
assertThat(defaultSettings.proxyUsername, is(emptyString()));
assertThat(defaultSettings.proxyPassword, is(emptyString()));
assertThat(defaultSettings.readTimeoutMillis, is(ClientConfiguration.DEFAULT_SOCKET_TIMEOUT));
assertThat(defaultSettings.maxRetries, is(ClientConfiguration.DEFAULT_RETRY_POLICY.getMaxErrorRetry()));
assertThat(defaultSettings.throttleRetries, is(ClientConfiguration.DEFAULT_THROTTLE_RETRIES));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import java.util.concurrent.atomic.AtomicBoolean;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.is;

public class EvilCommandTests extends ESTestCase {

Expand Down Expand Up @@ -62,7 +63,7 @@ public void close() throws IOException {
// ensure that we dump the stack trace too
assertThat(output, containsString("\tat org.elasticsearch.cli.EvilCommandTests$1.close"));
} else {
assertThat(output, isEmptyString());
assertThat(output, is(emptyString()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeThat;
Expand All @@ -76,7 +76,7 @@ public void test20PluginsListWithNoPlugins() throws Exception {
final Installation.Executables bin = installation.executables();
final Result r = bin.pluginTool.run("list");

assertThat(r.stdout, isEmptyString());
assertThat(r.stdout, emptyString());
}

public void test30MissingBundledJdk() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
import static org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.in;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.oneOf;
Expand Down Expand Up @@ -278,7 +278,7 @@ public void testRelocationWithConcurrentIndexing() throws Exception {
String xpath = "routing_table.indices." + index + ".shards.0.node";
@SuppressWarnings("unchecked") List<String> assignedNodes = (List<String>) XContentMapValues.extractValue(xpath, state);
assertNotNull(state.toString(), assignedNodes);
assertThat(state.toString(), newNode, isIn(assignedNodes));
assertThat(state.toString(), newNode, in(assignedNodes));
}, 60, TimeUnit.SECONDS);
ensureGreen(index);
client().performRequest(new Request("POST", index + "/_refresh"));
Expand Down
Loading

0 comments on commit 2ba3be9

Please sign in to comment.