潘志宝
2024-08-15 81c220fd9e0ea6c8ee84c9b766885b0322b4038c
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.datapermission.core.aop;
H 2
3 import com.iailab.framework.datapermission.core.annotation.DataPermission;
4 import org.junit.jupiter.api.BeforeEach;
5 import org.junit.jupiter.api.Test;
6
7 import static org.junit.jupiter.api.Assertions.assertEquals;
8 import static org.junit.jupiter.api.Assertions.assertSame;
9 import static org.mockito.Mockito.mock;
10
11 /**
12  * {@link DataPermissionContextHolder} 的单元测试
13  *
14  * @author iailab
15  */
16 class DataPermissionContextHolderTest {
17
18     @BeforeEach
19     public void setUp() {
20         DataPermissionContextHolder.clear();
21     }
22
23     @Test
24     public void testGet() {
25         // mock 方法
26         DataPermission dataPermission01 = mock(DataPermission.class);
27         DataPermissionContextHolder.add(dataPermission01);
28         DataPermission dataPermission02 = mock(DataPermission.class);
29         DataPermissionContextHolder.add(dataPermission02);
30
31         // 调用
32         DataPermission result = DataPermissionContextHolder.get();
33         // 断言
34         assertSame(result, dataPermission02);
35     }
36
37     @Test
38     public void testPush() {
39         // 调用
40         DataPermission dataPermission01 = mock(DataPermission.class);
41         DataPermissionContextHolder.add(dataPermission01);
42         DataPermission dataPermission02 = mock(DataPermission.class);
43         DataPermissionContextHolder.add(dataPermission02);
44         // 断言
45         DataPermission first = DataPermissionContextHolder.getAll().get(0);
46         DataPermission second = DataPermissionContextHolder.getAll().get(1);
47         assertSame(dataPermission01, first);
48         assertSame(dataPermission02, second);
49     }
50
51     @Test
52     public void testRemove() {
53         // mock 方法
54         DataPermission dataPermission01 = mock(DataPermission.class);
55         DataPermissionContextHolder.add(dataPermission01);
56         DataPermission dataPermission02 = mock(DataPermission.class);
57         DataPermissionContextHolder.add(dataPermission02);
58
59         // 调用
60         DataPermission result = DataPermissionContextHolder.remove();
61         // 断言
62         assertSame(result, dataPermission02);
63         assertEquals(1, DataPermissionContextHolder.getAll().size());
64     }
65
66 }