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.orphan_result_maps;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  
20  import org.apache.ibatis.session.Configuration;
21  import org.junit.jupiter.api.Test;
22  
23  class OrphanResultMapTest {
24  
25    private static String RESULT_MAP_BLOG = "BlogResultMap";
26    private static String RESULT_MAP_POST = "PostResultMap";
27    private static String RESULT_MAP_INNER = "mapper_resultMap[BlogResultMap]_collection[posts]";
28  
29    @Test
30    void testSeparateResultMaps() {
31      // given
32      Configuration configuration = new Configuration();
33      configuration.getTypeAliasRegistry().registerAlias(Blog.class);
34      configuration.getTypeAliasRegistry().registerAlias(Post.class);
35      configuration.addMapper(SeparateCollectionMapper.class);
36  
37      // there should be two result maps declared, with two name variants each
38      assertEquals(4, configuration.getResultMaps().size());
39  
40      // test short names
41      assertNotNull(configuration.getResultMap(RESULT_MAP_BLOG));
42      assertNotNull(configuration.getResultMap(RESULT_MAP_POST));
43      assertThrows(IllegalArgumentException.class, () -> configuration.getResultMap(RESULT_MAP_INNER));
44  
45      // test long names
46      String prefix = SeparateCollectionMapper.class.getName() + ".";
47      assertNotNull(configuration.getResultMap(prefix + RESULT_MAP_BLOG));
48      assertNotNull(configuration.getResultMap(prefix + RESULT_MAP_POST));
49      assertThrows(IllegalArgumentException.class, () -> configuration.getResultMap(prefix + RESULT_MAP_INNER));
50    }
51  
52    @Test
53    void testNestedResultMap() {
54      // given
55      Configuration configuration = new Configuration();
56      configuration.getTypeAliasRegistry().registerAlias(Blog.class);
57      configuration.getTypeAliasRegistry().registerAlias(Post.class);
58      configuration.addMapper(NestedCollectionMapper.class);
59  
60      // there should be two result maps declared, with two name variants each
61      assertEquals(4, configuration.getResultMaps().size());
62  
63      // test short names
64      assertNotNull(configuration.getResultMap(RESULT_MAP_BLOG));
65      assertNotNull(configuration.getResultMap(RESULT_MAP_INNER));
66  
67      // test long names
68      String prefix = NestedCollectionMapper.class.getName() + ".";
69      assertNotNull(configuration.getResultMap(prefix + RESULT_MAP_BLOG));
70      assertNotNull(configuration.getResultMap(prefix + RESULT_MAP_INNER));
71    }
72  
73  }