@@ -27,7 +27,7 @@ import org.springframework.data.domain.AuditorAware;
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class AuditorAwareImpl implements AuditorAware<AuditableUser> {
|
||||
class AuditorAwareImpl implements AuditorAware<AuditableUser> {
|
||||
|
||||
private Optional<AuditableUser> auditor = Optional.empty();
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
* Package showing auditing support with Spring Data repositories.
|
||||
*/
|
||||
package example.springdata.jpa.auditing;
|
||||
package example.springdata.jpa.auditing;
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package example.springdata.jpa.caching;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.Cache;
|
||||
@@ -40,8 +40,8 @@ class CachingConfiguration {
|
||||
|
||||
Cache cache = new ConcurrentMapCache("byUsername");
|
||||
|
||||
SimpleCacheManager manager = new SimpleCacheManager();
|
||||
manager.setCaches(Arrays.asList(cache));
|
||||
var manager = new SimpleCacheManager();
|
||||
manager.setCaches(Collections.singletonList(cache));
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public class User extends AbstractPersistable<Long> {
|
||||
/**
|
||||
* Creates a new user instance.
|
||||
*/
|
||||
public User(Long id) {
|
||||
private User(Long id) {
|
||||
this.setId(id);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,4 +2,3 @@
|
||||
* Sample for the integration of the Spring caching abstraction with Spring Data repositories.
|
||||
*/
|
||||
package example.springdata.jpa.caching;
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ package example.springdata.jpa.compositions;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface Contact {
|
||||
interface Contact {
|
||||
|
||||
/**
|
||||
* @return the first name.
|
||||
|
||||
@@ -22,7 +22,7 @@ import java.util.List;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface ContactRepository {
|
||||
interface ContactRepository {
|
||||
|
||||
/**
|
||||
* Find relatives of this {@link Contact}.
|
||||
|
||||
@@ -20,7 +20,7 @@ package example.springdata.jpa.compositions;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface FlushOnSaveRepository<T> {
|
||||
interface FlushOnSaveRepository<T> {
|
||||
|
||||
/**
|
||||
* Saves a given entity and flush immediately. Use the returned instance for further operations as the save operation
|
||||
|
||||
@@ -57,7 +57,7 @@ public class FlushOnSaveRepositoryImpl<T> implements FlushOnSaveRepository<T> {
|
||||
*/
|
||||
private <S extends T> void doSave(S entity) {
|
||||
|
||||
EntityInformation<Object, S> entityInformation = getEntityInformation(entity);
|
||||
var entityInformation = getEntityInformation(entity);
|
||||
|
||||
if (entityInformation.isNew(entity)) {
|
||||
entityManager.persist(entity);
|
||||
@@ -84,7 +84,7 @@ public class FlushOnSaveRepositoryImpl<T> implements FlushOnSaveRepository<T> {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private <S extends T> EntityInformation<Object, S> getEntityInformation(S entity) {
|
||||
|
||||
Class<?> userClass = ClassUtils.getUserClass(entity.getClass());
|
||||
var userClass = ClassUtils.getUserClass(entity.getClass());
|
||||
|
||||
if (entity instanceof AbstractPersistable<?>) {
|
||||
return new JpaPersistableEntityInformation(userClass, entityManager.getMetamodel());
|
||||
|
||||
@@ -19,7 +19,6 @@ import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
|
||||
/**
|
||||
* Implementation fo the custom repository functionality declared in {@link UserRepositoryCustom} based on JPA. To use
|
||||
@@ -66,7 +65,7 @@ class UserRepositoryImpl implements UserRepositoryCustom {
|
||||
*/
|
||||
public List<User> myCustomBatchOperation() {
|
||||
|
||||
CriteriaQuery<User> criteriaQuery = em.getCriteriaBuilder().createQuery(User.class);
|
||||
var criteriaQuery = em.getCriteriaBuilder().createQuery(User.class);
|
||||
criteriaQuery.select(criteriaQuery.from(User.class));
|
||||
return em.createQuery(criteriaQuery).getResultList();
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ class UserRepositoryImplJdbc extends JdbcDaoSupport implements UserRepositoryCus
|
||||
*/
|
||||
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
|
||||
User user = new User(rs.getLong("id"));
|
||||
var user = new User(rs.getLong("id"));
|
||||
user.setUsername(rs.getString("username"));
|
||||
user.setLastname(rs.getString("lastname"));
|
||||
user.setFirstname(rs.getString("firstname"));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Package showing a repository interface to use basic query method execution functionality as well as <em>customized</em> repository functionality.
|
||||
* Package showing a repository interface to use basic query method execution functionality as well as
|
||||
* <em>customized</em> repository functionality.
|
||||
*/
|
||||
package example.springdata.jpa.custom;
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ import javax.persistence.Id;
|
||||
* @soundtrack Tim Neuhaus - As life found you (The Cabinet)
|
||||
*/
|
||||
@Entity
|
||||
public class User {
|
||||
class User {
|
||||
|
||||
private @Id @GeneratedValue Long id;
|
||||
}
|
||||
|
||||
@@ -15,13 +15,13 @@
|
||||
*/
|
||||
package example.springdata.jpa.projections;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
|
||||
@@ -15,15 +15,9 @@
|
||||
*/
|
||||
package example.springdata.jpa.projections;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Data
|
||||
@RequiredArgsConstructor
|
||||
public class CustomerDto {
|
||||
public record CustomerDto(String firstname) {
|
||||
|
||||
private final String firstname;
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package example.springdata.jpa.simple;
|
||||
|
||||
import example.springdata.jpa.projections.Customer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@@ -2,4 +2,3 @@
|
||||
* Package showing a simple repository interface to use basic query method execution functionality.
|
||||
*/
|
||||
package example.springdata.jpa.simple;
|
||||
|
||||
|
||||
@@ -15,24 +15,22 @@
|
||||
*/
|
||||
package example.springdata.jpa.auditing;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Divya Srivastava
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@Transactional
|
||||
@SpringBootTest
|
||||
public class AuditableUserSample {
|
||||
@@ -42,11 +40,11 @@ public class AuditableUserSample {
|
||||
@Autowired AuditingEntityListener listener;
|
||||
|
||||
@Test
|
||||
public void auditEntityCreation() throws Exception {
|
||||
void auditEntityCreation() {
|
||||
|
||||
assertThat(ReflectionTestUtils.getField(listener, "handler")).isNotNull();
|
||||
|
||||
AuditableUser user = new AuditableUser();
|
||||
var user = new AuditableUser();
|
||||
user.setUsername("username");
|
||||
|
||||
auditorAware.setAuditor(user);
|
||||
|
||||
@@ -35,7 +35,7 @@ import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
|
||||
* @author Oliver Gierke
|
||||
* @author Divya Srivastava
|
||||
*/
|
||||
public class BasicFactorySetup {
|
||||
class BasicFactorySetup {
|
||||
|
||||
private static final EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa.sample.plain");
|
||||
|
||||
@@ -50,7 +50,7 @@ public class BasicFactorySetup {
|
||||
* @throws Exception
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
void setUp() {
|
||||
|
||||
em = factory.createEntityManager();
|
||||
|
||||
@@ -71,7 +71,7 @@ public class BasicFactorySetup {
|
||||
* Rollback transaction.
|
||||
*/
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
void tearDown() {
|
||||
|
||||
em.getTransaction().rollback();
|
||||
}
|
||||
@@ -80,7 +80,7 @@ public class BasicFactorySetup {
|
||||
* Showing invocation of finder method.
|
||||
*/
|
||||
@Test
|
||||
public void executingFinders() {
|
||||
void executingFinders() {
|
||||
|
||||
assertThat(userRepository.findByTheUsersName("username")).isEqualTo(user);
|
||||
assertThat(userRepository.findByLastname("lastname")).first().isEqualTo(user);
|
||||
|
||||
@@ -24,12 +24,12 @@ import static org.assertj.core.api.Assertions.*;
|
||||
import example.springdata.jpa.simple.User;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
@@ -40,27 +40,27 @@ import org.springframework.data.repository.CrudRepository;
|
||||
* @author Thomas Darimont
|
||||
* @author Divya Srivastava
|
||||
*/
|
||||
public class BasicSample {
|
||||
class BasicSample {
|
||||
|
||||
CrudRepository<User, Long> userRepository;
|
||||
EntityManager em;
|
||||
private CrudRepository<User, Long> userRepository;
|
||||
private EntityManager em;
|
||||
|
||||
/**
|
||||
* Sets up a {@link SimpleJpaRepository} instance.
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
void setUp() {
|
||||
|
||||
EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa.sample.plain");
|
||||
var factory = Persistence.createEntityManagerFactory("jpa.sample.plain");
|
||||
em = factory.createEntityManager();
|
||||
|
||||
userRepository = new SimpleJpaRepository<User, Long>(User.class, em);
|
||||
userRepository = new SimpleJpaRepository<>(User.class, em);
|
||||
|
||||
em.getTransaction().begin();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
void tearDown() {
|
||||
em.getTransaction().rollback();
|
||||
}
|
||||
|
||||
@@ -69,9 +69,9 @@ public class BasicSample {
|
||||
* exception. Simplification serves descriptiveness.
|
||||
*/
|
||||
@Test
|
||||
public void savingUsers() {
|
||||
void savingUsers() {
|
||||
|
||||
User user = new User();
|
||||
var user = new User();
|
||||
user.setUsername("username");
|
||||
|
||||
user = userRepository.save(user);
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
/**
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
package example.springdata.jpa.basics;
|
||||
|
||||
@@ -15,16 +15,14 @@
|
||||
*/
|
||||
package example.springdata.jpa.caching;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
@@ -35,18 +33,17 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* @author Andrea Rizzini
|
||||
* @author Divya Srivastava
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@Transactional
|
||||
@SpringBootTest
|
||||
public class CachingRepositoryTests {
|
||||
class CachingRepositoryTests {
|
||||
|
||||
@Autowired CachingUserRepository repository;
|
||||
@Autowired CacheManager cacheManager;
|
||||
|
||||
@Test
|
||||
public void checkCachedValue() {
|
||||
void checkCachedValue() {
|
||||
|
||||
User dave = new User();
|
||||
var dave = new User();
|
||||
dave.setUsername("dmatthews");
|
||||
|
||||
dave = repository.save(dave);
|
||||
@@ -54,19 +51,19 @@ public class CachingRepositoryTests {
|
||||
assertThat(repository.findByUsername("dmatthews")).isEqualTo(dave);
|
||||
|
||||
// Verify entity cached
|
||||
Cache cache = cacheManager.getCache("byUsername");
|
||||
var cache = cacheManager.getCache("byUsername");
|
||||
assertThat(cache.get("dmatthews").get()).isEqualTo(dave);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkCacheEviction() {
|
||||
void checkCacheEviction() {
|
||||
|
||||
User dave = new User();
|
||||
var dave = new User();
|
||||
dave.setUsername("dmatthews");
|
||||
repository.save(dave);
|
||||
|
||||
// Verify entity evicted on cache
|
||||
Cache cache = cacheManager.getCache("byUsername");
|
||||
assertThat(cache.get("dmatthews")).isEqualTo(null);
|
||||
var cache = cacheManager.getCache("byUsername");
|
||||
assertThat(cache.get("dmatthews")).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,15 +15,14 @@
|
||||
*/
|
||||
package example.springdata.jpa.compositions;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
@@ -32,10 +31,10 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* @author Mark Paluch
|
||||
* @author Divya Srivastava
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
|
||||
@Transactional
|
||||
@SpringBootTest
|
||||
public class ComposedRepositoryTests {
|
||||
class ComposedRepositoryTests {
|
||||
|
||||
@Autowired UserRepository repository;
|
||||
|
||||
@@ -43,9 +42,9 @@ public class ComposedRepositoryTests {
|
||||
* Tests inserting a user and asserts it can be loaded again.
|
||||
*/
|
||||
@Test
|
||||
public void testInsert() {
|
||||
void testInsert() {
|
||||
|
||||
User user = new User();
|
||||
var user = new User();
|
||||
user.setUsername("username");
|
||||
|
||||
user = repository.save(user);
|
||||
@@ -57,14 +56,14 @@ public class ComposedRepositoryTests {
|
||||
* Testing {@link ContactRepository} fragment.
|
||||
*/
|
||||
@Test
|
||||
public void testContactRepository() {
|
||||
void testContactRepository() {
|
||||
|
||||
User walter = new User();
|
||||
var walter = new User();
|
||||
walter.setUsername("heisenberg");
|
||||
walter.setFirstname("Walter");
|
||||
walter.setLastname("White");
|
||||
|
||||
User walterJr = new User();
|
||||
var walterJr = new User();
|
||||
walterJr.setUsername("flynn");
|
||||
walterJr.setFirstname("Walter Jr.");
|
||||
walterJr.setLastname("White");
|
||||
@@ -78,20 +77,20 @@ public class ComposedRepositoryTests {
|
||||
* Testing {@link EmployeeRepository} fragment.
|
||||
*/
|
||||
@Test
|
||||
public void testFindCoworkers() {
|
||||
void testFindCoworkers() {
|
||||
|
||||
User gustavo = new User();
|
||||
var gustavo = new User();
|
||||
gustavo.setUsername("pollosh");
|
||||
gustavo.setFirstname("Gustavo");
|
||||
gustavo.setLastname("Fring");
|
||||
|
||||
User walter = new User();
|
||||
var walter = new User();
|
||||
walter.setUsername("heisenberg");
|
||||
walter.setFirstname("Walter");
|
||||
walter.setLastname("White");
|
||||
walter.setManager(gustavo);
|
||||
|
||||
User jesse = new User();
|
||||
var jesse = new User();
|
||||
jesse.setUsername("capncook");
|
||||
jesse.setFirstname("Jesse");
|
||||
jesse.setLastname("Pinkman");
|
||||
@@ -106,20 +105,20 @@ public class ComposedRepositoryTests {
|
||||
* Testing {@link EmployeeRepository} fragment.
|
||||
*/
|
||||
@Test
|
||||
public void testFindSubordinates() {
|
||||
void testFindSubordinates() {
|
||||
|
||||
User gustavo = new User();
|
||||
var gustavo = new User();
|
||||
gustavo.setUsername("pollosh");
|
||||
gustavo.setFirstname("Gustavo");
|
||||
gustavo.setLastname("Fring");
|
||||
|
||||
User walter = new User();
|
||||
var walter = new User();
|
||||
walter.setUsername("heisenberg");
|
||||
walter.setFirstname("Walter");
|
||||
walter.setLastname("White");
|
||||
walter.setManager(gustavo);
|
||||
|
||||
User jesse = new User();
|
||||
var jesse = new User();
|
||||
jesse.setUsername("capncook");
|
||||
jesse.setFirstname("Jesse");
|
||||
jesse.setLastname("Pinkman");
|
||||
|
||||
@@ -15,15 +15,12 @@
|
||||
*/
|
||||
package example.springdata.jpa.custom;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
@@ -33,11 +30,11 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* @author Thomas Darimont
|
||||
* @author Divya Srivastava
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
|
||||
@Transactional
|
||||
@SpringBootTest
|
||||
// @ActiveProfiles("jdbc") // Uncomment @ActiveProfiles to enable the JDBC Implementation of the custom repository
|
||||
public class UserRepositoryCustomizationTests {
|
||||
class UserRepositoryCustomizationTests {
|
||||
|
||||
@Autowired UserRepository repository;
|
||||
|
||||
@@ -45,9 +42,9 @@ public class UserRepositoryCustomizationTests {
|
||||
* Tests inserting a user and asserts it can be loaded again.
|
||||
*/
|
||||
@Test
|
||||
public void testInsert() {
|
||||
void testInsert() {
|
||||
|
||||
User user = new User();
|
||||
var user = new User();
|
||||
user.setUsername("username");
|
||||
|
||||
user = repository.save(user);
|
||||
@@ -56,15 +53,15 @@ public class UserRepositoryCustomizationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveAndFindByLastNameAndFindByUserName() {
|
||||
void saveAndFindByLastNameAndFindByUserName() {
|
||||
|
||||
User user = new User();
|
||||
var user = new User();
|
||||
user.setUsername("foobar");
|
||||
user.setLastname("lastname");
|
||||
|
||||
user = repository.save(user);
|
||||
|
||||
List<User> users = repository.findByLastname("lastname");
|
||||
var users = repository.findByLastname("lastname");
|
||||
|
||||
assertThat(users).contains(user);
|
||||
assertThat(user).isEqualTo(repository.findByTheUsersName("foobar"));
|
||||
@@ -74,14 +71,14 @@ public class UserRepositoryCustomizationTests {
|
||||
* Test invocation of custom method.
|
||||
*/
|
||||
@Test
|
||||
public void testCustomMethod() {
|
||||
void testCustomMethod() {
|
||||
|
||||
User user = new User();
|
||||
var user = new User();
|
||||
user.setUsername("username");
|
||||
|
||||
user = repository.save(user);
|
||||
|
||||
List<User> users = repository.myCustomBatchOperation();
|
||||
var users = repository.myCustomBatchOperation();
|
||||
|
||||
assertThat(users).contains(user);
|
||||
}
|
||||
|
||||
@@ -15,13 +15,12 @@
|
||||
*/
|
||||
package example.springdata.jpa.customall;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
@@ -31,15 +30,15 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* @author Divya Srivastava
|
||||
* @soundtrack Elen - It's you (Elen)
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
|
||||
@Transactional
|
||||
@SpringBootTest
|
||||
public class UserRepositoryCustomizationTests {
|
||||
class UserRepositoryCustomizationTests {
|
||||
|
||||
@Autowired UserRepository repository;
|
||||
|
||||
@Test
|
||||
public void invokesCustomMethod() {
|
||||
void invokesCustomMethod() {
|
||||
assertThat(repository.customMethod()).isEqualTo(0L);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,24 +15,21 @@
|
||||
*/
|
||||
package example.springdata.jpa.projections;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.data.projection.TargetAware;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
@@ -41,10 +38,10 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* @author Oliver Gierke
|
||||
* @author Divya Srivastava
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
|
||||
@SpringBootTest
|
||||
@Transactional
|
||||
public class CustomerRepositoryIntegrationTest {
|
||||
class CustomerRepositoryIntegrationTest {
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@@ -52,17 +49,18 @@ public class CustomerRepositoryIntegrationTest {
|
||||
|
||||
@Autowired CustomerRepository customers;
|
||||
|
||||
Customer dave, carter;
|
||||
private Customer dave;
|
||||
private Customer carter;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
void setUp() {
|
||||
|
||||
this.dave = customers.save(new Customer("Dave", "Matthews"));
|
||||
this.carter = customers.save(new Customer("Carter", "Beauford"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectsEntityIntoInterface() {
|
||||
void projectsEntityIntoInterface() {
|
||||
|
||||
assertThat(customers.findAllProjectedBy())//
|
||||
.hasSize(2)//
|
||||
@@ -70,7 +68,7 @@ public class CustomerRepositoryIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectsMapIntoInterface() {
|
||||
void projectsMapIntoInterface() {
|
||||
|
||||
assertThat(customers.findsByProjectedColumns())//
|
||||
.hasSize(2)//
|
||||
@@ -79,15 +77,15 @@ public class CustomerRepositoryIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectsToDto() {
|
||||
void projectsToDto() {
|
||||
|
||||
assertThat(customers.findAllDtoedBy())//
|
||||
.hasSize(2)//
|
||||
.first().satisfies(it -> assertThat(it.getFirstname()).isEqualTo("Dave"));
|
||||
.first().satisfies(it -> assertThat(it.firstname()).isEqualTo("Dave"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectsDynamically() {
|
||||
void projectsDynamically() {
|
||||
|
||||
assertThat(customers.findByFirstname("Dave", CustomerProjection.class))//
|
||||
.hasSize(1)//
|
||||
@@ -96,9 +94,9 @@ public class CustomerRepositoryIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectsIndividualDynamically() {
|
||||
void projectsIndividualDynamically() {
|
||||
|
||||
CustomerSummary result = customers.findProjectedById(dave.getId(), CustomerSummary.class);
|
||||
var result = customers.findProjectedById(dave.getId(), CustomerSummary.class);
|
||||
|
||||
assertThat(result.getFullName()).isEqualTo("Dave Matthews");
|
||||
|
||||
@@ -108,9 +106,9 @@ public class CustomerRepositoryIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectIndividualInstance() {
|
||||
void projectIndividualInstance() {
|
||||
|
||||
CustomerProjection projectedDave = customers.findProjectedById(dave.getId());
|
||||
var projectedDave = customers.findProjectedById(dave.getId());
|
||||
|
||||
assertThat(projectedDave.getFirstname()).isEqualTo("Dave");
|
||||
assertThat(projectedDave).isInstanceOfSatisfying(TargetAware.class,
|
||||
@@ -118,25 +116,24 @@ public class CustomerRepositoryIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectsDtoUsingConstructorExpression() {
|
||||
void projectsDtoUsingConstructorExpression() {
|
||||
|
||||
Collection<CustomerDto> result = customers.findDtoWithConstructorExpression("Dave");
|
||||
var result = customers.findDtoWithConstructorExpression("Dave");
|
||||
|
||||
assertThat(result).hasSize(1);
|
||||
assertThat(result.iterator().next().getFirstname()).isEqualTo("Dave");
|
||||
assertThat(result.iterator().next().firstname()).isEqualTo("Dave");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsProjectionInCombinationWithPagination() {
|
||||
void supportsProjectionInCombinationWithPagination() {
|
||||
|
||||
Page<CustomerProjection> page = customers
|
||||
.findPagedProjectedBy(PageRequest.of(0, 1, Sort.by(Direction.ASC, "lastname")));
|
||||
var page = customers.findPagedProjectedBy(PageRequest.of(0, 1, Sort.by(Direction.ASC, "lastname")));
|
||||
|
||||
assertThat(page.getContent().get(0).getFirstname()).isEqualTo("Carter");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void appliesProjectionToOptional() {
|
||||
void appliesProjectionToOptional() {
|
||||
assertThat(customers.findOptionalProjectionByLastname("Beauford")).isPresent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,14 +32,12 @@ import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -52,17 +50,16 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* @author Divya Srivastava
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@Transactional
|
||||
@SpringBootTest
|
||||
@Slf4j
|
||||
public class SimpleUserRepositoryTests {
|
||||
class SimpleUserRepositoryTests {
|
||||
|
||||
@Autowired SimpleUserRepository repository;
|
||||
User user;
|
||||
private User user;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
void setUp() {
|
||||
|
||||
user = new User();
|
||||
user.setUsername("foobar");
|
||||
@@ -71,7 +68,7 @@ public class SimpleUserRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSavedUserById() {
|
||||
void findSavedUserById() {
|
||||
|
||||
user = repository.save(user);
|
||||
|
||||
@@ -79,7 +76,7 @@ public class SimpleUserRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSavedUserByLastname() throws Exception {
|
||||
void findSavedUserByLastname() {
|
||||
|
||||
user = repository.save(user);
|
||||
|
||||
@@ -87,7 +84,7 @@ public class SimpleUserRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByFirstnameOrLastname() throws Exception {
|
||||
void findByFirstnameOrLastname() {
|
||||
|
||||
user = repository.save(user);
|
||||
|
||||
@@ -95,7 +92,7 @@ public class SimpleUserRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useOptionalAsReturnAndParameterType() {
|
||||
void useOptionalAsReturnAndParameterType() {
|
||||
|
||||
assertThat(repository.findByUsername(Optional.of("foobar"))).isEmpty();
|
||||
|
||||
@@ -105,7 +102,7 @@ public class SimpleUserRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeByLastname() {
|
||||
void removeByLastname() {
|
||||
|
||||
// create a 2nd user with the same lastname as user
|
||||
var user2 = new User();
|
||||
@@ -122,13 +119,13 @@ public class SimpleUserRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useSliceToLoadContent() {
|
||||
void useSliceToLoadContent() {
|
||||
|
||||
repository.deleteAll();
|
||||
|
||||
// int repository with some values that can be ordered
|
||||
var totalNumberUsers = 11;
|
||||
List<User> source = new ArrayList<User>(totalNumberUsers);
|
||||
List<User> source = new ArrayList<>(totalNumberUsers);
|
||||
|
||||
for (var i = 1; i <= totalNumberUsers; i++) {
|
||||
|
||||
@@ -146,7 +143,7 @@ public class SimpleUserRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findFirst2ByOrderByLastnameAsc() {
|
||||
void findFirst2ByOrderByLastnameAsc() {
|
||||
|
||||
var user0 = new User();
|
||||
user0.setLastname("lastname-0");
|
||||
@@ -166,7 +163,7 @@ public class SimpleUserRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTop2ByWithSort() {
|
||||
void findTop2ByWithSort() {
|
||||
|
||||
var user0 = new User();
|
||||
user0.setLastname("lastname-0");
|
||||
@@ -190,7 +187,7 @@ public class SimpleUserRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByFirstnameOrLastnameUsingSpEL() {
|
||||
void findByFirstnameOrLastnameUsingSpEL() {
|
||||
|
||||
var first = new User();
|
||||
first.setLastname("lastname");
|
||||
@@ -216,7 +213,7 @@ public class SimpleUserRepositoryTests {
|
||||
* resulting {@link Stream} contains state it needs to be closed explicitly after use!
|
||||
*/
|
||||
@Test
|
||||
public void useJava8StreamsWithCustomQuery() {
|
||||
void useJava8StreamsWithCustomQuery() {
|
||||
|
||||
var user1 = repository.save(new User("Customer1", "Foo"));
|
||||
var user2 = repository.save(new User("Customer2", "Bar"));
|
||||
@@ -231,7 +228,7 @@ public class SimpleUserRepositoryTests {
|
||||
* Note, that since the resulting {@link Stream} contains state it needs to be closed explicitly after use!
|
||||
*/
|
||||
@Test
|
||||
public void useJava8StreamsWithDerivedQuery() {
|
||||
void useJava8StreamsWithDerivedQuery() {
|
||||
|
||||
var user1 = repository.save(new User("Customer1", "Foo"));
|
||||
var user2 = repository.save(new User("Customer2", "Bar"));
|
||||
@@ -247,7 +244,7 @@ public class SimpleUserRepositoryTests {
|
||||
*/
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public void rejectsStreamExecutionIfNoSurroundingTransactionActive() {
|
||||
void rejectsStreamExecutionIfNoSurroundingTransactionActive() {
|
||||
Assertions.assertThrows(InvalidDataAccessApiUsageException.class, () -> {
|
||||
repository.findAllByLastnameIsNotNull();
|
||||
});
|
||||
@@ -260,7 +257,7 @@ public class SimpleUserRepositoryTests {
|
||||
*/
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public void supportsCompletableFuturesAsReturnTypeWrapper() throws Exception {
|
||||
void supportsCompletableFuturesAsReturnTypeWrapper() throws Exception {
|
||||
|
||||
repository.save(new User("Customer1", "Foo"));
|
||||
repository.save(new User("Customer2", "Bar"));
|
||||
|
||||
Reference in New Issue
Block a user