修复NumberWithFormat导致转换Long异常问题

This commit is contained in:
Looly 2023-03-08 22:40:56 +08:00
parent 11e3d7acf2
commit 3d10a39ea3
6 changed files with 51 additions and 25 deletions

View File

@ -2,12 +2,12 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.15.M1 (2023-03-07)
# 5.8.15.M1 (2023-03-08)
### 🐣新特性
* 【all 】 修复Automatic-Module-Name错误问题issue#2952@Github
### 🐞Bug修复
* 【all 】 修复Automatic-Module-Name错误问题issue#2952@Github
* 【all 】 修复NumberWithFormat导致转换Long异常问题issue#I6L2LO@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -14,6 +14,7 @@ import java.util.Date;
* @since 5.8.13
*/
public class NumberWithFormat extends Number implements TypeConverter{
private static final long serialVersionUID = 1L;
private final Number number;
private final String format;
@ -31,7 +32,8 @@ public class NumberWithFormat extends Number implements TypeConverter{
@SuppressWarnings("unchecked")
@Override
public Object convert(Type targetType, Object value) {
if (targetType instanceof Class) {
// 自定义日期格式
if (null != this.format && targetType instanceof Class) {
final Class<?> clazz = (Class<?>) targetType;
// https://gitee.com/dromara/hutool/issues/I6IS5B
if (Date.class.isAssignableFrom(clazz)) {
@ -41,8 +43,12 @@ public class NumberWithFormat extends Number implements TypeConverter{
} else if(String.class == clazz){
return toString();
}
// 其他情况按照正常数字转换
}
throw new ConvertException("Unsupported target type {}", targetType);
// 按照正常数字转换
return Convert.convertWithCheck(targetType, this.number, null, false);
}
@Override

View File

@ -41,6 +41,9 @@ public class NumberConverter extends AbstractConverter<Number> {
private final Class<? extends Number> targetType;
/**
* 构造
*/
public NumberConverter() {
this.targetType = Number.class;
}
@ -67,7 +70,7 @@ public class NumberConverter extends AbstractConverter<Number> {
@Override
protected String convertToStr(Object value) {
String result = StrUtil.trim(super.convertToStr(value));
final String result = StrUtil.trim(super.convertToStr(value));
if (null != result && result.length() > 1) {
final char c = Character.toUpperCase(result.charAt(result.length() - 1));
if (c == 'D' || c == 'L' || c == 'F') {

View File

@ -1,11 +1,11 @@
package cn.hutool.core.getter;
import cn.hutool.core.convert.Convert;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import cn.hutool.core.convert.Convert;
/**
* 基本类型的getter接口抽象实现所有类型的值获取都是通过将String转换而来<br>
* 用户只需实现getStr方法即可其他类型将会从String结果中转换 在不提供默认值的情况下 如果值不存在或获取错误返回null<br>

View File

@ -17,13 +17,7 @@ import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.concurrent.atomic.DoubleAdder;
@ -143,6 +137,13 @@ public class ConvertTest {
Assert.assertEquals(8, dLong2);
}
@Test
public void toLongFromNumberWithFormatTest() {
final NumberWithFormat value = new NumberWithFormat(1678285713935L, null);
final Long aLong = Convert.convertWithCheck(Long.class, value, null, false);
Assert.assertEquals(new Long(1678285713935L), aLong);
}
@Test
public void toCharTest() {
final String str = "aadfdsfs";

View File

@ -10,19 +10,19 @@ public class JWTTest {
@Test
public void createHs256Test(){
byte[] key = "1234567890".getBytes();
JWT jwt = JWT.create()
final byte[] key = "1234567890".getBytes();
final JWT jwt = JWT.create()
.setPayload("sub", "1234567890")
.setPayload("name", "looly")
.setPayload("admin", true)
.setExpiresAt(DateUtil.parse("2022-01-01"))
.setKey(key);
String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." +
final String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." +
"eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Imxvb2x5IiwiYWRtaW4iOnRydWUsImV4cCI6MTY0MDk2NjQwMH0." +
"bXlSnqVeJXWqUIt7HyEhgKNVlIPjkumHlAwFY-5YCtk";
String token = jwt.sign();
final String token = jwt.sign();
Assert.assertEquals(rightToken, token);
Assert.assertTrue(JWT.of(rightToken).setKey(key).verify());
@ -30,7 +30,7 @@ public class JWTTest {
@Test
public void parseTest(){
String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." +
final String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." +
"eyJzdWIiOiIxMjM0NTY3ODkwIiwiYWRtaW4iOnRydWUsIm5hbWUiOiJsb29seSJ9." +
"U2aQkC2THYV9L0fTN-yBBI7gmo5xhmvMhATtu8v0zEA";
@ -51,16 +51,16 @@ public class JWTTest {
@Test
public void createNoneTest(){
JWT jwt = JWT.create()
final JWT jwt = JWT.create()
.setPayload("sub", "1234567890")
.setPayload("name", "looly")
.setPayload("admin", true)
.setSigner(JWTSignerUtil.none());
String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." +
final String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." +
"eyJzdWIiOiIxMjM0NTY3ODkwIiwiYWRtaW4iOnRydWUsIm5hbWUiOiJsb29seSJ9.";
String token = jwt.sign();
final String token = jwt.sign();
Assert.assertEquals(token, token);
Assert.assertTrue(JWT.of(rightToken).setSigner(JWTSignerUtil.none()).verify());
@ -71,7 +71,7 @@ public class JWTTest {
*/
@Test(expected = JWTException.class)
public void needSignerTest(){
JWT jwt = JWT.create()
final JWT jwt = JWT.create()
.setPayload("sub", "1234567890")
.setPayload("name", "looly")
.setPayload("admin", true);
@ -81,11 +81,27 @@ public class JWTTest {
@Test
public void verifyTest(){
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
final String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
"eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbImFsbCJdLCJleHAiOjE2MjQwMDQ4MjIsInVzZXJJZCI6MSwiYXV0aG9yaXRpZXMiOlsiUk9MRV_op5LoibLkuozlj7ciLCJzeXNfbWVudV8xIiwiUk9MRV_op5LoibLkuIDlj7ciLCJzeXNfbWVudV8yIl0sImp0aSI6ImQ0YzVlYjgwLTA5ZTctNGU0ZC1hZTg3LTVkNGI5M2FhNmFiNiIsImNsaWVudF9pZCI6ImhhbmR5LXNob3AifQ." +
"aixF1eKlAKS_k3ynFnStE7-IRGiD5YaqznvK2xEjBew";
final boolean verify = JWT.of(token).setKey(StrUtil.utf8Bytes("123456")).verify();
Assert.assertTrue(verify);
}
@Test
public void getLongTest(){
final String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9"
+ ".eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJhZG1pbiIsImRldmljZSI6ImRlZmF1bHQtZGV2aWNlIiwiZWZmIjoxNjc4Mjg1NzEzOTM1LCJyblN0ciI6IkVuMTczWFhvWUNaaVZUWFNGOTNsN1pabGtOalNTd0pmIn0"
+ ".wRe2soTaWYPhwcjxdzesDi1BgEm9D61K-mMT3fPc4YM"
+ "";
final JWT jwt = JWTUtil.parseToken(rightToken);
Assert.assertEquals(
"{\"loginType\":\"login\",\"loginId\":\"admin\",\"device\":\"default-device\"," +
"\"eff\":1678285713935,\"rnStr\":\"En173XXoYCZiVTXSF93l7ZZlkNjSSwJf\"}",
jwt.getPayloads().toString());
Assert.assertEquals(Long.valueOf(1678285713935L), jwt.getPayloads().getLong("eff"));
}
}