Skip to content

Commit ff2a303

Browse files
committed
Reuse PostgreSQL container without restarting
We need to drop schema/table at the beginning of each init SQL script to avoid interference between tests.
1 parent 0fd6c52 commit ff2a303

File tree

12 files changed

+73
-64
lines changed

12 files changed

+73
-64
lines changed

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,23 @@
2929
import org.apache.ibatis.session.SqlSession;
3030
import org.apache.ibatis.session.SqlSessionFactory;
3131
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
32+
import org.apache.ibatis.testcontainers.PgContainer;
3233
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
3334
import org.junit.jupiter.api.BeforeAll;
3435
import org.junit.jupiter.api.Tag;
3536
import org.junit.jupiter.api.Test;
36-
import org.testcontainers.containers.PostgreSQLContainer;
37-
import org.testcontainers.junit.jupiter.Container;
38-
import org.testcontainers.junit.jupiter.Testcontainers;
3937

4038
@Tag("TestcontainersTests")
41-
@Testcontainers
4239
class PostgresCursorTest {
4340

44-
@Container
45-
private static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>().withDatabaseName("cursor_simple")
46-
.withUsername("u").withPassword("p");
47-
4841
private static SqlSessionFactory sqlSessionFactory;
4942

5043
@BeforeAll
5144
static void setUp() throws Exception {
52-
String url = postgres.getJdbcUrl();
45+
String url = PgContainer.INSTANCE.getJdbcUrl();
5346
Configuration configuration = new Configuration();
5447
Environment environment = new Environment("development", new JdbcTransactionFactory(),
55-
new UnpooledDataSource("org.postgresql.Driver", url, "u", "p"));
48+
new UnpooledDataSource(PgContainer.DRIVER, url, PgContainer.USER, PgContainer.PW));
5649
configuration.setEnvironment(environment);
5750
configuration.addMapper(Mapper.class);
5851
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

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: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,26 @@
2828
import org.apache.ibatis.session.SqlSession;
2929
import org.apache.ibatis.session.SqlSessionFactory;
3030
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31+
import org.apache.ibatis.testcontainers.PgContainer;
3132
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
3233
import org.junit.jupiter.api.BeforeAll;
3334
import org.junit.jupiter.api.Tag;
3435
import org.junit.jupiter.api.Test;
35-
import org.testcontainers.containers.PostgreSQLContainer;
36-
import org.testcontainers.junit.jupiter.Container;
37-
import org.testcontainers.junit.jupiter.Testcontainers;
3836

3937
/**
4038
* @author Jeff Butler
4139
*/
4240
@Tag("TestcontainersTests")
43-
@Testcontainers
4441
class InsertTest {
4542

46-
@Container
47-
private static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>().withDatabaseName("keycolumn")
48-
.withUsername("u").withPassword("p");
49-
5043
private static SqlSessionFactory sqlSessionFactory;
5144

5245
@BeforeAll
5346
static void setUp() throws Exception {
54-
String url = postgres.getJdbcUrl();
47+
String url = PgContainer.INSTANCE.getJdbcUrl();
5548
Configuration configuration = new Configuration();
5649
Environment environment = new Environment("development", new JdbcTransactionFactory(),
57-
new UnpooledDataSource("org.postgresql.Driver", url, "u", "p"));
50+
new UnpooledDataSource(PgContainer.DRIVER, url, PgContainer.USER, PgContainer.PW));
5851
configuration.setEnvironment(environment);
5952
configuration.addMapper(InsertMapper.class);
6053
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

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: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,28 @@
2525
import org.apache.ibatis.session.SqlSession;
2626
import org.apache.ibatis.session.SqlSessionFactory;
2727
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.apache.ibatis.testcontainers.PgContainer;
2829
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
2930
import org.junit.jupiter.api.Assertions;
3031
import org.junit.jupiter.api.BeforeAll;
3132
import org.junit.jupiter.api.Tag;
3233
import org.junit.jupiter.api.Test;
33-
import org.testcontainers.containers.PostgreSQLContainer;
34-
import org.testcontainers.junit.jupiter.Container;
35-
import org.testcontainers.junit.jupiter.Testcontainers;
3634

3735
/*
3836
* This class contains tests for multiple results.
3937
* It is based on Jeff's ref cursor tests.
4038
*/
4139
@Tag("TestcontainersTests")
42-
@Testcontainers
4340
class MultipleResultTest {
4441

45-
@Container
46-
private static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>().withDatabaseName("multiple_resultsets")
47-
.withUsername("u").withPassword("p");
48-
4942
private static SqlSessionFactory sqlSessionFactory;
5043

5144
@BeforeAll
5245
static void setUp() throws Exception {
53-
String url = postgres.getJdbcUrl();
46+
String url = PgContainer.INSTANCE.getJdbcUrl();
5447
Configuration configuration = new Configuration();
5548
Environment environment = new Environment("development", new JdbcTransactionFactory(),
56-
new UnpooledDataSource("org.postgresql.Driver", url, "u", "p"));
49+
new UnpooledDataSource(PgContainer.DRIVER, url, PgContainer.USER, PgContainer.PW));
5750
configuration.setEnvironment(environment);
5851
configuration.setMapUnderscoreToCamelCase(true);
5952
configuration.addMapper(Mapper.class);

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: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,23 @@
2424
import org.apache.ibatis.session.SqlSession;
2525
import org.apache.ibatis.session.SqlSessionFactory;
2626
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27+
import org.apache.ibatis.testcontainers.PgContainer;
2728
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
2829
import org.junit.jupiter.api.BeforeAll;
2930
import org.junit.jupiter.api.Tag;
3031
import org.junit.jupiter.api.Test;
31-
import org.testcontainers.containers.PostgreSQLContainer;
32-
import org.testcontainers.junit.jupiter.Container;
33-
import org.testcontainers.junit.jupiter.Testcontainers;
3432

3533
@Tag("TestcontainersTests")
36-
@Testcontainers
3734
class PostgresGeneratedKeysTest {
3835

39-
@Container
40-
private static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>().withDatabaseName("postgres_genkeys")
41-
.withUsername("u").withPassword("p");
42-
4336
private static SqlSessionFactory sqlSessionFactory;
4437

4538
@BeforeAll
4639
static void setUp() throws Exception {
47-
String url = postgres.getJdbcUrl();
40+
String url = PgContainer.INSTANCE.getJdbcUrl();
4841
Configuration configuration = new Configuration();
4942
Environment environment = new Environment("development", new JdbcTransactionFactory(),
50-
new UnpooledDataSource("org.postgresql.Driver", url, "u", "p"));
43+
new UnpooledDataSource(PgContainer.DRIVER, url, PgContainer.USER, PgContainer.PW));
5144
configuration.setEnvironment(environment);
5245
configuration.setUseGeneratedKeys(true);
5346
configuration.addMapper(Mapper.class);

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

src/test/java/org/apache/ibatis/submitted/refcursor/RefCursorTest.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,26 @@
3131
import org.apache.ibatis.session.SqlSession;
3232
import org.apache.ibatis.session.SqlSessionFactory;
3333
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
34+
import org.apache.ibatis.testcontainers.PgContainer;
3435
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
3536
import org.junit.jupiter.api.BeforeAll;
3637
import org.junit.jupiter.api.Tag;
3738
import org.junit.jupiter.api.Test;
38-
import org.testcontainers.containers.PostgreSQLContainer;
39-
import org.testcontainers.junit.jupiter.Container;
40-
import org.testcontainers.junit.jupiter.Testcontainers;
4139

4240
/**
4341
* @author Jeff Butler
4442
*/
4543
@Tag("TestcontainersTests")
46-
@Testcontainers
4744
class RefCursorTest {
4845

49-
@Container
50-
private static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>().withDatabaseName("refcursor")
51-
.withUsername("u").withPassword("p");
52-
5346
private static SqlSessionFactory sqlSessionFactory;
5447

5548
@BeforeAll
5649
static void setUp() throws Exception {
57-
String url = postgres.getJdbcUrl();
50+
String url = PgContainer.INSTANCE.getJdbcUrl();
5851
Configuration configuration = new Configuration();
5952
Environment environment = new Environment("development", new JdbcTransactionFactory(),
60-
new UnpooledDataSource("org.postgresql.Driver", url, "u", "p"));
53+
new UnpooledDataSource(PgContainer.DRIVER, url, PgContainer.USER, PgContainer.PW));
6154
configuration.setEnvironment(environment);
6255
configuration.addMapper(OrdersMapper.class);
6356
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
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+
17+
package org.apache.ibatis.testcontainers;
18+
19+
import org.testcontainers.containers.PostgreSQLContainer;
20+
21+
public class PgContainer {
22+
23+
public static String USER = "u";
24+
public static String PW = "p";
25+
public static String DRIVER = "org.postgresql.Driver";
26+
27+
public static final PostgreSQLContainer<?> INSTANCE = initContainer();
28+
29+
private static PostgreSQLContainer<?> initContainer() {
30+
@SuppressWarnings("resource")
31+
PostgreSQLContainer<?> container = new PostgreSQLContainer<>().withDatabaseName("mybatis_test").withUsername(USER)
32+
.withPassword(PW);
33+
container.start();
34+
return container;
35+
}
36+
37+
private PgContainer() {
38+
super();
39+
}
40+
}

0 commit comments

Comments
 (0)