forked from sfms3.0/sfms3.0
663 changed files with 608 additions and 16324 deletions
@ -1,64 +0,0 @@ |
|||
package com.win.framework.common.util.collection; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Data; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import java.util.Arrays; |
|||
import java.util.Collection; |
|||
import java.util.List; |
|||
import java.util.function.BiFunction; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals; |
|||
|
|||
/** |
|||
* {@link CollectionUtils} 的单元测试 |
|||
*/ |
|||
public class CollectionUtilsTest { |
|||
|
|||
@Data |
|||
@AllArgsConstructor |
|||
private static class Dog { |
|||
|
|||
private Integer id; |
|||
private String name; |
|||
private String code; |
|||
|
|||
} |
|||
|
|||
@Test |
|||
public void testDiffList() { |
|||
// 准备参数
|
|||
Collection<Dog> oldList = Arrays.asList( |
|||
new Dog(1, "花花", "hh"), |
|||
new Dog(2, "旺财", "wc") |
|||
); |
|||
Collection<Dog> newList = Arrays.asList( |
|||
new Dog(null, "花花2", "hh"), |
|||
new Dog(null, "小白", "xb") |
|||
); |
|||
BiFunction<Dog, Dog, Boolean> sameFunc = (oldObj, newObj) -> { |
|||
boolean same = oldObj.getCode().equals(newObj.getCode()); |
|||
// 如果相等的情况下,需要设置下 id,后续好更新
|
|||
if (same) { |
|||
newObj.setId(oldObj.getId()); |
|||
} |
|||
return same; |
|||
}; |
|||
|
|||
// 调用
|
|||
List<List<Dog>> result = CollectionUtils.diffList(oldList, newList, sameFunc); |
|||
// 断言
|
|||
assertEquals(result.size(), 3); |
|||
// 断言 create
|
|||
assertEquals(result.get(0).size(), 1); |
|||
assertEquals(result.get(0).get(0), new Dog(null, "小白", "xb")); |
|||
// 断言 update
|
|||
assertEquals(result.get(1).size(), 1); |
|||
assertEquals(result.get(1).get(0), new Dog(1, "花花2", "hh")); |
|||
// 断言 delete
|
|||
assertEquals(result.get(2).size(), 1); |
|||
assertEquals(result.get(2).get(0), new Dog(2, "旺财", "wc")); |
|||
} |
|||
|
|||
} |
@ -1 +0,0 @@ |
|||
<http://www.iocoder.cn/Spring-Boot/Validation/?win> |
@ -1,6 +1,6 @@ |
|||
/** |
|||
* Banner 用于在 console 控制台,打印开发文档、接口文档等 |
|||
* |
|||
* @author 芋道源码 |
|||
* @author 闻荫源码 |
|||
*/ |
|||
package com.win.framework.banner; |
|||
|
@ -1,6 +1,6 @@ |
|||
/** |
|||
* 基于部门的数据权限规则 |
|||
* |
|||
* @author 芋道源码 |
|||
* @author 闻荫源码 |
|||
*/ |
|||
package com.win.framework.datapermission.core.rule.dept; |
|||
|
@ -1,108 +0,0 @@ |
|||
package com.win.framework.datapermission.core.aop; |
|||
|
|||
import cn.hutool.core.collection.CollUtil; |
|||
import com.win.framework.datapermission.core.annotation.DataPermission; |
|||
import com.win.framework.test.core.ut.BaseMockitoUnitTest; |
|||
import org.aopalliance.intercept.MethodInvocation; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.InjectMocks; |
|||
import org.mockito.Mock; |
|||
|
|||
import java.lang.reflect.Method; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
/** |
|||
* {@link DataPermissionAnnotationInterceptor} 的单元测试 |
|||
* |
|||
* @author 芋道源码 |
|||
*/ |
|||
public class DataPermissionAnnotationInterceptorTest extends BaseMockitoUnitTest { |
|||
|
|||
@InjectMocks |
|||
private DataPermissionAnnotationInterceptor interceptor; |
|||
|
|||
@Mock |
|||
private MethodInvocation methodInvocation; |
|||
|
|||
@BeforeEach |
|||
public void setUp() { |
|||
interceptor.getDataPermissionCache().clear(); |
|||
} |
|||
|
|||
@Test // 无 @DataPermission 注解
|
|||
public void testInvoke_none() throws Throwable { |
|||
// 参数
|
|||
mockMethodInvocation(TestNone.class); |
|||
|
|||
// 调用
|
|||
Object result = interceptor.invoke(methodInvocation); |
|||
// 断言
|
|||
assertEquals("none", result); |
|||
assertEquals(1, interceptor.getDataPermissionCache().size()); |
|||
assertTrue(CollUtil.getFirst(interceptor.getDataPermissionCache().values()).enable()); |
|||
} |
|||
|
|||
@Test // 在 Method 上有 @DataPermission 注解
|
|||
public void testInvoke_method() throws Throwable { |
|||
// 参数
|
|||
mockMethodInvocation(TestMethod.class); |
|||
|
|||
// 调用
|
|||
Object result = interceptor.invoke(methodInvocation); |
|||
// 断言
|
|||
assertEquals("method", result); |
|||
assertEquals(1, interceptor.getDataPermissionCache().size()); |
|||
assertFalse(CollUtil.getFirst(interceptor.getDataPermissionCache().values()).enable()); |
|||
} |
|||
|
|||
@Test // 在 Class 上有 @DataPermission 注解
|
|||
public void testInvoke_class() throws Throwable { |
|||
// 参数
|
|||
mockMethodInvocation(TestClass.class); |
|||
|
|||
// 调用
|
|||
Object result = interceptor.invoke(methodInvocation); |
|||
// 断言
|
|||
assertEquals("class", result); |
|||
assertEquals(1, interceptor.getDataPermissionCache().size()); |
|||
assertFalse(CollUtil.getFirst(interceptor.getDataPermissionCache().values()).enable()); |
|||
} |
|||
|
|||
private void mockMethodInvocation(Class<?> clazz) throws Throwable { |
|||
Object targetObject = clazz.newInstance(); |
|||
Method method = targetObject.getClass().getMethod("echo"); |
|||
when(methodInvocation.getThis()).thenReturn(targetObject); |
|||
when(methodInvocation.getMethod()).thenReturn(method); |
|||
when(methodInvocation.proceed()).then(invocationOnMock -> method.invoke(targetObject)); |
|||
} |
|||
|
|||
static class TestMethod { |
|||
|
|||
@DataPermission(enable = false) |
|||
public String echo() { |
|||
return "method"; |
|||
} |
|||
|
|||
} |
|||
|
|||
@DataPermission(enable = false) |
|||
static class TestClass { |
|||
|
|||
public String echo() { |
|||
return "class"; |
|||
} |
|||
|
|||
} |
|||
|
|||
static class TestNone { |
|||
|
|||
public String echo() { |
|||
return "none"; |
|||
} |
|||
|
|||
} |
|||
|
|||
} |
@ -1,66 +0,0 @@ |
|||
package com.win.framework.datapermission.core.aop; |
|||
|
|||
import com.win.framework.datapermission.core.annotation.DataPermission; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals; |
|||
import static org.junit.jupiter.api.Assertions.assertSame; |
|||
import static org.mockito.Mockito.mock; |
|||
|
|||
/** |
|||
* {@link DataPermissionContextHolder} 的单元测试 |
|||
* |
|||
* @author 芋道源码 |
|||
*/ |
|||
class DataPermissionContextHolderTest { |
|||
|
|||
@BeforeEach |
|||
public void setUp() { |
|||
DataPermissionContextHolder.clear(); |
|||
} |
|||
|
|||
@Test |
|||
public void testGet() { |
|||
// mock 方法
|
|||
DataPermission dataPermission01 = mock(DataPermission.class); |
|||
DataPermissionContextHolder.add(dataPermission01); |
|||
DataPermission dataPermission02 = mock(DataPermission.class); |
|||
DataPermissionContextHolder.add(dataPermission02); |
|||
|
|||
// 调用
|
|||
DataPermission result = DataPermissionContextHolder.get(); |
|||
// 断言
|
|||
assertSame(result, dataPermission02); |
|||
} |
|||
|
|||
@Test |
|||
public void testPush() { |
|||
// 调用
|
|||
DataPermission dataPermission01 = mock(DataPermission.class); |
|||
DataPermissionContextHolder.add(dataPermission01); |
|||
DataPermission dataPermission02 = mock(DataPermission.class); |
|||
DataPermissionContextHolder.add(dataPermission02); |
|||
// 断言
|
|||
DataPermission first = DataPermissionContextHolder.getAll().get(0); |
|||
DataPermission second = DataPermissionContextHolder.getAll().get(1); |
|||
assertSame(dataPermission01, first); |
|||
assertSame(dataPermission02, second); |
|||
} |
|||
|
|||
@Test |
|||
public void testRemove() { |
|||
// mock 方法
|
|||
DataPermission dataPermission01 = mock(DataPermission.class); |
|||
DataPermissionContextHolder.add(dataPermission01); |
|||
DataPermission dataPermission02 = mock(DataPermission.class); |
|||
DataPermissionContextHolder.add(dataPermission02); |
|||
|
|||
// 调用
|
|||
DataPermission result = DataPermissionContextHolder.remove(); |
|||
// 断言
|
|||
assertSame(result, dataPermission02); |
|||
assertEquals(1, DataPermissionContextHolder.getAll().size()); |
|||
} |
|||
|
|||
} |
@ -1,190 +0,0 @@ |
|||
package com.win.framework.datapermission.core.db; |
|||
|
|||
import com.win.framework.common.util.collection.SetUtils; |
|||
import com.win.framework.datapermission.core.rule.DataPermissionRule; |
|||
import com.win.framework.datapermission.core.rule.DataPermissionRuleFactory; |
|||
import com.win.framework.mybatis.core.util.MyBatisUtils; |
|||
import com.win.framework.test.core.ut.BaseMockitoUnitTest; |
|||
import com.baomidou.mybatisplus.core.toolkit.PluginUtils; |
|||
import net.sf.jsqlparser.expression.Alias; |
|||
import net.sf.jsqlparser.expression.Expression; |
|||
import net.sf.jsqlparser.expression.LongValue; |
|||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo; |
|||
import net.sf.jsqlparser.schema.Column; |
|||
import org.apache.ibatis.executor.Executor; |
|||
import org.apache.ibatis.executor.statement.StatementHandler; |
|||
import org.apache.ibatis.mapping.BoundSql; |
|||
import org.apache.ibatis.mapping.MappedStatement; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.InjectMocks; |
|||
import org.mockito.Mock; |
|||
import org.mockito.MockedStatic; |
|||
|
|||
import java.sql.Connection; |
|||
import java.util.*; |
|||
|
|||
import static java.util.Collections.singletonList; |
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
import static org.mockito.Mockito.*; |
|||
|
|||
/** |
|||
* {@link DataPermissionDatabaseInterceptor} 的单元测试 |
|||
* 主要测试 {@link DataPermissionDatabaseInterceptor#beforePrepare(StatementHandler, Connection, Integer)} |
|||
* 和 {@link DataPermissionDatabaseInterceptor#beforeUpdate(Executor, MappedStatement, Object)} |
|||
* 以及在这个过程中,ContextHolder 和 MappedStatementCache |
|||
* |
|||
* @author 芋道源码 |
|||
*/ |
|||
public class DataPermissionDatabaseInterceptorTest extends BaseMockitoUnitTest { |
|||
|
|||
@InjectMocks |
|||
private DataPermissionDatabaseInterceptor interceptor; |
|||
|
|||
@Mock |
|||
private DataPermissionRuleFactory ruleFactory; |
|||
|
|||
@BeforeEach |
|||
public void setUp() { |
|||
// 清理上下文
|
|||
DataPermissionDatabaseInterceptor.ContextHolder.clear(); |
|||
// 清空缓存
|
|||
interceptor.getMappedStatementCache().clear(); |
|||
} |
|||
|
|||
@Test // 不存在规则,且不匹配
|
|||
public void testBeforeQuery_withoutRule() { |
|||
try (MockedStatic<PluginUtils> pluginUtilsMock = mockStatic(PluginUtils.class)) { |
|||
// 准备参数
|
|||
MappedStatement mappedStatement = mock(MappedStatement.class); |
|||
BoundSql boundSql = mock(BoundSql.class); |
|||
|
|||
// 调用
|
|||
interceptor.beforeQuery(null, mappedStatement, null, null, null, boundSql); |
|||
// 断言
|
|||
pluginUtilsMock.verify(() -> PluginUtils.mpBoundSql(boundSql), never()); |
|||
} |
|||
} |
|||
|
|||
@Test // 存在规则,且不匹配
|
|||
public void testBeforeQuery_withMatchRule() { |
|||
try (MockedStatic<PluginUtils> pluginUtilsMock = mockStatic(PluginUtils.class)) { |
|||
// 准备参数
|
|||
MappedStatement mappedStatement = mock(MappedStatement.class); |
|||
BoundSql boundSql = mock(BoundSql.class); |
|||
// mock 方法(数据权限)
|
|||
when(ruleFactory.getDataPermissionRule(same(mappedStatement.getId()))) |
|||
.thenReturn(singletonList(new DeptDataPermissionRule())); |
|||
// mock 方法(MPBoundSql)
|
|||
PluginUtils.MPBoundSql mpBs = mock(PluginUtils.MPBoundSql.class); |
|||
pluginUtilsMock.when(() -> PluginUtils.mpBoundSql(same(boundSql))).thenReturn(mpBs); |
|||
// mock 方法(SQL)
|
|||
String sql = "select * from t_user where id = 1"; |
|||
when(mpBs.sql()).thenReturn(sql); |
|||
// 针对 ContextHolder 和 MappedStatementCache 暂时不 mock,主要想校验过程中,数据是否正确
|
|||
|
|||
// 调用
|
|||
interceptor.beforeQuery(null, mappedStatement, null, null, null, boundSql); |
|||
// 断言
|
|||
verify(mpBs, times(1)).sql( |
|||
eq("SELECT * FROM t_user WHERE id = 1 AND t_user.dept_id = 100")); |
|||
// 断言缓存
|
|||
assertTrue(interceptor.getMappedStatementCache().getNoRewritableMappedStatements().isEmpty()); |
|||
} |
|||
} |
|||
|
|||
@Test // 存在规则,但不匹配
|
|||
public void testBeforeQuery_withoutMatchRule() { |
|||
try (MockedStatic<PluginUtils> pluginUtilsMock = mockStatic(PluginUtils.class)) { |
|||
// 准备参数
|
|||
MappedStatement mappedStatement = mock(MappedStatement.class); |
|||
BoundSql boundSql = mock(BoundSql.class); |
|||
// mock 方法(数据权限)
|
|||
when(ruleFactory.getDataPermissionRule(same(mappedStatement.getId()))) |
|||
.thenReturn(singletonList(new DeptDataPermissionRule())); |
|||
// mock 方法(MPBoundSql)
|
|||
PluginUtils.MPBoundSql mpBs = mock(PluginUtils.MPBoundSql.class); |
|||
pluginUtilsMock.when(() -> PluginUtils.mpBoundSql(same(boundSql))).thenReturn(mpBs); |
|||
// mock 方法(SQL)
|
|||
String sql = "select * from t_role where id = 1"; |
|||
when(mpBs.sql()).thenReturn(sql); |
|||
// 针对 ContextHolder 和 MappedStatementCache 暂时不 mock,主要想校验过程中,数据是否正确
|
|||
|
|||
// 调用
|
|||
interceptor.beforeQuery(null, mappedStatement, null, null, null, boundSql); |
|||
// 断言
|
|||
verify(mpBs, times(1)).sql( |
|||
eq("SELECT * FROM t_role WHERE id = 1")); |
|||
// 断言缓存
|
|||
assertFalse(interceptor.getMappedStatementCache().getNoRewritableMappedStatements().isEmpty()); |
|||
} |
|||
} |
|||
|
|||
@Test |
|||
public void testAddNoRewritable() { |
|||
// 准备参数
|
|||
MappedStatement ms = mock(MappedStatement.class); |
|||
List<DataPermissionRule> rules = singletonList(new DeptDataPermissionRule()); |
|||
// mock 方法
|
|||
when(ms.getId()).thenReturn("selectById"); |
|||
|
|||
// 调用
|
|||
interceptor.getMappedStatementCache().addNoRewritable(ms, rules); |
|||
// 断言
|
|||
Map<Class<? extends DataPermissionRule>, Set<String>> noRewritableMappedStatements = |
|||
interceptor.getMappedStatementCache().getNoRewritableMappedStatements(); |
|||
assertEquals(1, noRewritableMappedStatements.size()); |
|||
assertEquals(SetUtils.asSet("selectById"), noRewritableMappedStatements.get(DeptDataPermissionRule.class)); |
|||
} |
|||
|
|||
@Test |
|||
public void testNoRewritable() { |
|||
// 准备参数
|
|||
MappedStatement ms = mock(MappedStatement.class); |
|||
// mock 方法
|
|||
when(ms.getId()).thenReturn("selectById"); |
|||
// mock 数据
|
|||
List<DataPermissionRule> rules = singletonList(new DeptDataPermissionRule()); |
|||
interceptor.getMappedStatementCache().addNoRewritable(ms, rules); |
|||
|
|||
// 场景一,rules 为空
|
|||
assertTrue(interceptor.getMappedStatementCache().noRewritable(ms, null)); |
|||
// 场景二,rules 非空,可重写
|
|||
assertFalse(interceptor.getMappedStatementCache().noRewritable(ms, singletonList(new EmptyDataPermissionRule()))); |
|||
// 场景三,rule 非空,不可重写
|
|||
assertTrue(interceptor.getMappedStatementCache().noRewritable(ms, rules)); |
|||
} |
|||
|
|||
private static class DeptDataPermissionRule implements DataPermissionRule { |
|||
|
|||
private static final String COLUMN = "dept_id"; |
|||
|
|||
@Override |
|||
public Set<String> getTableNames() { |
|||
return SetUtils.asSet("t_user"); |
|||
} |
|||
|
|||
@Override |
|||
public Expression getExpression(String tableName, Alias tableAlias) { |
|||
Column column = MyBatisUtils.buildColumn(tableName, tableAlias, COLUMN); |
|||
LongValue value = new LongValue(100L); |
|||
return new EqualsTo(column, value); |
|||
} |
|||
|
|||
} |
|||
|
|||
private static class EmptyDataPermissionRule implements DataPermissionRule { |
|||
|
|||
@Override |
|||
public Set<String> getTableNames() { |
|||
return Collections.emptySet(); |
|||
} |
|||
|
|||
@Override |
|||
public Expression getExpression(String tableName, Alias tableAlias) { |
|||
return null; |
|||
} |
|||
|
|||
} |
|||
|
|||
} |
@ -1,533 +0,0 @@ |
|||
package com.win.framework.datapermission.core.db; |
|||
|
|||
import com.win.framework.datapermission.core.rule.DataPermissionRule; |
|||
import com.win.framework.datapermission.core.rule.DataPermissionRuleFactory; |
|||
import com.win.framework.mybatis.core.util.MyBatisUtils; |
|||
import com.win.framework.test.core.ut.BaseMockitoUnitTest; |
|||
import net.sf.jsqlparser.expression.Alias; |
|||
import net.sf.jsqlparser.expression.Expression; |
|||
import net.sf.jsqlparser.expression.LongValue; |
|||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo; |
|||
import net.sf.jsqlparser.expression.operators.relational.ExpressionList; |
|||
import net.sf.jsqlparser.expression.operators.relational.InExpression; |
|||
import net.sf.jsqlparser.schema.Column; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.InjectMocks; |
|||
import org.mockito.Mock; |
|||
|
|||
import java.util.Arrays; |
|||
import java.util.Set; |
|||
|
|||
import static com.win.framework.common.util.collection.SetUtils.asSet; |
|||
import static org.junit.jupiter.api.Assertions.assertEquals; |
|||
|
|||
/** |
|||
* {@link DataPermissionDatabaseInterceptor} 的单元测试 |
|||
* 主要复用了 MyBatis Plus 的 TenantLineInnerInterceptorTest 的单元测试 |
|||
* 不过它的单元测试不是很规范,考虑到是复用的,所以暂时不进行修改~ |
|||
* |
|||
* @author 芋道源码 |
|||
*/ |
|||
public class DataPermissionDatabaseInterceptorTest2 extends BaseMockitoUnitTest { |
|||
|
|||
@InjectMocks |
|||
private DataPermissionDatabaseInterceptor interceptor; |
|||
|
|||
@Mock |
|||
private DataPermissionRuleFactory ruleFactory; |
|||
|
|||
@BeforeEach |
|||
public void setUp() { |
|||
// 租户的数据权限规则
|
|||
DataPermissionRule tenantRule = new DataPermissionRule() { |
|||
|
|||
private static final String COLUMN = "tenant_id"; |
|||
|
|||
@Override |
|||
public Set<String> getTableNames() { |
|||
return asSet("entity", "entity1", "entity2", "entity3", "t1", "t2", "sys_dict_item", // 支持 MyBatis Plus 的单元测试
|
|||
"t_user", "t_role"); // 满足自己的单元测试
|
|||
} |
|||
|
|||
@Override |
|||
public Expression getExpression(String tableName, Alias tableAlias) { |
|||
Column column = MyBatisUtils.buildColumn(tableName, tableAlias, COLUMN); |
|||
LongValue value = new LongValue(1L); |
|||
return new EqualsTo(column, value); |
|||
} |
|||
|
|||
}; |
|||
// 部门的数据权限规则
|
|||
DataPermissionRule deptRule = new DataPermissionRule() { |
|||
|
|||
private static final String COLUMN = "dept_id"; |
|||
|
|||
@Override |
|||
public Set<String> getTableNames() { |
|||
return asSet("t_user"); // 满足自己的单元测试
|
|||
} |
|||
|
|||
@Override |
|||
public Expression getExpression(String tableName, Alias tableAlias) { |
|||
Column column = MyBatisUtils.buildColumn(tableName, tableAlias, COLUMN); |
|||
ExpressionList values = new ExpressionList(new LongValue(10L), |
|||
new LongValue(20L)); |
|||
return new InExpression(column, values); |
|||
} |
|||
|
|||
}; |
|||
// 设置到上下文,保证
|
|||
DataPermissionDatabaseInterceptor.ContextHolder.init(Arrays.asList(tenantRule, deptRule)); |
|||
} |
|||
|
|||
@Test |
|||
void delete() { |
|||
assertSql("delete from entity where id = ?", |
|||
"DELETE FROM entity WHERE id = ? AND entity.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void update() { |
|||
assertSql("update entity set name = ? where id = ?", |
|||
"UPDATE entity SET name = ? WHERE id = ? AND entity.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectSingle() { |
|||
// 单表
|
|||
assertSql("select * from entity where id = ?", |
|||
"SELECT * FROM entity WHERE id = ? AND entity.tenant_id = 1"); |
|||
|
|||
assertSql("select * from entity where id = ? or name = ?", |
|||
"SELECT * FROM entity WHERE (id = ? OR name = ?) AND entity.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM entity WHERE (id = ? OR name = ?)", |
|||
"SELECT * FROM entity WHERE (id = ? OR name = ?) AND entity.tenant_id = 1"); |
|||
|
|||
/* not */ |
|||
assertSql("SELECT * FROM entity WHERE not (id = ? OR name = ?)", |
|||
"SELECT * FROM entity WHERE NOT (id = ? OR name = ?) AND entity.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectSubSelectIn() { |
|||
/* in */ |
|||
assertSql("SELECT * FROM entity e WHERE e.id IN (select e1.id from entity1 e1 where e1.id = ?)", |
|||
"SELECT * FROM entity e WHERE e.id IN (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|||
// 在最前
|
|||
assertSql("SELECT * FROM entity e WHERE e.id IN " + |
|||
"(select e1.id from entity1 e1 where e1.id = ?) and e.id = ?", |
|||
"SELECT * FROM entity e WHERE e.id IN " + |
|||
"(SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.id = ? AND e.tenant_id = 1"); |
|||
// 在最后
|
|||
assertSql("SELECT * FROM entity e WHERE e.id = ? and e.id IN " + |
|||
"(select e1.id from entity1 e1 where e1.id = ?)", |
|||
"SELECT * FROM entity e WHERE e.id = ? AND e.id IN " + |
|||
"(SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|||
// 在中间
|
|||
assertSql("SELECT * FROM entity e WHERE e.id = ? and e.id IN " + |
|||
"(select e1.id from entity1 e1 where e1.id = ?) and e.id = ?", |
|||
"SELECT * FROM entity e WHERE e.id = ? AND e.id IN " + |
|||
"(SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.id = ? AND e.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectSubSelectEq() { |
|||
/* = */ |
|||
assertSql("SELECT * FROM entity e WHERE e.id = (select e1.id from entity1 e1 where e1.id = ?)", |
|||
"SELECT * FROM entity e WHERE e.id = (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectSubSelectInnerNotEq() { |
|||
/* inner not = */ |
|||
assertSql("SELECT * FROM entity e WHERE not (e.id = (select e1.id from entity1 e1 where e1.id = ?))", |
|||
"SELECT * FROM entity e WHERE NOT (e.id = (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1)) AND e.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM entity e WHERE not (e.id = (select e1.id from entity1 e1 where e1.id = ?) and e.id = ?)", |
|||
"SELECT * FROM entity e WHERE NOT (e.id = (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.id = ?) AND e.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectSubSelectExists() { |
|||
/* EXISTS */ |
|||
assertSql("SELECT * FROM entity e WHERE EXISTS (select e1.id from entity1 e1 where e1.id = ?)", |
|||
"SELECT * FROM entity e WHERE EXISTS (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|||
|
|||
|
|||
/* NOT EXISTS */ |
|||
assertSql("SELECT * FROM entity e WHERE NOT EXISTS (select e1.id from entity1 e1 where e1.id = ?)", |
|||
"SELECT * FROM entity e WHERE NOT EXISTS (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectSubSelect() { |
|||
/* >= */ |
|||
assertSql("SELECT * FROM entity e WHERE e.id >= (select e1.id from entity1 e1 where e1.id = ?)", |
|||
"SELECT * FROM entity e WHERE e.id >= (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|||
|
|||
|
|||
/* <= */ |
|||
assertSql("SELECT * FROM entity e WHERE e.id <= (select e1.id from entity1 e1 where e1.id = ?)", |
|||
"SELECT * FROM entity e WHERE e.id <= (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|||
|
|||
|
|||
/* <> */ |
|||
assertSql("SELECT * FROM entity e WHERE e.id <> (select e1.id from entity1 e1 where e1.id = ?)", |
|||
"SELECT * FROM entity e WHERE e.id <> (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectFromSelect() { |
|||
assertSql("SELECT * FROM (select e.id from entity e WHERE e.id = (select e1.id from entity1 e1 where e1.id = ?))", |
|||
"SELECT * FROM (SELECT e.id FROM entity e WHERE e.id = (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1)"); |
|||
} |
|||
|
|||
@Test |
|||
void selectBodySubSelect() { |
|||
assertSql("select t1.col1,(select t2.col2 from t2 t2 where t1.col1=t2.col1) from t1 t1", |
|||
"SELECT t1.col1, (SELECT t2.col2 FROM t2 t2 WHERE t1.col1 = t2.col1 AND t2.tenant_id = 1) FROM t1 t1 WHERE t1.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectLeftJoin() { |
|||
// left join
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"left join entity1 e1 on e1.id = e.id " + |
|||
"WHERE e.id = ? OR e.name = ?", |
|||
"SELECT * FROM entity e " + |
|||
"LEFT JOIN entity1 e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|||
"WHERE (e.id = ? OR e.name = ?) AND e.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"left join entity1 e1 on e1.id = e.id " + |
|||
"WHERE (e.id = ? OR e.name = ?)", |
|||
"SELECT * FROM entity e " + |
|||
"LEFT JOIN entity1 e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|||
"WHERE (e.id = ? OR e.name = ?) AND e.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"left join entity1 e1 on e1.id = e.id " + |
|||
"left join entity2 e2 on e1.id = e2.id", |
|||
"SELECT * FROM entity e " + |
|||
"LEFT JOIN entity1 e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|||
"LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1 " + |
|||
"WHERE e.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectRightJoin() { |
|||
// right join
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"right join entity1 e1 on e1.id = e.id", |
|||
"SELECT * FROM entity e " + |
|||
"RIGHT JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 " + |
|||
"WHERE e1.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM with_as_1 e " + |
|||
"right join entity1 e1 on e1.id = e.id", |
|||
"SELECT * FROM with_as_1 e " + |
|||
"RIGHT JOIN entity1 e1 ON e1.id = e.id " + |
|||
"WHERE e1.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"right join entity1 e1 on e1.id = e.id " + |
|||
"WHERE e.id = ? OR e.name = ?", |
|||
"SELECT * FROM entity e " + |
|||
"RIGHT JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 " + |
|||
"WHERE (e.id = ? OR e.name = ?) AND e1.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"right join entity1 e1 on e1.id = e.id " + |
|||
"right join entity2 e2 on e1.id = e2.id ", |
|||
"SELECT * FROM entity e " + |
|||
"RIGHT JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 " + |
|||
"RIGHT JOIN entity2 e2 ON e1.id = e2.id AND e1.tenant_id = 1 " + |
|||
"WHERE e2.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectMixJoin() { |
|||
assertSql("SELECT * FROM entity e " + |
|||
"right join entity1 e1 on e1.id = e.id " + |
|||
"left join entity2 e2 on e1.id = e2.id", |
|||
"SELECT * FROM entity e " + |
|||
"RIGHT JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 " + |
|||
"LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1 " + |
|||
"WHERE e1.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"left join entity1 e1 on e1.id = e.id " + |
|||
"right join entity2 e2 on e1.id = e2.id", |
|||
"SELECT * FROM entity e " + |
|||
"LEFT JOIN entity1 e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|||
"RIGHT JOIN entity2 e2 ON e1.id = e2.id AND e1.tenant_id = 1 " + |
|||
"WHERE e2.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"left join entity1 e1 on e1.id = e.id " + |
|||
"inner join entity2 e2 on e1.id = e2.id", |
|||
"SELECT * FROM entity e " + |
|||
"LEFT JOIN entity1 e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|||
"INNER JOIN entity2 e2 ON e1.id = e2.id AND e.tenant_id = 1 AND e2.tenant_id = 1"); |
|||
} |
|||
|
|||
|
|||
@Test |
|||
void selectJoinSubSelect() { |
|||
assertSql("select * from (select * from entity) e1 " + |
|||
"left join entity2 e2 on e1.id = e2.id", |
|||
"SELECT * FROM (SELECT * FROM entity WHERE entity.tenant_id = 1) e1 " + |
|||
"LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1"); |
|||
|
|||
assertSql("select * from entity1 e1 " + |
|||
"left join (select * from entity2) e2 " + |
|||
"on e1.id = e2.id", |
|||
"SELECT * FROM entity1 e1 " + |
|||
"LEFT JOIN (SELECT * FROM entity2 WHERE entity2.tenant_id = 1) e2 " + |
|||
"ON e1.id = e2.id " + |
|||
"WHERE e1.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectSubJoin() { |
|||
|
|||
assertSql("select * FROM " + |
|||
"(entity1 e1 right JOIN entity2 e2 ON e1.id = e2.id)", |
|||
"SELECT * FROM " + |
|||
"(entity1 e1 RIGHT JOIN entity2 e2 ON e1.id = e2.id AND e1.tenant_id = 1) " + |
|||
"WHERE e2.tenant_id = 1"); |
|||
|
|||
assertSql("select * FROM " + |
|||
"(entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id)", |
|||
"SELECT * FROM " + |
|||
"(entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1) " + |
|||
"WHERE e1.tenant_id = 1"); |
|||
|
|||
|
|||
assertSql("select * FROM " + |
|||
"(entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id) " + |
|||
"right join entity3 e3 on e1.id = e3.id", |
|||
"SELECT * FROM " + |
|||
"(entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1) " + |
|||
"RIGHT JOIN entity3 e3 ON e1.id = e3.id AND e1.tenant_id = 1 " + |
|||
"WHERE e3.tenant_id = 1"); |
|||
|
|||
|
|||
assertSql("select * FROM entity e " + |
|||
"LEFT JOIN (entity1 e1 right join entity2 e2 ON e1.id = e2.id) " + |
|||
"on e.id = e2.id", |
|||
"SELECT * FROM entity e " + |
|||
"LEFT JOIN (entity1 e1 RIGHT JOIN entity2 e2 ON e1.id = e2.id AND e1.tenant_id = 1) " + |
|||
"ON e.id = e2.id AND e2.tenant_id = 1 " + |
|||
"WHERE e.tenant_id = 1"); |
|||
|
|||
assertSql("select * FROM entity e " + |
|||
"LEFT JOIN (entity1 e1 left join entity2 e2 ON e1.id = e2.id) " + |
|||
"on e.id = e2.id", |
|||
"SELECT * FROM entity e " + |
|||
"LEFT JOIN (entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1) " + |
|||
"ON e.id = e2.id AND e1.tenant_id = 1 " + |
|||
"WHERE e.tenant_id = 1"); |
|||
|
|||
assertSql("select * FROM entity e " + |
|||
"RIGHT JOIN (entity1 e1 left join entity2 e2 ON e1.id = e2.id) " + |
|||
"on e.id = e2.id", |
|||
"SELECT * FROM entity e " + |
|||
"RIGHT JOIN (entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1) " + |
|||
"ON e.id = e2.id AND e.tenant_id = 1 " + |
|||
"WHERE e1.tenant_id = 1"); |
|||
} |
|||
|
|||
|
|||
@Test |
|||
void selectLeftJoinMultipleTrailingOn() { |
|||
// 多个 on 尾缀的
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"LEFT JOIN entity1 e1 " + |
|||
"LEFT JOIN entity2 e2 ON e2.id = e1.id " + |
|||
"ON e1.id = e.id " + |
|||
"WHERE (e.id = ? OR e.NAME = ?)", |
|||
"SELECT * FROM entity e " + |
|||
"LEFT JOIN entity1 e1 " + |
|||
"LEFT JOIN entity2 e2 ON e2.id = e1.id AND e2.tenant_id = 1 " + |
|||
"ON e1.id = e.id AND e1.tenant_id = 1 " + |
|||
"WHERE (e.id = ? OR e.NAME = ?) AND e.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"LEFT JOIN entity1 e1 " + |
|||
"LEFT JOIN with_as_A e2 ON e2.id = e1.id " + |
|||
"ON e1.id = e.id " + |
|||
"WHERE (e.id = ? OR e.NAME = ?)", |
|||
"SELECT * FROM entity e " + |
|||
"LEFT JOIN entity1 e1 " + |
|||
"LEFT JOIN with_as_A e2 ON e2.id = e1.id " + |
|||
"ON e1.id = e.id AND e1.tenant_id = 1 " + |
|||
"WHERE (e.id = ? OR e.NAME = ?) AND e.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
void selectInnerJoin() { |
|||
// inner join
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"inner join entity1 e1 on e1.id = e.id " + |
|||
"WHERE e.id = ? OR e.name = ?", |
|||
"SELECT * FROM entity e " + |
|||
"INNER JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 AND e1.tenant_id = 1 " + |
|||
"WHERE e.id = ? OR e.name = ?"); |
|||
|
|||
assertSql("SELECT * FROM entity e " + |
|||
"inner join entity1 e1 on e1.id = e.id " + |
|||
"WHERE (e.id = ? OR e.name = ?)", |
|||
"SELECT * FROM entity e " + |
|||
"INNER JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 AND e1.tenant_id = 1 " + |
|||
"WHERE (e.id = ? OR e.name = ?)"); |
|||
|
|||
// 隐式内连接
|
|||
assertSql("SELECT * FROM entity,entity1 " + |
|||
"WHERE entity.id = entity1.id", |
|||
"SELECT * FROM entity, entity1 " + |
|||
"WHERE entity.id = entity1.id AND entity.tenant_id = 1 AND entity1.tenant_id = 1"); |
|||
|
|||
// 隐式内连接
|
|||
assertSql("SELECT * FROM entity a, with_as_entity1 b " + |
|||
"WHERE a.id = b.id", |
|||
"SELECT * FROM entity a, with_as_entity1 b " + |
|||
"WHERE a.id = b.id AND a.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM with_as_entity a, with_as_entity1 b " + |
|||
"WHERE a.id = b.id", |
|||
"SELECT * FROM with_as_entity a, with_as_entity1 b " + |
|||
"WHERE a.id = b.id"); |
|||
|
|||
// SubJoin with 隐式内连接
|
|||
assertSql("SELECT * FROM (entity,entity1) " + |
|||
"WHERE entity.id = entity1.id", |
|||
"SELECT * FROM (entity, entity1) " + |
|||
"WHERE entity.id = entity1.id " + |
|||
"AND entity.tenant_id = 1 AND entity1.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM ((entity,entity1),entity2) " + |
|||
"WHERE entity.id = entity1.id and entity.id = entity2.id", |
|||
"SELECT * FROM ((entity, entity1), entity2) " + |
|||
"WHERE entity.id = entity1.id AND entity.id = entity2.id " + |
|||
"AND entity.tenant_id = 1 AND entity1.tenant_id = 1 AND entity2.tenant_id = 1"); |
|||
|
|||
assertSql("SELECT * FROM (entity,(entity1,entity2)) " + |
|||
"WHERE entity.id = entity1.id and entity.id = entity2.id", |
|||
"SELECT * FROM (entity, (entity1, entity2)) " + |
|||
"WHERE entity.id = entity1.id AND entity.id = entity2.id " + |
|||
"AND entity.tenant_id = 1 AND entity1.tenant_id = 1 AND entity2.tenant_id = 1"); |
|||
|
|||
// 沙雕的括号写法
|
|||
assertSql("SELECT * FROM (((entity,entity1))) " + |
|||
"WHERE entity.id = entity1.id", |
|||
"SELECT * FROM (((entity, entity1))) " + |
|||
"WHERE entity.id = entity1.id " + |
|||
"AND entity.tenant_id = 1 AND entity1.tenant_id = 1"); |
|||
|
|||
} |
|||
|
|||
|
|||
@Test |
|||
void selectWithAs() { |
|||
assertSql("with with_as_A as (select * from entity) select * from with_as_A", |
|||
"WITH with_as_A AS (SELECT * FROM entity WHERE entity.tenant_id = 1) SELECT * FROM with_as_A"); |
|||
} |
|||
|
|||
|
|||
@Test |
|||
void selectIgnoreTable() { |
|||
assertSql(" SELECT dict.dict_code, item.item_text AS \"text\", item.item_value AS \"value\" FROM sys_dict_item item INNER JOIN sys_dict dict ON dict.id = item.dict_id WHERE dict.dict_code IN (1, 2, 3) AND item.item_value IN (1, 2, 3)", |
|||
"SELECT dict.dict_code, item.item_text AS \"text\", item.item_value AS \"value\" FROM sys_dict_item item INNER JOIN sys_dict dict ON dict.id = item.dict_id AND item.tenant_id = 1 WHERE dict.dict_code IN (1, 2, 3) AND item.item_value IN (1, 2, 3)"); |
|||
} |
|||
|
|||
private void assertSql(String sql, String targetSql) { |
|||
assertEquals(targetSql, interceptor.parserSingle(sql, null)); |
|||
} |
|||
|
|||
|
|||
// ========== 额外的测试 ==========
|
|||
|
|||
@Test |
|||
public void testSelectSingle() { |
|||
// 单表
|
|||
assertSql("select * from t_user where id = ?", |
|||
"SELECT * FROM t_user WHERE id = ? AND t_user.tenant_id = 1 AND t_user.dept_id IN (10, 20)"); |
|||
|
|||
assertSql("select * from t_user where id = ? or name = ?", |
|||
"SELECT * FROM t_user WHERE (id = ? OR name = ?) AND t_user.tenant_id = 1 AND t_user.dept_id IN (10, 20)"); |
|||
|
|||
assertSql("SELECT * FROM t_user WHERE (id = ? OR name = ?)", |
|||
"SELECT * FROM t_user WHERE (id = ? OR name = ?) AND t_user.tenant_id = 1 AND t_user.dept_id IN (10, 20)"); |
|||
|
|||
/* not */ |
|||
assertSql("SELECT * FROM t_user WHERE not (id = ? OR name = ?)", |
|||
"SELECT * FROM t_user WHERE NOT (id = ? OR name = ?) AND t_user.tenant_id = 1 AND t_user.dept_id IN (10, 20)"); |
|||
} |
|||
|
|||
@Test |
|||
public void testSelectLeftJoin() { |
|||
// left join
|
|||
assertSql("SELECT * FROM t_user e " + |
|||
"left join t_role e1 on e1.id = e.id " + |
|||
"WHERE e.id = ? OR e.name = ?", |
|||
"SELECT * FROM t_user e " + |
|||
"LEFT JOIN t_role e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|||
"WHERE (e.id = ? OR e.name = ?) AND e.tenant_id = 1 AND e.dept_id IN (10, 20)"); |
|||
|
|||
// 条件 e.id = ? OR e.name = ? 带括号
|
|||
assertSql("SELECT * FROM t_user e " + |
|||
"left join t_role e1 on e1.id = e.id " + |
|||
"WHERE (e.id = ? OR e.name = ?)", |
|||
"SELECT * FROM t_user e " + |
|||
"LEFT JOIN t_role e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|||
"WHERE (e.id = ? OR e.name = ?) AND e.tenant_id = 1 AND e.dept_id IN (10, 20)"); |
|||
} |
|||
|
|||
@Test |
|||
public void testSelectRightJoin() { |
|||
// right join
|
|||
assertSql("SELECT * FROM t_user e " + |
|||
"right join t_role e1 on e1.id = e.id " + |
|||
"WHERE e.id = ? OR e.name = ?", |
|||
"SELECT * FROM t_user e " + |
|||
"RIGHT JOIN t_role e1 ON e1.id = e.id AND e.tenant_id = 1 AND e.dept_id IN (10, 20) " + |
|||
"WHERE (e.id = ? OR e.name = ?) AND e1.tenant_id = 1"); |
|||
|
|||
// 条件 e.id = ? OR e.name = ? 带括号
|
|||
assertSql("SELECT * FROM t_user e " + |
|||
"right join t_role e1 on e1.id = e.id " + |
|||
"WHERE (e.id = ? OR e.name = ?)", |
|||
"SELECT * FROM t_user e " + |
|||
"RIGHT JOIN t_role e1 ON e1.id = e.id AND e.tenant_id = 1 AND e.dept_id IN (10, 20) " + |
|||
"WHERE (e.id = ? OR e.name = ?) AND e1.tenant_id = 1"); |
|||
} |
|||
|
|||
@Test |
|||
public void testSelectInnerJoin() { |
|||
// inner join
|
|||
assertSql("SELECT * FROM t_user e " + |
|||
"inner join entity1 e1 on e1.id = e.id " + |
|||
"WHERE e.id = ? OR e.name = ?", |
|||
"SELECT * FROM t_user e " + |
|||
"INNER JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 AND e.dept_id IN (10, 20) AND e1.tenant_id = 1 " + |
|||
"WHERE e.id = ? OR e.name = ?"); |
|||
|
|||
// 条件 e.id = ? OR e.name = ? 带括号
|
|||
assertSql("SELECT * FROM t_user e " + |
|||
"inner join entity1 e1 on e1.id = e.id " + |
|||
"WHERE (e.id = ? OR e.name = ?)", |
|||
"SELECT * FROM t_user e " + |
|||
"INNER JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 AND e.dept_id IN (10, 20) AND e1.tenant_id = 1 " + |
|||
"WHERE (e.id = ? OR e.name = ?)"); |
|||
|
|||
// 没有 On 的 inner join
|
|||
assertSql("SELECT * FROM entity,entity1 " + |
|||
"WHERE entity.id = entity1.id", |
|||
"SELECT * FROM entity, entity1 " + |
|||
"WHERE entity.id = entity1.id AND entity.tenant_id = 1 AND entity1.tenant_id = 1"); |
|||
} |
|||
|
|||
} |
@ -1,145 +0,0 @@ |
|||
package com.win.framework.datapermission.core.rule; |
|||
|
|||
import com.win.framework.datapermission.core.annotation.DataPermission; |
|||
import com.win.framework.datapermission.core.aop.DataPermissionContextHolder; |
|||
import com.win.framework.test.core.ut.BaseMockitoUnitTest; |
|||
import net.sf.jsqlparser.expression.Alias; |
|||
import net.sf.jsqlparser.expression.Expression; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.InjectMocks; |
|||
import org.mockito.Spy; |
|||
import org.springframework.core.annotation.AnnotationUtils; |
|||
|
|||
import java.util.Arrays; |
|||
import java.util.List; |
|||
import java.util.Set; |
|||
|
|||
import static com.win.framework.test.core.util.RandomUtils.randomString; |
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
/** |
|||
* {@link DataPermissionRuleFactoryImpl} 单元测试 |
|||
* |
|||
* @author 芋道源码 |
|||
*/ |
|||
class DataPermissionRuleFactoryImplTest extends BaseMockitoUnitTest { |
|||
|
|||
@InjectMocks |
|||
private DataPermissionRuleFactoryImpl dataPermissionRuleFactory; |
|||
|
|||
@Spy |
|||
private List<DataPermissionRule> rules = Arrays.asList(new DataPermissionRule01(), |
|||
new DataPermissionRule02()); |
|||
|
|||
@BeforeEach |
|||
public void setUp() { |
|||
DataPermissionContextHolder.clear(); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetDataPermissionRule_02() { |
|||
// 准备参数
|
|||
String mappedStatementId = randomString(); |
|||
|
|||
// 调用
|
|||
List<DataPermissionRule> result = dataPermissionRuleFactory.getDataPermissionRule(mappedStatementId); |
|||
// 断言
|
|||
assertSame(rules, result); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetDataPermissionRule_03() { |
|||
// 准备参数
|
|||
String mappedStatementId = randomString(); |
|||
// mock 方法
|
|||
DataPermissionContextHolder.add(AnnotationUtils.findAnnotation(TestClass03.class, DataPermission.class)); |
|||
|
|||
// 调用
|
|||
List<DataPermissionRule> result = dataPermissionRuleFactory.getDataPermissionRule(mappedStatementId); |
|||
// 断言
|
|||
assertTrue(result.isEmpty()); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetDataPermissionRule_04() { |
|||
// 准备参数
|
|||
String mappedStatementId = randomString(); |
|||
// mock 方法
|
|||
DataPermissionContextHolder.add(AnnotationUtils.findAnnotation(TestClass04.class, DataPermission.class)); |
|||
|
|||
// 调用
|
|||
List<DataPermissionRule> result = dataPermissionRuleFactory.getDataPermissionRule(mappedStatementId); |
|||
// 断言
|
|||
assertEquals(1, result.size()); |
|||
assertEquals(DataPermissionRule01.class, result.get(0).getClass()); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetDataPermissionRule_05() { |
|||
// 准备参数
|
|||
String mappedStatementId = randomString(); |
|||
// mock 方法
|
|||
DataPermissionContextHolder.add(AnnotationUtils.findAnnotation(TestClass05.class, DataPermission.class)); |
|||
|
|||
// 调用
|
|||
List<DataPermissionRule> result = dataPermissionRuleFactory.getDataPermissionRule(mappedStatementId); |
|||
// 断言
|
|||
assertEquals(1, result.size()); |
|||
assertEquals(DataPermissionRule02.class, result.get(0).getClass()); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetDataPermissionRule_06() { |
|||
// 准备参数
|
|||
String mappedStatementId = randomString(); |
|||
// mock 方法
|
|||
DataPermissionContextHolder.add(AnnotationUtils.findAnnotation(TestClass06.class, DataPermission.class)); |
|||
|
|||
// 调用
|
|||
List<DataPermissionRule> result = dataPermissionRuleFactory.getDataPermissionRule(mappedStatementId); |
|||
// 断言
|
|||
assertSame(rules, result); |
|||
} |
|||
|
|||
@DataPermission(enable = false) |
|||
static class TestClass03 {} |
|||
|
|||
@DataPermission(includeRules = DataPermissionRule01.class) |
|||
static class TestClass04 {} |
|||
|
|||
@DataPermission(excludeRules = DataPermissionRule01.class) |
|||
static class TestClass05 {} |
|||
|
|||
@DataPermission |
|||
static class TestClass06 {} |
|||
|
|||
static class DataPermissionRule01 implements DataPermissionRule { |
|||
|
|||
@Override |
|||
public Set<String> getTableNames() { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public Expression getExpression(String tableName, Alias tableAlias) { |
|||
return null; |
|||
} |
|||
|
|||
} |
|||
|
|||
static class DataPermissionRule02 implements DataPermissionRule { |
|||
|
|||
@Override |
|||
public Set<String> getTableNames() { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public Expression getExpression(String tableName, Alias tableAlias) { |
|||
return null; |
|||
} |
|||
|
|||
} |
|||
|
|||
} |
@ -1,238 +0,0 @@ |
|||
package com.win.framework.datapermission.core.rule.dept; |
|||
|
|||
import cn.hutool.core.collection.CollUtil; |
|||
import cn.hutool.core.util.ReflectUtil; |
|||
import com.win.framework.common.enums.UserTypeEnum; |
|||
import com.win.framework.common.util.collection.SetUtils; |
|||
import com.win.framework.security.core.LoginUser; |
|||
import com.win.framework.security.core.util.SecurityFrameworkUtils; |
|||
import com.win.framework.test.core.ut.BaseMockitoUnitTest; |
|||
import com.win.module.system.api.permission.PermissionApi; |
|||
import com.win.module.system.api.permission.dto.DeptDataPermissionRespDTO; |
|||
import net.sf.jsqlparser.expression.Alias; |
|||
import net.sf.jsqlparser.expression.Expression; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.InjectMocks; |
|||
import org.mockito.Mock; |
|||
import org.mockito.MockedStatic; |
|||
|
|||
import java.util.Map; |
|||
|
|||
import static com.win.framework.datapermission.core.rule.dept.DeptDataPermissionRule.EXPRESSION_NULL; |
|||
import static com.win.framework.test.core.util.RandomUtils.randomPojo; |
|||
import static com.win.framework.test.core.util.RandomUtils.randomString; |
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
import static org.mockito.ArgumentMatchers.eq; |
|||
import static org.mockito.ArgumentMatchers.same; |
|||
import static org.mockito.Mockito.mockStatic; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
/** |
|||
* {@link DeptDataPermissionRule} 的单元测试 |
|||
* |
|||
* @author 芋道源码 |
|||
*/ |
|||
class DeptDataPermissionRuleTest extends BaseMockitoUnitTest { |
|||
|
|||
@InjectMocks |
|||
private DeptDataPermissionRule rule; |
|||
|
|||
@Mock |
|||
private PermissionApi permissionApi; |
|||
|
|||
@BeforeEach |
|||
@SuppressWarnings("unchecked") |
|||
public void setUp() { |
|||
// 清空 rule
|
|||
rule.getTableNames().clear(); |
|||
((Map<String, String>) ReflectUtil.getFieldValue(rule, "deptColumns")).clear(); |
|||
((Map<String, String>) ReflectUtil.getFieldValue(rule, "deptColumns")).clear(); |
|||
} |
|||
|
|||
@Test // 无 LoginUser
|
|||
public void testGetExpression_noLoginUser() { |
|||
// 准备参数
|
|||
String tableName = randomString(); |
|||
Alias tableAlias = new Alias(randomString()); |
|||
// mock 方法
|
|||
|
|||
// 调用
|
|||
Expression expression = rule.getExpression(tableName, tableAlias); |
|||
// 断言
|
|||
assertNull(expression); |
|||
} |
|||
|
|||
@Test // 无数据权限时
|
|||
public void testGetExpression_noDeptDataPermission() { |
|||
try (MockedStatic<SecurityFrameworkUtils> securityFrameworkUtilsMock |
|||
= mockStatic(SecurityFrameworkUtils.class)) { |
|||
// 准备参数
|
|||
String tableName = "t_user"; |
|||
Alias tableAlias = new Alias("u"); |
|||
// mock 方法
|
|||
LoginUser loginUser = randomPojo(LoginUser.class, o -> o.setId(1L) |
|||
.setUserType(UserTypeEnum.ADMIN.getValue())); |
|||
securityFrameworkUtilsMock.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser); |
|||
// mock 方法(permissionApi 返回 null)
|
|||
when(permissionApi.getDeptDataPermission(eq(loginUser.getId()))).thenReturn(null); |
|||
|
|||
// 调用
|
|||
NullPointerException exception = assertThrows(NullPointerException.class, |
|||
() -> rule.getExpression(tableName, tableAlias)); |
|||
// 断言
|
|||
assertEquals("LoginUser(1) Table(t_user/u) 未返回数据权限", exception.getMessage()); |
|||
} |
|||
} |
|||
|
|||
@Test // 全部数据权限
|
|||
public void testGetExpression_allDeptDataPermission() { |
|||
try (MockedStatic<SecurityFrameworkUtils> securityFrameworkUtilsMock |
|||
= mockStatic(SecurityFrameworkUtils.class)) { |
|||
// 准备参数
|
|||
String tableName = "t_user"; |
|||
Alias tableAlias = new Alias("u"); |
|||
// mock 方法(LoginUser)
|
|||
LoginUser loginUser = randomPojo(LoginUser.class, o -> o.setId(1L) |
|||
.setUserType(UserTypeEnum.ADMIN.getValue())); |
|||
securityFrameworkUtilsMock.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser); |
|||
// mock 方法(DeptDataPermissionRespDTO)
|
|||
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO().setAll(true); |
|||
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission); |
|||
|
|||
// 调用
|
|||
Expression expression = rule.getExpression(tableName, tableAlias); |
|||
// 断言
|
|||
assertNull(expression); |
|||
assertSame(deptDataPermission, loginUser.getContext(DeptDataPermissionRule.CONTEXT_KEY, DeptDataPermissionRespDTO.class)); |
|||
} |
|||
} |
|||
|
|||
@Test // 即不能查看部门,又不能查看自己,则说明 100% 无权限
|
|||
public void testGetExpression_noDept_noSelf() { |
|||
try (MockedStatic<SecurityFrameworkUtils> securityFrameworkUtilsMock |
|||
= mockStatic(SecurityFrameworkUtils.class)) { |
|||
// 准备参数
|
|||
String tableName = "t_user"; |
|||
Alias tableAlias = new Alias("u"); |
|||
// mock 方法(LoginUser)
|
|||
LoginUser loginUser = randomPojo(LoginUser.class, o -> o.setId(1L) |
|||
.setUserType(UserTypeEnum.ADMIN.getValue())); |
|||
securityFrameworkUtilsMock.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser); |
|||
// mock 方法(DeptDataPermissionRespDTO)
|
|||
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO(); |
|||
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission); |
|||
|
|||
// 调用
|
|||
Expression expression = rule.getExpression(tableName, tableAlias); |
|||
// 断言
|
|||
assertEquals("null = null", expression.toString()); |
|||
assertSame(deptDataPermission, loginUser.getContext(DeptDataPermissionRule.CONTEXT_KEY, DeptDataPermissionRespDTO.class)); |
|||
} |
|||
} |
|||
|
|||
@Test // 拼接 Dept 和 User 的条件(字段都不符合)
|
|||
public void testGetExpression_noDeptColumn_noSelfColumn() { |
|||
try (MockedStatic<SecurityFrameworkUtils> securityFrameworkUtilsMock |
|||
= mockStatic(SecurityFrameworkUtils.class)) { |
|||
// 准备参数
|
|||
String tableName = "t_user"; |
|||
Alias tableAlias = new Alias("u"); |
|||
// mock 方法(LoginUser)
|
|||
LoginUser loginUser = randomPojo(LoginUser.class, o -> o.setId(1L) |
|||
.setUserType(UserTypeEnum.ADMIN.getValue())); |
|||
securityFrameworkUtilsMock.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser); |
|||
// mock 方法(DeptDataPermissionRespDTO)
|
|||
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO() |
|||
.setDeptIds(SetUtils.asSet(10L, 20L)).setSelf(true); |
|||
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission); |
|||
|
|||
// 调用
|
|||
Expression expression = rule.getExpression(tableName, tableAlias); |
|||
// 断言
|
|||
assertSame(EXPRESSION_NULL, expression); |
|||
assertSame(deptDataPermission, loginUser.getContext(DeptDataPermissionRule.CONTEXT_KEY, DeptDataPermissionRespDTO.class)); |
|||
} |
|||
} |
|||
|
|||
@Test // 拼接 Dept 和 User 的条件(self 符合)
|
|||
public void testGetExpression_noDeptColumn_yesSelfColumn() { |
|||
try (MockedStatic<SecurityFrameworkUtils> securityFrameworkUtilsMock |
|||
= mockStatic(SecurityFrameworkUtils.class)) { |
|||
// 准备参数
|
|||
String tableName = "t_user"; |
|||
Alias tableAlias = new Alias("u"); |
|||
// mock 方法(LoginUser)
|
|||
LoginUser loginUser = randomPojo(LoginUser.class, o -> o.setId(1L) |
|||
.setUserType(UserTypeEnum.ADMIN.getValue())); |
|||
securityFrameworkUtilsMock.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser); |
|||
// mock 方法(DeptDataPermissionRespDTO)
|
|||
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO() |
|||
.setSelf(true); |
|||
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission); |
|||
// 添加 user 字段配置
|
|||
rule.addUserColumn("t_user", "id"); |
|||
|
|||
// 调用
|
|||
Expression expression = rule.getExpression(tableName, tableAlias); |
|||
// 断言
|
|||
assertEquals("u.id = 1", expression.toString()); |
|||
assertSame(deptDataPermission, loginUser.getContext(DeptDataPermissionRule.CONTEXT_KEY, DeptDataPermissionRespDTO.class)); |
|||
} |
|||
} |
|||
|
|||
@Test // 拼接 Dept 和 User 的条件(dept 符合)
|
|||
public void testGetExpression_yesDeptColumn_noSelfColumn() { |
|||
try (MockedStatic<SecurityFrameworkUtils> securityFrameworkUtilsMock |
|||
= mockStatic(SecurityFrameworkUtils.class)) { |
|||
// 准备参数
|
|||
String tableName = "t_user"; |
|||
Alias tableAlias = new Alias("u"); |
|||
// mock 方法(LoginUser)
|
|||
LoginUser loginUser = randomPojo(LoginUser.class, o -> o.setId(1L) |
|||
.setUserType(UserTypeEnum.ADMIN.getValue())); |
|||
securityFrameworkUtilsMock.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser); |
|||
// mock 方法(DeptDataPermissionRespDTO)
|
|||
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO() |
|||
.setDeptIds(CollUtil.newLinkedHashSet(10L, 20L)); |
|||
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission); |
|||
// 添加 dept 字段配置
|
|||
rule.addDeptColumn("t_user", "dept_id"); |
|||
|
|||
// 调用
|
|||
Expression expression = rule.getExpression(tableName, tableAlias); |
|||
// 断言
|
|||
assertEquals("u.dept_id IN (10, 20)", expression.toString()); |
|||
assertSame(deptDataPermission, loginUser.getContext(DeptDataPermissionRule.CONTEXT_KEY, DeptDataPermissionRespDTO.class)); |
|||
} |
|||
} |
|||
|
|||
@Test // 拼接 Dept 和 User 的条件(dept + self 符合)
|
|||
public void testGetExpression_yesDeptColumn_yesSelfColumn() { |
|||
try (MockedStatic<SecurityFrameworkUtils> securityFrameworkUtilsMock |
|||
= mockStatic(SecurityFrameworkUtils.class)) { |
|||
// 准备参数
|
|||
String tableName = "t_user"; |
|||
Alias tableAlias = new Alias("u"); |
|||
// mock 方法(LoginUser)
|
|||
LoginUser loginUser = randomPojo(LoginUser.class, o -> o.setId(1L) |
|||
.setUserType(UserTypeEnum.ADMIN.getValue())); |
|||
securityFrameworkUtilsMock.when(SecurityFrameworkUtils::getLoginUser).thenReturn(loginUser); |
|||
// mock 方法(DeptDataPermissionRespDTO)
|
|||
DeptDataPermissionRespDTO deptDataPermission = new DeptDataPermissionRespDTO() |
|||
.setDeptIds(CollUtil.newLinkedHashSet(10L, 20L)).setSelf(true); |
|||
when(permissionApi.getDeptDataPermission(same(1L))).thenReturn(deptDataPermission); |
|||
// 添加 user 字段配置
|
|||
rule.addUserColumn("t_user", "id"); |
|||
// 添加 dept 字段配置
|
|||
rule.addDeptColumn("t_user", "dept_id"); |
|||
|
|||
// 调用
|
|||
Expression expression = rule.getExpression(tableName, tableAlias); |
|||
// 断言
|
|||
assertEquals("(u.dept_id IN (10, 20) OR u.id = 1)", expression.toString()); |
|||
assertSame(deptDataPermission, loginUser.getContext(DeptDataPermissionRule.CONTEXT_KEY, DeptDataPermissionRespDTO.class)); |
|||
} |
|||
} |
|||
|
|||
} |
@ -1,15 +0,0 @@ |
|||
package com.win.framework.datapermission.core.util; |
|||
|
|||
import com.win.framework.datapermission.core.aop.DataPermissionContextHolder; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
public class DataPermissionUtilsTest { |
|||
|
|||
@Test |
|||
public void testExecuteIgnore() { |
|||
DataPermissionUtils.executeIgnore(() -> assertFalse(DataPermissionContextHolder.get().enable())); |
|||
} |
|||
|
|||
} |
@ -1,48 +0,0 @@ |
|||
package com.win.framework.dict.core.util; |
|||
|
|||
import com.win.framework.common.enums.CommonStatusEnum; |
|||
import com.win.framework.test.core.ut.BaseMockitoUnitTest; |
|||
import com.win.module.system.api.dict.DictDataApi; |
|||
import com.win.module.system.api.dict.dto.DictDataRespDTO; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.mockito.Mock; |
|||
|
|||
import static com.win.framework.test.core.util.RandomUtils.randomPojo; |
|||
import static org.junit.jupiter.api.Assertions.assertEquals; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
/** |
|||
* {@link DictFrameworkUtils} 的单元测试 |
|||
*/ |
|||
public class DictFrameworkUtilsTest extends BaseMockitoUnitTest { |
|||
|
|||
@Mock |
|||
private DictDataApi dictDataApi; |
|||
|
|||
@BeforeEach |
|||
public void setUp() { |
|||
DictFrameworkUtils.init(dictDataApi); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetDictDataLabel() { |
|||
// mock 数据
|
|||
DictDataRespDTO dataRespDTO = randomPojo(DictDataRespDTO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); |
|||
// mock 方法
|
|||
when(dictDataApi.getDictData(dataRespDTO.getDictType(), dataRespDTO.getValue())).thenReturn(dataRespDTO); |
|||
// 断言返回值
|
|||
assertEquals(dataRespDTO.getLabel(), DictFrameworkUtils.getDictDataLabel(dataRespDTO.getDictType(), dataRespDTO.getValue())); |
|||
} |
|||
|
|||
@Test |
|||
public void testParseDictDataValue() { |
|||
// mock 数据
|
|||
DictDataRespDTO resp = randomPojo(DictDataRespDTO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); |
|||
// mock 方法
|
|||
when(dictDataApi.parseDictData(resp.getDictType(), resp.getLabel())).thenReturn(resp); |
|||
// 断言返回值
|
|||
assertEquals(resp.getValue(), DictFrameworkUtils.parseDictDataValue(resp.getDictType(), resp.getLabel())); |
|||
} |
|||
|
|||
} |
@ -1,36 +0,0 @@ |
|||
package com.win.framework.ip.core.utils; |
|||
|
|||
|
|||
import com.win.framework.ip.core.Area; |
|||
import com.win.framework.ip.core.enums.AreaTypeEnum; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals; |
|||
|
|||
/** |
|||
* {@link AreaUtils} 的单元测试 |
|||
* |
|||
* @author 芋道源码 |
|||
*/ |
|||
public class AreaUtilsTest { |
|||
|
|||
@Test |
|||
public void testGetArea() { |
|||
// 调用:北京
|
|||
Area area = AreaUtils.getArea(110100); |
|||
// 断言
|
|||
assertEquals(area.getId(), 110100); |
|||
assertEquals(area.getName(), "北京市"); |
|||
assertEquals(area.getType(), AreaTypeEnum.CITY.getType()); |
|||
assertEquals(area.getParent().getId(), 110000); |
|||
assertEquals(area.getChildren().size(), 16); |
|||
} |
|||
|
|||
@Test |
|||
public void testFormat() { |
|||
assertEquals(AreaUtils.format(110105), "北京 北京市 朝阳区"); |
|||
assertEquals(AreaUtils.format(1), "中国"); |
|||
assertEquals(AreaUtils.format(2), "蒙古"); |
|||
} |
|||
|
|||
} |
@ -1,47 +0,0 @@ |
|||
package com.win.framework.ip.core.utils; |
|||
|
|||
import com.win.framework.ip.core.Area; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.lionsoul.ip2region.xdb.Searcher; |
|||
|
|||
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals; |
|||
|
|||
/** |
|||
* {@link IPUtils} 的单元测试 |
|||
* |
|||
* @author wanglhup |
|||
*/ |
|||
public class IPUtilsTest { |
|||
|
|||
@Test |
|||
public void testGetAreaId_string() { |
|||
// 120.202.4.0|120.202.4.255|420600
|
|||
Integer areaId = IPUtils.getAreaId("120.202.4.50"); |
|||
assertEquals(420600, areaId); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetAreaId_long() throws Exception { |
|||
// 120.203.123.0|120.203.133.255|360900
|
|||
long ip = Searcher.checkIP("120.203.123.250"); |
|||
Integer areaId = IPUtils.getAreaId(ip); |
|||
assertEquals(360900, areaId); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetArea_string() { |
|||
// 120.202.4.0|120.202.4.255|420600
|
|||
Area area = IPUtils.getArea("120.202.4.50"); |
|||
assertEquals("襄阳市", area.getName()); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetArea_long() throws Exception { |
|||
// 120.203.123.0|120.203.133.255|360900
|
|||
long ip = Searcher.checkIP("120.203.123.252"); |
|||
Area area = IPUtils.getArea(ip); |
|||
assertEquals("宜春市", area.getName()); |
|||
} |
|||
|
|||
} |
@ -1,6 +1,6 @@ |
|||
/** |
|||
* 用户操作日志:记录用户的操作,用于对用户的操作的审计与追溯,永久保存。 |
|||
* |
|||
* @author 芋道源码 |
|||
* @author 闻荫源码 |
|||
*/ |
|||
package com.win.framework.operatelog; |
|||
|
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue