Java 16 migration for R2DBC examples.

See #606.
This commit is contained in:
Mark Paluch
2021-04-28 10:14:45 +02:00
parent 75e2763054
commit a4c9302780
10 changed files with 83 additions and 76 deletions

View File

@@ -15,22 +15,32 @@
*/
package example.springdata.r2dbc.basics;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Objects;
import org.springframework.data.annotation.Id;
/**
* @author Oliver Gierke
*/
@Data
@AllArgsConstructor
class Customer {
@Id Integer id;
String firstname, lastname;
record Customer(@Id Integer id, String firstname, String lastname) {
boolean hasId() {
return id != null;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Customer customer)) {
return false;
}
return Objects.equals(firstname, customer.firstname) && Objects.equals(lastname, customer.lastname);
}
@Override
public int hashCode() {
return Objects.hash(firstname, lastname);
}
}

View File

@@ -42,7 +42,7 @@ class TransactionalService {
return repository.save(customer).map(it -> {
if (it.firstname.equals("Dave")) {
if (it.firstname().equals("Dave")) {
throw new IllegalStateException();
} else {
return it;

View File

@@ -39,7 +39,7 @@ class ApplicationConfiguration {
return (customer, sqlIdentifier) -> {
if (customer.getId() == null) {
if (customer.id() == null) {
return databaseClient.sql("SELECT primary_key.nextval") //
.map(row -> row.get(0, Long.class)) //
@@ -54,7 +54,7 @@ class ApplicationConfiguration {
@Bean
ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {
ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
var initializer = new ConnectionFactoryInitializer();
initializer.setConnectionFactory(connectionFactory);
initializer.setDatabasePopulator(new ResourceDatabasePopulator(new ByteArrayResource(("CREATE SEQUENCE primary_key;"
+ "DROP TABLE IF EXISTS customer;"

View File

@@ -15,20 +15,14 @@
*/
package example.springdata.r2dbc.entitycallback;
import lombok.AllArgsConstructor;
import lombok.Value;
import lombok.With;
import org.springframework.data.annotation.Id;
/**
* @author Mark Paluch
*/
@Value
@AllArgsConstructor
@With
class Customer {
record Customer(@Id Long id, String firstname, String lastname) {
@Id Long id;
String firstname, lastname;
public Customer withId(long id) {
return new Customer(id, firstname, lastname);
}
}

View File

@@ -20,32 +20,29 @@ import reactor.test.StepVerifier;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = InfrastructureConfiguration.class)
public class CustomerRepositoryIntegrationTests {
class CustomerRepositoryIntegrationTests {
@Autowired CustomerRepository customers;
@Autowired DatabaseClient database;
@Before
public void setUp() {
@BeforeEach
void setUp() {
Hooks.onOperatorDebug();
List<String> statements = Arrays.asList(//
var statements = Arrays.asList(//
"DROP TABLE IF EXISTS customer;",
"CREATE TABLE customer ( id SERIAL PRIMARY KEY, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL);");
@@ -58,31 +55,31 @@ public class CustomerRepositoryIntegrationTests {
}
@Test
public void executesFindAll() throws IOException {
void executesFindAll() throws IOException {
Customer dave = new Customer(null, "Dave", "Matthews");
Customer carter = new Customer(null, "Carter", "Beauford");
var dave = new Customer(null, "Dave", "Matthews");
var carter = new Customer(null, "Carter", "Beauford");
insertCustomers(dave, carter);
customers.findAll() //
.as(StepVerifier::create) //
.assertNext(dave::equals) //
.assertNext(carter::equals) //
.expectNext(dave) //
.expectNext(carter) //
.verifyComplete();
}
@Test
public void executesAnnotatedQuery() throws IOException {
void executesAnnotatedQuery() throws IOException {
Customer dave = new Customer(null, "Dave", "Matthews");
Customer carter = new Customer(null, "Carter", "Beauford");
var dave = new Customer(null, "Dave", "Matthews");
var carter = new Customer(null, "Carter", "Beauford");
insertCustomers(dave, carter);
customers.findByLastname("Matthews") //
.as(StepVerifier::create) //
.assertNext(dave::equals) //
.expectNext(dave) //
.verifyComplete();
}

View File

@@ -21,13 +21,12 @@ import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration tests for {@link TransactionalService}.
@@ -35,20 +34,19 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Oliver Drotbohm
* @soundtrack Shame - Tedeschi Trucks Band (Signs)
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = InfrastructureConfiguration.class)
public class TransactionalServiceIntegrationTests {
class TransactionalServiceIntegrationTests {
@Autowired TransactionalService service;
@Autowired CustomerRepository repository;
@Autowired DatabaseClient database;
@Before
public void setUp() {
@BeforeEach
void setUp() {
Hooks.onOperatorDebug();
List<String> statements = Arrays.asList(//
var statements = Arrays.asList(//
"DROP TABLE IF EXISTS customer;",
"CREATE TABLE customer ( id SERIAL PRIMARY KEY, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL);");
@@ -61,7 +59,7 @@ public class TransactionalServiceIntegrationTests {
}
@Test // #500
public void exceptionTriggersRollback() {
void exceptionTriggersRollback() {
service.save(new Customer(null, "Dave", "Matthews")) //
.as(StepVerifier::create) //
@@ -75,7 +73,7 @@ public class TransactionalServiceIntegrationTests {
}
@Test // #500
public void insertsDataTransactionally() {
void insertsDataTransactionally() {
service.save(new Customer(null, "Carter", "Beauford")) //
.as(StepVerifier::create) //

View File

@@ -21,13 +21,11 @@ import reactor.test.StepVerifier;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration test using {@link org.springframework.data.r2dbc.mapping.event.BeforeConvertCallback} to auto-generate an
@@ -36,24 +34,23 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Mark Paluch
* @see ApplicationConfiguration#idGeneratingCallback(DatabaseClient)
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryIntegrationTests {
class CustomerRepositoryIntegrationTests {
@Autowired CustomerRepository customers;
@Autowired DatabaseClient database;
@Test
public void generatesIdOnInsert() throws IOException {
void generatesIdOnInsert() throws IOException {
Customer dave = new Customer(null, "Dave", "Matthews");
var dave = new Customer(null, "Dave", "Matthews");
this.customers.save(dave) //
.as(StepVerifier::create) //
.assertNext(actual -> {
assertThat(dave.getId()).isNull(); // immutable before save
assertThat(actual.getId()).isNotNull(); // after save
assertThat(dave.id()).isNull(); // immutable before save
assertThat(actual.id()).isNotNull(); // after save
}).verifyComplete();
}
}