mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-04-04 23:39:27 +08:00
test: 新增 json 序列化插件的单元测试
This commit is contained in:
parent
9058e7edc4
commit
35cbd38b3b
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package cn.dev33.satoken.json;
|
||||
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
/**
|
||||
@ -30,6 +31,9 @@ public class SaJsonTemplateForFastjson implements SaJsonTemplate {
|
||||
*/
|
||||
@Override
|
||||
public String objectToJson(Object obj) {
|
||||
if(SaFoxUtil.isEmpty(obj)) {
|
||||
return null;
|
||||
}
|
||||
return JSON.toJSONString(obj);
|
||||
}
|
||||
|
||||
@ -38,6 +42,9 @@ public class SaJsonTemplateForFastjson implements SaJsonTemplate {
|
||||
*/
|
||||
@Override
|
||||
public<T> T jsonToObject(String jsonStr, Class<T> type) {
|
||||
if(SaFoxUtil.isEmpty(jsonStr)) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parseObject(jsonStr, type);
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
package cn.dev33.satoken.json;
|
||||
|
||||
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
|
||||
/**
|
||||
@ -31,6 +32,9 @@ public class SaJsonTemplateForFastjson2 implements SaJsonTemplate {
|
||||
*/
|
||||
@Override
|
||||
public String objectToJson(Object obj) {
|
||||
if(SaFoxUtil.isEmpty(obj)) {
|
||||
return null;
|
||||
}
|
||||
return JSON.toJSONString(obj);
|
||||
}
|
||||
|
||||
@ -39,6 +43,9 @@ public class SaJsonTemplateForFastjson2 implements SaJsonTemplate {
|
||||
*/
|
||||
@Override
|
||||
public <T>T jsonToObject(String jsonStr, Class<T> type) {
|
||||
if(SaFoxUtil.isEmpty(jsonStr)) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parseObject(jsonStr, type);
|
||||
}
|
||||
|
||||
|
@ -156,6 +156,9 @@ public class SaJsonTemplateForJackson implements SaJsonTemplate {
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> jsonToMap(String jsonStr) {
|
||||
if(SaFoxUtil.isEmpty(jsonStr)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> map = mapObjectMapper.readValue(jsonStr, Map.class);
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package cn.dev33.satoken.json;
|
||||
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import org.noear.snack.ONode;
|
||||
|
||||
/**
|
||||
@ -31,6 +32,9 @@ public class SaJsonTemplateForSnack3 implements SaJsonTemplate {
|
||||
*/
|
||||
@Override
|
||||
public String objectToJson(Object obj) {
|
||||
if(SaFoxUtil.isEmpty(obj)) {
|
||||
return null;
|
||||
}
|
||||
return ONode.stringify(obj);
|
||||
}
|
||||
|
||||
@ -39,6 +43,9 @@ public class SaJsonTemplateForSnack3 implements SaJsonTemplate {
|
||||
*/
|
||||
@Override
|
||||
public <T> T jsonToObject(String jsonStr, Class<T> type) {
|
||||
if(SaFoxUtil.isEmpty(jsonStr)) {
|
||||
return null;
|
||||
}
|
||||
return ONode.deserialize(jsonStr, type);
|
||||
}
|
||||
}
|
||||
|
@ -21,9 +21,10 @@
|
||||
<!-- 所有子模块 -->
|
||||
<modules>
|
||||
<!-- <module>sa-token-core-test</module> -->
|
||||
<module>sa-token-springboot-test</module>
|
||||
<module>sa-token-springboot-test</module>
|
||||
<!-- <module>sa-token-springboot-integrate-test</module> -->
|
||||
<module>sa-token-jwt-test</module>
|
||||
<module>sa-token-json-test</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
49
sa-token-test/sa-token-json-test/pom.xml
Normal file
49
sa-token-test/sa-token-json-test/pom.xml
Normal file
@ -0,0 +1,49 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-test</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>sa-token-json-test</name>
|
||||
<artifactId>sa-token-json-test</artifactId>
|
||||
<description>sa-token-json-test</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-jackson</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-fastjson</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-fastjson2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-snack3</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- jackson more -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,157 @@
|
||||
package com.pj.test;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.exception.NotImplException;
|
||||
import cn.dev33.satoken.json.*;
|
||||
import com.pj.test.model.SysUser;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Sa-Token json 序列化模块测试
|
||||
*
|
||||
* @author click33
|
||||
*
|
||||
*/
|
||||
public class SaJsonTemplateTest {
|
||||
|
||||
// 开始
|
||||
@BeforeAll
|
||||
public static void beforeClass() {
|
||||
System.out.println("\n\n------------------------ SaJsonTemplateTest star ...");
|
||||
}
|
||||
|
||||
// 结束
|
||||
@AfterAll
|
||||
public static void afterClass() {
|
||||
System.out.println("\n\n------------------------ SaJsonTemplateTest end ... \n");
|
||||
}
|
||||
|
||||
// 测试:DefaultImpl
|
||||
@Test
|
||||
public void testDefaultImpl() {
|
||||
SaManager.setSaJsonTemplate(new SaJsonTemplateDefaultImpl());
|
||||
Assertions.assertEquals(SaManager.getSaJsonTemplate().getClass(), SaJsonTemplateDefaultImpl.class);
|
||||
|
||||
// test Object -> Json
|
||||
SysUser user = new SysUser(10001, "张三", 18);
|
||||
Assertions.assertThrows(NotImplException.class, () -> SaManager.getSaJsonTemplate().objectToJson(user) );
|
||||
Assertions.assertThrows(NotImplException.class, () -> SaManager.getSaJsonTemplate().jsonToObject("xxx", SysUser.class) );
|
||||
Assertions.assertThrows(NotImplException.class, () -> SaManager.getSaJsonTemplate().jsonToObject("xxx") );
|
||||
Assertions.assertThrows(NotImplException.class, () -> SaManager.getSaJsonTemplate().jsonToMap("xxx") );
|
||||
}
|
||||
|
||||
// 测试:Jackson
|
||||
@Test
|
||||
public void testJackson() {
|
||||
SaManager.setSaJsonTemplate(new SaJsonTemplateForJackson());
|
||||
Assertions.assertEquals(SaManager.getSaJsonTemplate().getClass(), SaJsonTemplateForJackson.class);
|
||||
|
||||
// test Object -> Json
|
||||
SysUser user = new SysUser(10001, "张三", 18);
|
||||
String objectJson = SaManager.getSaJsonTemplate().objectToJson(user);
|
||||
Assertions.assertEquals("{\"@class\":\"com.pj.test.model.SysUser\",\"id\":10001,\"name\":\"张三\",\"age\":18,\"role\":null}", objectJson);
|
||||
|
||||
// test Json -> Object
|
||||
SysUser user2 = SaManager.getSaJsonTemplate().jsonToObject(objectJson, SysUser.class);
|
||||
Assertions.assertEquals(user2.toString(), user.toString());
|
||||
|
||||
SysUser user3 = (SysUser)SaManager.getSaJsonTemplate().jsonToObject(objectJson);
|
||||
Assertions.assertEquals(user3.toString(), user.toString());
|
||||
|
||||
// more
|
||||
testNull();
|
||||
testMap();
|
||||
}
|
||||
|
||||
// 测试:Fastjson
|
||||
@Test
|
||||
public void testFastjson() {
|
||||
SaManager.setSaJsonTemplate(new SaJsonTemplateForFastjson());
|
||||
Assertions.assertEquals(SaManager.getSaJsonTemplate().getClass(), SaJsonTemplateForFastjson.class);
|
||||
|
||||
// test Object -> Json
|
||||
SysUser user = new SysUser(10001, "张三", 18);
|
||||
String objectJson = SaManager.getSaJsonTemplate().objectToJson(user);
|
||||
Assertions.assertEquals("{\"age\":18,\"id\":10001,\"name\":\"张三\"}", objectJson);
|
||||
|
||||
// test Json -> Object
|
||||
SysUser user2 = SaManager.getSaJsonTemplate().jsonToObject(objectJson, SysUser.class);
|
||||
Assertions.assertEquals(user2.toString(), user.toString());
|
||||
|
||||
// more
|
||||
testNull();
|
||||
testMap();
|
||||
}
|
||||
|
||||
// 测试:Fastjson2
|
||||
@Test
|
||||
public void testFastjson2() {
|
||||
SaManager.setSaJsonTemplate(new SaJsonTemplateForFastjson2());
|
||||
Assertions.assertEquals(SaManager.getSaJsonTemplate().getClass(), SaJsonTemplateForFastjson2.class);
|
||||
|
||||
// test Object -> Json
|
||||
SysUser user = new SysUser(10001, "张三", 18);
|
||||
String objectJson = SaManager.getSaJsonTemplate().objectToJson(user);
|
||||
Assertions.assertEquals("{\"age\":18,\"id\":10001,\"name\":\"张三\"}", objectJson);
|
||||
|
||||
// test Json -> Object
|
||||
SysUser user2 = SaManager.getSaJsonTemplate().jsonToObject(objectJson, SysUser.class);
|
||||
Assertions.assertEquals(user2.toString(), user.toString());
|
||||
|
||||
// more
|
||||
testNull();
|
||||
testMap();
|
||||
}
|
||||
|
||||
// 测试:Snack3
|
||||
@Test
|
||||
public void testSnack3() {
|
||||
SaManager.setSaJsonTemplate(new SaJsonTemplateForSnack3());
|
||||
Assertions.assertEquals(SaManager.getSaJsonTemplate().getClass(), SaJsonTemplateForSnack3.class);
|
||||
|
||||
// test Object -> Json
|
||||
SysUser user = new SysUser(10001, "张三", 18);
|
||||
String objectJson = SaManager.getSaJsonTemplate().objectToJson(user);
|
||||
Assertions.assertEquals("{\"id\":10001,\"name\":\"张三\",\"age\":18}", objectJson);
|
||||
|
||||
// test Json -> Object
|
||||
SysUser user2 = SaManager.getSaJsonTemplate().jsonToObject(objectJson, SysUser.class);
|
||||
Assertions.assertEquals(user2.toString(), user.toString());
|
||||
|
||||
// more
|
||||
testNull();
|
||||
testMap();
|
||||
}
|
||||
|
||||
// 测试 Map 的转换
|
||||
public void testMap() {
|
||||
|
||||
// test Map -> Json
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", 10001);
|
||||
map.put("name", "张三");
|
||||
map.put("age", 18);
|
||||
String mapJson = SaManager.getSaJsonTemplate().objectToJson(map);
|
||||
Assertions.assertEquals("{\"name\":\"张三\",\"id\":10001,\"age\":18}", mapJson);
|
||||
|
||||
// test Json -> Map
|
||||
Map<String, Object> map2 = SaManager.getSaJsonTemplate().jsonToMap(mapJson);
|
||||
Assertions.assertEquals(map2.toString(), map.toString());
|
||||
|
||||
}
|
||||
|
||||
// 测试 Null 值
|
||||
public void testNull() {
|
||||
Assertions.assertEquals(null, SaManager.getSaJsonTemplate().objectToJson(null) );
|
||||
Assertions.assertEquals(null, SaManager.getSaJsonTemplate().jsonToObject(null, SysUser.class) );
|
||||
Assertions.assertEquals(null, SaManager.getSaJsonTemplate().jsonToObject(null) );
|
||||
Assertions.assertEquals(null, SaManager.getSaJsonTemplate().jsonToMap(null) );
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.pj.test.model;
|
||||
|
||||
/**
|
||||
* Role 实体类
|
||||
*
|
||||
* @author click33
|
||||
* @since 2022-10-15
|
||||
*/
|
||||
public class SysRole {
|
||||
//
|
||||
// public SysRole() {
|
||||
// }
|
||||
//
|
||||
// public SysRole(long id, String name) {
|
||||
// super();
|
||||
// this.id = id;
|
||||
// this.name = name;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 角色id
|
||||
// */
|
||||
// private long id;
|
||||
//
|
||||
// /**
|
||||
// * 角色名称
|
||||
// */
|
||||
// private String name;
|
||||
//
|
||||
// /**
|
||||
// * @return id
|
||||
// */
|
||||
// public long getId() {
|
||||
// return id;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @param id 要设置的 id
|
||||
// */
|
||||
// public void setId(long id) {
|
||||
// this.id = id;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @return name
|
||||
// */
|
||||
// public String getName() {
|
||||
// return name;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @param name 要设置的 name
|
||||
// */
|
||||
// public void setName(String name) {
|
||||
// this.name = name;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String toString() {
|
||||
// return "SysRole [id=" + id + ", name=" + name + "]";
|
||||
// }
|
||||
//
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
package com.pj.test.model;
|
||||
|
||||
/**
|
||||
* User 实体类
|
||||
*
|
||||
* @author click33
|
||||
* @since 2022-10-15
|
||||
*/
|
||||
public class SysUser {
|
||||
|
||||
public SysUser() {
|
||||
}
|
||||
|
||||
public SysUser(long id, String name, int age) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
private long id;
|
||||
|
||||
/**
|
||||
* 用户名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 用户年龄
|
||||
*/
|
||||
private int age;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
private SysRole role;
|
||||
|
||||
/**
|
||||
* @return id
|
||||
*/
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id 要设置的 id
|
||||
*/
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name 要设置的 name
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return age
|
||||
*/
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param age 要设置的 age
|
||||
*/
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public SysRole getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
public SysUser setRole(SysRole role) {
|
||||
this.role = role;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SysUser{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
", role=" + role +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user