tbel: add parseLong Test
This commit is contained in:
parent
f15d84e44a
commit
35dfa1e7bd
@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.StringUtils;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
@ -211,67 +212,71 @@ public class TbUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Integer parseInt(String value) {
|
public static Integer parseInt(String value) {
|
||||||
if (value != null) {
|
int radix = getRadix(value);
|
||||||
try {
|
return parseInt(value, radix);
|
||||||
int radix = 10;
|
|
||||||
if (isHexadecimal(value)) {
|
|
||||||
radix = 16;
|
|
||||||
}
|
|
||||||
return Integer.parseInt(prepareNumberString(value), radix);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
Float f = parseFloat(value);
|
|
||||||
if (f != null) {
|
|
||||||
return f.intValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Integer parseInt(String value, int radix) {
|
public static Integer parseInt(String value, int radix) {
|
||||||
if (value != null) {
|
if (StringUtils.isNotBlank(value)) {
|
||||||
try {
|
try {
|
||||||
return Integer.parseInt(prepareNumberString(value), radix);
|
String valueP = prepareNumberString(value);
|
||||||
|
isValidRadix(valueP, radix);
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(valueP, radix);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
Float f = parseFloat(value);
|
BigInteger bi = new BigInteger(valueP, radix);
|
||||||
|
if (bi.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0)
|
||||||
|
throw new NumberFormatException("Value \"" + value + "\" is greater than the maximum Integer value " + Integer.MAX_VALUE + " !");
|
||||||
|
if (bi.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0)
|
||||||
|
throw new NumberFormatException("Value \"" + value + "\" is less than the minimum Integer value " + Integer.MIN_VALUE + " !");
|
||||||
|
Float f = parseFloat(valueP);
|
||||||
if (f != null) {
|
if (f != null) {
|
||||||
return f.intValue();
|
return f.intValue();
|
||||||
|
} else {
|
||||||
|
throw new NumberFormatException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new NumberFormatException(e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Long parseLong(String value) {
|
public static Long parseLong(String value) {
|
||||||
if (value != null) {
|
int radix = getRadix(value);
|
||||||
|
return parseLong(value, radix);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Long parseLong(String value, int radix) {
|
||||||
|
if (StringUtils.isNotBlank(value)) {
|
||||||
try {
|
try {
|
||||||
int radix = 10;
|
String valueP = prepareNumberString(value);
|
||||||
if (isHexadecimal(value)) {
|
isValidRadix(valueP, radix);
|
||||||
radix = 16;
|
try {
|
||||||
}
|
return Long.parseLong(valueP, radix);
|
||||||
return Long.parseLong(prepareNumberString(value), radix);
|
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
Double d = parseDouble(value);
|
BigInteger bi = new BigInteger(valueP, radix);
|
||||||
if (d != null) {
|
if (bi.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0)
|
||||||
return d.longValue();
|
throw new NumberFormatException("Value \"" + value + "\"is greater than the maximum Long value " + Long.MAX_VALUE + " !");
|
||||||
|
if (bi.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0)
|
||||||
|
throw new NumberFormatException("Value \"" + value + "\" is less than the minimum Long value " + Long.MIN_VALUE + " !");
|
||||||
|
Double dd = parseDouble(valueP);
|
||||||
|
if (dd != null) {
|
||||||
|
return dd.longValue();
|
||||||
|
} else {
|
||||||
|
throw new NumberFormatException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new NumberFormatException(e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Long parseLong(String value, int radix) {
|
private static int getRadix(String value, int... radixS) {
|
||||||
if (value != null) {
|
return radixS.length > 0 ? radixS[0] : isHexadecimal(value) ? 16 : 10;
|
||||||
try {
|
|
||||||
return Long.parseLong(prepareNumberString(value), radix);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
Double d = parseDouble(value);
|
|
||||||
if (d != null) {
|
|
||||||
return d.longValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Float parseFloat(String value) {
|
public static Float parseFloat(String value) {
|
||||||
@ -622,4 +627,19 @@ public class TbUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isValidRadix(String value, int radix) {
|
||||||
|
for (int i = 0; i < value.length(); i++) {
|
||||||
|
if (i == 0 && value.charAt(i) == '-') {
|
||||||
|
if (value.length() == 1)
|
||||||
|
throw new NumberFormatException("Failed radix [" + radix + "] for value: \"" + value + "\"!");
|
||||||
|
else
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (Character.digit(value.charAt(i), radix) < 0)
|
||||||
|
throw new NumberFormatException("Failed radix: [" + radix + "] for value: \"" + value + "\"!");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,6 +26,7 @@ import org.mvel2.SandboxedParserConfiguration;
|
|||||||
import org.mvel2.execution.ExecutionArrayList;
|
import org.mvel2.execution.ExecutionArrayList;
|
||||||
import org.mvel2.execution.ExecutionHashMap;
|
import org.mvel2.execution.ExecutionHashMap;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
@ -182,6 +183,98 @@ public class TbUtilsTest {
|
|||||||
Assert.assertEquals(expectedMapWithoutPaths, actualMapWithoutPaths);
|
Assert.assertEquals(expectedMapWithoutPaths, actualMapWithoutPaths);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parseInt() {
|
||||||
|
Assert.assertNull(TbUtils.parseInt(null));
|
||||||
|
Assert.assertNull(TbUtils.parseInt(""));
|
||||||
|
Assert.assertNull(TbUtils.parseInt(" "));
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(0).get(), TbUtils.parseInt("0"));
|
||||||
|
Assert.assertEquals(java.util.Optional.of(0).get(), TbUtils.parseInt("-0"));
|
||||||
|
Assert.assertEquals(java.util.Optional.of(473).get(), TbUtils.parseInt("473"));
|
||||||
|
Assert.assertEquals(java.util.Optional.of(-255).get(), TbUtils.parseInt("-0xFF"));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("FF"));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("0xFG"));
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(102).get(), TbUtils.parseInt("1100110", 2));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("1100210", 2));
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(63).get(), TbUtils.parseInt("77", 8));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("18", 8));
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(-255).get(), TbUtils.parseInt("-FF", 16));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("FG", 16));
|
||||||
|
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(Integer.MAX_VALUE).get(), TbUtils.parseInt(Integer.toString(Integer.MAX_VALUE), 10));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt(BigInteger.valueOf(Integer.MAX_VALUE).add(BigInteger.valueOf(1)).toString(10), 10));
|
||||||
|
Assert.assertEquals(java.util.Optional.of(Integer.MIN_VALUE).get(), TbUtils.parseInt(Integer.toString(Integer.MIN_VALUE), 10));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt(BigInteger.valueOf(Integer.MIN_VALUE).subtract(BigInteger.valueOf(1)).toString(10), 10));
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(506070563).get(), TbUtils.parseInt("KonaIn", 30));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("KonaIn", 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parseLong() {
|
||||||
|
Assert.assertNull(TbUtils.parseLong(null));
|
||||||
|
Assert.assertNull(TbUtils.parseLong(""));
|
||||||
|
Assert.assertNull(TbUtils.parseLong(" "));
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(0L).get(), TbUtils.parseLong("0"));
|
||||||
|
Assert.assertEquals(java.util.Optional.of(0L).get(), TbUtils.parseLong("-0"));
|
||||||
|
Assert.assertEquals(java.util.Optional.of(473L).get(), TbUtils.parseLong("473"));
|
||||||
|
Assert.assertEquals(java.util.Optional.of(-65535L).get(), TbUtils.parseLong("-0xFFFF"));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("FFFF"));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("0xFGFF"));
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(13158L).get(), TbUtils.parseLong("11001101100110", 2));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("11001101100210", 2));
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(9223372036854775807L).get(), TbUtils.parseLong("777777777777777777777", 8));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("1787", 8));
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(-255L).get(), TbUtils.parseLong("-FF", 16));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("FG", 16));
|
||||||
|
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(Long.MAX_VALUE).get(), TbUtils.parseLong(Long.toString(Long.MAX_VALUE), 10));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.valueOf(1)).toString(10), 10));
|
||||||
|
Assert.assertEquals(java.util.Optional.of(Long.MIN_VALUE).get(), TbUtils.parseLong(Long.toString(Long.MIN_VALUE), 10));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.valueOf(1)).toString(10), 10));
|
||||||
|
|
||||||
|
Assert.assertEquals(java.util.Optional.of(218840926543L).get(), TbUtils.parseLong("KonaLong", 27));
|
||||||
|
Assert.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("KonaLong", 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// parseLong String.class, int.class
|
||||||
|
// parseLittleEndianHexToLong String.class
|
||||||
|
// parseBigEndianHexToLong String.class
|
||||||
|
// parseHexToLong String.class
|
||||||
|
// parseHexToLong String.class boolean.class
|
||||||
|
// parseBytesToLong List.class, int.class, int.class
|
||||||
|
// parseBytesToLong List.class, int.class, int.class, boolean.class
|
||||||
|
// parseBytesToLong byte[].class, int.class, int.class
|
||||||
|
// parseBytesToLong byte[].class, int.class, int.class, boolean.class
|
||||||
|
// parseLittleEndianHexToFloat String.class
|
||||||
|
// parseBigEndianHexToFloat String.class
|
||||||
|
// parseHexToFloat String.class
|
||||||
|
// parseHexToFloat String.class boolean.class
|
||||||
|
// parseBytesToFloat byte[].class, int.class, boolean.class
|
||||||
|
// parseBytesToFloat byte[].class, int.class
|
||||||
|
// parseBytesToFloat List.class, int.class, boolean.class
|
||||||
|
// parseBytesToFloat List.class, int.class
|
||||||
|
// toFixed float.class, int.class
|
||||||
|
// parseLittleEndianHexToDouble String.class
|
||||||
|
// parseBigEndianHexToDouble String.class
|
||||||
|
// parseHexToDouble String.class
|
||||||
|
// parseHexToDouble String.class boolean.class
|
||||||
|
// parseBytesToDouble byte[].class, int.class
|
||||||
|
// parseBytesToDouble byte[].class, int.class, boolean.class
|
||||||
|
// parseBytesToDouble List.class, int.class
|
||||||
|
// parseBytesToDouble List.class, int.class boolean.class
|
||||||
|
|
||||||
|
|
||||||
private static String keyToValue(String key, String extraSymbol) {
|
private static String keyToValue(String key, String extraSymbol) {
|
||||||
return key + "Value" + (extraSymbol == null ? "" : extraSymbol);
|
return key + "Value" + (extraSymbol == null ? "" : extraSymbol);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user