ConfigGetter { public synchronized T getConfig(String configId) { try (ConfigSubscriber subscriber = source == null ? new ConfigSubscriber() : new ConfigSubscriber(source)) { ConfigHandle<T> handle = subscriber.subscribe(clazz, configId); subscriber.nextConfig(); return handle.getConfig(); } } ConfigGetter(Class<T> clazz);  ConfigGetter(ConfigSource source, Class<T> clazz); synchronized T getConfig(String configId); static T getConfig(Class<T> c, String configId); static T getConfig(Class<T> c, String configId, ConfigSource source);  }
@Test public void testGetConfig() { int times = 11; String message = "testGetConfig"; String a0 = "a0"; String configId = "raw:times " + times + "\nmessage " + message + "\na[1]\na[0].name " + a0; ConfigGetter<AppConfig> getter = new ConfigGetter<>(AppConfig.class); AppConfig config = getter.getConfig(configId); assertThat(config.times(), is(times)); assertThat(config.message(), is(message)); assertThat(config.a().size(), is(1)); assertThat(config.a(0).name(), is(a0)); AppService service = new AppService(configId, sourceSet); AppConfig serviceConfig = service.getConfig(); assertTrue(service.isConfigured()); assertThat(config, is(serviceConfig)); service.cancelSubscription(); }
@Test public void testGetFromRawSource() { ConfigGetter<AppConfig> getter = new ConfigGetter<>(new RawSource("message \"one\""), AppConfig.class); AppConfig config = getter.getConfig("test"); assertThat(config.message(), is("one")); }
@Test public void testGetTwice() { ConfigGetter<AppConfig> getter = new ConfigGetter<>(AppConfig.class); AppConfig config = getter.getConfig("raw:message \"one\""); assertThat(config.message(), is("one")); config = getter.getConfig("raw:message \"two\""); assertThat(config.message(), is("two")); }
@Test public void testGetFromFile() { ConfigGetter<AppConfig> getter = new ConfigGetter<>(AppConfig.class); AppConfig config = getter.getConfig("file:src/test/resources/configs/foo/app.cfg"); verifyFooValues(config); }
@Test public void testGetFromFileSource() { ConfigGetter<AppConfig> getter = new ConfigGetter<>(new FileSource(new File("src/test/resources/configs/foo/app.cfg")), AppConfig.class); AppConfig config = getter.getConfig("test"); verifyFooValues(config); }
@Test public void testGetFromDir() { ConfigGetter<AppConfig> getter = new ConfigGetter<>(AppConfig.class); AppConfig config = getter.getConfig("dir:src/test/resources/configs/foo/"); verifyFooValues(config); }
@Test public void testGetFromDirSource() { AppConfig config = ConfigGetter.getConfig(AppConfig.class, "test", new DirSource(new File("src/test/resources/configs/foo/"))); verifyFooValues(config); }
@Test public void testsStaticGetConfig() { int times = 11; String message = "testGetConfig"; String a0 = "a0"; String configId = "raw:times " + times + "\nmessage " + message + "\na[1]\na[0].name " + a0; AppConfig config = ConfigGetter.getConfig(AppConfig.class, configId); assertThat(config.times(), is(times)); assertThat(config.message(), is(message)); assertThat(config.a().size(), is(1)); assertThat(config.a(0).name(), is(a0)); AppService service = new AppService(configId, sourceSet); AppConfig serviceConfig = service.getConfig(); assertTrue(service.isConfigured()); assertThat(config, is(serviceConfig)); service.cancelSubscription(); }