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