> implement
return propertyCache.get(name);
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mapping.PersistentEntity#getPersistentProperty(java.lang.Class)
+ */
+ @Override
+ public P getPersistentProperty(Class extends Annotation> annotationType) {
+
+ Assert.notNull(annotationType, "Annotation type must not be null!");
+
+ for (P property : properties) {
+ if (property.isAnnotationPresent(annotationType)) {
+ return property;
+ }
+ }
+
+ for (Association association : associations) {
+
+ P property = association.getInverse();
+
+ if (property.isAnnotationPresent(annotationType)) {
+ return property;
+ }
+ }
+
+ return null;
+ }
+
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.PersistentEntity#getType()
diff --git a/src/test/java/org/springframework/data/auditing/AuditingHandlerUnitTests.java b/src/test/java/org/springframework/data/auditing/AuditingHandlerUnitTests.java
index b32f80663..c42ee0c08 100644
--- a/src/test/java/org/springframework/data/auditing/AuditingHandlerUnitTests.java
+++ b/src/test/java/org/springframework/data/auditing/AuditingHandlerUnitTests.java
@@ -46,6 +46,7 @@ public class AuditingHandlerUnitTests {
when(auditorAware.getCurrentAuditor()).thenReturn(user);
}
+ @SuppressWarnings("deprecation")
protected AuditingHandler getHandler() {
return new AuditingHandler();
}
diff --git a/src/test/java/org/springframework/data/auditing/IsNewAwareAuditingHandlerUnitTests.java b/src/test/java/org/springframework/data/auditing/IsNewAwareAuditingHandlerUnitTests.java
index eed7c6b49..a15a8da93 100644
--- a/src/test/java/org/springframework/data/auditing/IsNewAwareAuditingHandlerUnitTests.java
+++ b/src/test/java/org/springframework/data/auditing/IsNewAwareAuditingHandlerUnitTests.java
@@ -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());
+ }
}
diff --git a/src/test/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactoryUnitTests.java b/src/test/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactoryUnitTests.java
new file mode 100644
index 000000000..34ef05606
--- /dev/null
+++ b/src/test/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactoryUnitTests.java
@@ -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