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.sql;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.util.List;
21  import java.util.Properties;
22  
23  import org.apache.ibatis.BaseDataTest;
24  import org.apache.ibatis.mapping.Environment;
25  import org.apache.ibatis.session.Configuration;
26  import org.apache.ibatis.session.SqlSession;
27  import org.apache.ibatis.session.SqlSessionFactory;
28  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29  import org.apache.ibatis.testcontainers.PgContainer;
30  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.Tag;
33  import org.junit.jupiter.api.Test;
34  
35  @Tag("TestcontainersTests")
36  class PostgresSQLTest {
37  
38    private static SqlSessionFactory sqlSessionFactory;
39  
40    @BeforeAll
41    static void setUp() throws Exception {
42      Configuration configuration = new Configuration();
43      Environment environment = new Environment("development", new JdbcTransactionFactory(),
44          PgContainer.getUnpooledDataSource());
45      configuration.setEnvironment(environment);
46      configuration.setUseGeneratedKeys(true);
47      configuration.addMapper(Mapper.class);
48      Properties properties = new Properties();
49      properties.setProperty("schema", "mbtest.");
50      configuration.setVariables(properties);
51      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
52  
53      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
54          "org/apache/ibatis/submitted/sql/CreateDB.sql");
55    }
56  
57    @Test
58    void testFetchFirst() {
59      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
60        Mapper mapper = sqlSession.getMapper(Mapper.class);
61        {
62          List<User> users = mapper.findAll(0, 2);
63          assertEquals(2, users.size());
64          assertEquals("Jimmy", users.get(0).getName());
65          assertEquals("Iwao", users.get(1).getName());
66        }
67        {
68          List<User> users = mapper.findAll(1, 2);
69          assertEquals(2, users.size());
70          assertEquals("Iwao", users.get(0).getName());
71          assertEquals("Kazuki", users.get(1).getName());
72        }
73        {
74          List<User> users = mapper.findAll(2, 2);
75          assertEquals(1, users.size());
76          assertEquals("Kazuki", users.get(0).getName());
77        }
78      }
79    }
80  
81  }