Skip to content

Commit ee5e3a7

Browse files
committed
#2 - Refactor R2dbc test support code into R2dbcIntegrationTestSupport.
1 parent 893149d commit ee5e3a7

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

src/test/java/org/springframework/data/jdbc/core/function/DatabaseClientIntegrationTests.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,25 @@
1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.springframework.data.domain.Sort.Order.*;
2020

21-
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
22-
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
2321
import io.r2dbc.spi.ConnectionFactory;
2422
import lombok.Data;
2523
import reactor.core.publisher.Hooks;
2624
import reactor.test.StepVerifier;
2725

2826
import org.junit.Before;
29-
import org.junit.ClassRule;
3027
import org.junit.Test;
31-
import org.postgresql.ds.PGSimpleDataSource;
3228
import org.springframework.data.domain.PageRequest;
3329
import org.springframework.data.domain.Sort;
34-
import org.springframework.data.jdbc.core.function.ExternalDatabase.ProvidedDatabase;
3530
import org.springframework.data.jdbc.core.mapping.Table;
31+
import org.springframework.data.jdbc.testing.R2dbcIntegrationTestSupport;
3632
import org.springframework.jdbc.core.JdbcTemplate;
3733

3834
/**
3935
* Integration tests for {@link DatabaseClient} against PostgreSQL.
4036
*
4137
* @author Mark Paluch
4238
*/
43-
public class DatabaseClientIntegrationTests {
44-
45-
/**
46-
* Local test database at {@code postgres:@localhost:5432/postgres}.
47-
*/
48-
@ClassRule public static final ExternalDatabase database = ProvidedDatabase.builder().hostname("localhost").port(5432)
49-
.database("postgres").username("postgres").password("").build();
39+
public class DatabaseClientIntegrationTests extends R2dbcIntegrationTestSupport {
5040

5141
private ConnectionFactory connectionFactory;
5242

@@ -57,22 +47,13 @@ public void before() {
5747

5848
Hooks.onOperatorDebug();
5949

60-
connectionFactory = new PostgresqlConnectionFactory(
61-
PostgresqlConnectionConfiguration.builder().host(database.getHostname()).database(database.getDatabase())
62-
.username(database.getUsername()).password(database.getPassword()).build());
63-
64-
PGSimpleDataSource dataSource = new PGSimpleDataSource();
65-
dataSource.setUser(database.getUsername());
66-
dataSource.setPassword(database.getPassword());
67-
dataSource.setDatabaseName(database.getDatabase());
68-
dataSource.setServerName(database.getHostname());
69-
dataSource.setPortNumber(database.getPort());
50+
connectionFactory = createConnectionFactory();
7051

7152
String tableToCreate = "CREATE TABLE IF NOT EXISTS legoset (\n"
7253
+ " id integer CONSTRAINT id PRIMARY KEY,\n" + " name varchar(255) NOT NULL,\n"
7354
+ " manual integer NULL\n" + ");";
7455

75-
jdbc = new JdbcTemplate(dataSource);
56+
jdbc = createJdbcTemplate(createDataSource());
7657
jdbc.execute(tableToCreate);
7758
jdbc.execute("DELETE FROM legoset");
7859
}

src/test/java/org/springframework/data/jdbc/core/function/ExternalDatabase.java renamed to src/test/java/org/springframework/data/jdbc/testing/ExternalDatabase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jdbc.core.function;
16+
package org.springframework.data.jdbc.testing;
1717

1818
import lombok.Builder;
1919

@@ -75,7 +75,7 @@ protected void before() {
7575
* Provided (unmanaged resource) database connection coordinates.
7676
*/
7777
@Builder
78-
static class ProvidedDatabase extends ExternalDatabase {
78+
public static class ProvidedDatabase extends ExternalDatabase {
7979

8080
private final int port;
8181
private final String hostname;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2018 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.springframework.data.jdbc.testing;
17+
18+
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
19+
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
20+
import io.r2dbc.spi.ConnectionFactory;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.junit.ClassRule;
25+
import org.postgresql.ds.PGSimpleDataSource;
26+
import org.springframework.data.jdbc.testing.ExternalDatabase.ProvidedDatabase;
27+
import org.springframework.jdbc.core.JdbcTemplate;
28+
29+
/**
30+
* Base class for R2DBC integration tests.
31+
*
32+
* @author Mark Paluch
33+
*/
34+
public abstract class R2dbcIntegrationTestSupport {
35+
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+
64+
/**
65+
* Creates a new {@link JdbcTemplate} for a {@link DataSource}.
66+
*/
67+
protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
68+
return new JdbcTemplate(dataSource);
69+
}
70+
}

0 commit comments

Comments
 (0)