When MathUtils.cast gets a null input, return null

This commit is contained in:
Ruben Dijkstra 2016-02-02 16:57:58 +01:00
parent ff1ae7186a
commit 54ddc2fb2e
2 changed files with 8 additions and 3 deletions

View File

@ -60,7 +60,7 @@ public final class MathUtils {
public static <D extends Number> D cast(Number num, Class<D> type) {
D rv;
if (type.isInstance(num)) {
if (num == null || type.isInstance(num)) {
rv = type.cast(num);
} else if (type.equals(Byte.class)) {
rv = type.cast(num.byteValue());

View File

@ -13,8 +13,7 @@
*/
package com.querydsl.core.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.*;
import java.math.BigDecimal;
import java.math.BigInteger;
@ -63,6 +62,12 @@ public class MathUtilsTest {
checkSame((byte) 1, Byte.class);
}
@Test
public void cast_returns_null_when_input_is_null() {
Integer result = MathUtils.cast(null, Integer.class);
assertNull(result);
}
@Test
public void cast_throws_on_unsupported_numbers() {
expectedException.expect(IllegalArgumentException.class);