ConfigManager { @SuppressWarnings({"unchecked"}) public <E extends Enum<E>> Config<E> getConfig(Class<E> clazz) { return (Config<E>) Preconditions.checkNotNull(config.get(clazz), "No config loaded for root: %s", clazz.getSimpleName()); } private  ConfigManager(CLISetup commandLineArgs, ImmutableMap<Class<? extends Enum<?>>, Config<?>> config); @SuppressWarnings({"unchecked"}) //Effectively checked on insert (see the Builder below) Config<E> getConfig(Class<E> clazz); ImmutableCollection<Config<?>> getAllConfig(); ConfigManager getPrefixBiasedConfigManager(String prefix); final CLISetup commandLineArgs; }
@Test void loadConfigFromResourceOrFile_testGenericIdsParsing() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("test-config-resource.properties"), ImmutableSet.of(TestConfig.class)) .build(); Config<TestConfig> config = configManager.getConfig(TestConfig.class); ImmutableList<Id<Double>> listOfIds = config.getValue(FirstSubConfig.WOO).asList().ofIds(); assertThat(listOfIds.get(0)).isEqualTo(Id.create(1)); assertThat(listOfIds.get(1)).isEqualTo(Id.create(2)); assertThat(listOfIds.get(2)).isEqualTo(Id.create(3)); assertThat(listOfIds.get(3)).isEqualTo(Id.create(4)); }
@Test void loadConfigFromResourceOrFile_whenGenericIdsListIsEmpty() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("test-config-resource.properties"), ImmutableSet.of(TestConfig.class)) .build(); Config<TestConfig> config = configManager.getConfig(TestConfig.class); ImmutableList<Id<Double>> listOfIds = config.getValue(FirstSubConfig.HOO).asList().ofIds(); assertThat(listOfIds).isEmpty(); }
@Test void loadConfigFromResourceOrFile_whenResourceOnClassPath_usesResource() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("test-config-resource.properties"), ImmutableSet.of(TestConfig.class)) .build(); Config<TestConfig> config = configManager.getConfig(TestConfig.class); assertThat(config.getValue(TestConfig.FOO).asInt()).isEqualTo(1); assertThat(config.getValue(TestConfig.BAR).asInt()).isEqualTo(2); }
@Test void loadConfigFromResourceOrFile_whenResourceNotOnClassPath_usesFile() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("src/test/test-config-file.properties"), ImmutableSet.of(TestConfig.class)) .build(); Config<TestConfig> config = configManager.getConfig(TestConfig.class); assertThat(config.getValue(TestConfig.FOO).asInt()).isEqualTo(2); assertThat(config.getValue(TestConfig.BAR).asInt()).isEqualTo(1); }
@Test void getPrefixes() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile( ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); ImmutableSet<String> actual = configManager.getConfig(TestConfig.class).getPrefixes(); ImmutableSet<String> expected = ImmutableSet.of( "Prefix1", "Prefix2", "Prefix3", "Prefix5", "Prefix6"); assertThat(actual).isEqualTo(expected); }
@Test void loadPrefixedConfigItems_singlePrefix() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile( ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfig> testConfig = configManager.getConfig(TestConfig.class); assertThat(testConfig.getValue(TestConfig.FOO).asInt()).isEqualTo(1); assertThat(testConfig.getValue(TestConfig.BAR).asInt()).isEqualTo(2); Config<TestConfig> prefixedConfig = testConfig.getPrefixedConfigItems("Prefix1"); assertThat(prefixedConfig.getValues().keySet()).isEqualTo(ImmutableSet.of( TestConfig.FOO, TestConfig.BAR, TestConfig.BAZ)); assertThat(prefixedConfig.getValue(TestConfig.FOO).asInt()).isEqualTo(3); assertThat(prefixedConfig.getValue(TestConfig.BAR).asInt()).isEqualTo(4); assertThat(prefixedConfig.areKeyAndValueDefined(TestConfig.BAZ)).isFalse(); }
@Test void loadPrefixedConfigItems_clashingNames() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile( ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfig> testConfig = configManager.getConfig(TestConfig.class); Config<TestConfig> prefixedConfig = testConfig.getPrefixedConfigItems("Prefix6"); assertThat(prefixedConfig.getValue(FirstSubConfig.WOO).asInt()).isEqualTo(1); assertThat(prefixedConfig.getValue(SecondSubConfig.WOO).asInt()).isEqualTo(2); }
@Test void loadPrefixedConfigItems_noUnprefixedEquivalent() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile( ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfig> testConfig = configManager.getConfig(TestConfig.class); assertThat(testConfig.getPrefixedConfigItems("Prefix5").getValue(TestConfig.BAZ).asInt()).isEqualTo(13); }
@Test void loadPrefixedConfigItems_prefixNotFound() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile( ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfig> testConfig = configManager.getConfig(TestConfig.class); assertThat(testConfig.getValue(TestConfig.FOO).asInt()).isEqualTo(1); assertThat(testConfig.getValue(TestConfig.BAR).asInt()).isEqualTo(2); assertThat(testConfig.getPrefixedConfigItems("InvalidPrefix").getValue(TestConfig.FOO).asInt()).isEqualTo(1); assertThat(testConfig.getPrefixedConfigItems("InvalidPrefix").getValue(TestConfig.BAR).asInt()).isEqualTo(2); }
@Test void loadPrefixedConfigItems_multiplePrefixes_callFirstPrefix() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfig> testConfig = configManager.getConfig(TestConfig.class); assertThat(testConfig.getValue(TestConfig.FOO).asInt()).isEqualTo(1); assertThat(testConfig.getValue(TestConfig.BAR).asInt()).isEqualTo(2); assertThat(testConfig.getPrefixedConfigItems("Prefix2").getValues().values().stream() .allMatch(configValue -> configValue.currentValue == null)).isFalse(); assertThat(testConfig.getPrefixedConfigItems("Prefix2").getValues().values().stream() .allMatch(configValue -> configValue.prefixedValues == null)).isFalse(); }
@Test void loadPrefixedConfigItems_multiplePrefixes_callSecondPrefix() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfig> testConfig = configManager.getConfig(TestConfig.class); assertThat(testConfig.getValue(TestConfig.FOO).asInt()).isEqualTo(1); assertThat(testConfig.getValue(TestConfig.BAR).asInt()).isEqualTo(2); assertThat(testConfig.getPrefixedConfigItems("Prefix3").getValues().values().stream() .allMatch(configValue -> configValue.currentValue == null)).isFalse(); assertThat(testConfig.getPrefixedConfigItems("Prefix3").getValues().values().stream() .allMatch(configValue -> configValue.prefixedValues == null)).isFalse(); }
@Test void loadPrefixedConfigItems_multiplePrefixes_callBothPrefixes() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfig> testConfig = configManager.getConfig(TestConfig.class); assertThat(testConfig.getValue(TestConfig.FOO).asInt()).isEqualTo(1); assertThat(testConfig.getValue(TestConfig.BAR).asInt()).isEqualTo(2); assertThat(testConfig.getPrefixedConfigItems("Prefix2").getPrefixedConfigItems("Prefix3").getValue(TestConfig.BAR).asInt()).isEqualTo(5); assertThat(testConfig.getPrefixedConfigItems("Prefix2").getPrefixedConfigItems("Prefix3").getValue(TestConfig.FOO).asInt()).isEqualTo(6); }
@Test void loadPrefixedConfigItems_testCallOrder() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfig> testConfig = configManager.getConfig(TestConfig.class); Config<TestConfig.Colours> testConfigPrefixSubConfig = testConfig.getPrefixedConfigItems("Prefix1") .getSubConfig(Colours.class); Config<TestConfig.Colours> testConfigSubConfigPrefix = testConfig.getSubConfig(Colours.class) .getPrefixedConfigItems("Prefix1"); assertThat(testConfigPrefixSubConfig.getValues().keySet()).isEqualTo(testConfigSubConfigPrefix.getValues().keySet()); }
@Test void loadPrefixedConfigItems_testDefaultCurrentValue() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfigDummy> testConfig = configManager.getConfig(TestConfigDummy.class); assertThat(testConfig.getValue(TestConfigDummy.BAR).asInt()).isEqualTo(10); assertThat(testConfig.getValue(TestConfigDummy.FOO).asInt()).isEqualTo(12); assertThat(testConfig.getPrefixedConfigItems("Prefix4").getValue(TestConfigDummy.BAR).asInt()).isEqualTo(11); assertThat(testConfig.getPrefixedConfigItems("Prefix4").getValue(TestConfigDummy.FOO).asInt()).isEqualTo(12); }
@Test void loadPrefixedConfigItems_singlePrefix_testSubConfigs() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfig.Colours> subTestConfig = configManager.getConfig(TestConfig.class).getSubConfig(TestConfig.Colours.class); assertThat(subTestConfig.getValue(Colours.BLUE).asInt()).isEqualTo(7); assertThat(subTestConfig.getPrefixedConfigItems("Prefix1").getValue(TestConfig.Colours.RED).asInt()).isEqualTo(8); assertThat(subTestConfig.getPrefixedConfigItems("Prefix1").getValue(TestConfig.Colours.GREEN).asInt()).isEqualTo(9); }
@Test void loadPrefixedConfigItems_testSubConfigPenetration() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfig> testConfig = configManager.getConfig(TestConfig.class); assertThat(testConfig.getValue(Colours.RED).asInt()).isEqualTo(80); assertThat(testConfig.getValue(Colours.GREEN).asInt()).isEqualTo(90); assertThat(testConfig.getPrefixedConfigItems("Prefix1").getValue(Colours.RED).asInt()).isEqualTo(8); assertThat(testConfig.getPrefixedConfigItems("Prefix1").getValue(Colours.GREEN).asInt()).isEqualTo(9); }
@Test void loadPrefixedConfigItems_testCommandLineOverrides() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{"-OPrefix1@TestConfig.FOO=100"}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("src/test/prefixes-test-config-file.properties"), ImmutableSet.of(TestConfig.class, TestConfigDummy.class)) .build(); Config<TestConfig> testConfig = configManager.getConfig(TestConfig.class); assertThat(testConfig.getPrefixedConfigItems("Prefix1").getValue(TestConfig.FOO).asInt()).isEqualTo(100); assertThat(testConfig.getValue(TestConfig.FOO).asInt()).isEqualTo(1); }
@Test void loadConfigFromResourceOrFile_whenFileInBothLocations_thenDefaultToTheClasspath() throws IOException, ConfigKeysNotRecognisedException { Builder builder = new Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromResourceOrFile(ImmutableList.of("src/test/potentially-overridden-file.properties"), ImmutableSet.of(TestConfig.class)) .build(); Config<TestConfig> config = configManager.getConfig(TestConfig.class); assertThat(config.getValue(TestConfig.FOO).asInt()).isEqualTo(1); assertThat(config.getValue(TestConfig.BAR).asInt()).isEqualTo(2); }
@Test void loadConfigFromEnvVarsSingleClass() throws Exception { ConfigManager.Builder builder = new ConfigManager.Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromEnvironmentVariables( ImmutableMap.of("FOO", "1", "BAR", "2"), ImmutableSet.of(TestConfig.class) ).build(); Config<TestConfig> config = configManager.getConfig(TestConfig.class); assertThat(config.getValue(TestConfig.FOO).asInt()).isEqualTo(1); assertThat(config.getValue(TestConfig.BAR).asInt()).isEqualTo(2); }
@Test void loadConfigFromEnvVarsMultipleClassesNoOverlappingEnums() throws Exception { ConfigManager.Builder builder = new ConfigManager.Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromEnvironmentVariables( ImmutableMap.of("MOO", "5", "QUACK", "4"), ImmutableSet.of(TestConfigTwo.class, TestConfigThree.class) ).build(); assertThat(configManager.getConfig(TestConfigTwo.class).getValue(TestConfigTwo.MOO).asInt()).isEqualTo(5); assertThat(configManager.getConfig(TestConfigThree.class).getValue(TestConfigThree.QUACK).asInt()).isEqualTo(4); }
@Test void loadConfigFromEnvVarsMultipleClassesOverlappingEnumsNotSet() throws Exception { ConfigManager.Builder builder = new ConfigManager.Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromEnvironmentVariables( ImmutableMap.of("BAR", "4", "MOO", "5"), ImmutableSet.of(TestConfig.class, TestConfigTwo.class) ).build(); assertThat(configManager.getConfig(TestConfig.class).getValue(TestConfig.BAR).asInt()).isEqualTo(4); assertThat(configManager.getConfig(TestConfigTwo.class).getValue(TestConfigTwo.MOO).asInt()).isEqualTo(5); }
@Test void loadConfigFromEnvVarsUnrecognisedValuesNoException() throws Exception { ConfigManager.Builder builder = new ConfigManager.Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromEnvironmentVariables( ImmutableMap.of("UNKNOWN-KEY", "5"), ImmutableSet.of(TestConfig.class) ).build(); configManager.getConfig(TestConfig.class); }
@Test void commandLineArgsTakePrecedenceOverEnvVars() throws Exception { ConfigManager.Builder builder = new ConfigManager.Builder(new String[]{"-OTestConfig.FOO=20"}); ConfigManager configManager = builder.loadConfigFromEnvironmentVariables( ImmutableMap.of("FOO", "1", "BAR", "2"), ImmutableSet.of(TestConfig.class) ).build(); Config<TestConfig> config = configManager.getConfig(TestConfig.class); assertThat(config.getValue(TestConfig.FOO).asInt()).isEqualTo(20); assertThat(config.getValue(TestConfig.BAR).asInt()).isEqualTo(2); }
@Test void environmentVariableMatchesEnumInSubclass() throws Exception { ConfigManager.Builder builder = new ConfigManager.Builder(new String[]{}); ConfigManager configManager = builder.loadConfigFromEnvironmentVariables( ImmutableMap.of("HOO", "1"), ImmutableSet.of(TestConfig.class) ).build(); Config<TestConfig> config = configManager.getConfig(TestConfig.class); assertThat(config.getValue(FirstSubConfig.HOO).asInt()).isEqualTo(1); }
@Test void loadConfigFromResource_whenUsingAlternateArg() throws ConfigKeysNotRecognisedException { ConfigManager cm = new ConfigManager.Builder(new String[] { "-a=test-config-resource.properties" }).withConfig(TestConfig.class).build(); assertThat(cm.getConfig(TestConfig.class).areKeyAndValueDefined(TestConfig.FOO)).isTrue(); }
@Test void loadConfigFromResource_whenUsingAlternateArgThenActualCommandLineTakesPriority() throws ConfigKeysNotRecognisedException { { ConfigManager cm1 = new ConfigManager.Builder(new String[]{"-a:test-config-resource.properties"}).withConfig(TestConfig.class).build(); assertThat(cm1.getConfig(TestConfig.class).getValue(TestConfig.FOO).asInt()).isEqualTo(1); } { ConfigManager cm2 = new ConfigManager.Builder(new String[]{"-a:test-config-resource.properties", "-OTestConfig.FOO=2"}).withConfig(TestConfig.class).build(); assertThat(cm2.getConfig(TestConfig.class).getValue(TestConfig.FOO).asInt()).isEqualTo(2); } }