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.nestedresulthandler_gh1551;
17  
18  import java.io.Reader;
19  
20  import org.apache.ibatis.BaseDataTest;
21  import org.apache.ibatis.exceptions.PersistenceException;
22  import org.apache.ibatis.io.Resources;
23  import org.apache.ibatis.session.SqlSession;
24  import org.apache.ibatis.session.SqlSessionFactory;
25  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
26  import org.junit.jupiter.api.Assertions;
27  import org.junit.jupiter.api.BeforeAll;
28  import org.junit.jupiter.api.Test;
29  
30  class NestedResultHandlerGh1551Test {
31    private static SqlSessionFactory sqlSessionFactory;
32  
33    @BeforeAll
34    static void setUp() throws Exception {
35      // create a SqlSessionFactory
36      try (Reader reader = Resources
37          .getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_gh1551/mybatis-config.xml")) {
38        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39      }
40  
41      // populate in-memory database
42      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
43          "org/apache/ibatis/submitted/nestedresulthandler_gh1551/CreateDB.sql");
44    }
45  
46    @Test
47    void useColumnLabelIsTrue() {
48      sqlSessionFactory.getConfiguration().setUseColumnLabel(true);
49      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
50        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
51  
52        ProductResp productResp = mapper.selectAllInfo("P001").get(0);
53  
54        Assertions.assertEquals("10000000000000000000000000000001", productResp.getId());
55        Assertions.assertEquals("P001", productResp.getCode());
56        Assertions.assertEquals("Product 001", productResp.getName());
57  
58        Assertions.assertEquals(1, productResp.getProductInfo().getId());
59        Assertions.assertEquals("10000000000000000000000000000001", productResp.getProductInfo().getProductId());
60        Assertions.assertEquals("Sale 50% Off", productResp.getProductInfo().getOtherInfo());
61  
62        Assertions.assertEquals("20000000000000000000000000000001", productResp.getSkus().get(0).getId());
63        Assertions.assertEquals("10000000000000000000000000000001", productResp.getSkus().get(0).getProductId());
64        Assertions.assertEquals("red", productResp.getSkus().get(0).getColor());
65        Assertions.assertEquals("80", productResp.getSkus().get(0).getSize());
66        Assertions.assertEquals("20000000000000000000000000000002", productResp.getSkus().get(1).getId());
67        Assertions.assertEquals("10000000000000000000000000000001", productResp.getSkus().get(1).getProductId());
68        Assertions.assertEquals("blue", productResp.getSkus().get(1).getColor());
69        Assertions.assertEquals("10", productResp.getSkus().get(1).getSize());
70      }
71    }
72  
73    @Test
74    void useColumnLabelIsFalse() {
75      sqlSessionFactory.getConfiguration().setUseColumnLabel(false);
76      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
77        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
78        PersistenceException exception = Assertions.assertThrows(PersistenceException.class,
79            () -> mapper.selectAllInfo("P001"));
80        Assertions.assertTrue(exception.getMessage().contains(
81            "Error attempting to get column 'ID' from result set.  Cause: java.sql.SQLSyntaxErrorException: incompatible data type in conversion: from SQL type VARCHAR to java.lang.Integer, value: 10000000000000000000000000000001"));
82      }
83    }
84  
85  }