Browse Source
Re-engineered our auditing setup to leverage the APIs introduced from DATACMNS-137.pull/19/head
10 changed files with 84 additions and 405 deletions
@ -1,36 +0,0 @@
@@ -1,36 +0,0 @@
|
||||
/* |
||||
* Copyright 2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.jpa.domain.support; |
||||
|
||||
import org.joda.time.DateTime; |
||||
|
||||
/** |
||||
* Default {@link DateTimeProvider} simply creating new {@link DateTime} instances for each method call. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public enum CurrentDateTimeProvider implements DateTimeProvider { |
||||
|
||||
INSTANCE; |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.data.jpa.domain.support.DateTimeProvider#getDateTime() |
||||
*/ |
||||
public DateTime getDateTime() { |
||||
return new DateTime(); |
||||
} |
||||
} |
||||
@ -1,33 +0,0 @@
@@ -1,33 +0,0 @@
|
||||
/* |
||||
* Copyright 2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.jpa.domain.support; |
||||
|
||||
import org.joda.time.DateTime; |
||||
|
||||
/** |
||||
* SPI to calculate the {@link DateTime} instance to be used when auditing. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public interface DateTimeProvider { |
||||
|
||||
/** |
||||
* Returns the {@link DateTime} to be used as modification date. |
||||
* |
||||
* @return |
||||
*/ |
||||
DateTime getDateTime(); |
||||
} |
||||
@ -1,4 +1,5 @@
@@ -1,4 +1,5 @@
|
||||
http\://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd=org/springframework/data/jpa/repository/config/spring-jpa-1.0.xsd |
||||
http\://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd=org/springframework/data/jpa/repository/config/spring-jpa-1.1.xsd |
||||
http\://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd=org/springframework/data/jpa/repository/config/spring-jpa-1.2.xsd |
||||
http\://www.springframework.org/schema/data/jpa/spring-jpa.xsd=org/springframework/data/jpa/repository/config/spring-jpa-1.2.xsd |
||||
http\://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd=org/springframework/data/jpa/repository/config/spring-jpa-1.3.xsd |
||||
http\://www.springframework.org/schema/data/jpa/spring-jpa.xsd=org/springframework/data/jpa/repository/config/spring-jpa-1.3.xsd |
||||
|
||||
@ -0,0 +1,54 @@
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?> |
||||
<xsd:schema xmlns="http://www.springframework.org/schema/data/jpa" |
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema" |
||||
xmlns:tool="http://www.springframework.org/schema/tool" |
||||
xmlns:context="http://www.springframework.org/schema/context" |
||||
xmlns:repository="http://www.springframework.org/schema/data/repository" |
||||
targetNamespace="http://www.springframework.org/schema/data/jpa" |
||||
elementFormDefault="qualified" attributeFormDefault="unqualified"> |
||||
|
||||
<xsd:import namespace="http://www.springframework.org/schema/tool" |
||||
schemaLocation="http://www.springframework.org/schema/tool/spring-tool.xsd"/> |
||||
<xsd:import namespace="http://www.springframework.org/schema/context" |
||||
schemaLocation="http://www.springframework.org/schema/context/spring-context.xsd" /> |
||||
<xsd:import namespace="http://www.springframework.org/schema/data/repository" |
||||
schemaLocation="http://www.springframework.org/schema/data/repository/spring-repository.xsd" /> |
||||
|
||||
<xsd:element name="repositories"> |
||||
<xsd:complexType> |
||||
<xsd:complexContent> |
||||
<xsd:extension base="repository:repositories"> |
||||
<xsd:attributeGroup ref="repository:transactional-repository-attributes" /> |
||||
<xsd:attribute name="entity-manager-factory-ref" type="entityManagerFactoryRef" /> |
||||
</xsd:extension> |
||||
</xsd:complexContent> |
||||
</xsd:complexType> |
||||
</xsd:element> |
||||
|
||||
<xsd:element name="auditing"> |
||||
<xsd:annotation> |
||||
<xsd:appinfo> |
||||
<tool:annotation> |
||||
<tool:exports type="org.springframework.data.jpa.domain.support.AuditingEntityListener" /> |
||||
<tool:exports type="org.springframework.data.auditing.AuditingHandler" /> |
||||
</tool:annotation> |
||||
</xsd:appinfo> |
||||
</xsd:annotation> |
||||
<xsd:complexType> |
||||
<xsd:attributeGroup ref="repository:auditing-attributes" /> |
||||
</xsd:complexType> |
||||
</xsd:element> |
||||
|
||||
<xsd:simpleType name="entityManagerFactoryRef"> |
||||
<xsd:annotation> |
||||
<xsd:appinfo> |
||||
<tool:annotation kind="ref"> |
||||
<tool:assignable-to type="org.springframework.orm.jpa.AbstractEntityManagerFactoryBean" /> |
||||
</tool:annotation> |
||||
</xsd:appinfo> |
||||
</xsd:annotation> |
||||
<xsd:union memberTypes="xsd:string" /> |
||||
</xsd:simpleType> |
||||
|
||||
|
||||
</xsd:schema> |
||||
@ -1,153 +0,0 @@
@@ -1,153 +0,0 @@
|
||||
/* |
||||
* Copyright 2008-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.jpa.domain.support; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import static org.mockito.Mockito.*; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.springframework.data.domain.AuditorAware; |
||||
import org.springframework.data.jpa.domain.sample.AuditableUser; |
||||
|
||||
/** |
||||
* Unit test for {@code AuditingEntityListener}. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
public class AuditingEntityListenerUnitTests { |
||||
|
||||
AuditingEntityListener<AuditableUser> listener; |
||||
AuditorAware<AuditableUser> auditorAware; |
||||
|
||||
AuditableUser user; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
|
||||
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(); |
||||
|
||||
auditorAware = mock(AuditorAware.class); |
||||
when(auditorAware.getCurrentAuditor()).thenReturn(user); |
||||
} |
||||
|
||||
/** |
||||
* Checks that the advice does not set auditor on the target entity if no {@code AuditorAware} was configured. |
||||
*/ |
||||
@Test |
||||
public void doesNotSetAuditorIfNotConfigured() { |
||||
|
||||
listener.touchForCreate(user); |
||||
|
||||
assertNotNull(user.getCreatedDate()); |
||||
assertNotNull(user.getLastModifiedDate()); |
||||
|
||||
assertNull(user.getCreatedBy()); |
||||
assertNull(user.getLastModifiedBy()); |
||||
} |
||||
|
||||
/** |
||||
* Checks that the advice sets the auditor on the target entity if an {@code AuditorAware} was configured. |
||||
*/ |
||||
@Test |
||||
public void setsAuditorIfConfigured() { |
||||
|
||||
listener.setAuditorAware(auditorAware); |
||||
|
||||
listener.touchForCreate(user); |
||||
|
||||
assertNotNull(user.getCreatedDate()); |
||||
assertNotNull(user.getLastModifiedDate()); |
||||
|
||||
assertNotNull(user.getCreatedBy()); |
||||
assertNotNull(user.getLastModifiedBy()); |
||||
|
||||
verify(auditorAware).getCurrentAuditor(); |
||||
} |
||||
|
||||
/** |
||||
* Checks that the advice does not set modification information on creation if the falg is set to {@code false}. |
||||
*/ |
||||
@Test |
||||
public void honoursModifiedOnCreationFlag() { |
||||
|
||||
listener.setAuditorAware(auditorAware); |
||||
listener.setModifyOnCreation(false); |
||||
listener.touchForCreate(user); |
||||
|
||||
assertNotNull(user.getCreatedDate()); |
||||
assertNotNull(user.getCreatedBy()); |
||||
|
||||
assertNull(user.getLastModifiedBy()); |
||||
assertNull(user.getLastModifiedDate()); |
||||
|
||||
verify(auditorAware).getCurrentAuditor(); |
||||
} |
||||
|
||||
/** |
||||
* Tests that the advice only sets modification data if a not-new entity is handled. |
||||
*/ |
||||
@Test |
||||
public void onlySetsModificationDataOnNotNewEntities() { |
||||
|
||||
user = new AuditableUser(1L); |
||||
|
||||
listener.setAuditorAware(auditorAware); |
||||
listener.touchForUpdate(user); |
||||
|
||||
assertNull(user.getCreatedBy()); |
||||
assertNull(user.getCreatedDate()); |
||||
|
||||
assertNotNull(user.getLastModifiedBy()); |
||||
assertNotNull(user.getLastModifiedDate()); |
||||
|
||||
verify(auditorAware).getCurrentAuditor(); |
||||
} |
||||
|
||||
@Test |
||||
public void doesNotSetTimeIfConfigured() { |
||||
|
||||
listener.setDateTimeForNow(false); |
||||
listener.setAuditorAware(auditorAware); |
||||
listener.touchForCreate(user); |
||||
|
||||
assertNotNull(user.getCreatedBy()); |
||||
assertNull(user.getCreatedDate()); |
||||
|
||||
assertNotNull(user.getLastModifiedBy()); |
||||
assertNull(user.getLastModifiedDate()); |
||||
} |
||||
|
||||
/** |
||||
* @see DATAJPA-9 |
||||
*/ |
||||
@Test |
||||
public void usesDateTimeProviderIfConfigured() { |
||||
|
||||
DateTimeProvider provider = mock(DateTimeProvider.class); |
||||
|
||||
listener.setDateTimeProvider(provider); |
||||
listener.touchForCreate(user); |
||||
|
||||
verify(provider, times(1)).getDateTime(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue