This commit is contained in:
Looly
2022-06-16 17:49:37 +08:00
parent 7cf1b3758d
commit e491349b81
18 changed files with 239 additions and 184 deletions

View File

@@ -13,7 +13,7 @@ public class Issue2369Test {
final String s = JSONUtil.toJsonStr(bytes);
Assert.assertEquals("[10,11]", s);
final Object o = JSONUtil.toBean(s, byte[].class, false);
final Object o = JSONUtil.toBean(s, byte[].class);
Assert.assertArrayEquals(bytes, (byte[])o);
}
}

View File

@@ -17,7 +17,7 @@ public class Issue2377Test {
final String paramBytesStr = JSONUtil.toJsonStr(paramList.get(1));
Assert.assertEquals("[10,11]", paramBytesStr);
final byte[] paramBytes = JSONUtil.toBean(paramBytesStr, byte[].class, false);
final byte[] paramBytes = JSONUtil.toBean(paramBytesStr, byte[].class);
Assert.assertArrayEquals((byte[]) paramArray[1], paramBytes);
}
}

View File

@@ -1,13 +1,12 @@
package cn.hutool.json;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.reflect.TypeReference;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
public class Issue488Test {
@@ -15,8 +14,8 @@ public class Issue488Test {
public void toBeanTest() {
final String jsonStr = ResourceUtil.readUtf8Str("issue488.json");
final ResultSuccess<List<EmailAddress>> result = JSONUtil.toBean(jsonStr,
new TypeReference<ResultSuccess<List<EmailAddress>>>() {}, false);
final ResultSuccess<List<EmailAddress>> result = JSONUtil.toBean(jsonStr, JSONConfig.of(),
new TypeReference<ResultSuccess<List<EmailAddress>>>() {});
Assert.assertEquals("https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.emailAddress)", result.getContext());
@@ -35,8 +34,8 @@ public class Issue488Test {
public void toCollctionBeanTest() {
final String jsonStr = ResourceUtil.readUtf8Str("issue488Array.json");
final List<ResultSuccess<List<EmailAddress>>> resultList = JSONUtil.toBean(jsonStr,
new TypeReference<List<ResultSuccess<List<EmailAddress>>>>() {}, false);
final List<ResultSuccess<List<EmailAddress>>> resultList = JSONUtil.toBean(jsonStr, JSONConfig.of(),
new TypeReference<List<ResultSuccess<List<EmailAddress>>>>() {});
final ResultSuccess<List<EmailAddress>> result = resultList.get(0);

View File

@@ -218,14 +218,14 @@ public class JSONObjectTest {
@Test
public void toBeanNullStrTest() {
final JSONObject json = JSONUtil.createObj()//
final JSONObject json = JSONUtil.createObj(JSONConfig.of().setIgnoreError(true))//
.set("strValue", "null")//
.set("intValue", 123)//
// 子对象对应"null"字符串,如果忽略错误,跳过,否则抛出转换异常
.set("beanValue", "null")//
.set("list", JSONUtil.createArray().set("a").set("b"));
final TestBean bean = json.toBean(TestBean.class, true);
final TestBean bean = json.toBean(TestBean.class);
// 当JSON中为字符串"null"时应被当作字符串处理
Assert.assertEquals("null", bean.getStrValue());
// 当JSON中为字符串"null"时Bean中的字段类型不匹配应在ignoreError模式下忽略注入
@@ -483,6 +483,22 @@ public class JSONObjectTest {
Assert.assertEquals(DateUtil.beginOfDay(date), parse.getDate("date"));
}
@Test
public void setDateFormatTest3() {
// 自定义格式为只有秒的时间戳一版用于JWT
final JSONConfig jsonConfig = JSONConfig.of().setDateFormat("#sss");
final Date date = DateUtil.parse("2020-06-05 11:16:11");
final JSONObject json = new JSONObject(jsonConfig);
json.set("date", date);
Assert.assertEquals("{\"date\":1591326971}", json.toString());
// 解析测试
final JSONObject parse = JSONUtil.parseObj(json.toString(), jsonConfig);
Assert.assertEquals(date, DateUtil.date(parse.getDate("date")));
}
@Test
public void setCustomDateFormatTest() {
final JSONConfig jsonConfig = JSONConfig.of();

View File

@@ -1,13 +1,13 @@
package cn.hutool.json.issueIVMD5;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.reflect.TypeReference;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
public class IssueIVMD5Test {
@@ -19,7 +19,7 @@ public class IssueIVMD5Test {
final String jsonStr = ResourceUtil.readUtf8Str("issueIVMD5.json");
final TypeReference<BaseResult<StudentInfo>> typeReference = new TypeReference<BaseResult<StudentInfo>>() {};
final BaseResult<StudentInfo> bean = JSONUtil.toBean(jsonStr, typeReference.getType(), false);
final BaseResult<StudentInfo> bean = JSONUtil.toBean(jsonStr, JSONConfig.of(), typeReference.getType());
final StudentInfo data2 = bean.getData2();
Assert.assertEquals("B4DDF491FDF34074AE7A819E1341CB6C", data2.getAccountId());
@@ -33,7 +33,7 @@ public class IssueIVMD5Test {
final String jsonStr = ResourceUtil.readUtf8Str("issueIVMD5.json");
final TypeReference<BaseResult<StudentInfo>> typeReference = new TypeReference<BaseResult<StudentInfo>>() {};
final BaseResult<StudentInfo> bean = JSONUtil.toBean(jsonStr, typeReference.getType(), false);
final BaseResult<StudentInfo> bean = JSONUtil.toBean(jsonStr, JSONConfig.of(), typeReference.getType());
final List<StudentInfo> data = bean.getData();
final StudentInfo studentInfo = data.get(0);

View File

@@ -1,11 +1,19 @@
package cn.hutool.json.jwt;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.json.jwt.signers.JWTSignerUtil;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JWTTest {
@Test
@@ -88,4 +96,67 @@ public class JWTTest {
final boolean verify = JWT.of(token).setKey(StrUtil.utf8Bytes("123456")).verify();
Assert.assertTrue(verify);
}
@Data
public static class UserTest {
private String name;
private Integer age;
}
@Test
public void payloadTest() {
final UserTest bean = new UserTest();
bean.setAge(18);
bean.setName("takaki");
final Date date = new Date();
final List<Integer> list = Arrays.asList(1, 2, 3);
final Integer num = 18;
final String username = "takaki";
final HashMap<String, String> map = new HashMap<>();
map.put("test1", "1");
map.put("test2", "2");
final Map<String, Object> payload = new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put("username", username);
put("bean", bean);
put("number", num);
put("list", list);
put("date", date);
put("map", map);
}
};
final String token = JWTUtil.createToken(payload, "123".getBytes());
final JWT jwt = JWT.of(token);
final String strRes = jwt.getPayload("username", String.class);
final UserTest beanRes = jwt.getPayload("bean", UserTest.class);
final Date dateRes = jwt.getPayload("date", Date.class);
final List<?> listRes = jwt.getPayload("list", List.class);
final Integer numRes = jwt.getPayload("number", Integer.class);
final HashMap<?, ?> mapRes = jwt.getPayload("map", HashMap.class);
Assert.assertEquals(bean, beanRes);
Assert.assertEquals(numRes, num);
Assert.assertEquals(username, strRes);
Assert.assertEquals(list, listRes);
final String formattedDate = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
final String formattedRes = DateUtil.format(dateRes, "yyyy-MM-dd HH:mm:ss");
Assert.assertEquals(formattedDate, formattedRes);
Assert.assertEquals(map, mapRes);
}
@Test()
public void getDateTest(){
final String token = JWT.create()
.setIssuedAt(DateUtil.parse("2022-02-02"))
.setKey("123456".getBytes())
.sign();
// 签发时间早于被检查的时间
final Date date = JWT.of(token).getPayload().getClaimsJson().getDate(JWTPayload.ISSUED_AT);
Assert.assertEquals("2022-02-02", DateUtil.format(date, DatePattern.NORM_DATE_PATTERN));
}
}