1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.ognl_enum;
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.SqlSession;
24 import org.apache.ibatis.session.SqlSessionFactory;
25 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
26 import org.apache.ibatis.submitted.ognl_enum.Person.Type;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
30
31 class EnumWithOgnlTest {
32
33 private static SqlSessionFactory sqlSessionFactory;
34
35 @BeforeAll
36 static void initDatabase() throws Exception {
37 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/ognl_enum/ibatisConfig.xml")) {
38 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39 }
40
41 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
42 "org/apache/ibatis/submitted/ognl_enum/CreateDB.sql");
43 }
44
45 @Test
46 void testEnumWithOgnl() {
47 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
48 PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
49 List<Person> persons = personMapper.selectAllByType(null);
50 Assertions.assertEquals(3, persons.size(), "Persons must contain 3 persons");
51 }
52 }
53
54 @Test
55 void testEnumWithOgnlDirector() {
56 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
57 PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
58 List<Person> persons = personMapper.selectAllByType(Person.Type.DIRECTOR);
59 Assertions.assertEquals(1, persons.size(), "Persons must contain 1 persons");
60 }
61 }
62
63 @Test
64 void testEnumWithOgnlDirectorNameAttribute() {
65 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
66 PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
67 List<Person> persons = personMapper.selectAllByTypeNameAttribute(Person.Type.DIRECTOR);
68 Assertions.assertEquals(1, persons.size(), "Persons must contain 1 persons");
69 }
70 }
71
72 @Test
73 void testEnumWithOgnlDirectorWithInterface() {
74 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
75 PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
76 List<Person> persons = personMapper.selectAllByTypeWithInterface(() -> Type.DIRECTOR);
77 Assertions.assertEquals(1, persons.size(), "Persons must contain 1 persons");
78 }
79 }
80 @Test
81 void testEnumWithOgnlDirectorNameAttributeWithInterface() {
82 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
83 PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
84 List<Person> persons = personMapper.selectAllByTypeNameAttributeWithInterface(() -> Type.DIRECTOR);
85 Assertions.assertEquals(1, persons.size(), "Persons must contain 1 persons");
86 }
87 }
88 }