刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package jnpf.dmsMapper;
 
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Test;
 
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
 
import static org.junit.jupiter.api.Assertions.*;
 
class DmsPermissionGroupDisplayMapperTest {
    private MappedStatement statement() throws Exception {
        Configuration configuration = new Configuration();
        String resource = "mapper/DmsPermissionGroupMapper.xml";
        try (InputStream stream = getClass().getClassLoader().getResourceAsStream(resource)) {
            assertNotNull(stream);
            new XMLMapperBuilder(stream, configuration, resource, configuration.getSqlFragments()).parse();
        }
        return configuration.getMappedStatement("jnpf.dmsMapper.DmsPermissionGroupMapper.selectDisplayByIds");
    }
 
    @Test
    void bindsIdsAndMapsNamesWithoutHidingDisabledHistoricalGroups() throws Exception {
        MappedStatement statement = statement();
        BoundSql bound = statement.getBoundSql(Collections.singletonMap("ids", Arrays.asList("group-1", "group-2")));
        String sql = bound.getSql().replaceAll("\\s+", " ");
        assertTrue(sql.contains("FROM base_permission_group"));
        assertTrue(sql.contains("f_delete_mark IS NULL"));
        assertTrue(sql.contains("f_id IN"));
        assertFalse(sql.contains("enabled_mark"));
        assertFalse(sql.contains("group-1"));
        assertEquals(2, bound.getParameterMappings().size());
        assertEquals("group-1", bound.getAdditionalParameter(bound.getParameterMappings().get(0).getProperty()));
        assertEquals("group-2", bound.getAdditionalParameter(bound.getParameterMappings().get(1).getProperty()));
        assertTrue(statement.getResultMaps().get(0).getResultMappings().stream()
                .anyMatch(mapping -> "id".equals(mapping.getProperty()) && "f_id".equals(mapping.getColumn())));
        assertTrue(statement.getResultMaps().get(0).getResultMappings().stream()
                .anyMatch(mapping -> "fullName".equals(mapping.getProperty()) && "f_full_name".equals(mapping.getColumn())));
    }
 
    @Test
    void emptyOrNullIdsCannotQueryAllGroups() throws Exception {
        MappedStatement statement = statement();
        assertTrue(statement.getBoundSql(Collections.singletonMap("ids", Collections.emptyList())).getSql().contains("1 = 0"));
        assertTrue(statement.getBoundSql(Collections.singletonMap("ids", null)).getSql().contains("1 = 0"));
    }
}