Fix registerSettingsFilterIfMissing and add unittest for settings filter registration

This commit is contained in:
Simon Willnauer 2016-02-12 09:25:24 +01:00
parent 15a9da4d84
commit 691c5c49ed
2 changed files with 23 additions and 1 deletions

View File

@ -102,7 +102,7 @@ public class SettingsModule extends AbstractModule {
} }
public void registerSettingsFilterIfMissing(String filter) { public void registerSettingsFilterIfMissing(String filter) {
if (settingsFilterPattern.contains(filter)) { if (settingsFilterPattern.contains(filter) == false) {
registerSettingsFilter(filter); registerSettingsFilter(filter);
} }
} }

View File

@ -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"));
}
} }