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