Re-activated @Configurable DI in AuditingEntityListener.
Added test cases to verify setting the auditor works.
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -129,7 +129,6 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<!--
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>aspectj-maven-plugin</artifactId>
|
||||
@@ -162,12 +161,12 @@
|
||||
</aspectLibrary>
|
||||
</aspectLibraries>
|
||||
<includes>
|
||||
<include>**/auditing/support/*.java</include>
|
||||
<include>**/domain/support/AuditingEntityListener.java</include>
|
||||
</includes>
|
||||
<source>1.5</source>
|
||||
<target>1.5</target>
|
||||
</configuration>
|
||||
</plugin>-->
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
@@ -24,16 +24,24 @@ import org.springframework.data.domain.AuditorAware;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AuditorAwareStub implements AuditorAware<User> {
|
||||
public class AuditorAwareStub implements AuditorAware<AuditableUser> {
|
||||
|
||||
private AuditableUser auditor;
|
||||
|
||||
|
||||
public void setAuditor(AuditableUser auditor) {
|
||||
|
||||
this.auditor = auditor;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.domain.AuditorAware#getCurrentAuditor()
|
||||
*/
|
||||
public User getCurrentAuditor() {
|
||||
public AuditableUser getCurrentAuditor() {
|
||||
|
||||
return null;
|
||||
return auditor;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,35 +18,54 @@ package org.springframework.data.jpa.domain.support;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Auditable;
|
||||
import org.springframework.data.jpa.domain.sample.AuditableRole;
|
||||
import org.springframework.data.jpa.domain.sample.AuditableUser;
|
||||
import org.springframework.data.jpa.domain.sample.AuditorAwareStub;
|
||||
import org.springframework.data.jpa.repository.sample.AuditableUserRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
||||
/**
|
||||
* Integration test for {@link AuditingEntityListener}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:auditing/auditing-entity-listener.xml")
|
||||
@Transactional
|
||||
public class AuditingEntityListenerTests {
|
||||
|
||||
@Autowired
|
||||
AuditableUserRepository dao;
|
||||
|
||||
@Autowired
|
||||
AuditorAwareStub auditorAware;
|
||||
|
||||
AuditableUser user;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
user = new AuditableUser();
|
||||
auditorAware.setAuditor(user);
|
||||
|
||||
dao.save(user);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void auditsRootEntityCorrectly() throws Exception {
|
||||
|
||||
AuditableUser user = new AuditableUser();
|
||||
dao.save(user);
|
||||
|
||||
assertDatesSet(user);
|
||||
assertUserIsAuditor(user, user);
|
||||
}
|
||||
|
||||
|
||||
@@ -56,18 +75,28 @@ public class AuditingEntityListenerTests {
|
||||
AuditableRole role = new AuditableRole();
|
||||
role.setName("ADMIN");
|
||||
|
||||
AuditableUser user = new AuditableUser();
|
||||
user.addRole(role);
|
||||
dao.save(user);
|
||||
role = user.getRoles().iterator().next();
|
||||
|
||||
assertDatesSet(user);
|
||||
assertDatesSet(role);
|
||||
assertUserIsAuditor(user, user);
|
||||
assertUserIsAuditor(user, role);
|
||||
}
|
||||
|
||||
|
||||
private void assertDatesSet(Auditable<?, ?> auditable) {
|
||||
private static void assertDatesSet(Auditable<?, ?> auditable) {
|
||||
|
||||
assertThat(auditable.getCreatedDate(), is(notNullValue()));
|
||||
assertThat(auditable.getLastModifiedDate(), is(notNullValue()));
|
||||
}
|
||||
|
||||
|
||||
private static void assertUserIsAuditor(AuditableUser user,
|
||||
Auditable<AuditableUser, ?> auditable) {
|
||||
|
||||
assertThat(auditable.getCreatedBy(), is(user));
|
||||
assertThat(auditable.getLastModifiedBy(), is(user));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.domain.AuditorAware;
|
||||
import org.springframework.data.jpa.domain.sample.AuditableUser;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
|
||||
/**
|
||||
@@ -33,7 +32,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
@SuppressWarnings("unchecked")
|
||||
public class AuditingEntityListenerUnitTests {
|
||||
|
||||
AuditingEntityListener<AuditableUser> auditionAdvice;
|
||||
AuditingEntityListener<AuditableUser> listener;
|
||||
AuditorAware<AuditableUser> auditorAware;
|
||||
|
||||
AuditableUser user;
|
||||
@@ -42,7 +41,10 @@ public class AuditingEntityListenerUnitTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
auditionAdvice = new AuditingEntityListener<AuditableUser>();
|
||||
listener = new AuditingEntityListener<AuditableUser>();
|
||||
// Explicitly null the AuditorAware as it might have been DI'ed if test
|
||||
// is run in a test suite with integration tests
|
||||
listener.setAuditorAware(null);
|
||||
|
||||
user = new AuditableUser();
|
||||
|
||||
@@ -58,11 +60,12 @@ public class AuditingEntityListenerUnitTests {
|
||||
@Test
|
||||
public void doesNotSetAuditorIfNotConfigured() {
|
||||
|
||||
auditionAdvice.touch(user);
|
||||
listener.touch(user);
|
||||
|
||||
assertNotNull(user.getCreatedDate());
|
||||
assertNotNull(user.getLastModifiedDate());
|
||||
|
||||
System.out.println(user.getCreatedBy());
|
||||
assertNull(user.getCreatedBy());
|
||||
assertNull(user.getLastModifiedBy());
|
||||
}
|
||||
@@ -75,9 +78,9 @@ public class AuditingEntityListenerUnitTests {
|
||||
@Test
|
||||
public void setsAuditorIfConfigured() {
|
||||
|
||||
auditionAdvice.setAuditorAware(auditorAware);
|
||||
listener.setAuditorAware(auditorAware);
|
||||
|
||||
auditionAdvice.touch(user);
|
||||
listener.touch(user);
|
||||
|
||||
assertNotNull(user.getCreatedDate());
|
||||
assertNotNull(user.getLastModifiedDate());
|
||||
@@ -96,9 +99,9 @@ public class AuditingEntityListenerUnitTests {
|
||||
@Test
|
||||
public void honoursModifiedOnCreationFlag() {
|
||||
|
||||
auditionAdvice.setAuditorAware(auditorAware);
|
||||
auditionAdvice.setModifyOnCreation(false);
|
||||
auditionAdvice.touch(user);
|
||||
listener.setAuditorAware(auditorAware);
|
||||
listener.setModifyOnCreation(false);
|
||||
listener.touch(user);
|
||||
|
||||
assertNotNull(user.getCreatedDate());
|
||||
assertNotNull(user.getCreatedBy());
|
||||
@@ -119,8 +122,8 @@ public class AuditingEntityListenerUnitTests {
|
||||
|
||||
user = new AuditableUser(1L);
|
||||
|
||||
auditionAdvice.setAuditorAware(auditorAware);
|
||||
auditionAdvice.touch(user);
|
||||
listener.setAuditorAware(auditorAware);
|
||||
listener.touch(user);
|
||||
|
||||
assertNull(user.getCreatedBy());
|
||||
assertNull(user.getCreatedDate());
|
||||
@@ -135,9 +138,9 @@ public class AuditingEntityListenerUnitTests {
|
||||
@Test
|
||||
public void doesNotSetTimeIfConfigured() throws Exception {
|
||||
|
||||
auditionAdvice.setDateTimeForNow(false);
|
||||
auditionAdvice.setAuditorAware(auditorAware);
|
||||
auditionAdvice.touch(user);
|
||||
listener.setDateTimeForNow(false);
|
||||
listener.setAuditorAware(auditorAware);
|
||||
listener.touch(user);
|
||||
|
||||
assertNotNull(user.getCreatedBy());
|
||||
assertNull(user.getCreatedDate());
|
||||
|
||||
Reference in New Issue
Block a user