#297 - Upgraded to Boot 2.0 and Spring Data Kay.

Bumped version number to 2.0. Upgraded to Spring Boot 2.0.

Stuff disabled in the meantime:

- Cassandra: needs API adaptions in configuration
- JPA > Security: test fails with weird Hibernate error
- Redis > Reactive: API updates needed
- Solr: configration updates necessary

adjust versions

Updated elastic search to the new version.

Fixed the reactor version to Bismuth-BUILD-SNAPSHOT. This probably should be undone when boot references the proper bom.
This commit is contained in:
Oliver Gierke
2017-05-04 19:28:40 +02:00
parent 90546357c7
commit 4164bc4607
98 changed files with 319 additions and 368 deletions

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@@ -15,8 +15,7 @@
*/
package example.springdata.jpa.eclipselink;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import javax.transaction.Transactional;
@@ -43,6 +42,6 @@ public class CustomerRepositoryIntegrationTests {
Customer dave = customers.save(new Customer("Dave", "Matthews"));
assertThat(customers.findOne(dave.getId()), is(dave));
assertThat(customers.findById(dave.getId())).hasValue(dave);
}
}

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@@ -15,6 +15,8 @@
*/
package example.springdata.jpa.auditing;
import java.util.Optional;
import org.springframework.data.domain.AuditorAware;
/**
@@ -27,20 +29,20 @@ import org.springframework.data.domain.AuditorAware;
*/
public class AuditorAwareImpl implements AuditorAware<AuditableUser> {
private AuditableUser auditor;
private Optional<AuditableUser> auditor = Optional.empty();
/**
* @param auditor the auditor to set
*/
public void setAuditor(AuditableUser auditor) {
this.auditor = auditor;
this.auditor = Optional.of(auditor);
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.AuditorAware#getCurrentAuditor()
*/
public AuditableUser getCurrentAuditor() {
public Optional<AuditableUser> getCurrentAuditor() {
return auditor;
}
}

View File

@@ -15,7 +15,10 @@
*/
package example.springdata.jpa.basics;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import example.springdata.jpa.simple.SimpleUserRepository;
import example.springdata.jpa.simple.User;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
@@ -26,9 +29,6 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import example.springdata.jpa.simple.SimpleUserRepository;
import example.springdata.jpa.simple.User;
/**
* Test case showing how to use the basic {@link GenericDaoFactory}
*
@@ -81,8 +81,8 @@ public class BasicFactorySetup {
@Test
public void executingFinders() {
assertEquals(user, userRepository.findByTheUsersName("username"));
assertEquals(user, userRepository.findByLastname("lastname").get(0));
assertEquals(user, userRepository.findByFirstname("firstname").get(0));
assertThat(userRepository.findByTheUsersName("username")).isEqualTo(user);
assertThat(userRepository.findByLastname("lastname")).first().isEqualTo(user);
assertThat(userRepository.findByFirstname("firstname")).first().isEqualTo(user);
}
}

View File

@@ -19,7 +19,9 @@ ess or implied.
*/
package example.springdata.jpa.basics;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import example.springdata.jpa.simple.User;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
@@ -31,8 +33,6 @@ import org.junit.Test;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.CrudRepository;
import example.springdata.jpa.simple.User;
/**
* This unit tests shows plain usage of {@link SimpleJpaRepository}.
*
@@ -41,8 +41,8 @@ import example.springdata.jpa.simple.User;
*/
public class BasicSample {
private CrudRepository<User, Long> userRepository;
private EntityManager em;
CrudRepository<User, Long> userRepository;
EntityManager em;
/**
* Sets up a {@link SimpleJpaRepository} instance.
@@ -65,7 +65,7 @@ public class BasicSample {
/**
* Tests saving users. Don't mimic transactionality shown here. It seriously lacks resource cleanup in case of an
* exception. Simplification serves descriptivness.
* exception. Simplification serves descriptiveness.
*/
@Test
public void savingUsers() {
@@ -75,6 +75,6 @@ public class BasicSample {
user = userRepository.save(user);
assertEquals(user, userRepository.findOne(user.getId()));
assertThat(userRepository.findById(user.getId())).hasValue(user);
}
}

View File

@@ -15,15 +15,13 @@
*/
package example.springdata.jpa.caching;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.test.context.junit4.SpringRunner;
@@ -51,12 +49,10 @@ public abstract class CachingRepositoryTests {
dave = repository.save(dave);
User result = repository.findByUsername("dmatthews");
assertThat(result, is(dave));
assertThat(repository.findByUsername("dmatthews")).isEqualTo(dave);
// Verify entity cached
Cache cache = cacheManager.getCache("byUsername");
ValueWrapper wrapper = cache.get("dmatthews");
assertThat(wrapper.get(), is((Object) dave));
assertThat(cache.get("dmatthews").get()).isEqualTo(dave);
}
}

View File

@@ -15,7 +15,7 @@
*/
package example.springdata.jpa.custom;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.util.List;
@@ -51,7 +51,7 @@ public class UserRepositoryCustomizationTests {
user = repository.save(user);
assertEquals(user, repository.findOne(user.getId()));
assertThat(repository.findById(user.getId())).hasValue(user);
}
@Test
@@ -65,11 +65,8 @@ public class UserRepositoryCustomizationTests {
List<User> users = repository.findByLastname("lastname");
assertNotNull(users);
assertTrue(users.contains(user));
User reference = repository.findByTheUsersName("foobar");
assertEquals(user, reference);
assertThat(users).contains(user);
assertThat(user).isEqualTo(repository.findByTheUsersName("foobar"));
}
/**
@@ -85,7 +82,6 @@ public class UserRepositoryCustomizationTests {
List<User> users = repository.myCustomBatchOperation();
assertNotNull(users);
assertTrue(users.contains(user));
assertThat(users).contains(user);
}
}

View File

@@ -15,8 +15,7 @@
*/
package example.springdata.jpa.customall;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -40,6 +39,6 @@ public class UserRepositoryCustomizationTests {
@Test
public void invokesCustomMethod() {
assertThat(repository.customMethod(), is(0L));
assertThat(repository.customMethod()).isEqualTo(0L);
}
}

View File

@@ -15,8 +15,7 @@
*/
package example.springdata.jpa.projections;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.util.Collection;
import java.util.Map;
@@ -64,37 +63,35 @@ public class CustomerRepositoryIntegrationTest {
@Test
public void projectsEntityIntoInterface() {
Collection<CustomerProjection> result = customers.findAllProjectedBy();
assertThat(result, hasSize(2));
assertThat(result.iterator().next().getFirstname(), is("Dave"));
assertThat(customers.findAllProjectedBy())//
.hasSize(2)//
.first().satisfies(it -> assertThat(it.getFirstname()).isEqualTo("Dave"));
}
@Test
public void projectsMapIntoInterface() {
Collection<CustomerProjection> result = customers.findsByProjectedColumns();
assertThat(customers.findsByProjectedColumns())//
.hasSize(2)//
.first().satisfies(it -> assertThat(it.getFirstname()).isEqualTo("Dave"));
assertThat(result, hasSize(2));
assertThat(result.iterator().next().getFirstname(), is("Dave"));
}
@Test
public void projectsToDto() {
Collection<CustomerDto> result = customers.findAllDtoedBy();
assertThat(result, hasSize(2));
assertThat(result.iterator().next().getFirstname(), is("Dave"));
assertThat(customers.findAllDtoedBy())//
.hasSize(2)//
.first().satisfies(it -> assertThat(it.getFirstname()).isEqualTo("Dave"));
}
@Test
public void projectsDynamically() {
Collection<CustomerProjection> result = customers.findByFirstname("Dave", CustomerProjection.class);
assertThat(result, hasSize(1));
assertThat(result.iterator().next().getFirstname(), is("Dave"));
assertThat(customers.findByFirstname("Dave", CustomerProjection.class))//
.hasSize(1)//
.first()//
.satisfies(it -> assertThat(it.getFirstname()).isEqualTo("Dave"));
}
@Test
@@ -102,10 +99,11 @@ public class CustomerRepositoryIntegrationTest {
CustomerSummary result = customers.findProjectedById(dave.getId(), CustomerSummary.class);
assertThat(result.getFullName(), is("Dave Matthews"));
assertThat(result.getFullName()).isEqualTo("Dave Matthews");
// Proxy backed by original instance as the projection uses dynamic elements
assertThat(((TargetAware) result).getTarget(), is(instanceOf(Customer.class)));
assertThat(result).isInstanceOfSatisfying(TargetAware.class,
it -> assertThat(it.getTarget()).isInstanceOf(Customer.class));
}
@Test
@@ -113,8 +111,9 @@ public class CustomerRepositoryIntegrationTest {
CustomerProjection projectedDave = customers.findProjectedById(dave.getId());
assertThat(projectedDave.getFirstname(), is("Dave"));
assertThat(((TargetAware) projectedDave).getTarget(), is(instanceOf(Map.class)));
assertThat(projectedDave.getFirstname()).isEqualTo("Dave");
assertThat(projectedDave).isInstanceOfSatisfying(TargetAware.class,
it -> assertThat(it.getTarget()).isInstanceOf(Map.class));
}
@Test
@@ -122,21 +121,21 @@ public class CustomerRepositoryIntegrationTest {
Collection<CustomerDto> result = customers.findDtoWithConstructorExpression("Dave");
assertThat(result, hasSize(1));
assertThat(result.iterator().next().getFirstname(), is("Dave"));
assertThat(result).hasSize(1);
assertThat(result.iterator().next().getFirstname()).isEqualTo("Dave");
}
@Test
public void supportsProjectionInCombinationWithPagination() {
Page<CustomerProjection> page = customers
.findPagedProjectedBy(new PageRequest(0, 1, new Sort(Direction.ASC, "lastname")));
.findPagedProjectedBy(PageRequest.of(0, 1, Sort.by(Direction.ASC, "lastname")));
assertThat(page.getContent().get(0).getFirstname(), is("Carter"));
assertThat(page.getContent().get(0).getFirstname()).isEqualTo("Carter");
}
@Test
public void appliesProjectionToOptional() {
assertThat(customers.findOptionalProjectionByLastname("Beauford").isPresent(), is(true));
assertThat(customers.findOptionalProjectionByLastname("Beauford")).isPresent();
}
}

View File

@@ -15,8 +15,9 @@
*/
package example.springdata.jpa.simple;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import static org.springframework.data.domain.Sort.Direction.*;
import java.util.ArrayList;
@@ -64,7 +65,7 @@ public class SimpleUserRepositoryTests {
user = repository.save(user);
assertThat(repository.findOne(user.getId()), is(user));
assertThat(repository.findById(user.getId())).hasValue(user);
}
@Test
@@ -72,10 +73,7 @@ public class SimpleUserRepositoryTests {
user = repository.save(user);
List<User> users = repository.findByLastname("lastname");
assertThat(users, is(notNullValue()));
assertThat(users.contains(user), is(true));
assertThat(repository.findByLastname("lastname")).contains(user);
}
@Test
@@ -83,19 +81,17 @@ public class SimpleUserRepositoryTests {
user = repository.save(user);
List<User> users = repository.findByFirstnameOrLastname("lastname");
assertThat(users.contains(user), is(true));
assertThat(repository.findByFirstnameOrLastname("lastname")).contains(user);
}
@Test
public void useOptionalAsReturnAndParameterType() {
assertThat(repository.findByUsername(Optional.of("foobar")), is(Optional.empty()));
assertThat(repository.findByUsername(Optional.of("foobar"))).isEmpty();
repository.save(user);
assertThat(repository.findByUsername(Optional.of("foobar")).isPresent(), is(true));
assertThat(repository.findByUsername(Optional.of("foobar"))).isPresent();
}
@Test
@@ -109,10 +105,10 @@ public class SimpleUserRepositoryTests {
User user3 = new User();
user3.setLastname("no-positive-match");
repository.save(Arrays.asList(user, user2, user3));
repository.saveAll(Arrays.asList(user, user2, user3));
assertThat(repository.removeByLastname(user.getLastname()), is(2L));
assertThat(repository.exists(user3.getId()), is(true));
assertThat(repository.removeByLastname(user.getLastname())).isEqualTo(2L);
assertThat(repository.existsById(user3.getId())).isTrue();
}
@Test
@@ -132,11 +128,11 @@ public class SimpleUserRepositoryTests {
source.add(user);
}
repository.save(source);
repository.saveAll(source);
Slice<User> users = repository.findByLastnameOrderByUsernameAsc(this.user.getLastname(), new PageRequest(1, 5));
Slice<User> users = repository.findByLastnameOrderByUsernameAsc(this.user.getLastname(), PageRequest.of(1, 5));
assertThat(users, contains(source.subList(5, 10).toArray()));
assertThat(users).containsAll(source.subList(5, 10));
}
@Test
@@ -152,7 +148,7 @@ public class SimpleUserRepositoryTests {
user2.setLastname("lastname-2");
// we deliberatly save the items in reverse
repository.save(Arrays.asList(user2, user1, user0));
repository.saveAll(Arrays.asList(user2, user1, user0));
List<User> result = repository.findFirst2ByOrderByLastnameAsc();
@@ -173,7 +169,7 @@ public class SimpleUserRepositoryTests {
user2.setLastname("lastname-2");
// we deliberately save the items in reverse
repository.save(Arrays.asList(user2, user1, user0));
repository.saveAll(Arrays.asList(user2, user1, user0));
List<User> resultAsc = repository.findTop2By(new Sort(ASC, "lastname"));
@@ -197,7 +193,7 @@ public class SimpleUserRepositoryTests {
User third = new User();
repository.save(Arrays.asList(first, second, third));
repository.saveAll(Arrays.asList(first, second, third));
User reference = new User();
reference.setFirstname("firstname");

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-data-jpa-java8</artifactId>

View File

@@ -39,7 +39,7 @@ public interface CustomerRepository extends Repository<Customer, Long> {
* @param id
* @return
*/
Optional<Customer> findOne(Long id);
Optional<Customer> findById(Long id);
/**
* Saves the given {@link Customer}.

View File

@@ -15,8 +15,7 @@
*/
package example.springdata.jpa.java8;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import lombok.extern.slf4j.Slf4j;
@@ -54,8 +53,8 @@ public class Java8IntegrationTests {
Customer carter = repository.save(new Customer("Carter", "Beauford"));
assertThat(repository.findOne(carter.id).isPresent(), is(true));
assertThat(repository.findOne(carter.id + 1), is(Optional.<Customer> empty()));
assertThat(repository.findById(carter.id)).isPresent();
assertThat(repository.findById(carter.id + 1)).isEmpty();
}
@Test
@@ -63,8 +62,8 @@ public class Java8IntegrationTests {
Customer customer = repository.save(new Customer("Dave", "Matthews"));
assertThat(customer.createdDate, is(notNullValue()));
assertThat(customer.modifiedDate, is(notNullValue()));
assertThat(customer.createdDate).isNotNull();
assertThat(customer.modifiedDate).isNotNull();
}
@Test
@@ -73,8 +72,7 @@ public class Java8IntegrationTests {
Customer customer = repository.save(new Customer("Dave", "Matthews"));
Optional<Customer> result = repository.findByLastname(customer);
assertThat(result.isPresent(), is(true));
assertThat(result.get(), is(customer));
assertThat(result).hasValue(customer);
}
/**
@@ -88,7 +86,7 @@ public class Java8IntegrationTests {
Customer customer2 = repository.save(new Customer("Customer2", "Bar"));
try (Stream<Customer> stream = repository.streamAllCustomers()) {
assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2));
assertThat(stream.collect(Collectors.toList())).contains(customer1, customer2);
}
}
@@ -103,7 +101,7 @@ public class Java8IntegrationTests {
Customer customer2 = repository.save(new Customer("Customer2", "Bar"));
try (Stream<Customer> stream = repository.findAllByLastnameIsNotNull()) {
assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2));
assertThat(stream.collect(Collectors.toList())).contains(customer1, customer2);
}
}
@@ -131,7 +129,7 @@ public class Java8IntegrationTests {
CompletableFuture<Void> future = repository.readAllBy().thenAccept(customers -> {
assertThat(customers, hasSize(2));
assertThat(customers).hasSize(2);
customers.forEach(customer -> log.info(customer.toString()));
log.info("Completed!");
});

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-data-jpa-javaslang</artifactId>

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-data-jpa-jpa21</artifactId>

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-data-jpa-multiple-datasources</artifactId>

View File

@@ -8,7 +8,7 @@
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-examples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<name>Spring Data JPA - Examples</name>
@@ -23,7 +23,7 @@
<module>java8</module>
<module>javaslang</module>
<module>jpa21</module>
<module>security</module>
<!--<module>security</module>-->
<module>multiple-datasources</module>
<module>eclipselink</module>
<module>query-by-example</module>

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-data-jpa-query-by-example</artifactId>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@@ -15,23 +15,23 @@
*/
package example.springdata.jpa.showcase.snippets;
import java.util.List;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import example.springdata.jpa.showcase.core.Account;
import example.springdata.jpa.showcase.core.Customer;
import java.util.List;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
/**
* Repository to manage {@link Account} instances.
*
* @author Oliver Gierke
*/
@NoRepositoryBean
public interface AccountRepository extends CrudRepository<Account, Long>, AccountRepositoryCustom,
QueryDslPredicateExecutor<Account> {
public interface AccountRepository
extends CrudRepository<Account, Long>, AccountRepositoryCustom, QuerydslPredicateExecutor<Account> {
/**
* Returns all accounts belonging to the given {@link Customer}.

View File

@@ -16,14 +16,17 @@
package example.springdata.jpa.showcase.snippets.test;
import static example.springdata.jpa.showcase.snippets.AccountPredicates.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.joda.time.LocalDate;
import static org.junit.Assert.assertThat;
import example.springdata.jpa.showcase.core.Account;
import example.springdata.jpa.showcase.snippets.AccountRepository;
import java.util.Optional;
import org.joda.time.LocalDate;
/**
* @author Oliver Gierke
*/
@@ -39,12 +42,12 @@ public abstract class AccountRepositoryIntegrationTest {
public void findsExpiredAccounts() {
Account expired = accountRepository.findOne(1L);
Account valid = accountRepository.findOne(2L);
Optional<Account> expired = accountRepository.findById(1L);
Optional<Account> valid = accountRepository.findById(2L);
Iterable<Account> findAll = accountRepository.findAll(expiresBefore(new LocalDate(2011, 3, 1)));
assertThat(findAll, hasItem(expired));
assertThat(findAll, not(hasItem(valid)));
assertThat(findAll).contains(expired.get());
assertThat(findAll).doesNotContain(valid.get());
}
}

View File

@@ -16,18 +16,18 @@
package example.springdata.jpa.showcase.snippets.test;
import static example.springdata.jpa.showcase.snippets.CustomerSpecifications.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.jpa.domain.Specifications.*;
import java.util.List;
import org.joda.time.LocalDate;
import org.springframework.data.jpa.domain.Specification;
import example.springdata.jpa.showcase.after.CustomerRepository;
import example.springdata.jpa.showcase.core.Customer;
import java.util.List;
import java.util.Optional;
import org.joda.time.LocalDate;
import org.springframework.data.jpa.domain.Specification;
/**
* Snippets to show the usage of {@link Specification}s.
*
@@ -39,12 +39,12 @@ public class CustomerRepositoryIntegrationTest {
public void findsCustomersBySpecification() throws Exception {
Customer dave = repository.findOne(1L);
Optional<Customer> dave = repository.findById(1L);
LocalDate expiryLimit = new LocalDate(2011, 3, 1);
List<Customer> result = repository.findAll(where(accountExpiresBefore(expiryLimit)));
assertThat(result.size(), is(1));
assertThat(result, hasItems(dave));
assertThat(result).hasSize(1);
assertThat(dave).hasValueSatisfying(it -> assertThat(result).contains(it));
}
}

View File

@@ -15,18 +15,19 @@
*/
package example.springdata.jpa.showcase.after;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.assertj.core.api.Assertions.*;
import example.springdata.jpa.showcase.AbstractShowcaseTest;
import example.springdata.jpa.showcase.core.Account;
import example.springdata.jpa.showcase.core.Customer;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Integration tests for Spring Data JPA {@link AccountRepository}.
*
@@ -41,16 +42,17 @@ public class AccountRepositoryIntegrationTest extends AbstractShowcaseTest {
public void savesAccount() {
Account account = accountRepository.save(new Account());
assertThat(account.getId(), is(notNullValue()));
assertThat(account.getId()).isNotNull();
}
@Test
public void findsCustomersAccounts() {
Customer customer = customerRepository.findOne(1L);
List<Account> accounts = accountRepository.findByCustomer(customer);
Optional<Customer> customer = customerRepository.findById(1L);
List<Account> accounts = customer.map(accountRepository::findByCustomer).orElse(Collections.emptyList());
assertFalse(accounts.isEmpty());
assertThat(accounts.get(0).getCustomer(), is(customer));
assertThat(accounts).isNotEmpty();
assertThat(customer).hasValueSatisfying(it -> assertThat(accounts.get(0).getCustomer()).isEqualTo(it));
}
}

View File

@@ -16,11 +16,14 @@
package example.springdata.jpa.showcase.after;
import static example.springdata.jpa.showcase.snippets.CustomerSpecifications.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.jpa.domain.Specifications.*;
import example.springdata.jpa.showcase.AbstractShowcaseTest;
import example.springdata.jpa.showcase.core.Customer;
import java.util.List;
import java.util.Optional;
import org.joda.time.LocalDate;
import org.junit.Test;
@@ -28,9 +31,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import example.springdata.jpa.showcase.AbstractShowcaseTest;
import example.springdata.jpa.showcase.core.Customer;
/**
* Integration tests for Spring Data JPA {@link CustomerRepository}.
*
@@ -42,40 +42,36 @@ public class CustomerRepositoryIntegrationTest extends AbstractShowcaseTest {
@Test
public void findsAllCustomers() throws Exception {
Iterable<Customer> result = repository.findAll();
assertThat(result, is(notNullValue()));
assertTrue(result.iterator().hasNext());
assertThat(repository.findAll()).isNotEmpty();
}
@Test
public void findsFirstPageOfMatthews() throws Exception {
Page<Customer> customers = repository.findByLastname("Matthews", new PageRequest(0, 2));
Page<Customer> customers = repository.findByLastname("Matthews", PageRequest.of(0, 2));
assertThat(customers.getContent().size(), is(2));
assertFalse(customers.hasPrevious());
assertThat(customers.getContent()).hasSize(2);
assertThat(customers.hasPrevious()).isFalse();
}
@Test
public void findsCustomerById() throws Exception {
Customer customer = repository.findOne(2L);
assertThat(customer.getFirstname(), is("Carter"));
assertThat(customer.getLastname(), is("Beauford"));
assertThat(repository.findById(2L)).hasValueSatisfying(it -> {
assertThat(it.getFirstname()).isEqualTo("Carter");
assertThat(it.getLastname()).isEqualTo("Beauford");
});
}
@Test
public void findsCustomersBySpecification() throws Exception {
Customer dave = repository.findOne(1L);
Optional<Customer> dave = repository.findById(1L);
LocalDate expiryLimit = new LocalDate(2011, 3, 1);
List<Customer> result = repository.findAll(where(accountExpiresBefore(expiryLimit)));
assertThat(result.size(), is(1));
assertThat(result, hasItems(dave));
assertThat(result).hasSize(1);
assertThat(dave).hasValueSatisfying(it -> assertThat(result).contains(it));
}
}