3131
3232package org .opensearch .cluster .metadata ;
3333
34+ import org .hamcrest .Matchers ;
3435import org .opensearch .common .SuppressForbidden ;
3536import org .opensearch .common .settings .Settings ;
37+ import org .opensearch .common .settings .SettingsException ;
3638import org .opensearch .test .OpenSearchTestCase ;
3739
3840import static org .opensearch .cluster .metadata .IndexMetadata .DEFAULT_NUMBER_OF_SHARDS ;
@@ -43,20 +45,22 @@ public class EvilSystemPropertyTests extends OpenSearchTestCase {
4345
4446 @ SuppressForbidden (reason = "manipulates system properties for testing" )
4547 public void testNumShards () {
46- IllegalArgumentException exception = expectThrows (IllegalArgumentException .class , () ->
48+ SettingsException exception = expectThrows (SettingsException .class , () ->
4749 IndexMetadata .buildNumberOfShardsSetting ()
4850 .get (Settings .builder ().put (SETTING_NUMBER_OF_SHARDS , 1025 ).build ()));
49- assertEquals ("Failed to parse value [1025] for setting [" + SETTING_NUMBER_OF_SHARDS + "] must be <= 1024" , exception .getMessage ());
51+ assertEquals ("Failed to parse value [1025] for setting [" + SETTING_NUMBER_OF_SHARDS + "] must be <= 1024" , exception .getCause (). getMessage ());
5052
5153 Integer numShards = IndexMetadata .INDEX_NUMBER_OF_SHARDS_SETTING .get (Settings .builder ().put (SETTING_NUMBER_OF_SHARDS , 100 ).build ());
5254 assertEquals (100 , numShards .intValue ());
5355 int limit = randomIntBetween (1 , 10 );
5456 System .setProperty (MAX_NUMBER_OF_SHARDS , Integer .toString (limit ));
5557 try {
56- IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () ->
58+ SettingsException e = expectThrows (SettingsException .class , () ->
5759 IndexMetadata .buildNumberOfShardsSetting ()
5860 .get (Settings .builder ().put ("index.number_of_shards" , 11 ).build ()));
59- assertEquals ("Failed to parse value [11] for setting [index.number_of_shards] must be <= " + limit , e .getMessage ());
61+ Throwable cause = e .getCause ();
62+ assertThat (cause , Matchers .instanceOf (IllegalArgumentException .class ));
63+ assertEquals ("Failed to parse value [11] for setting [index.number_of_shards] must be <= " + limit , cause .getMessage ());
6064 System .clearProperty (MAX_NUMBER_OF_SHARDS );
6165
6266 Integer defaultFromSetting = IndexMetadata .INDEX_NUMBER_OF_SHARDS_SETTING .getDefault (Settings .EMPTY );
@@ -70,9 +74,10 @@ public void testNumShards() {
7074 randomDefault = randomIntBetween (1 , 10 );
7175 System .setProperty (MAX_NUMBER_OF_SHARDS , Integer .toString (randomDefault ));
7276 System .setProperty (DEFAULT_NUMBER_OF_SHARDS , Integer .toString (randomDefault + 1 ));
73- e = expectThrows (IllegalArgumentException .class , IndexMetadata ::buildNumberOfShardsSetting );
77+
78+ cause = expectThrows (IllegalArgumentException .class , IndexMetadata ::buildNumberOfShardsSetting );
7479 assertEquals (DEFAULT_NUMBER_OF_SHARDS + " value [" + (randomDefault + 1 ) + "] must between " +
75- "1 and " + MAX_NUMBER_OF_SHARDS + " [" + randomDefault + "]" , e .getMessage ());
80+ "1 and " + MAX_NUMBER_OF_SHARDS + " [" + randomDefault + "]" , cause .getMessage ());
7681 } finally {
7782 System .clearProperty (MAX_NUMBER_OF_SHARDS );
7883 System .clearProperty (DEFAULT_NUMBER_OF_SHARDS );
0 commit comments