View Javadoc
1   /*
2    *    Copyright 2009-2022 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.javassist;
17  
18  import static org.junit.jupiter.api.Assertions.assertTrue;
19  
20  import java.io.Reader;
21  
22  import javassist.util.proxy.Proxy;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.io.Resources;
26  import org.apache.ibatis.session.SqlSession;
27  import org.apache.ibatis.session.SqlSessionFactory;
28  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29  import org.junit.jupiter.api.Assertions;
30  import org.junit.jupiter.api.BeforeAll;
31  import org.junit.jupiter.api.Test;
32  
33  class JavassistTest {
34  
35    private static SqlSessionFactory sqlSessionFactory;
36  
37    @BeforeAll
38    static void setUp() throws Exception {
39      // create a SqlSessionFactory
40      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/javassist/mybatis-config.xml")) {
41        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42      }
43  
44      // populate in-memory database
45      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46          "org/apache/ibatis/submitted/javassist/CreateDB.sql");
47    }
48  
49    @Test
50    void shouldGetAUserAndGroups() {
51      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52        Mapper mapper = sqlSession.getMapper(Mapper.class);
53        User user = mapper.getUser(1);
54        Assertions.assertEquals("User1", user.getName());
55        assertTrue(user instanceof Proxy);
56        Assertions.assertEquals(1, user.getGroups().size());
57      }
58    }
59  
60  }