Skip to content

Commit d4aece5

Browse files
committed
#20 - Parametrize integration test classes.
Integration tests now declare abstract methods to be implemented with vendor-specific code.
1 parent 65db81c commit d4aece5

11 files changed

+174
-39
lines changed

src/test/java/org/springframework/data/r2dbc/function/AbstractDatabaseClientIntegrationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import reactor.core.publisher.Hooks;
2424
import reactor.test.StepVerifier;
2525

26+
import javax.sql.DataSource;
27+
2628
import org.junit.Before;
2729
import org.junit.Test;
2830
import org.springframework.dao.DuplicateKeyException;
@@ -55,6 +57,20 @@ public void before() {
5557
jdbc.execute("DELETE FROM legoset");
5658
}
5759

60+
/**
61+
* Creates a {@link DataSource} to be used in this test.
62+
*
63+
* @return the {@link DataSource} to be used in this test.
64+
*/
65+
protected abstract DataSource createDataSource();
66+
67+
/**
68+
* Creates a {@link ConnectionFactory} to be used in this test.
69+
*
70+
* @return the {@link ConnectionFactory} to be used in this test.
71+
*/
72+
protected abstract ConnectionFactory createConnectionFactory();
73+
5874
/**
5975
* Returns the the CREATE TABLE statement for table {@code legoset} with the following three columns:
6076
* <ul>

src/test/java/org/springframework/data/r2dbc/function/AbstractTransactionalDatabaseClientIntegrationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.Queue;
2929
import java.util.concurrent.ArrayBlockingQueue;
3030

31+
import javax.sql.DataSource;
32+
3133
import org.junit.Before;
3234
import org.junit.Test;
3335
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
@@ -57,6 +59,20 @@ public void before() {
5759
jdbc.execute("DELETE FROM legoset");
5860
}
5961

62+
/**
63+
* Creates a {@link DataSource} to be used in this test.
64+
*
65+
* @return the {@link DataSource} to be used in this test.
66+
*/
67+
protected abstract DataSource createDataSource();
68+
69+
/**
70+
* Creates a {@link ConnectionFactory} to be used in this test.
71+
*
72+
* @return the {@link ConnectionFactory} to be used in this test.
73+
*/
74+
protected abstract ConnectionFactory createConnectionFactory();
75+
6076
/**
6177
* Returns the the CREATE TABLE statement for table {@code legoset} with the following three columns:
6278
* <ul>

src/test/java/org/springframework/data/r2dbc/function/PostgresDatabaseClientIntegrationTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
*/
1616
package org.springframework.data.r2dbc.function;
1717

18+
import io.r2dbc.spi.ConnectionFactory;
19+
20+
import javax.sql.DataSource;
21+
22+
import org.junit.ClassRule;
23+
import org.springframework.data.r2dbc.testing.ExternalDatabase;
1824
import org.springframework.data.r2dbc.testing.PostgresTestSupport;
1925

2026
/**
@@ -24,6 +30,18 @@
2430
*/
2531
public class PostgresDatabaseClientIntegrationTests extends AbstractDatabaseClientIntegrationTests {
2632

33+
@ClassRule public static final ExternalDatabase database = PostgresTestSupport.database();
34+
35+
@Override
36+
protected DataSource createDataSource() {
37+
return PostgresTestSupport.createDataSource(database);
38+
}
39+
40+
@Override
41+
protected ConnectionFactory createConnectionFactory() {
42+
return PostgresTestSupport.createConnectionFactory(database);
43+
}
44+
2745
@Override
2846
protected String getCreateTableStatement() {
2947
return PostgresTestSupport.CREATE_TABLE_LEGOSET;

src/test/java/org/springframework/data/r2dbc/function/PostgresTransactionalDatabaseClientIntegrationTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package org.springframework.data.r2dbc.function;
22

3+
import io.r2dbc.spi.ConnectionFactory;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.junit.ClassRule;
8+
import org.springframework.data.r2dbc.testing.ExternalDatabase;
39
import org.springframework.data.r2dbc.testing.PostgresTestSupport;
410

511
/**
@@ -10,6 +16,18 @@
1016
public class PostgresTransactionalDatabaseClientIntegrationTests
1117
extends AbstractTransactionalDatabaseClientIntegrationTests {
1218

19+
@ClassRule public static final ExternalDatabase database = PostgresTestSupport.database();
20+
21+
@Override
22+
protected DataSource createDataSource() {
23+
return PostgresTestSupport.createDataSource(database);
24+
}
25+
26+
@Override
27+
protected ConnectionFactory createConnectionFactory() {
28+
return PostgresTestSupport.createConnectionFactory(database);
29+
}
30+
1331
@Override
1432
protected String getCreateTableStatement() {
1533
return PostgresTestSupport.CREATE_TABLE_LEGOSET;

src/test/java/org/springframework/data/r2dbc/repository/AbstractR2dbcRepositoryIntegrationTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import io.r2dbc.spi.ConnectionFactory;
2021
import lombok.AllArgsConstructor;
2122
import lombok.Data;
2223
import lombok.NoArgsConstructor;
@@ -29,6 +30,8 @@
2930
import java.util.Collections;
3031
import java.util.Map;
3132

33+
import javax.sql.DataSource;
34+
3235
import org.junit.Before;
3336
import org.junit.Test;
3437
import org.springframework.beans.factory.annotation.Autowired;
@@ -72,6 +75,20 @@ public void before() {
7275
this.jdbc.execute(getCreateTableStatement());
7376
}
7477

78+
/**
79+
* Creates a {@link DataSource} to be used in this test.
80+
*
81+
* @return the {@link DataSource} to be used in this test.
82+
*/
83+
protected abstract DataSource createDataSource();
84+
85+
/**
86+
* Creates a {@link ConnectionFactory} to be used in this test.
87+
*
88+
* @return the {@link ConnectionFactory} to be used in this test.
89+
*/
90+
protected abstract ConnectionFactory createConnectionFactory();
91+
7592
/**
7693
* Returns the the CREATE TABLE statement for table {@code legoset} with the following three columns:
7794
* <ul>

src/test/java/org/springframework/data/r2dbc/repository/PostgresR2dbcRepositoryIntegrationTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import reactor.core.publisher.Flux;
2020
import reactor.core.publisher.Mono;
2121

22+
import javax.sql.DataSource;
23+
24+
import org.junit.ClassRule;
2225
import org.junit.runner.RunWith;
2326
import org.springframework.context.annotation.ComponentScan.Filter;
2427
import org.springframework.context.annotation.Configuration;
@@ -27,6 +30,7 @@
2730
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
2831
import org.springframework.data.r2dbc.repository.query.Query;
2932
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
33+
import org.springframework.data.r2dbc.testing.ExternalDatabase;
3034
import org.springframework.data.r2dbc.testing.PostgresTestSupport;
3135
import org.springframework.test.context.ContextConfiguration;
3236
import org.springframework.test.context.junit4.SpringRunner;
@@ -40,17 +44,29 @@
4044
@ContextConfiguration
4145
public class PostgresR2dbcRepositoryIntegrationTests extends AbstractR2dbcRepositoryIntegrationTests {
4246

47+
@ClassRule public static final ExternalDatabase database = PostgresTestSupport.database();
48+
4349
@Configuration
4450
@EnableR2dbcRepositories(considerNestedRepositories = true,
4551
includeFilters = @Filter(classes = PostgresLegoSetRepository.class, type = FilterType.ASSIGNABLE_TYPE))
4652
static class IntegrationTestConfiguration extends AbstractR2dbcConfiguration {
4753

4854
@Override
4955
public ConnectionFactory connectionFactory() {
50-
return createConnectionFactory();
56+
return PostgresTestSupport.createConnectionFactory(database);
5157
}
5258
}
5359

60+
@Override
61+
protected DataSource createDataSource() {
62+
return PostgresTestSupport.createDataSource(database);
63+
}
64+
65+
@Override
66+
protected ConnectionFactory createConnectionFactory() {
67+
return PostgresTestSupport.createConnectionFactory(database);
68+
}
69+
5470
@Override
5571
protected String getCreateTableStatement() {
5672
return PostgresTestSupport.CREATE_TABLE_LEGOSET;

src/test/java/org/springframework/data/r2dbc/repository/support/AbstractSimpleR2dbcRepositoryIntegrationTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.Collections;
3030
import java.util.Map;
3131

32+
import javax.sql.DataSource;
33+
3234
import org.junit.Before;
3335
import org.junit.Test;
3436
import org.springframework.beans.factory.annotation.Autowired;
@@ -81,6 +83,13 @@ public void before() {
8183
this.jdbc.execute(getCreateTableStatement());
8284
}
8385

86+
/**
87+
* Creates a {@link DataSource} to be used in this test.
88+
*
89+
* @return the {@link DataSource} to be used in this test.
90+
*/
91+
protected abstract DataSource createDataSource();
92+
8493
/**
8594
* Returns the the CREATE TABLE statement for table {@code legoset} with the following three columns:
8695
* <ul>

src/test/java/org/springframework/data/r2dbc/repository/support/PostgresSimpleR2dbcRepositoryIntegrationTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717

1818
import io.r2dbc.spi.ConnectionFactory;
1919

20+
import javax.sql.DataSource;
21+
22+
import org.junit.ClassRule;
2023
import org.junit.runner.RunWith;
2124
import org.springframework.context.annotation.Configuration;
2225
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
26+
import org.springframework.data.r2dbc.testing.ExternalDatabase;
2327
import org.springframework.data.r2dbc.testing.PostgresTestSupport;
2428
import org.springframework.test.context.ContextConfiguration;
2529
import org.springframework.test.context.junit4.SpringRunner;
@@ -33,15 +37,22 @@
3337
@ContextConfiguration
3438
public class PostgresSimpleR2dbcRepositoryIntegrationTests extends AbstractSimpleR2dbcRepositoryIntegrationTests {
3539

40+
@ClassRule public static final ExternalDatabase database = PostgresTestSupport.database();
41+
3642
@Configuration
3743
static class IntegrationTestConfiguration extends AbstractR2dbcConfiguration {
3844

3945
@Override
4046
public ConnectionFactory connectionFactory() {
41-
return createConnectionFactory();
47+
return PostgresTestSupport.createConnectionFactory(database);
4248
}
4349
}
4450

51+
@Override
52+
protected DataSource createDataSource() {
53+
return PostgresTestSupport.createDataSource(database);
54+
}
55+
4556
@Override
4657
protected String getCreateTableStatement() {
4758
return PostgresTestSupport.CREATE_TABLE_LEGOSET;

src/test/java/org/springframework/data/r2dbc/testing/ExternalDatabase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ public abstract class ExternalDatabase extends ExternalResource {
5757
protected void before() {
5858

5959
try (Socket socket = new Socket()) {
60-
;
6160
socket.connect(new InetSocketAddress(getHostname(), getPort()), Math.toIntExact(TimeUnit.SECONDS.toMillis(5)));
6261

6362
} catch (IOException e) {
6463
throw new AssumptionViolatedException(
65-
String.format("Cannot connect to %s:%d. Skiping tests.", getHostname(), getPort()));
64+
String.format("Cannot connect to %s:%d. Skipping tests.", getHostname(), getPort()));
6665
}
6766
}
6867

src/test/java/org/springframework/data/r2dbc/testing/PostgresTestSupport.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package org.springframework.data.r2dbc.testing;
22

3+
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
4+
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
5+
import io.r2dbc.spi.ConnectionFactory;
6+
7+
import javax.sql.DataSource;
8+
9+
import org.postgresql.ds.PGSimpleDataSource;
10+
import org.springframework.data.r2dbc.testing.ExternalDatabase.ProvidedDatabase;
11+
312
/**
413
* Utility class for testing against Postgres.
514
*
@@ -14,4 +23,45 @@ public class PostgresTestSupport {
1423
+ ");";
1524

1625
public static String INSERT_INTO_LEGOSET = "INSERT INTO legoset (id, name, manual) VALUES($1, $2, $3)";
26+
27+
/**
28+
* Returns a locally provided database at {@code postgres:@localhost:5432/postgres}.
29+
*
30+
* @return
31+
*/
32+
public static ExternalDatabase database() {
33+
return local();
34+
}
35+
36+
/**
37+
* Returns a locally provided database at {@code postgres:@localhost:5432/postgres}.
38+
*
39+
* @return
40+
*/
41+
private static ExternalDatabase local() {
42+
return ProvidedDatabase.builder().hostname("localhost").port(5432).database("postgres").username("postgres")
43+
.password("").build();
44+
}
45+
46+
/**
47+
* Creates a new {@link ConnectionFactory} configured from the {@link ExternalDatabase}..
48+
*/
49+
public static ConnectionFactory createConnectionFactory(ExternalDatabase database) {
50+
return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder().host(database.getHostname())
51+
.database(database.getDatabase()).username(database.getUsername()).password(database.getPassword()).build());
52+
}
53+
54+
/**
55+
* Creates a new {@link DataSource} configured from the {@link ExternalDatabase}.
56+
*/
57+
public static DataSource createDataSource(ExternalDatabase database) {
58+
59+
PGSimpleDataSource dataSource = new PGSimpleDataSource();
60+
dataSource.setUser(database.getUsername());
61+
dataSource.setPassword(database.getPassword());
62+
dataSource.setDatabaseName(database.getDatabase());
63+
dataSource.setServerName(database.getHostname());
64+
dataSource.setPortNumber(database.getPort());
65+
return dataSource;
66+
}
1767
}

src/test/java/org/springframework/data/r2dbc/testing/R2dbcIntegrationTestSupport.java

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

18-
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
19-
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
20-
import io.r2dbc.spi.ConnectionFactory;
21-
2218
import javax.sql.DataSource;
2319

24-
import org.junit.ClassRule;
25-
import org.postgresql.ds.PGSimpleDataSource;
26-
import org.springframework.data.r2dbc.testing.ExternalDatabase.ProvidedDatabase;
2720
import org.springframework.jdbc.core.JdbcTemplate;
2821

2922
/**
@@ -33,34 +26,6 @@
3326
*/
3427
public abstract class R2dbcIntegrationTestSupport {
3528

36-
/**
37-
* Local test database at {@code postgres:@localhost:5432/postgres}.
38-
*/
39-
@ClassRule public static final ExternalDatabase database = ProvidedDatabase.builder().hostname("localhost").port(5432)
40-
.database("postgres").username("postgres").password("").build();
41-
42-
/**
43-
* Creates a new {@link ConnectionFactory} configured from the {@link ExternalDatabase}..
44-
*/
45-
protected static ConnectionFactory createConnectionFactory() {
46-
return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder().host(database.getHostname())
47-
.database(database.getDatabase()).username(database.getUsername()).password(database.getPassword()).build());
48-
}
49-
50-
/**
51-
* Creates a new {@link DataSource} configured from the {@link ExternalDatabase}.
52-
*/
53-
protected static DataSource createDataSource() {
54-
55-
PGSimpleDataSource dataSource = new PGSimpleDataSource();
56-
dataSource.setUser(database.getUsername());
57-
dataSource.setPassword(database.getPassword());
58-
dataSource.setDatabaseName(database.getDatabase());
59-
dataSource.setServerName(database.getHostname());
60-
dataSource.setPortNumber(database.getPort());
61-
return dataSource;
62-
}
63-
6429
/**
6530
* Creates a new {@link JdbcTemplate} for a {@link DataSource}.
6631
*/

0 commit comments

Comments
 (0)