#297 - Tweaked persistence context handling in security tests.

The modifying query now clears the EntityManager so that the changes are really flushed to the database. This is necessary as Boot 2.0 flips the default for Hibernate's new identifier mappings which causes entities less aggressively being flushed and the previously used detach apparently removing them from the persistence context without ever making it to the database.
This commit is contained in:
Oliver Gierke
2017-05-04 23:03:22 +02:00
parent 4164bc4607
commit 5d34f0a00d
3 changed files with 7 additions and 25 deletions

View File

@@ -23,16 +23,12 @@
<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>
</modules>
<properties>
<hibernate.version>5.2.7.Final</hibernate.version>
</properties>
<dependencies>
<dependency>

View File

@@ -28,18 +28,12 @@ import org.springframework.data.repository.Repository;
interface SecureBusinessObjectRepository extends Repository<BusinessObject, Long> {
/**
* Here we demonstrate the usage of SpEL expression within a custom query.
* With the {@link example.springdata.jpa.security.SecurityEvaluationContextExtension} in place
* we can safely access auth information provided by the Spring Security Context.
*
* The Spring Data Repository infrastructure will translate the given query string into the
* parameterized form:
*
* <code>
* Here we demonstrate the usage of SpEL expression within a custom query. With the
* {@link example.springdata.jpa.security.SecurityEvaluationContextExtension} in place we can safely access auth
* information provided by the Spring Security Context. The Spring Data Repository infrastructure will translate the
* given query string into the parameterized form: <code>
* select o from BusinessObject o where o.owner.emailAddress like ?
* </code>
*
* and set the the result SpEL expression evaluated at method invocation time as parameter value.
* </code> and set the the result SpEL expression evaluated at method invocation time as parameter value.
*
* @return
*/
@@ -57,7 +51,7 @@ interface SecureBusinessObjectRepository extends Repository<BusinessObject, Long
/**
* Here we demonstrate the use of SecurityContext information in dynamic SpEL parameters in a JPQL update statement.
*/
@Modifying
@Modifying(clearAutomatically = true)
@Query("update BusinessObject b set b.data = upper(b.data), b.lastModifiedBy = :#{#security.principal}, b.lastModifiedDate = :#{new java.util.Date()}")
void modifiyDataWithRecordingSecurityContext();
}

View File

@@ -21,8 +21,6 @@ import static org.junit.Assert.*;
import java.util.List;
import javax.persistence.EntityManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -48,7 +46,6 @@ public class SecurityIntegrationTests {
@Autowired UserRepository userRepository;
@Autowired BusinessObjectRepository businessObjectRepository;
@Autowired SecureBusinessObjectRepository secureBusinessObjectRepository;
@Autowired EntityManager em;
User tom, ollie, admin;
UsernamePasswordAuthenticationToken olliAuth, tomAuth, adminAuth;
@@ -134,11 +131,6 @@ public class SecurityIntegrationTests {
SecurityContextHolder.getContext().setAuthentication(adminAuth);
// Detaching items to get them out of the query cache in order to see the updated values.
em.detach(object1);
em.detach(object2);
em.detach(object3);
secureBusinessObjectRepository.modifiyDataWithRecordingSecurityContext();
for (BusinessObject bo : businessObjectRepository.findAll()) {