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.refcursor;
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.assertNull;
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.ibatis.BaseDataTest;
28  import org.apache.ibatis.mapping.Environment;
29  import org.apache.ibatis.session.Configuration;
30  import org.apache.ibatis.session.ResultContext;
31  import org.apache.ibatis.session.ResultHandler;
32  import org.apache.ibatis.session.SqlSession;
33  import org.apache.ibatis.session.SqlSessionFactory;
34  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
35  import org.apache.ibatis.testcontainers.PgContainer;
36  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
37  import org.junit.jupiter.api.BeforeAll;
38  import org.junit.jupiter.api.Tag;
39  import org.junit.jupiter.api.Test;
40  
41  /**
42   * @author Jeff Butler
43   */
44  @Tag("TestcontainersTests")
45  class RefCursorTest {
46  
47    private static SqlSessionFactory sqlSessionFactory;
48  
49    @BeforeAll
50    static void setUp() throws Exception {
51      Configuration configuration = new Configuration();
52      Environment environment = new Environment("development", new JdbcTransactionFactory(),
53          PgContainer.getUnpooledDataSource());
54      configuration.setEnvironment(environment);
55      configuration.addMapper(OrdersMapper.class);
56      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
57  
58      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
59          "org/apache/ibatis/submitted/refcursor/CreateDB.sql");
60    }
61  
62    @Test
63    void testRefCursor1() {
64      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
65        OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
66        Map<String, Object> parameter = new HashMap<>();
67        parameter.put("orderId", 1);
68        mapper.getOrder1(parameter);
69  
70        assertNotNull(parameter.get("order"));
71        @SuppressWarnings("unchecked")
72        List<Order> orders = (List<Order>) parameter.get("order");
73        assertEquals(1, orders.size());
74        Order order = orders.get(0);
75        assertEquals(3, order.getDetailLines().size());
76      }
77    }
78  
79    @Test
80    void testRefCursor2() {
81      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
82        OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
83        Map<String, Object> parameter = new HashMap<>();
84        parameter.put("orderId", 1);
85        mapper.getOrder2(parameter);
86  
87        assertNotNull(parameter.get("order"));
88        @SuppressWarnings("unchecked")
89        List<Order> orders = (List<Order>) parameter.get("order");
90        assertEquals(1, orders.size());
91        Order order = orders.get(0);
92        assertEquals(3, order.getDetailLines().size());
93      }
94    }
95  
96    @Test
97    void shouldUseResultHandlerOnOutputParam() {
98      class OrderResultHandler implements ResultHandler<Order> {
99        private List<Order> orders = new ArrayList<>();
100 
101       @Override
102       public void handleResult(ResultContext<? extends Order> resultContext) {
103         Order order = resultContext.getResultObject();
104         order.setCustomerName("Anonymous");
105         orders.add(order);
106       }
107 
108       List<Order> getResult() {
109         return orders;
110       }
111     }
112 
113     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
114       OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
115       OrderResultHandler handler = new OrderResultHandler();
116       Map<String, Object> parameter = new HashMap<>();
117       parameter.put("orderId", 1);
118       mapper.getOrder3(parameter, handler);
119 
120       assertNull(parameter.get("order"));
121       assertEquals(3, parameter.get("detailCount"));
122       assertEquals("Anonymous", handler.getResult().get(0).getCustomerName());
123     }
124   }
125 
126   @Test
127   void shouldNullResultSetNotCauseNpe() {
128     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
129       OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
130       Map<String, Object> parameter = new HashMap<>();
131       parameter.put("orderId", 99);
132       mapper.getOrder3(parameter, resultContext -> {
133         // won't be used
134       });
135       assertEquals(0, parameter.get("detailCount"));
136     }
137   }
138 }