Skip to content

Commit 1ad92ef

Browse files
authored
Merge pull request #1520 from harawata/rewrite-pgtests
Replace postgresql-embedded with Testcontainers
2 parents c84f858 + 7d63ee0 commit 1ad92ef

File tree

14 files changed

+103
-137
lines changed

14 files changed

+103
-137
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ jdk:
44
- oraclejdk11
55
- oraclejdk8
66

7+
services:
8+
- docker
9+
710
cache:
811
directories:
912
- $HOME/.m2

pom.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132

133133
<properties>
134134
<clirr.comparisonVersion>3.4.6</clirr.comparisonVersion>
135-
<excludedGroups>EmbeddedPostgresqlTests</excludedGroups>
135+
<excludedGroups>TestcontainersTests</excludedGroups>
136136
<maven.compiler.testCompilerArgument>-parameters</maven.compiler.testCompilerArgument>
137137
<module.name>org.mybatis</module.name>
138138
<osgi.export>org.apache.ibatis.*;version=${project.version};-noimport:=true</osgi.export>
@@ -262,9 +262,15 @@
262262
<scope>test</scope>
263263
</dependency>
264264
<dependency>
265-
<groupId>ru.yandex.qatools.embed</groupId>
266-
<artifactId>postgresql-embedded</artifactId>
267-
<version>2.10</version>
265+
<groupId>org.testcontainers</groupId>
266+
<artifactId>junit-jupiter</artifactId>
267+
<version>1.11.2</version>
268+
<scope>test</scope>
269+
</dependency>
270+
<dependency>
271+
<groupId>org.testcontainers</groupId>
272+
<artifactId>postgresql</artifactId>
273+
<version>1.11.2</version>
268274
<scope>test</scope>
269275
</dependency>
270276
</dependencies>

src/test/java/org/apache/ibatis/submitted/cursor_simple/PostgresCursorTest.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,32 @@
1818
import static org.junit.jupiter.api.Assertions.*;
1919

2020
import java.io.IOException;
21-
import java.nio.file.Paths;
22-
import java.util.Collections;
2321
import java.util.Iterator;
2422

2523
import org.apache.ibatis.BaseDataTest;
2624
import org.apache.ibatis.cursor.Cursor;
27-
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
2825
import org.apache.ibatis.mapping.Environment;
2926
import org.apache.ibatis.session.Configuration;
3027
import org.apache.ibatis.session.ExecutorType;
3128
import org.apache.ibatis.session.SqlSession;
3229
import org.apache.ibatis.session.SqlSessionFactory;
3330
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31+
import org.apache.ibatis.testcontainers.PgContainer;
3432
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
35-
import org.junit.jupiter.api.AfterAll;
3633
import org.junit.jupiter.api.BeforeAll;
3734
import org.junit.jupiter.api.Tag;
3835
import org.junit.jupiter.api.Test;
3936

40-
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;
41-
import ru.yandex.qatools.embed.postgresql.util.SocketUtil;
42-
43-
@Tag("EmbeddedPostgresqlTests")
37+
@Tag("TestcontainersTests")
4438
class PostgresCursorTest {
4539

46-
private static final EmbeddedPostgres postgres = new EmbeddedPostgres();
47-
4840
private static SqlSessionFactory sqlSessionFactory;
4941

5042
@BeforeAll
5143
static void setUp() throws Exception {
52-
// Launch PostgreSQL server. Download / unarchive if necessary.
53-
String url = postgres.start(
54-
EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost",
55-
SocketUtil.findFreePort(), "cursor_simple", "postgres", "root", Collections.emptyList());
56-
5744
Configuration configuration = new Configuration();
58-
Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource(
59-
"org.postgresql.Driver", url, null));
45+
Environment environment = new Environment("development", new JdbcTransactionFactory(),
46+
PgContainer.getUnpooledDataSource());
6047
configuration.setEnvironment(environment);
6148
configuration.addMapper(Mapper.class);
6249
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
@@ -65,11 +52,6 @@ static void setUp() throws Exception {
6552
"org/apache/ibatis/submitted/cursor_simple/CreateDB.sql");
6653
}
6754

68-
@AfterAll
69-
static void tearDown() {
70-
postgres.stop();
71-
}
72-
7355
@Test
7456
void shouldBeAbleToReuseStatement() throws IOException {
7557
// #1351

src/test/java/org/apache/ibatis/submitted/keycolumn/CreateDB.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2009-2018 the original author or authors.
2+
-- Copyright 2009-2019 the original author or authors.
33
--
44
-- Licensed under the Apache License, Version 2.0 (the "License");
55
-- you may not use this file except in compliance with the License.
@@ -14,6 +14,8 @@
1414
-- limitations under the License.
1515
--
1616

17+
DROP SCHEMA IF EXISTS mbtest;
18+
1719
CREATE SCHEMA mbtest;
1820

1921
CREATE TABLE mbtest.test_identity

src/test/java/org/apache/ibatis/submitted/keycolumn/InsertTest.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,37 @@
1515
*/
1616
package org.apache.ibatis.submitted.keycolumn;
1717

18-
import static org.junit.jupiter.api.Assertions.assertEquals;
19-
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
import static org.junit.jupiter.api.Assertions.*;
2019

21-
import java.nio.file.Paths;
22-
import java.util.Collections;
2320
import java.util.List;
2421

2522
import org.apache.ibatis.BaseDataTest;
26-
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
2723
import org.apache.ibatis.executor.BatchResult;
2824
import org.apache.ibatis.mapping.Environment;
2925
import org.apache.ibatis.session.Configuration;
3026
import org.apache.ibatis.session.ExecutorType;
3127
import org.apache.ibatis.session.SqlSession;
3228
import org.apache.ibatis.session.SqlSessionFactory;
3329
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30+
import org.apache.ibatis.testcontainers.PgContainer;
3431
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
35-
import org.junit.jupiter.api.AfterAll;
3632
import org.junit.jupiter.api.BeforeAll;
3733
import org.junit.jupiter.api.Tag;
3834
import org.junit.jupiter.api.Test;
3935

40-
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;
41-
import ru.yandex.qatools.embed.postgresql.util.SocketUtil;
42-
4336
/**
4437
* @author Jeff Butler
4538
*/
46-
@Tag("EmbeddedPostgresqlTests")
39+
@Tag("TestcontainersTests")
4740
class InsertTest {
4841

49-
private static final EmbeddedPostgres postgres = new EmbeddedPostgres();
50-
5142
private static SqlSessionFactory sqlSessionFactory;
5243

5344
@BeforeAll
5445
static void setUp() throws Exception {
55-
// Launch PostgreSQL server. Download / unarchive if necessary.
56-
String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", SocketUtil.findFreePort(), "keycolumn", "postgres", "root", Collections.emptyList());
57-
5846
Configuration configuration = new Configuration();
59-
Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource(
60-
"org.postgresql.Driver", url, null));
47+
Environment environment = new Environment("development", new JdbcTransactionFactory(),
48+
PgContainer.getUnpooledDataSource());
6149
configuration.setEnvironment(environment);
6250
configuration.addMapper(InsertMapper.class);
6351
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
@@ -66,11 +54,6 @@ static void setUp() throws Exception {
6654
"org/apache/ibatis/submitted/keycolumn/CreateDB.sql");
6755
}
6856

69-
@AfterAll
70-
static void tearDown() {
71-
postgres.stop();
72-
}
73-
7457
@Test
7558
void testInsertAnnotated() {
7659
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

src/test/java/org/apache/ibatis/submitted/multiple_resultsets/CreateDB.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2009-2018 the original author or authors.
2+
-- Copyright 2009-2019 the original author or authors.
33
--
44
-- Licensed under the Apache License, Version 2.0 (the "License");
55
-- you may not use this file except in compliance with the License.
@@ -14,6 +14,8 @@
1414
-- limitations under the License.
1515
--
1616

17+
DROP SCHEMA IF EXISTS mbtest;
18+
1719
CREATE SCHEMA mbtest;
1820

1921
CREATE TABLE mbtest.order_detail

src/test/java/org/apache/ibatis/submitted/multiple_resultsets/MultipleResultTest.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,35 @@
1616
package org.apache.ibatis.submitted.multiple_resultsets;
1717

1818
import java.io.IOException;
19-
import java.nio.file.Paths;
20-
import java.util.Collections;
2119
import java.util.List;
2220

2321
import org.apache.ibatis.BaseDataTest;
24-
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
2522
import org.apache.ibatis.mapping.Environment;
2623
import org.apache.ibatis.session.Configuration;
2724
import org.apache.ibatis.session.SqlSession;
2825
import org.apache.ibatis.session.SqlSessionFactory;
2926
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27+
import org.apache.ibatis.testcontainers.PgContainer;
3028
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
31-
import org.junit.jupiter.api.AfterAll;
3229
import org.junit.jupiter.api.Assertions;
3330
import org.junit.jupiter.api.BeforeAll;
3431
import org.junit.jupiter.api.Tag;
3532
import org.junit.jupiter.api.Test;
3633

37-
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;
38-
import ru.yandex.qatools.embed.postgresql.util.SocketUtil;
39-
4034
/*
4135
* This class contains tests for multiple results.
4236
* It is based on Jeff's ref cursor tests.
4337
*/
44-
@Tag("EmbeddedPostgresqlTests")
38+
@Tag("TestcontainersTests")
4539
class MultipleResultTest {
4640

47-
private static final EmbeddedPostgres postgres = new EmbeddedPostgres();
48-
4941
private static SqlSessionFactory sqlSessionFactory;
5042

5143
@BeforeAll
5244
static void setUp() throws Exception {
53-
// Launch PostgreSQL server. Download / unarchive if necessary.
54-
String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", SocketUtil.findFreePort(), "multiple_resultsets", "postgres", "root", Collections.emptyList());
55-
5645
Configuration configuration = new Configuration();
57-
Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource(
58-
"org.postgresql.Driver", url, null));
46+
Environment environment = new Environment("development", new JdbcTransactionFactory(),
47+
PgContainer.getUnpooledDataSource());
5948
configuration.setEnvironment(environment);
6049
configuration.setMapUnderscoreToCamelCase(true);
6150
configuration.addMapper(Mapper.class);
@@ -65,11 +54,6 @@ static void setUp() throws Exception {
6554
"org/apache/ibatis/submitted/multiple_resultsets/CreateDB.sql");
6655
}
6756

68-
@AfterAll
69-
static void tearDown() {
70-
postgres.stop();
71-
}
72-
7357
@Test
7458
void shouldGetMultipleResultSetsWithOneStatement() throws IOException {
7559
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

src/test/java/org/apache/ibatis/submitted/postgres_genkeys/CreateDB.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2009-2018 the original author or authors.
2+
-- Copyright 2009-2019 the original author or authors.
33
--
44
-- Licensed under the Apache License, Version 2.0 (the "License");
55
-- you may not use this file except in compliance with the License.
@@ -14,6 +14,8 @@
1414
-- limitations under the License.
1515
--
1616

17+
DROP SCHEMA IF EXISTS mbtest;
18+
1719
CREATE SCHEMA mbtest;
1820

1921
CREATE TABLE mbtest.users (

src/test/java/org/apache/ibatis/submitted/postgres_genkeys/PostgresGeneratedKeysTest.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,28 @@
1717

1818
import static org.junit.jupiter.api.Assertions.*;
1919

20-
import java.nio.file.Paths;
21-
import java.util.Collections;
22-
2320
import org.apache.ibatis.BaseDataTest;
24-
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
2521
import org.apache.ibatis.mapping.Environment;
2622
import org.apache.ibatis.session.Configuration;
2723
import org.apache.ibatis.session.SqlSession;
2824
import org.apache.ibatis.session.SqlSessionFactory;
2925
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
26+
import org.apache.ibatis.testcontainers.PgContainer;
3027
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
31-
import org.junit.jupiter.api.AfterAll;
3228
import org.junit.jupiter.api.BeforeAll;
3329
import org.junit.jupiter.api.Tag;
3430
import org.junit.jupiter.api.Test;
3531

36-
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;
37-
import ru.yandex.qatools.embed.postgresql.util.SocketUtil;
38-
39-
@Tag("EmbeddedPostgresqlTests")
32+
@Tag("TestcontainersTests")
4033
class PostgresGeneratedKeysTest {
4134

42-
private static final EmbeddedPostgres postgres = new EmbeddedPostgres();
43-
4435
private static SqlSessionFactory sqlSessionFactory;
4536

4637
@BeforeAll
4738
static void setUp() throws Exception {
48-
// Launch PostgreSQL server. Download / unarchive if necessary.
49-
String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost", SocketUtil.findFreePort(), "postgres_genkeys", "postgres", "root", Collections.emptyList());
50-
5139
Configuration configuration = new Configuration();
52-
Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource(
53-
"org.postgresql.Driver", url, null));
40+
Environment environment = new Environment("development", new JdbcTransactionFactory(),
41+
PgContainer.getUnpooledDataSource());
5442
configuration.setEnvironment(environment);
5543
configuration.setUseGeneratedKeys(true);
5644
configuration.addMapper(Mapper.class);
@@ -60,11 +48,6 @@ static void setUp() throws Exception {
6048
"org/apache/ibatis/submitted/postgres_genkeys/CreateDB.sql");
6149
}
6250

63-
@AfterAll
64-
static void tearDown() {
65-
postgres.stop();
66-
}
67-
6851
@Test
6952
void testInsertIntoTableWithNoSerialColumn() {
7053
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

src/test/java/org/apache/ibatis/submitted/refcursor/CreateDB.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2009-2018 the original author or authors.
2+
-- Copyright 2009-2019 the original author or authors.
33
--
44
-- Licensed under the Apache License, Version 2.0 (the "License");
55
-- you may not use this file except in compliance with the License.
@@ -14,6 +14,8 @@
1414
-- limitations under the License.
1515
--
1616

17+
DROP SCHEMA IF EXISTS mbtest;
18+
1719
CREATE SCHEMA mbtest;
1820

1921
CREATE TABLE mbtest.order_detail

0 commit comments

Comments
 (0)