View Javadoc
1   /**
2    *    Copyright 2009-2019 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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  }