1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.permissions;
17
18 import java.io.Reader;
19 import java.util.List;
20
21 import org.apache.ibatis.BaseDataTest;
22 import org.apache.ibatis.io.Resources;
23 import org.apache.ibatis.session.LocalCacheScope;
24 import org.apache.ibatis.session.SqlSession;
25 import org.apache.ibatis.session.SqlSessionFactory;
26 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.params.ParameterizedTest;
31 import org.junit.jupiter.params.provider.EnumSource;
32
33 class PermissionsTest {
34
35 private static SqlSessionFactory sqlSessionFactory;
36
37 @BeforeAll
38 static void setUp() throws Exception {
39
40 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/permissions/mybatis-config.xml")) {
41 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42 }
43
44
45 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46 "org/apache/ibatis/submitted/permissions/CreateDB.sql");
47 }
48
49 @Test
50 void checkNestedResultMapLoop() {
51 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52 final PermissionsMapper mapper = sqlSession.getMapper(PermissionsMapper.class);
53
54 final List<Resource> resources = mapper.getResources();
55 Assertions.assertEquals(2, resources.size());
56
57 final Resource firstResource = resources.get(0);
58 final List<Principal> principalPermissions = firstResource.getPrincipals();
59 Assertions.assertEquals(1, principalPermissions.size());
60
61 final Principal firstPrincipal = principalPermissions.get(0);
62 final List<Permission> permissions = firstPrincipal.getPermissions();
63 Assertions.assertEquals(2, permissions.size());
64
65 final Permission firstPermission = firstPrincipal.getPermissions().get(0);
66 Assertions.assertSame(firstResource, firstPermission.getResource());
67 final Permission secondPermission = firstPrincipal.getPermissions().get(1);
68 Assertions.assertSame(firstResource, secondPermission.getResource());
69 }
70 }
71
72 @ParameterizedTest
73 @EnumSource
74 void checkNestedSelectLoop(LocalCacheScope localCacheScope) {
75 sqlSessionFactory.getConfiguration().setLocalCacheScope(localCacheScope);
76 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
77 final PermissionsMapper mapper = sqlSession.getMapper(PermissionsMapper.class);
78
79 final List<Resource> resources = mapper.getResource("read");
80 Assertions.assertEquals(1, resources.size());
81
82 final Resource firstResource = resources.get(0);
83 final List<Principal> principalPermissions = firstResource.getPrincipals();
84 Assertions.assertEquals(1, principalPermissions.size());
85
86 final Principal firstPrincipal = principalPermissions.get(0);
87 final List<Permission> permissions = firstPrincipal.getPermissions();
88 Assertions.assertEquals(4, permissions.size());
89
90 boolean readFound = false;
91 for (Permission permission : permissions) {
92 if ("read".equals(permission.getPermission())) {
93 Assertions.assertSame(firstResource, permission.getResource());
94 readFound = true;
95 }
96 }
97
98 if (!readFound) {
99 Assertions.fail();
100 }
101 } finally {
102
103 sqlSessionFactory.getConfiguration().setLocalCacheScope(LocalCacheScope.SESSION);
104 }
105 }
106
107 }