Skip to content

Commit 0fe791f

Browse files
sedooeschauder
sedooe
authored andcommitted
DATAJDBC-168 - Run integration tests with testcontainers.
Added testcontainers as dependency and configured MySql and Postrgres datasources to use it. Original pull request: #34.
1 parent 0fdd885 commit 0fe791f

File tree

8 files changed

+106
-33
lines changed

8 files changed

+106
-33
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@
183183
<scope>test</scope>
184184
</dependency>
185185

186+
<dependency>
187+
<groupId>org.testcontainers</groupId>
188+
<artifactId>mysql</artifactId>
189+
<version>1.5.1</version>
190+
<scope>test</scope>
191+
</dependency>
192+
193+
<dependency>
194+
<groupId>org.testcontainers</groupId>
195+
<artifactId>postgresql</artifactId>
196+
<version>1.5.1</version>
197+
<scope>test</scope>
198+
</dependency>
199+
186200
</dependencies>
187201

188202
<build>

run-tests-against-all-dbs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/sh
22

3-
./start-all-dbs.sh && mvn clean install -Pall-dbs && ./stop-all-dbs.sh
3+
mvn clean install -Pall-dbs

src/test/java/org/springframework/data/jdbc/testing/MySqlDataSourceConfiguration.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
*/
1616
package org.springframework.data.jdbc.testing;
1717

18+
import java.sql.SQLException;
19+
1820
import javax.annotation.PostConstruct;
21+
import javax.script.ScriptException;
1922
import javax.sql.DataSource;
2023

2124
import org.springframework.context.annotation.Configuration;
2225
import org.springframework.context.annotation.Profile;
23-
import org.springframework.core.io.ClassPathResource;
24-
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
25-
import org.springframework.jdbc.datasource.init.DatabasePopulator;
26-
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
26+
import org.testcontainers.containers.MySQLContainer;
27+
import org.testcontainers.jdbc.ext.ScriptUtils;
2728

2829
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
2930

@@ -32,35 +33,45 @@
3233
*
3334
* @author Jens Schauder
3435
* @author Oliver Gierke
36+
* @author Sedat Gokcen
3537
*/
3638
@Configuration
3739
@Profile("mysql")
3840
class MySqlDataSourceConfiguration extends DataSourceConfiguration {
3941

42+
private static final MySQLContainer MYSQL_CONTAINER = new MySQLContainer();
43+
44+
static {
45+
MYSQL_CONTAINER.withConfigurationOverride("mysql_cnf_override").withDatabaseName("test").start();
46+
}
47+
4048
/*
4149
* (non-Javadoc)
4250
* @see org.springframework.data.jdbc.testing.DataSourceConfiguration#createDataSource()
4351
*/
52+
@Override
4453
protected DataSource createDataSource() {
4554

46-
MysqlDataSource dataSource = new MysqlDataSource();
47-
dataSource.setUrl("jdbc:mysql:///test?user=root");
55+
MysqlDataSource dataSource = getCommonDataSource();
56+
dataSource.setDatabaseName(MYSQL_CONTAINER.getDatabaseName());
4857

4958
return dataSource;
5059
}
5160

5261
@PostConstruct
53-
public void initDatabase() {
62+
public void initDatabase() throws SQLException, ScriptException {
5463

55-
MysqlDataSource dataSource = new MysqlDataSource();
56-
dataSource.setUrl("jdbc:mysql:///?user=root");
64+
MysqlDataSource dataSource = getCommonDataSource();
65+
ScriptUtils.executeSqlScript(dataSource.getConnection(), null, "DROP DATABASE test;CREATE DATABASE test;");
66+
}
5767

58-
ClassPathResource createScript = new ClassPathResource("create-mysql.sql");
59-
DatabasePopulator databasePopulator = new ResourceDatabasePopulator(createScript);
68+
private MysqlDataSource getCommonDataSource() {
6069

61-
DataSourceInitializer initializer = new DataSourceInitializer();
62-
initializer.setDatabasePopulator(databasePopulator);
63-
initializer.setDataSource(dataSource);
64-
initializer.afterPropertiesSet();
70+
MysqlDataSource dataSource = new MysqlDataSource();
71+
dataSource.setUrl(MYSQL_CONTAINER.getJdbcUrl());
72+
dataSource.setUser(MYSQL_CONTAINER.getUsername());
73+
dataSource.setPassword(MYSQL_CONTAINER.getPassword());
74+
75+
return dataSource;
6576
}
6677
}

src/test/java/org/springframework/data/jdbc/testing/PostgresDataSourceConfiguration.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,36 @@
2121
import org.springframework.context.annotation.Configuration;
2222
import org.springframework.context.annotation.Profile;
2323
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
24+
import org.testcontainers.containers.PostgreSQLContainer;
2425

2526
/**
2627
* {@link DataSource} setup for PostgreSQL
2728
*
2829
* @author Jens Schauder
2930
* @author Oliver Gierke
31+
* @author Sedat Gokcen
3032
*/
3133
@Configuration
3234
@Profile("postgres")
3335
public class PostgresDataSourceConfiguration extends DataSourceConfiguration {
3436

37+
private static final PostgreSQLContainer POSTGRESQL_CONTAINER = new PostgreSQLContainer();
38+
39+
static {
40+
POSTGRESQL_CONTAINER.start();
41+
}
42+
3543
/*
3644
* (non-Javadoc)
3745
* @see org.springframework.data.jdbc.testing.DataSourceConfiguration#createDataSource()
3846
*/
47+
@Override
3948
protected DataSource createDataSource() {
4049

4150
PGSimpleDataSource ds = new PGSimpleDataSource();
42-
ds.setUrl("jdbc:postgresql:///postgres");
51+
ds.setUrl(POSTGRESQL_CONTAINER.getJdbcUrl());
52+
ds.setUser(POSTGRESQL_CONTAINER.getUsername());
53+
ds.setPassword(POSTGRESQL_CONTAINER.getPassword());
4354

4455
return ds;
4556
}

src/test/resources/create-mysql.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[mysqld]
2+
3+
# the only change we make, other params with the comments all together are the default ones.
4+
lower_case_table_names = 1
5+
6+
user = mysql
7+
datadir = /var/lib/mysql
8+
port = 3306
9+
#socket = /tmp/mysql.sock
10+
skip-external-locking
11+
key_buffer_size = 16K
12+
max_allowed_packet = 1M
13+
table_open_cache = 4
14+
sort_buffer_size = 64K
15+
read_buffer_size = 256K
16+
read_rnd_buffer_size = 256K
17+
net_buffer_length = 2K
18+
skip-host-cache
19+
skip-name-resolve
20+
21+
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
22+
# if all processes that need to connect to mysqld run on the same host.
23+
# All interaction with mysqld must be made via Unix sockets or named pipes.
24+
# Note that using this option without enabling named pipes on Windows
25+
# (using the "enable-named-pipe" option) will render mysqld useless!
26+
#
27+
#skip-networking
28+
#server-id = 1
29+
30+
# Uncomment the following if you want to log updates
31+
#log-bin=mysql-bin
32+
33+
# binary logging format - mixed recommended
34+
#binlog_format=mixed
35+
36+
# Causes updates to non-transactional engines using statement format to be
37+
# written directly to binary log. Before using this option make sure that
38+
# there are no dependencies between transactional and non-transactional
39+
# tables such as in the statement INSERT INTO t_myisam SELECT * FROM
40+
# t_innodb; otherwise, slaves may diverge from the master.
41+
#binlog_direct_non_transactional_updates=TRUE
42+
43+
# Uncomment the following if you are using InnoDB tables
44+
innodb_data_file_path = ibdata1:10M:autoextend
45+
# You can set .._buffer_pool_size up to 50 - 80 %
46+
# of RAM but beware of setting memory usage too high
47+
innodb_buffer_pool_size = 16M
48+
#innodb_additional_mem_pool_size = 2M
49+
# Set .._log_file_size to 25 % of buffer pool size
50+
innodb_log_file_size = 5M
51+
innodb_log_buffer_size = 8M
52+
innodb_flush_log_at_trx_commit = 1
53+
innodb_lock_wait_timeout = 50

start-all-dbs.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.

stop-all-dbs.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)