Skip to content

Commit 1b74be1

Browse files
committed
Add integration test for mybatis#1582
1 parent 2c430be commit 1b74be1

File tree

6 files changed

+296
-0
lines changed

6 files changed

+296
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- Copyright 2009-2019 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+
-- http://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+
17+
DROP TABLE IF EXISTS users;
18+
19+
CREATE TABLE users (
20+
user_id identity PRIMARY KEY,
21+
name character varying(30)
22+
);
23+
24+
INSERT INTO users (name) values ('Jimmy');
25+
INSERT INTO users (name) values ('Iwao');
26+
INSERT INTO users (name) values ('Kazuki');
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--
2+
-- Copyright 2009-2019 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+
-- http://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+
17+
DROP SCHEMA IF EXISTS mbtest;
18+
19+
CREATE SCHEMA mbtest;
20+
21+
CREATE TABLE mbtest.users (
22+
user_id serial PRIMARY KEY,
23+
name character varying(30)
24+
);
25+
26+
INSERT INTO mbtest.users (name) values ('Jimmy');
27+
INSERT INTO mbtest.users (name) values ('Iwao');
28+
INSERT INTO mbtest.users (name) values ('Kazuki');
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Copyright 2009-2019 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+
* http://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 java.util.List;
19+
import java.util.Properties;
20+
21+
import org.apache.ibatis.BaseDataTest;
22+
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
23+
import org.apache.ibatis.mapping.Environment;
24+
import org.apache.ibatis.session.Configuration;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
29+
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.Test;
31+
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
34+
class HsqldbSQLTest {
35+
36+
private static SqlSessionFactory sqlSessionFactory;
37+
38+
@BeforeAll
39+
static void setUp() throws Exception {
40+
Configuration configuration = new Configuration();
41+
Environment environment = new Environment("development", new JdbcTransactionFactory(),
42+
new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:HsqldbSQLTest", "sa", ""));
43+
configuration.setEnvironment(environment);
44+
configuration.setUseGeneratedKeys(true);
45+
configuration.addMapper(Mapper.class);
46+
Properties properties = new Properties();
47+
properties.setProperty("schema", "");
48+
configuration.setVariables(properties);
49+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
50+
51+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
52+
"org/apache/ibatis/submitted/sql/CreateDB-hsqldb.sql");
53+
}
54+
55+
@Test
56+
void testFetchFirst() {
57+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
58+
Mapper mapper = sqlSession.getMapper(Mapper.class);
59+
{
60+
List<User> users = mapper.findAll(0, 2);
61+
assertEquals(2, users.size());
62+
assertEquals("Jimmy", users.get(0).getName());
63+
assertEquals("Iwao", users.get(1).getName());
64+
}
65+
{
66+
List<User> users = mapper.findAll(1, 2);
67+
assertEquals(2, users.size());
68+
assertEquals("Iwao", users.get(0).getName());
69+
assertEquals("Kazuki", users.get(1).getName());
70+
}
71+
{
72+
List<User> users = mapper.findAll(2, 2);
73+
assertEquals(1, users.size());
74+
assertEquals("Kazuki", users.get(0).getName());
75+
}
76+
}
77+
}
78+
79+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright 2009-2019 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+
* http://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 java.util.List;
19+
20+
import org.apache.ibatis.annotations.Param;
21+
import org.apache.ibatis.annotations.SelectProvider;
22+
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
23+
import org.apache.ibatis.jdbc.SQL;
24+
25+
public interface Mapper {
26+
27+
@SelectProvider(type = SqlProvider.class)
28+
List<User> findAll(@Param("offset") long offset, @Param("limit") int limit);
29+
30+
class SqlProvider implements ProviderMethodResolver {
31+
public String findAll() {
32+
return new SQL()
33+
.SELECT("user_id", "name")
34+
.FROM("${schema}users")
35+
.ORDER_BY("user_id")
36+
.OFFSET_ROWS("#{offset}")
37+
.FETCH_FIRST_ROWS_ONLY("#{limit}")
38+
.toString();
39+
}
40+
}
41+
42+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright 2009-2019 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+
* http://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 java.util.List;
19+
import java.util.Properties;
20+
21+
import org.apache.ibatis.BaseDataTest;
22+
import org.apache.ibatis.mapping.Environment;
23+
import org.apache.ibatis.session.Configuration;
24+
import org.apache.ibatis.session.SqlSession;
25+
import org.apache.ibatis.session.SqlSessionFactory;
26+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27+
import org.apache.ibatis.testcontainers.PgContainer;
28+
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
29+
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.Tag;
31+
import org.junit.jupiter.api.Test;
32+
33+
import static org.junit.jupiter.api.Assertions.assertEquals;
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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2009-2019 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+
* http://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+
public class User {
19+
20+
private Integer userId;
21+
22+
private String name;
23+
24+
public Integer getUserId() {
25+
return userId;
26+
}
27+
28+
public void setUserId(Integer userId) {
29+
this.userId = userId;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
}

0 commit comments

Comments
 (0)