diff --git a/core/src/main/java/org/elasticsearch/common/settings/SettingsModule.java b/core/src/main/java/org/elasticsearch/common/settings/SettingsModule.java index 84eba6ba7b4..b06f53459c8 100644 --- a/core/src/main/java/org/elasticsearch/common/settings/SettingsModule.java +++ b/core/src/main/java/org/elasticsearch/common/settings/SettingsModule.java @@ -102,7 +102,7 @@ public class SettingsModule extends AbstractModule { } public void registerSettingsFilterIfMissing(String filter) { - if (settingsFilterPattern.contains(filter)) { + if (settingsFilterPattern.contains(filter) == false) { registerSettingsFilter(filter); } } diff --git a/core/src/test/java/org/elasticsearch/common/settings/SettingsModuleTests.java b/core/src/test/java/org/elasticsearch/common/settings/SettingsModuleTests.java index 67ecb78a7cf..4f790c2d3a9 100644 --- a/core/src/test/java/org/elasticsearch/common/settings/SettingsModuleTests.java +++ b/core/src/test/java/org/elasticsearch/common/settings/SettingsModuleTests.java @@ -127,4 +127,26 @@ public class SettingsModuleTests extends ModuleTestCase { } } + + public void testRegisterSettingsFilter() { + Settings settings = Settings.builder().put("foo.bar", "false").put("bar.foo", false).put("bar.baz", false).build(); + SettingsModule module = new SettingsModule(settings); + module.registerSetting(Setting.boolSetting("foo.bar", true, false, Setting.Scope.CLUSTER)); + module.registerSetting(Setting.boolSetting("bar.foo", true, false, Setting.Scope.CLUSTER)); + module.registerSetting(Setting.boolSetting("bar.baz", true, false, Setting.Scope.CLUSTER)); + + module.registerSettingsFilter("foo.*"); + module.registerSettingsFilterIfMissing("bar.foo"); + try { + module.registerSettingsFilter("bar.foo"); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals("filter [bar.foo] has already been registered", ex.getMessage()); + } + assertInstanceBinding(module, Settings.class, (s) -> s == settings); + assertInstanceBinding(module, SettingsFilter.class, (s) -> s.filter(settings).getAsMap().size() == 1); + assertInstanceBinding(module, SettingsFilter.class, (s) -> s.filter(settings).getAsMap().containsKey("bar.baz")); + assertInstanceBinding(module, SettingsFilter.class, (s) -> s.filter(settings).getAsMap().get("bar.baz").equals("false")); + + } }