Skip to content

Commit

Permalink
HSEARCH-3386 Test and fix ConfigurationProperty conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Dec 4, 2018
1 parent 6030990 commit e5e2897
Show file tree
Hide file tree
Showing 4 changed files with 548 additions and 32 deletions.
Expand Up @@ -59,27 +59,29 @@ public static <T> Optional<T> convert(Class<T> expectedType, Function<String, T>
* @throws SearchException for invalid format or values.
*/
public static Optional<Boolean> convertBoolean(Object value) {
if ( value instanceof Boolean ) {
return Optional.of( (Boolean) value );
}
try {
Optional<String> optionalCleaned = optionalTrimmedNonEmpty( (String) value );
if ( optionalCleaned.isPresent() ) {
String cleaned = optionalCleaned.get();
// avoiding Boolean.valueOf() to have more checks: makes it easy to spot wrong type in cfg.
if ( "false".equalsIgnoreCase( cleaned ) ) {
return Optional.of( false );
}
else if ( "true".equalsIgnoreCase( cleaned ) ) {
return Optional.of( true );
if ( value instanceof Boolean ) {
return Optional.of( (Boolean) value );
}
if ( value instanceof String ) {
Optional<String> optionalCleaned = optionalTrimmedNonEmpty( (String) value );
if ( optionalCleaned.isPresent() ) {
String cleaned = optionalCleaned.get();
// avoiding Boolean.valueOf() to have more checks: makes it easy to spot wrong type in cfg.
if ( "false".equalsIgnoreCase( cleaned ) ) {
return Optional.of( false );
}
else if ( "true".equalsIgnoreCase( cleaned ) ) {
return Optional.of( true );
}
}
}
}
catch (RuntimeException e) {
throw log.invalidBooleanPropertyValue( e );
throw log.invalidBooleanPropertyValue( e.getMessage(), e );
}

throw log.invalidBooleanPropertyValue( null );
throw log.invalidBooleanPropertyValue( "", null );
}

/**
Expand All @@ -90,16 +92,20 @@ else if ( "true".equalsIgnoreCase( cleaned ) ) {
* @throws SearchException for invalid format or values.
*/
public static Optional<Integer> convertInteger(Object value) {
if ( value instanceof Number ) {
return Optional.of( ( (Number) value ).intValue() );
}
try {
return optionalTrimmedNonEmpty( (String) value )
.map( Integer::parseInt );
if ( value instanceof Number ) {
return Optional.of( ( (Number) value ).intValue() );
}
if ( value instanceof String ) {
return optionalTrimmedNonEmpty( (String) value )
.map( Integer::parseInt );
}
}
catch (RuntimeException e) {
throw log.invalidIntegerPropertyValue( e.getMessage(), e );
}

throw log.invalidIntegerPropertyValue( "", null );
}

/**
Expand All @@ -110,16 +116,20 @@ public static Optional<Integer> convertInteger(Object value) {
* @throws SearchException for invalid format or values.
*/
public static Optional<Long> convertLong(Object value) {
if ( value instanceof Number ) {
return Optional.of( ( (Number) value ).longValue() );
}
try {
return optionalTrimmedNonEmpty( (String) value )
.map( Long::parseLong );
if ( value instanceof Number ) {
return Optional.of( ( (Number) value ).longValue() );
}
if ( value instanceof String ) {
return optionalTrimmedNonEmpty( (String) value )
.map( Long::parseLong );
}
}
catch (RuntimeException e) {
throw log.invalidLongPropertyValue( e.getMessage(), e );
}

throw log.invalidLongPropertyValue( "", null );
}

public static <T> Optional<List<T>> convertMultiValue(Pattern separatorPattern,
Expand Down
Expand Up @@ -61,20 +61,20 @@ public interface Log extends BasicLogger {
SearchException unableToConvertConfigurationProperty(String key, Object rawValue, String errorMessage, @Cause Exception cause);

@Message(id = ID_OFFSET_2 + 2,
value = "Invalid value: the value is not an instance of '%1$s' and is not a String that can be parsed: %2$s")
SearchException invalidPropertyValue(Class<?> expectedType, String errorMessage, @Cause Exception cause);
value = "Invalid value: expected either an instance of '%1$s' or a String that can be parsed. %2$s")
SearchException invalidPropertyValue(@FormatWith(ClassFormatter.class) Class<?> expectedType, String errorMessage, @Cause Exception cause);

@Message(id = ID_OFFSET_2 + 3,
value = "Invalid boolean value: expected either a Boolean, the String 'true' or the String 'false'.")
SearchException invalidBooleanPropertyValue(@Cause Exception cause);
value = "Invalid Boolean value: expected either a Boolean, the String 'true' or the String 'false'. %1$s")
SearchException invalidBooleanPropertyValue(String nestedErrorMessage, @Cause Exception cause);

@Message(id = ID_OFFSET_2 + 4,
value = "%1$s")
SearchException invalidIntegerPropertyValue(String errorMessage, @Cause Exception cause);
value = "Invalid Integer value: expected either a Number or a String that can be parsed into an Integer. %1$s")
SearchException invalidIntegerPropertyValue(String nestedErrorMessage, @Cause Exception cause);

@Message(id = ID_OFFSET_2 + 5,
value = "%1$s")
SearchException invalidLongPropertyValue(String errorMessage, @Cause Exception cause);
value = "Invalid Long value: expected either a Number or a String that can be parsed into a Long. %1$s")
SearchException invalidLongPropertyValue(String nestedErrorMessage, @Cause Exception cause);

@Message(id = ID_OFFSET_2 + 6,
value = "Invalid multi value: expected either a Collection or a String.")
Expand Down

0 comments on commit e5e2897

Please sign in to comment.