Skip to content

Commit a4c9302

Browse files
committed
Java 16 migration for R2DBC examples.
See #606.
1 parent 75e2763 commit a4c9302

File tree

10 files changed

+83
-76
lines changed

10 files changed

+83
-76
lines changed

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<maven.compiler.target>16</maven.compiler.target>
4040
<byte-buddy.version>1.11.0</byte-buddy.version>
4141
<testcontainers.version>1.15.3</testcontainers.version>
42+
<kotlin.version>1.5.0</kotlin.version>
4243
<apt.version>1.1.3</apt.version>
4344
<jvm.enable-preview></jvm.enable-preview>
4445
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

r2dbc/example/src/main/java/example/springdata/r2dbc/basics/Customer.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,32 @@
1515
*/
1616
package example.springdata.r2dbc.basics;
1717

18-
import lombok.AllArgsConstructor;
19-
import lombok.Data;
18+
import java.util.Objects;
2019

2120
import org.springframework.data.annotation.Id;
2221

2322
/**
2423
* @author Oliver Gierke
2524
*/
26-
@Data
27-
@AllArgsConstructor
28-
class Customer {
29-
30-
@Id Integer id;
31-
String firstname, lastname;
25+
record Customer(@Id Integer id, String firstname, String lastname) {
3226

3327
boolean hasId() {
3428
return id != null;
3529
}
30+
31+
@Override
32+
public boolean equals(Object o) {
33+
if (this == o) {
34+
return true;
35+
}
36+
if (!(o instanceof Customer customer)) {
37+
return false;
38+
}
39+
return Objects.equals(firstname, customer.firstname) && Objects.equals(lastname, customer.lastname);
40+
}
41+
42+
@Override
43+
public int hashCode() {
44+
return Objects.hash(firstname, lastname);
45+
}
3646
}

r2dbc/example/src/main/java/example/springdata/r2dbc/basics/TransactionalService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Mono<Customer> save(Customer customer) {
4242

4343
return repository.save(customer).map(it -> {
4444

45-
if (it.firstname.equals("Dave")) {
45+
if (it.firstname().equals("Dave")) {
4646
throw new IllegalStateException();
4747
} else {
4848
return it;

r2dbc/example/src/main/java/example/springdata/r2dbc/entitycallback/ApplicationConfiguration.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ BeforeConvertCallback<Customer> idGeneratingCallback(DatabaseClient databaseClie
3939

4040
return (customer, sqlIdentifier) -> {
4141

42-
if (customer.getId() == null) {
42+
if (customer.id() == null) {
4343

4444
return databaseClient.sql("SELECT primary_key.nextval") //
4545
.map(row -> row.get(0, Long.class)) //
@@ -54,7 +54,7 @@ BeforeConvertCallback<Customer> idGeneratingCallback(DatabaseClient databaseClie
5454
@Bean
5555
ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {
5656

57-
ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
57+
var initializer = new ConnectionFactoryInitializer();
5858
initializer.setConnectionFactory(connectionFactory);
5959
initializer.setDatabasePopulator(new ResourceDatabasePopulator(new ByteArrayResource(("CREATE SEQUENCE primary_key;"
6060
+ "DROP TABLE IF EXISTS customer;"

r2dbc/example/src/main/java/example/springdata/r2dbc/entitycallback/Customer.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,14 @@
1515
*/
1616
package example.springdata.r2dbc.entitycallback;
1717

18-
import lombok.AllArgsConstructor;
19-
import lombok.Value;
20-
import lombok.With;
21-
2218
import org.springframework.data.annotation.Id;
2319

2420
/**
2521
* @author Mark Paluch
2622
*/
27-
@Value
28-
@AllArgsConstructor
29-
@With
30-
class Customer {
23+
record Customer(@Id Long id, String firstname, String lastname) {
3124

32-
@Id Long id;
33-
String firstname, lastname;
25+
public Customer withId(long id) {
26+
return new Customer(id, firstname, lastname);
27+
}
3428
}

r2dbc/example/src/test/java/example/springdata/r2dbc/basics/CustomerRepositoryIntegrationTests.java

+16-19
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,29 @@
2020

2121
import java.io.IOException;
2222
import java.util.Arrays;
23-
import java.util.List;
2423

25-
import org.junit.Before;
26-
import org.junit.Test;
27-
import org.junit.runner.RunWith;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
2827
import org.springframework.beans.factory.annotation.Autowired;
2928
import org.springframework.boot.test.context.SpringBootTest;
3029
import org.springframework.r2dbc.core.DatabaseClient;
31-
import org.springframework.test.context.junit4.SpringRunner;
3230

3331
/**
3432
* @author Oliver Gierke
3533
*/
36-
@RunWith(SpringRunner.class)
3734
@SpringBootTest(classes = InfrastructureConfiguration.class)
38-
public class CustomerRepositoryIntegrationTests {
35+
class CustomerRepositoryIntegrationTests {
3936

4037
@Autowired CustomerRepository customers;
4138
@Autowired DatabaseClient database;
4239

43-
@Before
44-
public void setUp() {
40+
@BeforeEach
41+
void setUp() {
4542

4643
Hooks.onOperatorDebug();
4744

48-
List<String> statements = Arrays.asList(//
45+
var statements = Arrays.asList(//
4946
"DROP TABLE IF EXISTS customer;",
5047
"CREATE TABLE customer ( id SERIAL PRIMARY KEY, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL);");
5148

@@ -58,31 +55,31 @@ public void setUp() {
5855
}
5956

6057
@Test
61-
public void executesFindAll() throws IOException {
58+
void executesFindAll() throws IOException {
6259

63-
Customer dave = new Customer(null, "Dave", "Matthews");
64-
Customer carter = new Customer(null, "Carter", "Beauford");
60+
var dave = new Customer(null, "Dave", "Matthews");
61+
var carter = new Customer(null, "Carter", "Beauford");
6562

6663
insertCustomers(dave, carter);
6764

6865
customers.findAll() //
6966
.as(StepVerifier::create) //
70-
.assertNext(dave::equals) //
71-
.assertNext(carter::equals) //
67+
.expectNext(dave) //
68+
.expectNext(carter) //
7269
.verifyComplete();
7370
}
7471

7572
@Test
76-
public void executesAnnotatedQuery() throws IOException {
73+
void executesAnnotatedQuery() throws IOException {
7774

78-
Customer dave = new Customer(null, "Dave", "Matthews");
79-
Customer carter = new Customer(null, "Carter", "Beauford");
75+
var dave = new Customer(null, "Dave", "Matthews");
76+
var carter = new Customer(null, "Carter", "Beauford");
8077

8178
insertCustomers(dave, carter);
8279

8380
customers.findByLastname("Matthews") //
8481
.as(StepVerifier::create) //
85-
.assertNext(dave::equals) //
82+
.expectNext(dave) //
8683
.verifyComplete();
8784
}
8885

r2dbc/example/src/test/java/example/springdata/r2dbc/basics/TransactionalServiceIntegrationTests.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,32 @@
2121
import java.util.Arrays;
2222
import java.util.List;
2323

24-
import org.junit.Before;
25-
import org.junit.Test;
26-
import org.junit.runner.RunWith;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
2727
import org.springframework.beans.factory.annotation.Autowired;
2828
import org.springframework.boot.test.context.SpringBootTest;
2929
import org.springframework.r2dbc.core.DatabaseClient;
30-
import org.springframework.test.context.junit4.SpringRunner;
3130

3231
/**
3332
* Integration tests for {@link TransactionalService}.
3433
*
3534
* @author Oliver Drotbohm
3635
* @soundtrack Shame - Tedeschi Trucks Band (Signs)
3736
*/
38-
@RunWith(SpringRunner.class)
3937
@SpringBootTest(classes = InfrastructureConfiguration.class)
40-
public class TransactionalServiceIntegrationTests {
38+
class TransactionalServiceIntegrationTests {
4139

4240
@Autowired TransactionalService service;
4341
@Autowired CustomerRepository repository;
4442
@Autowired DatabaseClient database;
4543

46-
@Before
47-
public void setUp() {
44+
@BeforeEach
45+
void setUp() {
4846

4947
Hooks.onOperatorDebug();
5048

51-
List<String> statements = Arrays.asList(//
49+
var statements = Arrays.asList(//
5250
"DROP TABLE IF EXISTS customer;",
5351
"CREATE TABLE customer ( id SERIAL PRIMARY KEY, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL);");
5452

@@ -61,7 +59,7 @@ public void setUp() {
6159
}
6260

6361
@Test // #500
64-
public void exceptionTriggersRollback() {
62+
void exceptionTriggersRollback() {
6563

6664
service.save(new Customer(null, "Dave", "Matthews")) //
6765
.as(StepVerifier::create) //
@@ -75,7 +73,7 @@ public void exceptionTriggersRollback() {
7573
}
7674

7775
@Test // #500
78-
public void insertsDataTransactionally() {
76+
void insertsDataTransactionally() {
7977

8078
service.save(new Customer(null, "Carter", "Beauford")) //
8179
.as(StepVerifier::create) //

r2dbc/example/src/test/java/example/springdata/r2dbc/entitycallback/CustomerRepositoryIntegrationTests.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121

2222
import java.io.IOException;
2323

24-
import org.junit.Test;
25-
import org.junit.runner.RunWith;
24+
import org.junit.jupiter.api.Test;
2625

2726
import org.springframework.beans.factory.annotation.Autowired;
2827
import org.springframework.boot.test.context.SpringBootTest;
2928
import org.springframework.r2dbc.core.DatabaseClient;
30-
import org.springframework.test.context.junit4.SpringRunner;
3129

3230
/**
3331
* Integration test using {@link org.springframework.data.r2dbc.mapping.event.BeforeConvertCallback} to auto-generate an
@@ -36,24 +34,23 @@
3634
* @author Mark Paluch
3735
* @see ApplicationConfiguration#idGeneratingCallback(DatabaseClient)
3836
*/
39-
@RunWith(SpringRunner.class)
4037
@SpringBootTest
41-
public class CustomerRepositoryIntegrationTests {
38+
class CustomerRepositoryIntegrationTests {
4239

4340
@Autowired CustomerRepository customers;
4441
@Autowired DatabaseClient database;
4542

4643
@Test
47-
public void generatesIdOnInsert() throws IOException {
44+
void generatesIdOnInsert() throws IOException {
4845

49-
Customer dave = new Customer(null, "Dave", "Matthews");
46+
var dave = new Customer(null, "Dave", "Matthews");
5047

5148
this.customers.save(dave) //
5249
.as(StepVerifier::create) //
5350
.assertNext(actual -> {
5451

55-
assertThat(dave.getId()).isNull(); // immutable before save
56-
assertThat(actual.getId()).isNotNull(); // after save
52+
assertThat(dave.id()).isNull(); // immutable before save
53+
assertThat(actual.id()).isNotNull(); // after save
5754
}).verifyComplete();
5855
}
5956
}

r2dbc/query-by-example/src/main/java/example/springdata/r2dbc/basics/Person.java

+19-9
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616
package example.springdata.r2dbc.basics;
1717

18-
import lombok.AllArgsConstructor;
19-
import lombok.Data;
18+
import java.util.Objects;
2019

2120
import org.springframework.data.annotation.Id;
2221

@@ -25,15 +24,26 @@
2524
*
2625
* @author Mark Paluch
2726
*/
28-
@Data
29-
@AllArgsConstructor
30-
class Person {
31-
32-
@Id Integer id;
33-
String firstname, lastname;
34-
Integer age;
27+
record Person(@Id Integer id, String firstname, String lastname, Integer age) {
3528

3629
boolean hasId() {
3730
return id != null;
3831
}
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) {
36+
return true;
37+
}
38+
if (!(o instanceof Person person)) {
39+
return false;
40+
}
41+
return Objects.equals(firstname, person.firstname) && Objects.equals(lastname, person.lastname)
42+
&& Objects.equals(age, person.age);
43+
}
44+
45+
@Override
46+
public int hashCode() {
47+
return Objects.hash(firstname, lastname, age);
48+
}
3949
}

r2dbc/query-by-example/src/test/java/example/springdata/r2dbc/basics/PersonRepositoryIntegrationTests.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void setUp() {
5252

5353
Hooks.onOperatorDebug();
5454

55-
List<String> statements = Arrays.asList(//
55+
var statements = Arrays.asList(//
5656
"DROP TABLE IF EXISTS person;",
5757
"CREATE TABLE person (id SERIAL PRIMARY KEY, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL, age INTEGER NOT NULL);");
5858

@@ -75,7 +75,7 @@ void setUp() {
7575
@Test
7676
void countBySimpleExample() {
7777

78-
Example<Person> example = Example.of(new Person(null, null, "White", null));
78+
var example = Example.of(new Person(null, null, "White", null));
7979

8080
people.count(example).as(StepVerifier::create) //
8181
.expectNext(3L) //
@@ -85,7 +85,7 @@ void countBySimpleExample() {
8585
@Test
8686
void ignorePropertiesAndMatchByAge() {
8787

88-
Example<Person> example = Example.of(flynn, matching(). //
88+
var example = Example.of(flynn, matching(). //
8989
withIgnorePaths("firstname", "lastname"));
9090

9191
people.findOne(example) //
@@ -97,7 +97,7 @@ void ignorePropertiesAndMatchByAge() {
9797
@Test
9898
void substringMatching() {
9999

100-
Example<Person> example = Example.of(new Person(null, "er", null, null), matching(). //
100+
var example = Example.of(new Person(null, "er", null, null), matching(). //
101101
withStringMatcher(StringMatcher.ENDING));
102102

103103
people.findAll(example).collectList() //
@@ -111,7 +111,7 @@ void substringMatching() {
111111
@Test
112112
void matchStartingStringsIgnoreCase() {
113113

114-
Example<Person> example = Example.of(new Person(null, "Walter", "WHITE", null), matching(). //
114+
var example = Example.of(new Person(null, "Walter", "WHITE", null), matching(). //
115115
withIgnorePaths("age"). //
116116
withMatcher("firstname", startsWith()). //
117117
withMatcher("lastname", ignoreCase()));
@@ -127,7 +127,7 @@ void matchStartingStringsIgnoreCase() {
127127
@Test
128128
void configuringMatchersUsingLambdas() {
129129

130-
Example<Person> example = Example.of(new Person(null, "Walter", "WHITE", null), matching(). //
130+
var example = Example.of(new Person(null, "Walter", "WHITE", null), matching(). //
131131
withIgnorePaths("age"). //
132132
withMatcher("firstname", GenericPropertyMatcher::startsWith). //
133133
withMatcher("lastname", GenericPropertyMatcher::ignoreCase));
@@ -143,7 +143,7 @@ void configuringMatchersUsingLambdas() {
143143
@Test
144144
void valueTransformer() {
145145

146-
Example<Person> example = Example.of(new Person(null, null, "White", 99), matching(). //
146+
var example = Example.of(new Person(null, null, "White", 99), matching(). //
147147
withMatcher("age", matcher -> matcher.transform(value -> Optional.of(50))));
148148

149149
people.findAll(example).collectList() //

0 commit comments

Comments
 (0)