Skip to content

Commit 8479990

Browse files
committed
spring-data-r2dbc/issue49
spring-projects/spring-data-r2dbc#49
1 parent dd5187b commit 8479990

File tree

8 files changed

+169
-30
lines changed

8 files changed

+169
-30
lines changed

pom.xml

+39-21
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
<mockito.version>2.23.4</mockito.version>
3333

3434
<!-- database -->
35-
<db.database>test</db.database>
35+
<db.name>test_r2dbc</db.name>
3636
<db.host>localhost</db.host>
37-
<db.user>test</db.user>
37+
<db.username>test</db.username>
3838
<db.password>password</db.password>
3939
</properties>
4040
<dependencyManagement>
@@ -64,6 +64,12 @@
6464
</dependencies>
6565
</dependencyManagement>
6666
<dependencies>
67+
<dependency>
68+
<groupId>tech.simter</groupId>
69+
<artifactId>simter-r2dbc-ext</artifactId>
70+
<version>1.1.1</version>
71+
<scope>test</scope>
72+
</dependency>
6773
<dependency>
6874
<groupId>org.springframework.data</groupId>
6975
<artifactId>spring-data-r2dbc</artifactId>
@@ -184,68 +190,80 @@
184190
<artifactId>maven-surefire-plugin</artifactId>
185191
<version>2.22.1</version>
186192
</plugin>
193+
<plugin>
194+
<groupId>org.apache.maven.plugins</groupId>
195+
<artifactId>maven-resources-plugin</artifactId>
196+
<version>3.1.0</version>
197+
<configuration>
198+
<delimiters>
199+
<!-- make yml file support maven properties (by @...@)-->
200+
<delimiter>@</delimiter>
201+
</delimiters>
202+
<useDefaultDelimiters>false</useDefaultDelimiters>
203+
</configuration>
204+
</plugin>
187205
</plugins>
188206
</build>
189207
<profiles>
190208
<!-- database -->
191209
<profile>
192-
<id>postgres</id>
210+
<id>h2</id>
193211
<activation>
194212
<activeByDefault>true</activeByDefault>
195213
</activation>
214+
<properties>
215+
<db.platform>h2</db.platform>
216+
<db.port>~</db.port>
217+
</properties>
196218
<dependencies>
197219
<dependency>
198220
<groupId>io.r2dbc</groupId>
199-
<artifactId>r2dbc-postgresql</artifactId>
221+
<artifactId>r2dbc-h2</artifactId>
200222
<version>${r2dbc.version}</version>
201223
<scope>runtime</scope>
202224
</dependency>
203225
<dependency>
204-
<groupId>org.postgresql</groupId>
205-
<artifactId>postgresql</artifactId>
206-
<version>42.2.5</version>
226+
<groupId>com.h2database</groupId>
227+
<artifactId>h2</artifactId>
228+
<version>1.4.197</version>
207229
<scope>runtime</scope>
208230
</dependency>
209231
</dependencies>
210232
</profile>
211233
<profile>
212-
<id>h2</id>
234+
<id>postgres</id>
213235
<activation>
214236
<activeByDefault>false</activeByDefault>
215237
</activation>
238+
<properties>
239+
<db.platform>postgres</db.platform>
240+
<db.port>5432</db.port>
241+
</properties>
216242
<dependencies>
217243
<dependency>
218244
<groupId>io.r2dbc</groupId>
219-
<artifactId>r2dbc-h2</artifactId>
245+
<artifactId>r2dbc-postgresql</artifactId>
220246
<version>${r2dbc.version}</version>
221247
<scope>runtime</scope>
222248
</dependency>
223-
<dependency>
224-
<groupId>com.h2database</groupId>
225-
<artifactId>h2</artifactId>
226-
<version>1.4.197</version>
227-
<scope>runtime</scope>
228-
</dependency>
229249
</dependencies>
230250
</profile>
231251
<profile>
232252
<id>mssql</id>
233253
<activation>
234254
<activeByDefault>false</activeByDefault>
235255
</activation>
256+
<properties>
257+
<db.platform>mssql</db.platform>
258+
<db.port>1234</db.port>
259+
</properties>
236260
<dependencies>
237261
<dependency>
238262
<groupId>io.r2dbc</groupId>
239263
<artifactId>r2dbc-mssql</artifactId>
240264
<version>${r2dbc.version}</version>
241265
<scope>runtime</scope>
242266
</dependency>
243-
<dependency>
244-
<groupId>com.microsoft.sqlserver</groupId>
245-
<artifactId>mssql-jdbc</artifactId>
246-
<version>7.1.3.jre8-preview</version>
247-
<scope>runtime</scope>
248-
</dependency>
249267
</dependencies>
250268
</profile>
251269
</profiles>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tech.simter.start.springdatar2dbc;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import org.springframework.data.annotation.Id;
7+
import org.springframework.data.relational.core.mapping.Table;
8+
9+
/**
10+
* @author RJ
11+
*/
12+
@Table("people")
13+
@Data
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
public class People {
17+
@Id
18+
private String id;
19+
private String name;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tech.simter.start.springdatar2dbc;
2+
3+
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
4+
5+
/**
6+
* @author RJ
7+
*/
8+
public interface PeopleRepository extends ReactiveCrudRepository<People, String> {
9+
}

src/main/resources/sql/h2/schema.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
drop table if exists people;
2+
create table people (
3+
id varchar(36) primary key,
4+
name varchar(50) not null
5+
);
+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
drop table if exists dream;
2-
create table dream (
3-
id int primary key,
4-
name varchar(50) not null,
5-
create_on timestamp
1+
drop table if exists people;
2+
create table people (
3+
id varchar(36) primary key,
4+
name varchar(50) not null
65
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tech.simter.start.springdatar2dbc;
2+
3+
import io.r2dbc.spi.ConnectionFactory;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Import;
8+
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
9+
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
10+
import tech.simter.r2dbc.R2dbcConfiguration;
11+
12+
/**
13+
* @author RJ
14+
*/
15+
@Configuration
16+
@EnableR2dbcRepositories
17+
@EnableConfigurationProperties // for tech.simter.r2dbc.R2dbcProperties
18+
@Import(R2dbcConfiguration.class) // auto generate connectionFactory bean by maven dependency (from simter-r2dbc-ext)
19+
public class UnitTestConfiguration extends AbstractR2dbcConfiguration {
20+
private final ConnectionFactory connectionFactory;
21+
22+
@Autowired
23+
public UnitTestConfiguration(ConnectionFactory connectionFactory) {
24+
this.connectionFactory = connectionFactory;
25+
}
26+
27+
@Override
28+
public ConnectionFactory connectionFactory() {
29+
return this.connectionFactory;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package tech.simter.start.springdatar2dbc.issue49;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.junit.jupiter.SpringExtension;
9+
import reactor.test.StepVerifier;
10+
import tech.simter.start.springdatar2dbc.People;
11+
import tech.simter.start.springdatar2dbc.PeopleRepository;
12+
import tech.simter.start.springdatar2dbc.UnitTestConfiguration;
13+
14+
import java.util.UUID;
15+
16+
/**
17+
* @author RJ
18+
*/
19+
@SpringBootTest(classes = UnitTestConfiguration.class)
20+
@ExtendWith(SpringExtension.class)
21+
class SaveMethodImplTest {
22+
@Autowired
23+
private PeopleRepository repository;
24+
25+
@BeforeEach
26+
void clean() {
27+
StepVerifier.create(repository.deleteAll()).verifyComplete();
28+
}
29+
30+
@Test
31+
void test() {
32+
// save it
33+
People po = new People();
34+
po.setId(UUID.randomUUID().toString());
35+
po.setName("simter");
36+
StepVerifier.create(repository.save(po))
37+
.expectNext(po)
38+
.verifyComplete();
39+
40+
// verify saved
41+
StepVerifier.create(repository.findById(po.getId()))
42+
.expectNext(po)
43+
.verifyComplete();
44+
}
45+
}

src/test/resources/application.yml

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
spring:
22
main:
3-
banner-mode: 'OFF'
3+
banner-mode: 'off'
4+
datasource:
5+
initialization-mode: always # embedded|always|never
6+
url: ./target/@db.name@ # for h2
7+
name: @db.name@ # database-name for postgres, mssql
8+
port: @db.port@
9+
username: @db.username@
10+
password: @db.password@
11+
schema: # available when initialization-mode=always
12+
- classpath:sql/@db.platform@/schema.sql
413
logging:
514
#file: target/test.log
615
level: # one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
716
root: ERROR
8-
tech.simter.start.java.r2dbc: WARN
17+
#---- simter ----#
18+
tech.simter.r2dbc: DEBUG
19+
tech.simter.start.r2dbc: DEBUG
920

1021
#org.springframework: WARN
1122
#org.springframework.boot: WARN
1223

1324
#---- r2dbc ----#
1425
io.r2dbc: WARN
15-
io.r2dbc.postgresql.client.ReactorNettyClient: WARN # debug to show postgres request and Response
26+
io.r2dbc.h2.client.SessionClient: DEBUG # DEBUG to show h2 request and Response
27+
io.r2dbc.postgresql.client.ReactorNettyClient: DEBUG # DEBUG to show postgres request and Response
1628
org.springframework.data.r2dbc: WARN
17-
org.springframework.data.r2dbc.function.DefaultDatabaseClient: DEBUG # debug to show sql
29+
org.springframework.data.r2dbc.function.DefaultDatabaseClient: DEBUG # DEBUG to show sql

0 commit comments

Comments
 (0)