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
37 .getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_gh1551/mybatis-config.xml")) {
38 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39 }
40
41
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 }