1
pom.xml
1
pom.xml
@@ -39,6 +39,7 @@
|
||||
<maven.compiler.target>16</maven.compiler.target>
|
||||
<byte-buddy.version>1.11.0</byte-buddy.version>
|
||||
<testcontainers.version>1.15.3</testcontainers.version>
|
||||
<kotlin.version>1.5.0</kotlin.version>
|
||||
<apt.version>1.1.3</apt.version>
|
||||
<jvm.enable-preview></jvm.enable-preview>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;"
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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) //
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package example.springdata.r2dbc.basics;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
@@ -25,15 +24,26 @@ import org.springframework.data.annotation.Id;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
class Person {
|
||||
|
||||
@Id Integer id;
|
||||
String firstname, lastname;
|
||||
Integer age;
|
||||
record Person(@Id Integer id, String firstname, String lastname, Integer age) {
|
||||
|
||||
boolean hasId() {
|
||||
return id != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof Person person)) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(firstname, person.firstname) && Objects.equals(lastname, person.lastname)
|
||||
&& Objects.equals(age, person.age);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(firstname, lastname, age);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ class PersonRepositoryIntegrationTests {
|
||||
|
||||
Hooks.onOperatorDebug();
|
||||
|
||||
List<String> statements = Arrays.asList(//
|
||||
var statements = Arrays.asList(//
|
||||
"DROP TABLE IF EXISTS person;",
|
||||
"CREATE TABLE person (id SERIAL PRIMARY KEY, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL, age INTEGER NOT NULL);");
|
||||
|
||||
@@ -75,7 +75,7 @@ class PersonRepositoryIntegrationTests {
|
||||
@Test
|
||||
void countBySimpleExample() {
|
||||
|
||||
Example<Person> example = Example.of(new Person(null, null, "White", null));
|
||||
var example = Example.of(new Person(null, null, "White", null));
|
||||
|
||||
people.count(example).as(StepVerifier::create) //
|
||||
.expectNext(3L) //
|
||||
@@ -85,7 +85,7 @@ class PersonRepositoryIntegrationTests {
|
||||
@Test
|
||||
void ignorePropertiesAndMatchByAge() {
|
||||
|
||||
Example<Person> example = Example.of(flynn, matching(). //
|
||||
var example = Example.of(flynn, matching(). //
|
||||
withIgnorePaths("firstname", "lastname"));
|
||||
|
||||
people.findOne(example) //
|
||||
@@ -97,7 +97,7 @@ class PersonRepositoryIntegrationTests {
|
||||
@Test
|
||||
void substringMatching() {
|
||||
|
||||
Example<Person> example = Example.of(new Person(null, "er", null, null), matching(). //
|
||||
var example = Example.of(new Person(null, "er", null, null), matching(). //
|
||||
withStringMatcher(StringMatcher.ENDING));
|
||||
|
||||
people.findAll(example).collectList() //
|
||||
@@ -111,7 +111,7 @@ class PersonRepositoryIntegrationTests {
|
||||
@Test
|
||||
void matchStartingStringsIgnoreCase() {
|
||||
|
||||
Example<Person> example = Example.of(new Person(null, "Walter", "WHITE", null), matching(). //
|
||||
var example = Example.of(new Person(null, "Walter", "WHITE", null), matching(). //
|
||||
withIgnorePaths("age"). //
|
||||
withMatcher("firstname", startsWith()). //
|
||||
withMatcher("lastname", ignoreCase()));
|
||||
@@ -127,7 +127,7 @@ class PersonRepositoryIntegrationTests {
|
||||
@Test
|
||||
void configuringMatchersUsingLambdas() {
|
||||
|
||||
Example<Person> example = Example.of(new Person(null, "Walter", "WHITE", null), matching(). //
|
||||
var example = Example.of(new Person(null, "Walter", "WHITE", null), matching(). //
|
||||
withIgnorePaths("age"). //
|
||||
withMatcher("firstname", GenericPropertyMatcher::startsWith). //
|
||||
withMatcher("lastname", GenericPropertyMatcher::ignoreCase));
|
||||
@@ -143,7 +143,7 @@ class PersonRepositoryIntegrationTests {
|
||||
@Test
|
||||
void valueTransformer() {
|
||||
|
||||
Example<Person> example = Example.of(new Person(null, null, "White", 99), matching(). //
|
||||
var example = Example.of(new Person(null, null, "White", 99), matching(). //
|
||||
withMatcher("age", matcher -> matcher.transform(value -> Optional.of(50))));
|
||||
|
||||
people.findAll(example).collectList() //
|
||||
|
||||
Reference in New Issue
Block a user