View Javadoc
1   /*
2    *    Copyright 2009-2024 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.associationtype;
17  
18  import java.io.Reader;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.ibatis.BaseDataTest;
23  import org.apache.ibatis.io.Resources;
24  import org.apache.ibatis.session.LocalCacheScope;
25  import org.apache.ibatis.session.SqlSession;
26  import org.apache.ibatis.session.SqlSessionFactory;
27  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28  import org.junit.jupiter.api.Assertions;
29  import org.junit.jupiter.api.BeforeAll;
30  import org.junit.jupiter.params.ParameterizedTest;
31  import org.junit.jupiter.params.provider.EnumSource;
32  
33  class AssociationTypeTest {
34  
35    private static SqlSessionFactory sqlSessionFactory;
36  
37    @BeforeAll
38    static void setUp() throws Exception {
39      // create a SqlSessionFactory
40      try (Reader reader = Resources
41          .getResourceAsReader("org/apache/ibatis/submitted/associationtype/mybatis-config.xml")) {
42        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
43      }
44  
45      // populate in-memory database
46      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
47          "org/apache/ibatis/submitted/associationtype/CreateDB.sql");
48    }
49  
50    @ParameterizedTest
51    @EnumSource
52    void shouldGetAUser(LocalCacheScope localCacheScope) {
53      sqlSessionFactory.getConfiguration().setLocalCacheScope(localCacheScope);
54      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
55        List<Map<String, ?>> results = sqlSession.selectList("getUser");
56        for (Map<String, ?> r : results) {
57          Object a1 = r.get("a1");
58          Object a2 = r.get("a2");
59          Assertions.assertEquals(String.class, a1.getClass());
60          Assertions.assertEquals(String.class, a2.getClass());
61          Assertions.assertSame(a1, a2,
62              "The result should be put into local cache regardless of localCacheScope setting.");
63        }
64      } finally {
65        // Reset the scope for other tests
66        sqlSessionFactory.getConfiguration().setLocalCacheScope(LocalCacheScope.SESSION);
67      }
68    }
69  
70  }