ConfigObserver { public ConfigObserver onStringValue(String key, String defaultValue, ConfigValueListener<String> listener) { return onSingleConfigValue(key, Function.identity(), defaultValue, listener); } ConfigObserver(String applicationName);  ConfigObserver(File configDirectory, String applicationName);  ConfigObserver(File configDirectory, String applicationName, List<String> profiles);  ConfigObserver(ConfigLoader configLoader); ConfigObserver onConfigChange(ConfigListener listener); ConfigObserver onSingleConfigValue(String key, Function<String, T> transformer, T defaultValue, ConfigValueListener<T> listener); ConfigObserver onStringValue(String key, String defaultValue, ConfigValueListener<String> listener); ConfigObserver onIntValue(String key, int defaultValue, ConfigValueListener<Integer> listener); ConfigObserver onLongValue(String key, long defaultValue, ConfigValueListener<Long> listener); ConfigObserver onInetSocketAddress(String key, int defaultPort, ConfigValueListener<InetSocketAddress> listener); ConfigObserver onInetSocketAddress(String key, InetSocketAddress defaultAddress, ConfigValueListener<InetSocketAddress> listener); ConfigObserver onDurationValue(String key, Duration defaultValue, ConfigValueListener<Duration> listener); ConfigObserver onStringListValue(String key, String defaultValue, ConfigValueListener<List<String>> listener); ConfigObserver onPrefixedValue(String prefix, ConfigValueListener<Map<String, String>> listener); ConfigObserver onPrefixedValue(String prefix, ConfigListener.Transformer<T> transformer, ConfigValueListener<T> listener); ConfigObserver onPrefixedOptionalValue(String prefix, ConfigListener.Transformer<T> transformer, ConfigValueListener<Optional<T>> listener); ConfigObserver onPrefixedOptionalValue(String prefix, ConfigValueListener<Optional<Map<String, String>>> listener); void updateConfiguration(Map<String, String> newConfiguration);  }
@Test public void shouldRecoverFromErrorInListener() { expectedLogEvents.expectPattern(ConfigObserver.class, Level.ERROR, "Failed to notify listener while reloading {}"); writeConfigLine("example.number=123"); String[] fooValue = { null }; observer.onStringValue("example.foo", null, v -> fooValue[0] = v ); observer.onStringValue("example.number", "100", s -> { throw new RuntimeException(""); }); assertThat(fooValue[0]).isNull(); writeConfigLines("example.number=one", "example.foo=real value"); assertThat(fooValue[0]).isEqualTo("real value"); }
@Test public void shouldReadStringValue() { List<String> list = new ArrayList<>(); observer.onStringValue("prop", "foo,bar", str -> { list.clear(); list.add(str); }); assertThat(list).containsOnly("foo,bar"); writeConfigLine("prop=a, b ,c"); assertThat(list).containsOnly("a, b ,c"); }
@Test public void shouldDefaultToEnvironmentVariable() { AtomicReference<String> path = new AtomicReference<>(); observer.onStringValue("path", null, path::set); assertThat(path.get()).isEqualTo(System.getenv("PATH")); writeConfigLine("path=test"); assertThat(path.get()).isEqualTo("test"); }