Archaius2ObjectConfigurationResolver implements ObjectConfigurationResolver<OBJECT, CONFIG>, ConfigListener, Closeable { @Override public CONFIG resolve(OBJECT object) { if (object == null) { return defaultConfig; } String selectorValue = selectorFieldAccessor.apply(object); if (selectorValue == null) { return defaultConfig; } for (Map.Entry<String, Rule<CONFIG>> entry : configMap.entrySet()) { Rule<CONFIG> rule = entry.getValue(); if (rule.getPattern().matcher(selectorValue).matches()) { return rule.getConfig(); } } return defaultConfig; } Archaius2ObjectConfigurationResolver(Config configuration,
                                         Function<OBJECT, String> selectorFieldAccessor,
                                         Class<CONFIG> configType,
                                         CONFIG defaultConfig);   Archaius2ObjectConfigurationResolver(Config configuration,
                                         Function<OBJECT, String> selectorFieldAccessor,
                                         Function<String, CONFIG> dynamicProxyFactory,
                                         CONFIG defaultConfig); @Override void close(); @Override CONFIG resolve(OBJECT object); @Override void onConfigAdded(Config config); @Override void onConfigRemoved(Config config); @Override void onConfigUpdated(Config config); @Override void onError(Throwable error, Config config);  }
@Test public void testDefaultObjectConfiguration() { assertThat(resolver.resolve(new MyObject("a")).getLimit()).isEqualTo(100); }
@Test public void testDefaultOverrides() { configuration.setProperty("default.limit", "200"); assertThat(resolver.resolve(new MyObject("a")).getLimit()).isEqualTo(200); }
@Test public void testObjectOverrides() { configuration.setProperty("object.a.pattern", "a"); configuration.setProperty("object.a.limit", "200"); configuration.setProperty("object.b.pattern", "b"); configuration.setProperty("object.b.limit", "300"); assertThat(resolver.resolve(new MyObject("a")).getLimit()).isEqualTo(200); assertThat(resolver.resolve(new MyObject("b")).getLimit()).isEqualTo(300); assertThat(resolver.resolve(new MyObject("x")).getLimit()).isEqualTo(100); }
@Test public void testDynamicUpdates() { configuration.setProperty("object.a.pattern", "a"); configuration.setProperty("object.a.limit", "200"); assertThat(resolver.resolve(new MyObject("a")).getLimit()).isEqualTo(200); configuration.setProperty("object.a.limit", "300"); assertThat(resolver.resolve(new MyObject("a")).getLimit()).isEqualTo(300); configuration.clearProperty("object.a.pattern"); assertThat(resolver.resolve(new MyObject("a")).getLimit()).isEqualTo(100); }
@Test public void testBadPattern() { configuration.setProperty("object.a.pattern", "a"); configuration.setProperty("object.a.limit", "200"); assertThat(resolver.resolve(new MyObject("a")).getLimit()).isEqualTo(200); configuration.setProperty("object.a.pattern", "*"); assertThat(resolver.resolve(new MyObject("a")).getLimit()).isEqualTo(200); }