Config config = Config.builder().withConfigClasses( singletonList( new MyDynamicSettings() ) ).build();
@Test public void updateDynamicShouldLogChanges() { String settingName = MyDynamicSettings.boolSetting.name(); String changedMessage = "Setting changed: '%s' changed from '%s' to '%s' via '%s'"; Config config = Config.builder().withConfigClasses( singletonList( new MyDynamicSettings() ) ).build(); Log log = mock( Log.class ); config.setLogger( log ); config.updateDynamicSetting( settingName, "false", ORIGIN ); config.updateDynamicSetting( settingName, "true", ORIGIN ); config.updateDynamicSetting( settingName, "", ORIGIN ); InOrder order = inOrder( log ); order.verify( log ).info( changedMessage, settingName, "default (true)", "false", "test" ); order.verify( log ).info( changedMessage, settingName, "false", "true", "test" ); order.verify( log ).info( changedMessage, settingName, "true", "default (true)", "test" ); verifyNoMoreInteractions( log ); }
@Test public void updateDynamicShouldInformRegisteredListeners() { Config config = Config.builder().withConfigClasses( singletonList( new MyDynamicSettings() ) ).build(); AtomicInteger counter = new AtomicInteger( 0 ); config.registerDynamicUpdateListener( MyDynamicSettings.boolSetting, ( previous, update ) -> { counter.getAndIncrement(); assertTrue( previous ); assertFalse( update ); } ); config.updateDynamicSetting( MyDynamicSettings.boolSetting.name(), "false", ORIGIN ); assertThat( counter.get(), is( 1 ) ); }
@Test public void shouldLogDeprecationWarnings() throws Exception { // Given Log log = mock( Log.class ); File confFile = testDirectory.file( "test.conf" ); assertTrue( confFile.createNewFile() ); Config config = Config.fromFile( confFile ) .withSetting( MySettingsWithDefaults.oldHello, "baah" ) .withSetting( MySettingsWithDefaults.oldSetting, "booh" ) .withConfigClasses( Arrays.asList( mySettingsWithDefaults, myMigratingSettings, new GraphDatabaseSettings() ) ) .build(); // When config.setLogger( log ); // Then verify( log ).warn( "%s is deprecated. Replaced by %s", MySettingsWithDefaults.oldHello.name(), MySettingsWithDefaults.hello.name() ); verify( log ).warn( "%s is deprecated.", MySettingsWithDefaults.oldSetting.name() ); verifyNoMoreInteractions( log ); }
@Test public void validatorsShouldBeCalledWhenBuilding() { // Should not throw Config.builder() .withSetting( MySettingsWithDefaults.hello, "neo4j" ) .withValidator( new HelloHasToBeNeo4jConfigurationValidator() ) .withConfigClasses( Arrays.asList( mySettingsWithDefaults, myMigratingSettings ) ).build(); expect.expect( InvalidSettingException.class ); expect.expectMessage( "Setting hello has to set to neo4j" ); // Should throw Config.builder() .withSetting( MySettingsWithDefaults.hello, "not-neo4j" ) .withValidator( new HelloHasToBeNeo4jConfigurationValidator() ) .withConfigClasses( Arrays.asList( mySettingsWithDefaults, myMigratingSettings ) ).build(); }
@Test public void shouldSetDocumentedDefaultValue() { // Given Config config = Config.builder() .withSetting( MySettingsWithDefaults.secretSetting, "false" ) .withSetting( MySettingsWithDefaults.hello, "ABC" ) .withConfigClasses( Arrays.asList( new MySettingsWithDefaults(), myMigratingSettings ) ) .build(); // Then assertEquals( Optional.of( "<documented default value>" ), config.getConfigValues().get( MySettingsWithDefaults.secretSetting.name() ) .documentedDefaultValue() ); assertEquals( Optional.empty(), config.getConfigValues().get( MySettingsWithDefaults.hello.name() ).documentedDefaultValue() ); }
@Test public void shouldSetSecretParameter() { // Given Config config = Config.builder() .withSetting( MySettingsWithDefaults.password, "this should not be visible" ) .withSetting( MySettingsWithDefaults.hello, "ABC" ) .withConfigClasses( Arrays.asList( mySettingsWithDefaults, myMigratingSettings ) ) .build(); // Then assertTrue( config.getConfigValues().get( MySettingsWithDefaults.password.name() ).secret() ); assertFalse( config.getConfigValues().get( MySettingsWithDefaults.hello.name() ).secret() ); String configText = config.toString(); assertTrue( configText.contains( Secret.OBSFUCATED ) ); assertFalse( configText.contains( "this should not be visible" ) ); assertFalse( configText.contains( config.get( MySettingsWithDefaults.password ) ) ); }
@Test public void updateDynamicShouldLogExceptionsFromUpdateListeners() { Config config = Config.builder().withConfigClasses( singletonList( new MyDynamicSettings() ) ).build(); IllegalStateException exception = new IllegalStateException( "Boo" ); config.registerDynamicUpdateListener( MyDynamicSettings.boolSetting, ( a, b ) -> { throw exception; } ); Log log = mock( Log.class ); config.setLogger( log ); String settingName = MyDynamicSettings.boolSetting.name(); config.updateDynamicSetting( settingName, "", ORIGIN ); verify( log ).error( "Failure when notifying listeners after dynamic setting change; " + "new setting might not have taken effect: Boo", exception ); }
@Test public void updateDynamicShouldThrowIfSettingIsNotDynamic() { Config config = Config.builder().withConfigClasses( singletonList( mySettingsWithDefaults ) ).build(); expect.expect( IllegalArgumentException.class ); config.updateDynamicSetting( MySettingsWithDefaults.hello.name(), "hello", ORIGIN ); }
private static Config Config( Map<String,String> params ) { return Config.fromSettings( params ) .withConfigClasses( Arrays.asList( mySettingsWithDefaults, myMigratingSettings ) ).build(); }
@Test public void updateDynamicShouldNotAllowInvalidSettings() { Config config = Config.builder().withConfigClasses( singletonList( new MyDynamicSettings() ) ).build(); expect.expect( InvalidSettingException.class ); config.updateDynamicSetting( MyDynamicSettings.boolSetting.name(), "this is not a boolean", ORIGIN ); }
public Config config( LoadableConfig settingsClasses ) { return Config.builder().withSettings( configuration ).withConfigClasses( Collections.singletonList( settingsClasses ) ).build(); }
@Test public void shouldSetInternalParameter() { // Given Config config = Config.builder() .withSetting( MySettingsWithDefaults.secretSetting, "false" ) .withSetting( MySettingsWithDefaults.hello, "ABC" ) .withConfigClasses( Arrays.asList( mySettingsWithDefaults, myMigratingSettings ) ) .build(); // Then assertTrue( config.getConfigValues().get( MySettingsWithDefaults.secretSetting.name() ).internal() ); assertFalse( config.getConfigValues().get( MySettingsWithDefaults.hello.name() ).internal() ); }
@Test public void registeringUpdateListenerOnNonDynamicSettingMustThrow() { Config config = Config.builder().withConfigClasses( singletonList( mySettingsWithDefaults ) ).build(); expect.expect( IllegalArgumentException.class ); config.registerDynamicUpdateListener( MySettingsWithDefaults.hello, ( a, b ) -> fail( "never called" ) ); }