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