Fix behaviour for convertValue with value null

I insinuate that the non primitive BigInteger and BigDecimal types
should also return default values for null input.

Rename the map to `baseTypeDefaults for better understanding and
remove the check for `isPrimitive` b/c this seemed to be kind of optimization
but breaked the check for included non-primitive types.

B/c the unit tests run successfully, we can use the existing default fields ZERO.
This commit is contained in:
Sebastian Peters
2018-12-21 23:42:03 +01:00
parent b58dc38c5c
commit d09d3dcb3c
2 changed files with 9 additions and 7 deletions
@@ -48,7 +48,7 @@ public abstract class DefaultTypeConverter implements TypeConverter {
private static final String NULL_STRING = "null";
private static final Map<Class<?>, Object> primitiveDefaults;
private static final Map<Class<?>, Object> baseTypeDefaults;
private Container container;
@@ -62,9 +62,9 @@ public abstract class DefaultTypeConverter implements TypeConverter {
map.put(Long.TYPE, Long.valueOf(0L));
map.put(Float.TYPE, new Float(0.0f));
map.put(Double.TYPE, new Double(0.0));
map.put(BigInteger.class, new BigInteger("0"));
map.put(BigDecimal.class, BigDecimal.valueOf(0.0));
primitiveDefaults = Collections.unmodifiableMap(map);
map.put(BigInteger.class, BigInteger.ZERO);
map.put(BigDecimal.class, BigDecimal.ZERO);
baseTypeDefaults = Collections.unmodifiableMap(map);
}
@Inject
@@ -153,8 +153,8 @@ public abstract class DefaultTypeConverter implements TypeConverter {
if (Enum.class.isAssignableFrom(toType))
result = enumValue(toType, value);
}
} else if (toType.isPrimitive()) {
result = primitiveDefaults.get(toType);
} else {
result = baseTypeDefaults.get(toType);
}
return result;
}
@@ -250,7 +250,9 @@ public class XWorkBasicConverterTest extends XWorkTestCase {
public void testBigDecimal() {
Object convertedObject = basicConverter.convertValue(null, BigDecimal.class);
assertNull(convertedObject);
assertEquals(BigDecimal.ZERO, convertedObject);
assertTrue(convertedObject instanceof BigDecimal);
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) convertedObject));
convertedObject = basicConverter.convertValue(new BigDecimal(0), BigDecimal.class);
assertEquals(BigDecimal.ZERO, convertedObject);