Skip to content

Commit ae8dee3

Browse files
Remove ExceptionsHelper#detailedMessage in tests (#37921)
With this commit we remove all usages of the deprecated method `ExceptionsHelper#detailedMessage` in tests. We do not address production code here but rather in dedicated follow-up PRs to keep the individual changes manageable. Relates #19069
1 parent 183d921 commit ae8dee3

File tree

18 files changed

+60
-58
lines changed

18 files changed

+60
-58
lines changed

server/src/test/java/org/elasticsearch/cluster/service/MasterServiceTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.logging.log4j.Level;
2222
import org.apache.logging.log4j.Logger;
2323
import org.apache.logging.log4j.LogManager;
24-
import org.elasticsearch.ExceptionsHelper;
2524
import org.elasticsearch.Version;
2625
import org.elasticsearch.cluster.AckedClusterStateUpdateTask;
2726
import org.elasticsearch.cluster.ClusterChangedEvent;
@@ -523,7 +522,7 @@ public void clusterStatePublished(ClusterChangedEvent clusterChangedEvent) {
523522
final ClusterStateTaskListener listener = new ClusterStateTaskListener() {
524523
@Override
525524
public void onFailure(String source, Exception e) {
526-
fail(ExceptionsHelper.detailedMessage(e));
525+
throw new AssertionError(e);
527526
}
528527

529528
@Override

server/src/test/java/org/elasticsearch/cluster/service/TaskBatcherTests.java

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

2222
import org.apache.logging.log4j.Logger;
2323
import org.apache.logging.log4j.message.ParameterizedMessage;
24-
import org.elasticsearch.ExceptionsHelper;
2524
import org.elasticsearch.cluster.ClusterStateTaskConfig;
2625
import org.elasticsearch.cluster.metadata.ProcessClusterEventTimeoutException;
2726
import org.elasticsearch.common.Priority;
@@ -273,7 +272,7 @@ public void processed(String source) {
273272

274273
@Override
275274
public void onFailure(String source, Exception e) {
276-
fail(ExceptionsHelper.detailedMessage(e));
275+
throw new AssertionError(e);
277276
}
278277
});
279278
usedKeys.add(key);
@@ -304,7 +303,7 @@ public void processed(String source) {
304303

305304
@Override
306305
public void onFailure(String source, Exception e) {
307-
fail(ExceptionsHelper.detailedMessage(e));
306+
throw new AssertionError(e);
308307
}
309308
};
310309

server/src/test/java/org/elasticsearch/common/joda/JodaDateMathParserTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.elasticsearch.common.joda;
2121

2222
import org.elasticsearch.ElasticsearchParseException;
23-
import org.elasticsearch.ExceptionsHelper;
2423
import org.elasticsearch.common.time.DateFormatter;
2524
import org.elasticsearch.common.time.DateMathParser;
2625
import org.elasticsearch.test.ESTestCase;
@@ -278,7 +277,7 @@ void assertParseException(String msg, String date, String exc) {
278277
parser.parse(date, () -> 0);
279278
fail("Date: " + date + "\n" + msg);
280279
} catch (ElasticsearchParseException e) {
281-
assertThat(ExceptionsHelper.detailedMessage(e).contains(exc), equalTo(true));
280+
assertThat(e.getMessage().contains(exc), equalTo(true));
282281
}
283282
}
284283

server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.elasticsearch.common.time;
2121

2222
import org.elasticsearch.ElasticsearchParseException;
23-
import org.elasticsearch.ExceptionsHelper;
2423
import org.elasticsearch.test.ESTestCase;
2524

2625
import java.time.Instant;
@@ -251,7 +250,7 @@ public void testTimestamps() {
251250

252251
void assertParseException(String msg, String date, String exc) {
253252
ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> parser.parse(date, () -> 0));
254-
assertThat(msg, ExceptionsHelper.detailedMessage(e), containsString(exc));
253+
assertThat(msg, e.getMessage(), containsString(exc));
255254
}
256255

257256
public void testIllegalMathFormat() {

server/src/test/java/org/elasticsearch/common/util/concurrent/EsExecutorsTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.common.util.concurrent;
2121

22-
import org.elasticsearch.ExceptionsHelper;
2322
import org.elasticsearch.common.settings.Settings;
2423
import org.elasticsearch.test.ESTestCase;
2524
import org.hamcrest.Matcher;
@@ -276,7 +275,7 @@ public String toString() {
276275
fail("Didn't get a rejection when we expected one.");
277276
} catch (EsRejectedExecutionException e) {
278277
assertFalse("Thread pool registering as terminated when it isn't", e.isExecutorShutdown());
279-
String message = ExceptionsHelper.detailedMessage(e);
278+
String message = e.getMessage();
280279
assertThat(message, containsString("of dummy runnable"));
281280
assertThat(message, containsString("on EsThreadPoolExecutor[name = " + getName()));
282281
assertThat(message, containsString("queue capacity = " + queue));
@@ -316,7 +315,7 @@ public String toString() {
316315
fail("Didn't get a rejection when we expected one.");
317316
} catch (EsRejectedExecutionException e) {
318317
assertTrue("Thread pool not registering as terminated when it is", e.isExecutorShutdown());
319-
String message = ExceptionsHelper.detailedMessage(e);
318+
String message = e.getMessage();
320319
assertThat(message, containsString("of dummy runnable"));
321320
assertThat(message, containsString("on EsThreadPoolExecutor[name = " + getName()));
322321
assertThat(message, containsString("queue capacity = " + queue));

server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.index.mapper;
2121

22-
import org.elasticsearch.ExceptionsHelper;
2322
import org.elasticsearch.Version;
2423
import org.elasticsearch.cluster.metadata.IndexMetaData;
2524
import org.elasticsearch.common.Strings;
@@ -51,6 +50,7 @@
5150
import static org.hamcrest.Matchers.equalTo;
5251
import static org.hamcrest.Matchers.instanceOf;
5352
import static org.hamcrest.Matchers.not;
53+
import static org.hamcrest.Matchers.notNullValue;
5454

5555
// TODO: make this a real unit test
5656
public class DocumentParserTests extends ESSingleNodeTestCase {
@@ -1443,7 +1443,8 @@ public void testBlankFieldNames() throws Exception {
14431443

14441444
MapperParsingException err = expectThrows(MapperParsingException.class, () ->
14451445
client().prepareIndex("idx", "type").setSource(bytes, XContentType.JSON).get());
1446-
assertThat(ExceptionsHelper.detailedMessage(err), containsString("field name cannot be an empty string"));
1446+
assertThat(err.getCause(), notNullValue());
1447+
assertThat(err.getCause().getMessage(), containsString("field name cannot be an empty string"));
14471448

14481449
final BytesReference bytes2 = BytesReference.bytes(XContentFactory.jsonBuilder()
14491450
.startObject()
@@ -1454,7 +1455,8 @@ public void testBlankFieldNames() throws Exception {
14541455

14551456
err = expectThrows(MapperParsingException.class, () ->
14561457
client().prepareIndex("idx", "type").setSource(bytes2, XContentType.JSON).get());
1457-
assertThat(ExceptionsHelper.detailedMessage(err), containsString("field name cannot be an empty string"));
1458+
assertThat(err.getCause(), notNullValue());
1459+
assertThat(err.getCause().getMessage(), containsString("field name cannot be an empty string"));
14581460
}
14591461

14601462
public void testWriteToFieldAlias() throws Exception {

server/src/test/java/org/elasticsearch/index/shard/IndexShardIT.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
import static org.hamcrest.Matchers.greaterThan;
126126
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
127127
import static org.hamcrest.Matchers.instanceOf;
128+
import static org.hamcrest.Matchers.notNullValue;
128129

129130
public class IndexShardIT extends ESSingleNodeTestCase {
130131

@@ -625,8 +626,9 @@ public void testCircuitBreakerIncrementedByIndexShard() throws Exception {
625626
Exception e = expectThrows(Exception.class,
626627
() -> client().prepareSearch("test")
627628
.addAggregation(AggregationBuilders.terms("foo_terms").field("foo.keyword")).get());
628-
logger.info("--> got: {}", ExceptionsHelper.detailedMessage(e));
629-
assertThat(ExceptionsHelper.detailedMessage(e), containsString("[parent] Data too large, data for [<agg [foo_terms]>]"));
629+
logger.info("--> got an expected exception", e);
630+
assertThat(e.getCause(), notNullValue());
631+
assertThat(e.getCause().getMessage(), containsString("[parent] Data too large, data for [<agg [foo_terms]>]"));
630632

631633
client().admin().cluster().prepareUpdateSettings()
632634
.setTransientSettings(Settings.builder()

server/src/test/java/org/elasticsearch/index/store/StoreTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import static org.hamcrest.Matchers.is;
9797
import static org.hamcrest.Matchers.not;
9898
import static org.hamcrest.Matchers.notNullValue;
99+
import static org.hamcrest.Matchers.startsWith;
99100

100101
public class StoreTests extends ESTestCase {
101102

@@ -977,32 +978,30 @@ public void testCanReadOldCorruptionMarker() throws IOException {
977978
String uuid = Store.CORRUPTED + UUIDs.randomBase64UUID();
978979
try (IndexOutput output = dir.createOutput(uuid, IOContext.DEFAULT)) {
979980
CodecUtil.writeHeader(output, Store.CODEC, Store.VERSION_STACK_TRACE);
980-
output.writeString(ExceptionsHelper.detailedMessage(exception));
981+
output.writeString(exception.getMessage());
981982
output.writeString(ExceptionsHelper.stackTrace(exception));
982983
CodecUtil.writeFooter(output);
983984
}
984985
try {
985986
store.failIfCorrupted();
986987
fail("should be corrupted");
987988
} catch (CorruptIndexException e) {
988-
assertTrue(e.getMessage().startsWith("[index][1] Preexisting corrupted index [" + uuid +
989-
"] caused by: CorruptIndexException[foo (resource=bar)]"));
989+
assertThat(e.getMessage(), startsWith("[index][1] Preexisting corrupted index [" + uuid + "] caused by: foo (resource=bar)"));
990990
assertTrue(e.getMessage().contains(ExceptionsHelper.stackTrace(exception)));
991991
}
992992

993993
store.removeCorruptionMarker();
994994

995995
try (IndexOutput output = dir.createOutput(uuid, IOContext.DEFAULT)) {
996996
CodecUtil.writeHeader(output, Store.CODEC, Store.VERSION_START);
997-
output.writeString(ExceptionsHelper.detailedMessage(exception));
997+
output.writeString(exception.getMessage());
998998
CodecUtil.writeFooter(output);
999999
}
10001000
try {
10011001
store.failIfCorrupted();
10021002
fail("should be corrupted");
10031003
} catch (CorruptIndexException e) {
1004-
assertTrue(e.getMessage().startsWith("[index][1] Preexisting corrupted index [" + uuid +
1005-
"] caused by: CorruptIndexException[foo (resource=bar)]"));
1004+
assertThat(e.getMessage(), startsWith("[index][1] Preexisting corrupted index [" + uuid + "] caused by: foo (resource=bar)"));
10061005
assertFalse(e.getMessage().contains(ExceptionsHelper.stackTrace(exception)));
10071006
}
10081007

server/src/test/java/org/elasticsearch/indices/store/IndicesStoreIntegrationIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.elasticsearch.indices.store;
2121

2222
import org.apache.logging.log4j.Logger;
23-
import org.elasticsearch.ExceptionsHelper;
2423
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
2524
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
2625
import org.elasticsearch.cluster.ClusterState;
@@ -456,7 +455,7 @@ public void onSuccess(String source) {
456455
@Override
457456
public void onFailure(String source, Exception e) {
458457
latch.countDown();
459-
fail("Excepted proper response " + ExceptionsHelper.detailedMessage(e));
458+
throw new AssertionError("Expected a proper response", e);
460459
}
461460
});
462461
latch.await();

server/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.search.aggregations.bucket;
2121

22-
import org.elasticsearch.ExceptionsHelper;
2322
import org.elasticsearch.common.xcontent.XContentParseException;
2423
import org.elasticsearch.common.xcontent.XContentParser;
2524
import org.elasticsearch.common.xcontent.json.JsonXContent;
@@ -30,6 +29,7 @@
3029
import java.io.IOException;
3130

3231
import static org.hamcrest.Matchers.containsString;
32+
import static org.hamcrest.Matchers.notNullValue;
3333

3434
public class DateRangeTests extends BaseAggregationTestCase<DateRangeAggregationBuilder> {
3535

@@ -81,7 +81,8 @@ public void testParsingRangeStrict() throws IOException {
8181
XContentParser parser = createParser(JsonXContent.jsonXContent, rangeAggregation);
8282
XContentParseException ex = expectThrows(XContentParseException.class,
8383
() -> DateRangeAggregationBuilder.parse("aggregationName", parser));
84-
assertThat(ExceptionsHelper.detailedMessage(ex), containsString("badField"));
84+
assertThat(ex.getCause(), notNullValue());
85+
assertThat(ex.getCause().getMessage(), containsString("badField"));
8586
}
8687

8788
}

server/src/test/java/org/elasticsearch/search/aggregations/bucket/GeoDistanceRangeTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.search.aggregations.bucket;
2121

22-
import org.elasticsearch.ExceptionsHelper;
2322
import org.elasticsearch.common.geo.GeoDistance;
2423
import org.elasticsearch.common.geo.GeoPoint;
2524
import org.elasticsearch.common.unit.DistanceUnit;
@@ -34,6 +33,7 @@
3433
import java.io.IOException;
3534

3635
import static org.hamcrest.Matchers.containsString;
36+
import static org.hamcrest.Matchers.notNullValue;
3737

3838
public class GeoDistanceRangeTests extends BaseAggregationTestCase<GeoDistanceAggregationBuilder> {
3939

@@ -81,7 +81,8 @@ public void testParsingRangeStrict() throws IOException {
8181
XContentParser parser = createParser(JsonXContent.jsonXContent, rangeAggregation);
8282
XContentParseException ex = expectThrows(XContentParseException.class,
8383
() -> GeoDistanceAggregationBuilder.parse("aggregationName", parser));
84-
assertThat(ExceptionsHelper.detailedMessage(ex), containsString("badField"));
84+
assertThat(ex.getCause(), notNullValue());
85+
assertThat(ex.getCause().getMessage(), containsString("badField"));
8586
}
8687

8788
/**

server/src/test/java/org/elasticsearch/search/aggregations/bucket/RangeTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.search.aggregations.bucket;
2121

22-
import org.elasticsearch.ExceptionsHelper;
2322
import org.elasticsearch.common.xcontent.XContentParseException;
2423
import org.elasticsearch.common.xcontent.XContentParser;
2524
import org.elasticsearch.common.xcontent.json.JsonXContent;
@@ -30,6 +29,7 @@
3029
import java.io.IOException;
3130

3231
import static org.hamcrest.Matchers.containsString;
32+
import static org.hamcrest.Matchers.notNullValue;
3333

3434
public class RangeTests extends BaseAggregationTestCase<RangeAggregationBuilder> {
3535

@@ -77,7 +77,8 @@ public void testParsingRangeStrict() throws IOException {
7777
XContentParser parser = createParser(JsonXContent.jsonXContent, rangeAggregation);
7878
XContentParseException ex = expectThrows(XContentParseException.class,
7979
() -> RangeAggregationBuilder.parse("aggregationName", parser));
80-
assertThat(ExceptionsHelper.detailedMessage(ex), containsString("badField"));
80+
assertThat(ex.getCause(), notNullValue());
81+
assertThat(ex.getCause().getMessage(), containsString("badField"));
8182
}
8283

8384
/**

server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.elasticsearch.search.aggregations.bucket.geogrid;
2020

21-
import org.elasticsearch.ExceptionsHelper;
2221
import org.elasticsearch.common.unit.DistanceUnit;
2322
import org.elasticsearch.common.xcontent.XContentParseException;
2423
import org.elasticsearch.common.xcontent.XContentParser;
@@ -99,8 +98,7 @@ public void testParseErrorOnBooleanPrecision() throws Exception {
9998
assertSame(XContentParser.Token.START_OBJECT, token);
10099
XContentParseException e = expectThrows(XContentParseException.class,
101100
() -> GeoHashGridAggregationBuilder.parse("geohash_grid", stParser));
102-
assertThat(ExceptionsHelper.detailedMessage(e),
103-
containsString("[geohash_grid] precision doesn't support values of type: VALUE_BOOLEAN"));
101+
assertThat(e.getMessage(), containsString("[geohash_grid] precision doesn't support values of type: VALUE_BOOLEAN"));
104102
}
105103

106104
public void testParseErrorOnPrecisionOutOfRange() throws Exception {

server/src/test/java/org/elasticsearch/search/aggregations/metrics/PercentilesTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.search.aggregations.metrics;
2121

22-
import org.elasticsearch.ExceptionsHelper;
2322
import org.elasticsearch.common.xcontent.XContentParseException;
2423
import org.elasticsearch.common.xcontent.XContentParser;
2524
import org.elasticsearch.common.xcontent.json.JsonXContent;
@@ -89,6 +88,6 @@ public void testExceptionMultipleMethods() throws IOException {
8988
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
9089
XContentParseException e = expectThrows(XContentParseException.class,
9190
() -> PercentilesAggregationBuilder.parse("myPercentiles", parser));
92-
assertThat(ExceptionsHelper.detailedMessage(e), containsString("[percentiles] failed to parse field [hdr]"));
91+
assertThat(e.getMessage(), containsString("[percentiles] failed to parse field [hdr]"));
9392
}
9493
}

server/src/test/java/org/elasticsearch/search/query/SimpleQueryStringIT.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import org.apache.lucene.analysis.TokenFilter;
2323
import org.apache.lucene.analysis.TokenStream;
2424
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
25-
import org.elasticsearch.ExceptionsHelper;
2625
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
2726
import org.elasticsearch.action.index.IndexRequestBuilder;
27+
import org.elasticsearch.action.search.SearchPhaseExecutionException;
2828
import org.elasticsearch.action.search.SearchResponse;
2929
import org.elasticsearch.common.Strings;
3030
import org.elasticsearch.common.settings.Settings;
@@ -564,11 +564,10 @@ public void testAllFieldsWithSpecifiedLeniency() throws IOException {
564564
prepareCreate("test").setSource(indexBody, XContentType.JSON).get();
565565
ensureGreen("test");
566566

567-
Exception e = expectThrows(Exception.class, () ->
567+
SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, () ->
568568
client().prepareSearch("test").setQuery(
569569
simpleQueryStringQuery("foo123").lenient(false)).get());
570-
assertThat(ExceptionsHelper.detailedMessage(e),
571-
containsString("NumberFormatException[For input string: \"foo123\"]"));
570+
assertThat(e.getDetailedMessage(), containsString("NumberFormatException[For input string: \"foo123\"]"));
572571
}
573572

574573
public void testLimitOnExpandedFields() throws Exception {
@@ -591,15 +590,15 @@ public void testLimitOnExpandedFields() throws Exception {
591590
client().prepareIndex("toomanyfields", "type1", "1").setSource("field1", "foo bar baz").get();
592591
refresh();
593592

594-
Exception e = expectThrows(Exception.class, () -> {
593+
SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, () -> {
595594
SimpleQueryStringBuilder qb = simpleQueryStringQuery("bar");
596595
if (randomBoolean()) {
597596
qb.field("*");
598597
}
599598
client().prepareSearch("toomanyfields").setQuery(qb).get();
600599
});
601-
assertThat(ExceptionsHelper.detailedMessage(e),
602-
containsString("field expansion matches too many fields, limit: " + CLUSTER_MAX_CLAUSE_COUNT + ", got: "
600+
assertThat(e.getDetailedMessage(),
601+
containsString("field expansion matches too many fields, limit: " + CLUSTER_MAX_CLAUSE_COUNT + ", got: "
603602
+ (CLUSTER_MAX_CLAUSE_COUNT + 1)));
604603
}
605604

0 commit comments

Comments
 (0)