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.mapping;
17  
18  import static org.junit.jupiter.api.Assertions.assertFalse;
19  import static org.junit.jupiter.api.Assertions.assertTrue;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.ibatis.session.Configuration;
27  import org.junit.jupiter.api.Test;
28  
29  class BoundSqlTest {
30  
31    @Test
32    void testHasAdditionalParameter() {
33      List<ParameterMapping> params = Collections.emptyList();
34      BoundSql boundSql = new BoundSql(new Configuration(), "some sql", params, new Object());
35  
36      Map<String, String> map = new HashMap<>();
37      map.put("key1", "value1");
38      boundSql.setAdditionalParameter("map", map);
39  
40      Person bean = new Person();
41      bean.id = 1;
42      boundSql.setAdditionalParameter("person", bean);
43  
44      String[] array = { "User1", "User2" };
45      boundSql.setAdditionalParameter("array", array);
46  
47      assertFalse(boundSql.hasAdditionalParameter("pet"));
48      assertFalse(boundSql.hasAdditionalParameter("pet.name"));
49  
50      assertTrue(boundSql.hasAdditionalParameter("map"));
51      assertTrue(boundSql.hasAdditionalParameter("map.key1"));
52      assertTrue(boundSql.hasAdditionalParameter("map.key2"),
53          "should return true even if the child property does not exists.");
54  
55      assertTrue(boundSql.hasAdditionalParameter("person"));
56      assertTrue(boundSql.hasAdditionalParameter("person.id"));
57      assertTrue(boundSql.hasAdditionalParameter("person.name"),
58          "should return true even if the child property does not exists.");
59  
60      assertTrue(boundSql.hasAdditionalParameter("array[0]"));
61      assertTrue(boundSql.hasAdditionalParameter("array[99]"), "should return true even if the element does not exists.");
62    }
63  
64    public static class Person {
65      public Integer id;
66    }
67  
68  }