View Javadoc
1   /*
2    *    Copyright 2009-2021 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.language;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  
20  import java.io.Reader;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.ibatis.BaseDataTest;
26  import org.apache.ibatis.io.Resources;
27  import org.apache.ibatis.session.SqlSession;
28  import org.apache.ibatis.session.SqlSessionFactory;
29  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30  import org.junit.jupiter.api.BeforeAll;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Just a test case. Not a real Velocity implementation.
35   */
36  class LanguageTest {
37  
38    protected static SqlSessionFactory sqlSessionFactory;
39  
40    @BeforeAll
41    static void setUp() throws Exception {
42      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/language/MapperConfig.xml")) {
43        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44      }
45  
46      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
47          "org/apache/ibatis/submitted/language/CreateDB.sql");
48    }
49  
50    @Test
51    void testDynamicSelectWithPropertyParams() {
52      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
53  
54        Parameter p = new Parameter(true, "Fli%");
55        List<Name> answer = sqlSession.selectList("selectNames", p);
56        assertEquals(3, answer.size());
57        for (Name n : answer) {
58          assertEquals("Flintstone", n.getLastName());
59        }
60  
61        p = new Parameter(false, "Fli%");
62        answer = sqlSession.selectList("selectNames", p);
63        assertEquals(3, answer.size());
64        for (Name n : answer) {
65          assertNull(n.getLastName());
66        }
67  
68        p = new Parameter(false, "Rub%");
69        answer = sqlSession.selectList("selectNames", p);
70        assertEquals(2, answer.size());
71        for (Name n : answer) {
72          assertNull(n.getLastName());
73        }
74      }
75    }
76  
77    @Test
78    void testDynamicSelectWithExpressionParams() {
79      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
80  
81        Parameter p = new Parameter(true, "Fli");
82        List<Name> answer = sqlSession.selectList("selectNamesWithExpressions", p);
83        assertEquals(3, answer.size());
84        for (Name n : answer) {
85          assertEquals("Flintstone", n.getLastName());
86        }
87  
88        p = new Parameter(false, "Fli");
89        answer = sqlSession.selectList("selectNamesWithExpressions", p);
90        assertEquals(3, answer.size());
91        for (Name n : answer) {
92          assertNull(n.getLastName());
93        }
94  
95        p = new Parameter(false, "Rub");
96        answer = sqlSession.selectList("selectNamesWithExpressions", p);
97        assertEquals(2, answer.size());
98        for (Name n : answer) {
99          assertNull(n.getLastName());
100       }
101     }
102   }
103 
104   @Test
105   void testDynamicSelectWithIteration() {
106     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
107 
108       int[] ids = { 2, 4, 5 };
109       Map<String, Object> param = new HashMap<>();
110       param.put("ids", ids);
111       List<Name> answer = sqlSession.selectList("selectNamesWithIteration", param);
112       assertEquals(3, answer.size());
113       for (int i = 0; i < ids.length; i++) {
114         assertEquals(ids[i], answer.get(i).getId());
115       }
116     }
117   }
118 
119   @Test
120   void testLangRaw() {
121     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
122       Parameter p = new Parameter(true, "Fli%");
123       List<Name> answer = sqlSession.selectList("selectRaw", p);
124       assertEquals(3, answer.size());
125       for (Name n : answer) {
126         assertEquals("Flintstone", n.getLastName());
127       }
128     }
129   }
130 
131   @Test
132   void testLangRawWithInclude() {
133     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
134       Parameter p = new Parameter(true, "Fli%");
135       List<Name> answer = sqlSession.selectList("selectRawWithInclude", p);
136       assertEquals(3, answer.size());
137       for (Name n : answer) {
138         assertEquals("Flintstone", n.getLastName());
139       }
140     }
141   }
142 
143   @Test
144   void testLangRawWithIncludeAndCData() {
145     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
146       Parameter p = new Parameter(true, "Fli%");
147       List<Name> answer = sqlSession.selectList("selectRawWithIncludeAndCData", p);
148       assertEquals(3, answer.size());
149       for (Name n : answer) {
150         assertEquals("Flintstone", n.getLastName());
151       }
152     }
153   }
154 
155   @Test
156   void testLangXmlTags() {
157     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
158       Parameter p = new Parameter(true, "Fli%");
159       List<Name> answer = sqlSession.selectList("selectXml", p);
160       assertEquals(3, answer.size());
161       for (Name n : answer) {
162         assertEquals("Flintstone", n.getLastName());
163       }
164     }
165   }
166 
167   @Test
168   void testLangRawWithMapper() {
169     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
170       Parameter p = new Parameter(true, "Fli%");
171       Mapper m = sqlSession.getMapper(Mapper.class);
172       List<Name> answer = m.selectRawWithMapper(p);
173       assertEquals(3, answer.size());
174       for (Name n : answer) {
175         assertEquals("Flintstone", n.getLastName());
176       }
177     }
178   }
179 
180   @Test
181   void testLangVelocityWithMapper() {
182     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
183       Parameter p = new Parameter(true, "Fli%");
184       Mapper m = sqlSession.getMapper(Mapper.class);
185       List<Name> answer = m.selectVelocityWithMapper(p);
186       assertEquals(3, answer.size());
187       for (Name n : answer) {
188         assertEquals("Flintstone", n.getLastName());
189       }
190     }
191   }
192 
193   @Test
194   void testLangXmlWithMapper() {
195     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
196       Parameter p = new Parameter(true, "Fli%");
197       Mapper m = sqlSession.getMapper(Mapper.class);
198       List<Name> answer = m.selectXmlWithMapper(p);
199       assertEquals(3, answer.size());
200       for (Name n : answer) {
201         assertEquals("Flintstone", n.getLastName());
202       }
203     }
204   }
205 
206   @Test
207   void testLangXmlWithMapperAndSqlSymbols() {
208     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
209       Parameter p = new Parameter(true, "Fli%");
210       Mapper m = sqlSession.getMapper(Mapper.class);
211       List<Name> answer = m.selectXmlWithMapperAndSqlSymbols(p);
212       assertEquals(3, answer.size());
213       for (Name n : answer) {
214         assertEquals("Flintstone", n.getLastName());
215       }
216     }
217   }
218 
219 }