mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-06-28 04:35:16 +08:00
完善单元测试
This commit is contained in:
parent
6048fe3e90
commit
68521356ae
@ -18,7 +18,9 @@
|
||||
## 前言:
|
||||
- [在线文档:http://sa-token.dev33.cn/](http://sa-token.dev33.cn/)
|
||||
|
||||
- 注:学习测试请拉取 master 分支,dev 为正在开发的分支,有很多特性并不稳定。(开源不易,点个 star 鼓励一下吧!)
|
||||
- 注:学习测试请拉取 master 分支,dev 为正在开发的分支,有很多特性并不稳定。
|
||||
|
||||
- 开源不易,点个 star 鼓励一下吧!
|
||||
|
||||
|
||||
## Sa-Token 介绍
|
||||
|
1
pom.xml
1
pom.xml
@ -21,6 +21,7 @@
|
||||
<module>sa-token-core</module>
|
||||
<module>sa-token-starter</module>
|
||||
<module>sa-token-plugin</module>
|
||||
<!-- <module>sa-token-test</module> -->
|
||||
<!-- <module>sa-token-demo/sa-token-demo-solon</module> -->
|
||||
</modules>
|
||||
|
||||
|
@ -99,7 +99,7 @@ public class SaTokenDaoDefaultImpl implements SaTokenDao {
|
||||
if(getKeyTimeout(key) == SaTokenDao.NOT_VALUE_EXPIRE) {
|
||||
return;
|
||||
}
|
||||
// 无动作
|
||||
dataMap.put(key, object);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -10,6 +10,7 @@ import java.util.Map;
|
||||
* code=状态码 <br>
|
||||
* msg=描述信息 <br>
|
||||
* data=携带对象 <br>
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
|
13
sa-token-test/.gitignore
vendored
Normal file
13
sa-token-test/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
target/
|
||||
|
||||
node_modules/
|
||||
bin/
|
||||
.settings/
|
||||
unpackage/
|
||||
.classpath
|
||||
.project
|
||||
|
||||
.factorypath
|
||||
|
||||
.idea/
|
||||
.iml
|
42
sa-token-test/pom.xml
Normal file
42
sa-token-test/pom.xml
Normal file
@ -0,0 +1,42 @@
|
||||
<?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-parent</artifactId>
|
||||
<version>1.28.0</version>
|
||||
</parent>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<!-- Sa-Token 单元测试合集 -->
|
||||
<name>sa-token-test</name>
|
||||
<artifactId>sa-token-test</artifactId>
|
||||
<description>sa-token-test</description>
|
||||
|
||||
|
||||
<!-- 所有子模块 -->
|
||||
<modules>
|
||||
<module>sa-token-core-test</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<!-- test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- config -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
12
sa-token-test/sa-token-core-test/.gitignore
vendored
Normal file
12
sa-token-test/sa-token-core-test/.gitignore
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
target/
|
||||
|
||||
node_modules/
|
||||
bin/
|
||||
.settings/
|
||||
unpackage/
|
||||
.classpath
|
||||
.project
|
||||
|
||||
.factorypath
|
||||
|
||||
.idea/
|
28
sa-token-test/sa-token-core-test/pom.xml
Normal file
28
sa-token-test/sa-token-core-test/pom.xml
Normal file
@ -0,0 +1,28 @@
|
||||
<?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>1.28.0</version>
|
||||
</parent>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>sa-token-core-test</name>
|
||||
<artifactId>sa-token-core-test</artifactId>
|
||||
<description>sa-token-core-test</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-core</artifactId>
|
||||
<version>${sa-token-version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,30 @@
|
||||
package cn.dev33.satoken.context.model;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* SaFoxUtil 工具类测试
|
||||
*
|
||||
* @author kong
|
||||
* @date: 2022-2-8 22:14:25
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
public class SaCookieTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
SaCookie cookie = new SaCookie("satoken", "xxxx-xxxx-xxxx-xxxx")
|
||||
.setDomain("https://sa-token.dev33.cn/")
|
||||
.setMaxAge(-1)
|
||||
.setPath("/")
|
||||
.setSameSite("Lax")
|
||||
.setHttpOnly(true)
|
||||
.setSecure(true);
|
||||
|
||||
Assert.assertEquals(cookie.toHeaderValue(), "satoken=xxxx-xxxx-xxxx-xxxx; Domain=https://sa-token.dev33.cn/; Path=/; Secure; HttpOnly; sameSite=Lax");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package cn.dev33.satoken.dao;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
|
||||
/**
|
||||
* SaTokenDao 持久层 测试
|
||||
*
|
||||
* @author kong
|
||||
* @date: 2022-2-9 15:39:38
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
public class SaTokenDaoTest {
|
||||
|
||||
SaTokenDao dao = new SaTokenDaoDefaultImpl();
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
dao.set("name", "zhangsan", 60);
|
||||
Assert.assertEquals(dao.get("name"), "zhangsan");
|
||||
Assert.assertTrue(dao.getTimeout("name") <= 60);
|
||||
Assert.assertEquals(dao.getTimeout("name2"), -2);
|
||||
|
||||
dao.update("name", "lisi");
|
||||
Assert.assertEquals(dao.get("name"), "lisi");
|
||||
|
||||
dao.updateTimeout("name", 100);
|
||||
Assert.assertTrue(dao.getTimeout("name") <= 100);
|
||||
|
||||
dao.delete("name");
|
||||
Assert.assertEquals(dao.get("name"), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getObject() {
|
||||
dao.setObject("name", "zhangsan", 60);
|
||||
Assert.assertEquals(dao.getObject("name"), "zhangsan");
|
||||
Assert.assertTrue(dao.getObjectTimeout("name") <= 60);
|
||||
|
||||
dao.updateObject("name", "lisi");
|
||||
Assert.assertEquals(dao.getObject("name"), "lisi");
|
||||
|
||||
dao.updateObjectTimeout("name", 100);
|
||||
Assert.assertTrue(dao.getObjectTimeout("name") <= 100);
|
||||
|
||||
dao.deleteObject("name");
|
||||
Assert.assertEquals(dao.getObject("name"), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSession() {
|
||||
SaSession session = new SaSession("session-1001");
|
||||
|
||||
dao.setSession(session, 60);
|
||||
Assert.assertEquals(dao.getSession("session-1001").getId(), session.getId());
|
||||
Assert.assertTrue(dao.getSessionTimeout("session-1001") <= 60);
|
||||
|
||||
SaSession session2 = new SaSession("session-1001");
|
||||
dao.updateSession(session2);
|
||||
Assert.assertEquals(dao.getSession("session-1001").getId(), session2.getId());
|
||||
|
||||
dao.updateSessionTimeout("session-1001", 100);
|
||||
Assert.assertTrue(dao.getSessionTimeout("session-1001") <= 100);
|
||||
|
||||
dao.deleteSession("session-1001");
|
||||
Assert.assertEquals(dao.getSession("session-1001"), null);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package cn.dev33.satoken.fun;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* IsRunFunction 测试
|
||||
*
|
||||
* @author kong
|
||||
* @date: 2022-2-9 16:11:10
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
public class IsRunFunctionTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
class TempClass{
|
||||
int count = 1;
|
||||
}
|
||||
TempClass obj = new TempClass();
|
||||
|
||||
IsRunFunction fun = new IsRunFunction(true);
|
||||
fun.exe(()->{
|
||||
obj.count = 2;
|
||||
}).noExe(()->{
|
||||
obj.count = 3;
|
||||
});
|
||||
|
||||
Assert.assertEquals(obj.count, 2);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package cn.dev33.satoken.secure;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* BCrypt 加密测试
|
||||
*
|
||||
* @author dream.
|
||||
* @date 2022/1/20
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
public class BCryptTest {
|
||||
|
||||
@Test
|
||||
public void checkpwTest() {
|
||||
final String hashed = BCrypt.hashpw("12345");
|
||||
// System.out.println(hashed);
|
||||
Assert.assertTrue(BCrypt.checkpw("12345", hashed));
|
||||
Assert.assertFalse(BCrypt.checkpw("123456", hashed));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package cn.dev33.satoken.secure;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* SaBase64Util 测试
|
||||
*
|
||||
* @author kong
|
||||
* @date: 2022-2-9
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
public class SaBase64UtilTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
// 文本
|
||||
String text = "Sa-Token 一个轻量级java权限认证框架";
|
||||
|
||||
// 使用Base64编码
|
||||
String base64Text = SaBase64Util.encode(text);
|
||||
Assert.assertEquals(base64Text, "U2EtVG9rZW4g5LiA5Liq6L276YeP57qnamF2Yeadg+mZkOiupOivgeahhuaetg==");
|
||||
|
||||
// 使用Base64解码
|
||||
String text2 = SaBase64Util.decode(base64Text);
|
||||
Assert.assertEquals(text2, "Sa-Token 一个轻量级java权限认证框架");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package cn.dev33.satoken.secure;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* SaSecureUtil 加密工具类 测试
|
||||
*
|
||||
* @author kong
|
||||
* @date: 2022-2-9
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
public class SaSecureUtilTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
// md5加密
|
||||
Assert.assertEquals(SaSecureUtil.md5("123456"), "e10adc3949ba59abbe56e057f20f883e");
|
||||
|
||||
// sha1加密
|
||||
Assert.assertEquals(SaSecureUtil.sha1("123456"), "7c4a8d09ca3762af61e59520943dc26494f8941b");
|
||||
|
||||
// sha256加密
|
||||
Assert.assertEquals(SaSecureUtil.sha256("123456"), "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
|
||||
|
||||
// md5加盐加密: md5(md5(str) + md5(salt))
|
||||
Assert.assertEquals(SaSecureUtil.md5BySalt("123456", "salt"), "f52020dca765fd3943ed40a615dc2c5c");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesEncrypt() {
|
||||
// 定义秘钥和明文
|
||||
String key = "123456";
|
||||
String text = "Sa-Token 一个轻量级java权限认证框架";
|
||||
|
||||
// 加密
|
||||
String ciphertext = SaSecureUtil.aesEncrypt(key, text);
|
||||
Assert.assertEquals(ciphertext, "KmSqfwxY5BRuWoHMWJqtebcOZ2lEEZaj2OSi1Ei8pRx4zdi24wsnwsTQVjbXRQ0M");
|
||||
|
||||
// 解密
|
||||
String text2 = SaSecureUtil.aesDecrypt(key, ciphertext);
|
||||
Assert.assertEquals(text2, "Sa-Token 一个轻量级java权限认证框架");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaEncryptByPublic() {
|
||||
// 定义私钥和公钥
|
||||
String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAO+wmt01pwm9lHMdq7A8gkEigk0XKMfjv+4IjAFhWCSiTeP7dtlnceFJbkWxvbc7Qo3fCOpwmfcskwUc3VSgyiJkNJDs9ivPbvlt8IU2bZ+PBDxYxSCJFrgouVOpAr8ar/b6gNuYTi1vt3FkGtSjACFb002/68RKUTye8/tdcVilAgMBAAECgYA1COmrSqTUJeuD8Su9ChZ0HROhxR8T45PjMmbwIz7ilDsR1+E7R4VOKPZKW4Kz2VvnklMhtJqMs4MwXWunvxAaUFzQTTg2Fu/WU8Y9ha14OaWZABfChMZlpkmpJW9arKmI22ZuxCEsFGxghTiJQ3tK8npj5IZq5vk+6mFHQ6aJAQJBAPghz91Dpuj+0bOUfOUmzi22obWCBncAD/0CqCLnJlpfOoa9bOcXSusGuSPuKy5KiGyblHMgKI6bq7gcM2DWrGUCQQD3SkOcmia2s/6i7DUEzMKaB0bkkX4Ela/xrfV+A3GzTPv9bIBamu0VIHznuiZbeNeyw7sVo4/GTItq/zn2QJdBAkEA8xHsVoyXTVeShaDIWJKTFyT5dJ1TR++/udKIcuiNIap34tZdgGPI+EM1yoTduBM7YWlnGwA9urW0mj7F9e9WIQJAFjxqSfmeg40512KP/ed/lCQVXtYqU7U2BfBTg8pBfhLtEcOg4wTNTroGITwe2NjL5HovJ2n2sqkNXEio6Ji0QQJAFLW1Kt80qypMqot+mHhS+0KfdOpaKeMWMSR4Ij5VfE63WzETEeWAMQESxzhavN1WOTb3/p6icgcVbgPQBaWhGg==";
|
||||
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDvsJrdNacJvZRzHauwPIJBIoJNFyjH47/uCIwBYVgkok3j+3bZZ3HhSW5Fsb23O0KN3wjqcJn3LJMFHN1UoMoiZDSQ7PYrz275bfCFNm2fjwQ8WMUgiRa4KLlTqQK/Gq/2+oDbmE4tb7dxZBrUowAhW9NNv+vESlE8nvP7XXFYpQIDAQAB";
|
||||
|
||||
// 文本
|
||||
String text = "Sa-Token 一个轻量级java权限认证框架";
|
||||
|
||||
// 使用公钥加密
|
||||
String ciphertext = SaSecureUtil.rsaEncryptByPublic(publicKey, text);
|
||||
// Assert.assertEquals(ciphertext, "d9e01fd105b059e975c524a1f4dccbe10dfc3a23b931a9e168ecb0a5758a29c45532254679f86cf83a63e5cc21ef631802fe70ea47e7519f5d96e0d1fab38a6f6dbebdb34b106ce7f27c341838e4e88a8ff3298c519c29a3f0944cf8f668bfecd9394f16945d85d84c4d813d12ecadf34bfb21850c383977b5b2de848fa40995");
|
||||
|
||||
// 使用私钥解密
|
||||
String text2 = SaSecureUtil.rsaDecryptByPrivate(privateKey, ciphertext);
|
||||
Assert.assertEquals(text2, "Sa-Token 一个轻量级java权限认证框架");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package cn.dev33.satoken.session;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* SaSession 测试
|
||||
*
|
||||
* @author kong
|
||||
* @date: 2022-2-9
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
public class SaSessionTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
SaSession session = new SaSession("session-1001");
|
||||
Assert.assertEquals(session.getId(), "session-1001");
|
||||
|
||||
// 基础取值
|
||||
session.set("name", "zhangsan");
|
||||
session.set("age", 18);
|
||||
Assert.assertEquals(session.get("name"), "zhangsan");
|
||||
Assert.assertEquals((int)session.get("age", 20), 18);
|
||||
Assert.assertEquals((int)session.get("age2", 20), 20);
|
||||
Assert.assertEquals(session.getModel("age", Double.class).getClass(), Double.class);
|
||||
|
||||
// 复杂取值
|
||||
class User {
|
||||
String name;
|
||||
int age;
|
||||
User(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
User user = new User("zhangsan", 18);
|
||||
session.set("user", user);
|
||||
|
||||
User user2 = session.getModel("user", User.class);
|
||||
Assert.assertNotNull(user2);
|
||||
Assert.assertEquals(user2.name, "zhangsan");
|
||||
Assert.assertEquals(user2.age, 18);
|
||||
|
||||
// Token签名
|
||||
session.addTokenSign("xxxx-xxxx-xxxx-xxxx-1", "PC");
|
||||
session.addTokenSign("xxxx-xxxx-xxxx-xxxx-2", "APP");
|
||||
|
||||
Assert.assertEquals(session.getTokenSignList().size(), 2);
|
||||
Assert.assertEquals(session.getTokenSign("xxxx-xxxx-xxxx-xxxx-1").getDevice(), "PC");
|
||||
Assert.assertEquals(session.getTokenSign("xxxx-xxxx-xxxx-xxxx-2").getDevice(), "APP");
|
||||
|
||||
session.removeTokenSign("xxxx-xxxx-xxxx-xxxx-1");
|
||||
Assert.assertEquals(session.getTokenSignList().size(), 1);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,205 @@
|
||||
package cn.dev33.satoken.util;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* SaFoxUtil 工具类测试
|
||||
*
|
||||
* @author kong
|
||||
* @date: 2022-2-8 22:14:25
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
public class SaFoxUtilTest {
|
||||
|
||||
@Test
|
||||
public void getRandomString() {
|
||||
String randomString = SaFoxUtil.getRandomString(8);
|
||||
Assert.assertEquals(randomString.length(), 8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmpty() {
|
||||
Assert.assertFalse(SaFoxUtil.isEmpty("abc"));
|
||||
Assert.assertTrue(SaFoxUtil.isEmpty(""));
|
||||
Assert.assertTrue(SaFoxUtil.isEmpty(null));
|
||||
|
||||
Assert.assertTrue(SaFoxUtil.isNotEmpty("abc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMarking28() {
|
||||
Assert.assertNotEquals(SaFoxUtil.getMarking28(), SaFoxUtil.getMarking28());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatDate() {
|
||||
String formatDate = SaFoxUtil.formatDate(new Date(1644328600364L));
|
||||
Assert.assertEquals(formatDate, "2022-02-08 21:56:40");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void searchList() {
|
||||
// 原始数据
|
||||
List<String> dataList = Arrays.asList("token1", "token2", "token3", "token4", "token5", "aaa1");
|
||||
|
||||
// 分页
|
||||
List<String> list1 = SaFoxUtil.searchList(dataList, 1, 2);
|
||||
Assert.assertEquals(list1.size(), 2);
|
||||
Assert.assertEquals(list1.get(0), "token2");
|
||||
Assert.assertEquals(list1.get(1), "token3");
|
||||
|
||||
// 前缀筛选
|
||||
List<String> list2 = SaFoxUtil.searchList(dataList, "token", "", 0, 10);
|
||||
Assert.assertEquals(list2.size(), 5);
|
||||
|
||||
// 关键字筛选
|
||||
List<String> list3 = SaFoxUtil.searchList(dataList, "", "1", 0, 10);
|
||||
Assert.assertEquals(list3.size(), 2);
|
||||
|
||||
// 综合筛选
|
||||
List<String> list4 = SaFoxUtil.searchList(dataList, "token", "1", 0, 10);
|
||||
Assert.assertEquals(list4.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void vagueMatch() {
|
||||
Assert.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello"));
|
||||
Assert.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello world"));
|
||||
Assert.assertFalse(SaFoxUtil.vagueMatch("hello*", "he"));
|
||||
Assert.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello*"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getValueByType() {
|
||||
Assert.assertEquals(SaFoxUtil.getValueByType("1", Integer.class).getClass(), Integer.class);
|
||||
Assert.assertEquals(SaFoxUtil.getValueByType("1", Long.class).getClass(), Long.class);
|
||||
Assert.assertEquals(SaFoxUtil.getValueByType("1", Double.class).getClass(), Double.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void joinParam() {
|
||||
Assert.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn", "id=1"), "https://sa-token.dev33.cn?id=1");
|
||||
Assert.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?", "id=1"), "https://sa-token.dev33.cn?id=1");
|
||||
Assert.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?name=zhang", "id=1"), "https://sa-token.dev33.cn?name=zhang&id=1");
|
||||
Assert.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?name=zhang&", "id=1"), "https://sa-token.dev33.cn?name=zhang&id=1");
|
||||
|
||||
Assert.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?name=zhang&", "id", 1), "https://sa-token.dev33.cn?name=zhang&id=1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void joinSharpParam() {
|
||||
Assert.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn", "id=1"), "https://sa-token.dev33.cn#id=1");
|
||||
Assert.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#", "id=1"), "https://sa-token.dev33.cn#id=1");
|
||||
Assert.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#name=zhang", "id=1"), "https://sa-token.dev33.cn#name=zhang&id=1");
|
||||
Assert.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#name=zhang&", "id=1"), "https://sa-token.dev33.cn#name=zhang&id=1");
|
||||
|
||||
Assert.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#name=zhang&", "id", 1), "https://sa-token.dev33.cn#name=zhang&id=1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void arrayJoin() {
|
||||
Assert.assertEquals(SaFoxUtil.arrayJoin(new String[] {"a", "b", "c"}), "a,b,c");
|
||||
Assert.assertEquals(SaFoxUtil.arrayJoin(new String[] {}), "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUrl() {
|
||||
Assert.assertTrue(SaFoxUtil.isUrl("https://sa-token.dev33.cn"));
|
||||
Assert.assertTrue(SaFoxUtil.isUrl("https://www.baidu.com/"));
|
||||
|
||||
Assert.assertFalse(SaFoxUtil.isUrl("htt://www.baidu.com/"));
|
||||
Assert.assertFalse(SaFoxUtil.isUrl("https:www.baidu.com/"));
|
||||
Assert.assertFalse(SaFoxUtil.isUrl("httpswwwbaiducom/"));
|
||||
Assert.assertFalse(SaFoxUtil.isUrl("https://www.baidu.com/,"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeUrl() {
|
||||
Assert.assertEquals(SaFoxUtil.encodeUrl("https://sa-token.dev33.cn"), "https%3A%2F%2Fsa-token.dev33.cn");
|
||||
Assert.assertEquals(SaFoxUtil.decoderUrl("https%3A%2F%2Fsa-token.dev33.cn"), "https://sa-token.dev33.cn");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToList() {
|
||||
List<String> list = SaFoxUtil.convertStringToList("a,b,c");
|
||||
Assert.assertEquals(list.size(), 3);
|
||||
Assert.assertEquals(list.get(0), "a");
|
||||
Assert.assertEquals(list.get(1), "b");
|
||||
Assert.assertEquals(list.get(2), "c");
|
||||
|
||||
List<String> list2 = SaFoxUtil.convertStringToList("a,");
|
||||
Assert.assertEquals(list2.size(), 1);
|
||||
|
||||
List<String> list3 = SaFoxUtil.convertStringToList(",");
|
||||
Assert.assertEquals(list3.size(), 0);
|
||||
|
||||
List<String> list4 = SaFoxUtil.convertStringToList("");
|
||||
Assert.assertEquals(list4.size(), 0);
|
||||
|
||||
List<String> list5 = SaFoxUtil.convertStringToList(null);
|
||||
Assert.assertEquals(list5.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertListToString() {
|
||||
List<String> list = Arrays.asList("a", "b", "c");
|
||||
Assert.assertEquals(SaFoxUtil.convertListToString(list), "a,b,c");
|
||||
|
||||
List<String> list2 = Arrays.asList();
|
||||
Assert.assertEquals(SaFoxUtil.convertListToString(list2), "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertStringToArray() {
|
||||
String[] array = SaFoxUtil.convertStringToArray("a,b,c");
|
||||
Assert.assertEquals(array.length, 3);
|
||||
Assert.assertEquals(array[0], "a");
|
||||
Assert.assertEquals(array[1], "b");
|
||||
Assert.assertEquals(array[2], "c");
|
||||
|
||||
String[] array2 = SaFoxUtil.convertStringToArray("a,");
|
||||
Assert.assertEquals(array2.length, 1);
|
||||
|
||||
String[] array3 = SaFoxUtil.convertStringToArray(",");
|
||||
Assert.assertEquals(array3.length, 0);
|
||||
|
||||
String[] array4 = SaFoxUtil.convertStringToArray("");
|
||||
Assert.assertEquals(array4.length, 0);
|
||||
|
||||
String[] array5 = SaFoxUtil.convertStringToArray(null);
|
||||
Assert.assertEquals(array5.length, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertArrayToString() {
|
||||
String[] array = new String[] {"a", "b", "c"};
|
||||
Assert.assertEquals(SaFoxUtil.convertArrayToString(array), "a,b,c");
|
||||
|
||||
String[] array2 = new String[] {};
|
||||
Assert.assertEquals(SaFoxUtil.convertArrayToString(array2), "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyList() {
|
||||
List<String> list = SaFoxUtil.emptyList();
|
||||
Assert.assertEquals(list.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toList() {
|
||||
List<String> list = SaFoxUtil.toList("a","b", "c");
|
||||
Assert.assertEquals(list.size(), 3);
|
||||
Assert.assertEquals(list.get(0), "a");
|
||||
Assert.assertEquals(list.get(1), "b");
|
||||
Assert.assertEquals(list.get(2), "c");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package cn.dev33.satoken.util;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* SaResult 结果集 测试
|
||||
*
|
||||
* @author kong
|
||||
* @date: 2022-2-8 22:14:25
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
public class SaResultTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
SaResult res = new SaResult(200, "ok", "zhangsan");
|
||||
Assert.assertEquals((int)res.getCode(), 200);
|
||||
Assert.assertEquals(res.getMsg(), "ok");
|
||||
Assert.assertEquals(res.getData(), "zhangsan");
|
||||
|
||||
res.set("age", 18);
|
||||
Assert.assertEquals(res.get("age"), 18);
|
||||
Assert.assertEquals(res.getOrDefault("age", 20), 18);
|
||||
Assert.assertEquals(res.getOrDefault("age2", 20), 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
Assert.assertEquals((int)SaResult.ok().getCode(), 200);
|
||||
Assert.assertEquals((int)SaResult.error().getCode(), 500);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user