1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_gh1551/mybatis-config.xml")) {
37 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
38 }
39
40
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 }