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.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.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_gh1551/mybatis-config.xml")) {
37        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
38      }
39  
40      // populate in-memory database
41      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
42          "org/apache/ibatis/submitted/nestedresulthandler_gh1551/CreateDB.sql");
43    }
44  
45    @Test
46    void useColumnLabelIsTrue() {
47      sqlSessionFactory.getConfiguration().setUseColumnLabel(true);
48      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
49        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
50  
51        ProductResp productResp = mapper.selectAllInfo("P001").get(0);
52  
53        Assertions.assertEquals("10000000000000000000000000000001", productResp.getId());
54        Assertions.assertEquals("P001", productResp.getCode());
55        Assertions.assertEquals("Product 001", productResp.getName());
56  
57        Assertions.assertEquals(1, productResp.getProductInfo().getId());
58        Assertions.assertEquals("10000000000000000000000000000001", productResp.getProductInfo().getProductId());
59        Assertions.assertEquals("Sale 50% Off", productResp.getProductInfo().getOtherInfo());
60  
61        Assertions.assertEquals("20000000000000000000000000000001", productResp.getSkus().get(0).getId());
62        Assertions.assertEquals("10000000000000000000000000000001", productResp.getSkus().get(0).getProductId());
63        Assertions.assertEquals("red", productResp.getSkus().get(0).getColor());
64        Assertions.assertEquals("80", productResp.getSkus().get(0).getSize());
65        Assertions.assertEquals("20000000000000000000000000000002", productResp.getSkus().get(1).getId());
66        Assertions.assertEquals("10000000000000000000000000000001", productResp.getSkus().get(1).getProductId());
67        Assertions.assertEquals("blue", productResp.getSkus().get(1).getColor());
68        Assertions.assertEquals("10", productResp.getSkus().get(1).getSize());
69      }
70    }
71  
72    @Test
73    void useColumnLabelIsFalse() {
74      sqlSessionFactory.getConfiguration().setUseColumnLabel(false);
75      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
76        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
77        PersistenceException exception = Assertions.assertThrows(PersistenceException.class, () -> mapper.selectAllInfo("P001"));
78        Assertions.assertTrue(exception.getMessage().contains("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"));
79      }
80    }
81  
82  }