@Test void testPaths() { File directory = new File( "myDirectory" ); Setting<File> config = buildSetting( "config", PATH, new File( directory, "config.properties" ).getAbsolutePath() ).constraint( isFile ).build(); assertThat( config.apply( map( stringMap() ) ).getAbsolutePath(), equalTo( new File( directory, "config.properties" ).getAbsolutePath() ) ); }
@Test void testDuration() { Setting<Duration> setting = buildSetting( "foo.bar", DURATION, "3s").constraint( min( DURATION.apply( "3s" ) ) ).build(); assertThat( setting.apply( map( stringMap( "foo.bar", "4s" ) ) ), equalTo( Duration.ofSeconds( 4 ) ) ); }
@Test void testMin() { Setting<Integer> setting = buildSetting( "foo", INTEGER, "3" ).constraint( min( 2 ) ).build(); // Ok assertThat( setting.apply( map( stringMap( "foo", "4" ) ) ), equalTo( 4 ) ); // Bad assertThrows( InvalidSettingException.class, () -> setting.apply( map( stringMap( "foo", "1" ) ) ) ); }
@Test void testMax() { Setting<Integer> setting = buildSetting( "foo", INTEGER, "3" ).constraint( max( 5 ) ).build(); // Ok assertThat( setting.apply( map( stringMap( "foo", "4" ) ) ), equalTo( 4 ) ); // Bad assertThrows( InvalidSettingException.class, () -> setting.apply( map( stringMap( "foo", "7" ) ) ) ); }
@Test void exceptDoesNotAllowForbiddenValues() { Setting<String> restrictedSetting = buildSetting( "foo", STRING, "test" ).constraint( except( "a", "b", "c" ) ).build(); assertEquals( "test", restrictedSetting.apply( map( stringMap() ) ) ); assertEquals( "d", restrictedSetting.apply( map( stringMap( "foo", "d" ) ) ) ); assertThrows( InvalidSettingException.class, () -> restrictedSetting.apply( map( stringMap( "foo", "a" ) ) ) ); assertThrows( InvalidSettingException.class, () -> restrictedSetting.apply( map( stringMap( "foo", "b" ) ) ) ); InvalidSettingException exception = assertThrows( InvalidSettingException.class, () -> restrictedSetting.apply( map( stringMap( "foo", "c" ) ) ) ); assertThat( exception.getMessage(), containsString( "not allowed value is: c" ) ); }
@Test void testDurationWithBrokenDefault() { // Notice that the default value is less that the minimum Setting<Duration> setting = buildSetting( "foo.bar", DURATION, "1s" ).constraint( min( DURATION.apply( "3s" ) ) ).build(); assertThrows( InvalidSettingException.class, () -> setting.apply( map( stringMap() ) ) ); }
@Test void testMatches() { Setting<String> setting = buildSetting( "foo", STRING, "abc" ).constraint( matches( "a*b*c*" ) ).build(); // Ok assertThat( setting.apply( map( stringMap( "foo", "aaabbbccc" ) ) ), equalTo( "aaabbbccc" ) ); // Bad assertThrows( InvalidSettingException.class, () -> setting.apply( map( stringMap( "foo", "cba" ) ) ) ); }
@Test void testRange() { Setting<Integer> setting = buildSetting( "foo", INTEGER, "3" ).constraint( range( 2, 5 ) ).build(); // Ok assertThat( setting.apply( map( stringMap( "foo", "4" ) ) ), equalTo( 4 ) ); // Bad assertThrows( InvalidSettingException.class, () -> setting.apply( map( stringMap( "foo", "1" ) ) ) ); assertThrows( InvalidSettingException.class, () -> setting.apply( map( stringMap( "foo", "6" ) ) ) ); }
@Test void testDurationWithValueNotWithinConstraint() { Setting<Duration> setting = buildSetting( "foo.bar", DURATION, "3s" ).constraint( min( DURATION.apply( "3s" ) ) ).build(); assertThrows( InvalidSettingException.class, () -> setting.apply( map( stringMap( "foo.bar", "2s" ) ) ) ); }