DATACMNS-365 - Enhanced auditing subsystem to work with accessor annotations.
Added a MappingAuditableBeanWrapperFactory to be able to use themapping metamodel to lookup annotations on persistent properties. This propagates into AuditingHandler and IsNewAwareAuditingHandler getting new constructors taking a MappingContext to set themselves up correctly. This will probably need store specific updates in the setup of the auditing infrastructure for namespace implementations and annotation based JavaConfig. Introduced ….getPersistentProperty(Class<? extends Annotation> annotationType) on PersistentEntity to be able to access properties with a given annotation. Updated SonarGraph architecture description and moved auditing related config classes into auditing.config package.
This commit is contained in:
@@ -46,6 +46,7 @@ public class AuditingHandlerUnitTests {
|
||||
when(auditorAware.getCurrentAuditor()).thenReturn(user);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
protected AuditingHandler getHandler() {
|
||||
return new AuditingHandler();
|
||||
}
|
||||
|
||||
@@ -25,6 +25,10 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.support.IsNewStrategy;
|
||||
import org.springframework.data.support.IsNewStrategyFactory;
|
||||
|
||||
@@ -46,6 +50,7 @@ public class IsNewAwareAuditingHandlerUnitTests extends AuditingHandlerUnitTests
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
protected IsNewAwareAuditingHandler getHandler() {
|
||||
return new IsNewAwareAuditingHandler(factory);
|
||||
}
|
||||
@@ -71,4 +76,21 @@ public class IsNewAwareAuditingHandlerUnitTests extends AuditingHandlerUnitTests
|
||||
assertThat(user.createdDate, is(nullValue()));
|
||||
assertThat(user.modifiedDate, is(notNullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-365
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullMappingContext() {
|
||||
new IsNewAwareAuditingHandler(
|
||||
(MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>>) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-365
|
||||
*/
|
||||
@Test
|
||||
public void setsUpHandlerWithMappingContext() {
|
||||
new IsNewAwareAuditingHandler(new SampleMappingContext());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright 2014 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.auditing;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.LastModifiedBy;
|
||||
import org.springframework.data.auditing.AuditableBeanWrapperFactory.AuditableInterfaceBeanWrapper;
|
||||
import org.springframework.data.domain.Auditable;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MappingAuditableBeanWrapperFactory}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 1.8
|
||||
*/
|
||||
public class MappingAuditableBeanWrapperFactoryUnitTests {
|
||||
|
||||
AuditableBeanWrapperFactory factory;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
factory = new MappingAuditableBeanWrapperFactory(new SampleMappingContext());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-365
|
||||
*/
|
||||
@Test
|
||||
public void discoversAuditingPropertyOnField() {
|
||||
|
||||
Sample sample = new Sample();
|
||||
AuditableBeanWrapper wrapper = factory.getBeanWrapperFor(sample);
|
||||
|
||||
assertThat(wrapper, is(notNullValue()));
|
||||
|
||||
wrapper.setCreatedBy("Me!");
|
||||
assertThat(sample.createdBy, is(notNullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-365
|
||||
*/
|
||||
@Test
|
||||
public void discoversAuditingPropertyOnAccessor() {
|
||||
|
||||
Sample sample = new Sample();
|
||||
AuditableBeanWrapper wrapper = factory.getBeanWrapperFor(sample);
|
||||
|
||||
assertThat(wrapper, is(notNullValue()));
|
||||
|
||||
wrapper.setLastModifiedBy("Me, too!");
|
||||
assertThat(sample.lastModifiedBy, is(notNullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-365
|
||||
*/
|
||||
@Test
|
||||
public void settingInavailablePropertyIsNoop() {
|
||||
|
||||
Sample sample = new Sample();
|
||||
AuditableBeanWrapper wrapper = factory.getBeanWrapperFor(sample);
|
||||
|
||||
wrapper.setLastModifiedDate(new GregorianCalendar());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-365
|
||||
*/
|
||||
@Test
|
||||
public void doesNotReturnWrapperForEntityNotUsingAuditing() {
|
||||
assertThat(factory.getBeanWrapperFor(new NoAuditing()), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-365
|
||||
*/
|
||||
@Test
|
||||
public void returnsAuditableWrapperForAuditable() {
|
||||
|
||||
assertThat(factory.getBeanWrapperFor(mock(ExtendingAuditable.class)),
|
||||
is(instanceOf(AuditableInterfaceBeanWrapper.class)));
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
@CreatedBy private Object createdBy;
|
||||
private Object lastModifiedBy;
|
||||
|
||||
@LastModifiedBy
|
||||
public Object getLastModifiedBy() {
|
||||
return lastModifiedBy;
|
||||
}
|
||||
}
|
||||
|
||||
static class NoAuditing {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
static abstract class ExtendingAuditable implements Auditable<Object, Long> {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 2011-2014 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.mapping.model;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
@@ -8,16 +23,23 @@ import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedBy;
|
||||
import org.springframework.data.annotation.TypeAlias;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentEntitySpec;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.Person;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
@@ -29,8 +51,9 @@ import org.springframework.test.util.ReflectionTestUtils;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
|
||||
@Mock
|
||||
T property;
|
||||
@Rule public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Mock T property;
|
||||
|
||||
@Test
|
||||
public void assertInvariants() {
|
||||
@@ -122,13 +145,28 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
when(property.isIdProperty()).thenReturn(true);
|
||||
|
||||
entity.addPersistentProperty(property);
|
||||
exception.expect(MappingException.class);
|
||||
entity.addPersistentProperty(property);
|
||||
}
|
||||
|
||||
try {
|
||||
entity.addPersistentProperty(property);
|
||||
fail("Expected MappingException!");
|
||||
} catch (MappingException e) {
|
||||
// expected
|
||||
}
|
||||
/**
|
||||
* @see DATACMNS-365
|
||||
*/
|
||||
@Test
|
||||
public void detectsPropertyWithAnnotation() {
|
||||
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = context.getPersistentEntity(Entity.class);
|
||||
|
||||
PersistentProperty<?> property = entity.getPersistentProperty(LastModifiedBy.class);
|
||||
assertThat(property, is(notNullValue()));
|
||||
assertThat(property.getName(), is("field"));
|
||||
|
||||
property = entity.getPersistentProperty(CreatedBy.class);
|
||||
assertThat(property, is(notNullValue()));
|
||||
assertThat(property.getName(), is("property"));
|
||||
|
||||
assertThat(entity.getPersistentProperty(CreatedDate.class), is(nullValue()));
|
||||
}
|
||||
|
||||
private BasicPersistentEntity<Person, T> createEntity(Comparator<T> comparator) {
|
||||
@@ -142,5 +180,15 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
|
||||
static class Entity {
|
||||
|
||||
@LastModifiedBy String field;
|
||||
String property;
|
||||
|
||||
/**
|
||||
* @return the property
|
||||
*/
|
||||
@CreatedBy
|
||||
public String getProperty() {
|
||||
return property;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user