DATACMNS-266 - Use new common Maven build infrastructure.
Simplified project setup to be a single module build again. Using Spring Data Build parent POM to simplify project setup. See https://github.com/SpringSource/spring-data-build#spring-data-build-infrastructure
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package org.springframework.data.auditing;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedBy;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
|
||||
/**
|
||||
* Sample entity using annotation based auditing.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 1.5
|
||||
*/
|
||||
class AnnotatedUser {
|
||||
|
||||
@CreatedBy
|
||||
Object createdBy;
|
||||
|
||||
@CreatedDate
|
||||
DateTime createdDate;
|
||||
|
||||
@LastModifiedBy
|
||||
Object lastModifiedBy;
|
||||
|
||||
@LastModifiedDate
|
||||
Date lastModifiedDate;
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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.auditing;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Unit test for {@link org.springframework.data.auditing.AnnotationAuditingMetadata}.
|
||||
*
|
||||
* @author Ranie Jade Ramiso
|
||||
* @author Oliver Gierke
|
||||
* @since 1.5
|
||||
*/
|
||||
public class AnnotationAuditingMetadataUnitTests {
|
||||
|
||||
static final Field createdByField = ReflectionUtils.findField(AnnotatedUser.class, "createdBy");
|
||||
static final Field createdDateField = ReflectionUtils.findField(AnnotatedUser.class, "createdDate");
|
||||
static final Field lastModifiedByField = ReflectionUtils.findField(AnnotatedUser.class, "lastModifiedBy");
|
||||
static final Field lastModifiedDateField = ReflectionUtils.findField(AnnotatedUser.class, "lastModifiedDate");
|
||||
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void checkAnnotationDiscovery() {
|
||||
|
||||
AnnotationAuditingMetadata metadata = AnnotationAuditingMetadata.getMetadata(AnnotatedUser.class);
|
||||
assertThat(metadata, is(notNullValue()));
|
||||
|
||||
assertThat(createdByField, is(metadata.getCreatedByField()));
|
||||
assertThat(createdDateField, is(metadata.getCreatedDateField()));
|
||||
assertThat(lastModifiedByField, is(metadata.getLastModifiedByField()));
|
||||
assertThat(lastModifiedDateField, is(metadata.getLastModifiedDateField()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkCaching() {
|
||||
|
||||
AnnotationAuditingMetadata firstCall = AnnotationAuditingMetadata.getMetadata(AnnotatedUser.class);
|
||||
assertThat(firstCall, is(notNullValue()));
|
||||
|
||||
AnnotationAuditingMetadata secondCall = AnnotationAuditingMetadata.getMetadata(AnnotatedUser.class);
|
||||
assertThat(firstCall, is(secondCall));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkIsAuditable() {
|
||||
|
||||
AnnotationAuditingMetadata metadata = AnnotationAuditingMetadata.getMetadata(AnnotatedUser.class);
|
||||
assertThat(metadata, is(notNullValue()));
|
||||
;
|
||||
assertThat(metadata.isAuditable(), is(true));
|
||||
|
||||
metadata = AnnotationAuditingMetadata.getMetadata(NonAuditableUser.class);
|
||||
assertThat(metadata, is(notNullValue()));
|
||||
assertThat(metadata.isAuditable(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectsInvalidDateTypeField() {
|
||||
|
||||
class Sample {
|
||||
@CreatedDate
|
||||
String field;
|
||||
}
|
||||
|
||||
exception.expect(IllegalStateException.class);
|
||||
exception.expectMessage(String.class.getName());
|
||||
exception.expectMessage("field");
|
||||
|
||||
AnnotationAuditingMetadata.getMetadata(Sample.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class NonAuditableUser {
|
||||
private Object nonAuditProperty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.auditing;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.auditing.AuditableBeanWrapperFactory.AuditableInterfaceBeanWrapper;
|
||||
import org.springframework.data.auditing.AuditableBeanWrapperFactory.ReflectionAuditingBeanWrapper;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @since 1.5
|
||||
*/
|
||||
public class AuditableBeanWrapperFactoryUnitTests {
|
||||
|
||||
AuditableBeanWrapperFactory factory = new AuditableBeanWrapperFactory();
|
||||
|
||||
@Test
|
||||
public void returnsNullForNullSource() {
|
||||
assertThat(factory.getBeanWrapperFor(null), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsAuditableInterfaceBeanWrapperForAuditable() {
|
||||
|
||||
AuditableBeanWrapper wrapper = factory.getBeanWrapperFor(new AuditedUser());
|
||||
assertThat(wrapper, is(instanceOf(AuditableInterfaceBeanWrapper.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsReflectionAuditingBeanWrapperForNonAuditableButAnnotated() {
|
||||
|
||||
AuditableBeanWrapper wrapper = factory.getBeanWrapperFor(new AnnotatedUser());
|
||||
assertThat(wrapper, is(instanceOf(ReflectionAuditingBeanWrapper.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsNullForNonAuditableType() {
|
||||
|
||||
AuditableBeanWrapper wrapper = factory.getBeanWrapperFor(new Object());
|
||||
assertThat(wrapper, is(nullValue()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.auditing;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.springframework.data.domain.Auditable;
|
||||
|
||||
/**
|
||||
* Sample implementation of {@link Auditable}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 1.5
|
||||
*/
|
||||
class AuditedUser implements Auditable<AuditedUser, Long> {
|
||||
|
||||
private static final long serialVersionUID = -840865084027597951L;
|
||||
|
||||
Long id;
|
||||
AuditedUser createdBy;
|
||||
AuditedUser modifiedBy;
|
||||
DateTime createdDate;
|
||||
DateTime modifiedDate;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean isNew() {
|
||||
return id == null;
|
||||
}
|
||||
|
||||
public AuditedUser getCreatedBy() {
|
||||
return createdBy;
|
||||
}
|
||||
|
||||
public void setCreatedBy(AuditedUser createdBy) {
|
||||
this.createdBy = createdBy;
|
||||
}
|
||||
|
||||
public DateTime getCreatedDate() {
|
||||
return createdDate;
|
||||
}
|
||||
|
||||
public void setCreatedDate(DateTime creationDate) {
|
||||
this.createdDate = creationDate;
|
||||
}
|
||||
|
||||
public AuditedUser getLastModifiedBy() {
|
||||
return modifiedBy;
|
||||
}
|
||||
|
||||
public void setLastModifiedBy(AuditedUser lastModifiedBy) {
|
||||
this.modifiedBy = lastModifiedBy;
|
||||
}
|
||||
|
||||
public DateTime getLastModifiedDate() {
|
||||
return modifiedDate;
|
||||
}
|
||||
|
||||
public void setLastModifiedDate(DateTime lastModifiedDate) {
|
||||
this.modifiedDate = lastModifiedDate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* 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.auditing;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.domain.AuditorAware;
|
||||
|
||||
/**
|
||||
* Unit test for {@code AuditingHandler}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 1.5
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public class AuditingHandlerUnitTests {
|
||||
|
||||
AuditingHandler<AuditedUser> handler;
|
||||
AuditorAware<AuditedUser> auditorAware;
|
||||
|
||||
AuditedUser user;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
handler = getHandler();
|
||||
user = new AuditedUser();
|
||||
|
||||
auditorAware = mock(AuditorAware.class);
|
||||
when(auditorAware.getCurrentAuditor()).thenReturn(user);
|
||||
}
|
||||
|
||||
protected AuditingHandler<AuditedUser> getHandler() {
|
||||
return new AuditingHandler<AuditedUser>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the advice does not set auditor on the target entity if no {@code AuditorAware} was configured.
|
||||
*/
|
||||
@Test
|
||||
public void doesNotSetAuditorIfNotConfigured() {
|
||||
|
||||
handler.markCreated(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() {
|
||||
|
||||
handler.setAuditorAware(auditorAware);
|
||||
|
||||
handler.markCreated(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() {
|
||||
|
||||
handler.setAuditorAware(auditorAware);
|
||||
handler.setModifyOnCreation(false);
|
||||
handler.markCreated(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 AuditedUser();
|
||||
user.id = 1L;
|
||||
|
||||
handler.setAuditorAware(auditorAware);
|
||||
handler.markModified(user);
|
||||
|
||||
assertNull(user.getCreatedBy());
|
||||
assertNull(user.getCreatedDate());
|
||||
|
||||
assertNotNull(user.getLastModifiedBy());
|
||||
assertNotNull(user.getLastModifiedDate());
|
||||
|
||||
verify(auditorAware).getCurrentAuditor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotSetTimeIfConfigured() {
|
||||
|
||||
handler.setDateTimeForNow(false);
|
||||
handler.setAuditorAware(auditorAware);
|
||||
handler.markCreated(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);
|
||||
|
||||
handler.setDateTimeProvider(provider);
|
||||
handler.markCreated(user);
|
||||
|
||||
verify(provider, times(1)).getDateTime();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.auditing;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.support.IsNewStrategy;
|
||||
import org.springframework.data.support.IsNewStrategyFactory;
|
||||
|
||||
/**
|
||||
* Unit test for {@code AuditingHandler}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 1.5
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class IsNewAwareAuditingHandlerUnitTests extends AuditingHandlerUnitTests {
|
||||
|
||||
@Mock
|
||||
IsNewStrategyFactory factory;
|
||||
@Mock
|
||||
IsNewStrategy strategy;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
when(factory.getIsNewStrategy(Mockito.any(Class.class))).thenReturn(strategy);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IsNewAwareAuditingHandler<AuditedUser> getHandler() {
|
||||
return new IsNewAwareAuditingHandler<AuditedUser>(factory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delegatesToMarkCreatedForNewEntity() {
|
||||
|
||||
when(strategy.isNew(Mockito.any(Object.class))).thenReturn(true);
|
||||
AuditedUser user = new AuditedUser();
|
||||
getHandler().markAudited(user);
|
||||
|
||||
assertThat(user.createdDate, is(notNullValue()));
|
||||
assertThat(user.modifiedDate, is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delegatesToMarkModifiedForNonNewEntity() {
|
||||
|
||||
when(strategy.isNew(Mockito.any(Object.class))).thenReturn(false);
|
||||
AuditedUser user = new AuditedUser();
|
||||
getHandler().markAudited(user);
|
||||
|
||||
assertThat(user.createdDate, is(nullValue()));
|
||||
assertThat(user.modifiedDate, is(notNullValue()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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.auditing;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
import org.springframework.data.auditing.AuditableBeanWrapperFactory.ReflectionAuditingBeanWrapper;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ReflectionAuditingBeanWrapper}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 1.5
|
||||
*/
|
||||
public class ReflectionAuditingBeanWrapperUnitTests {
|
||||
|
||||
AnnotationAuditingMetadata metadata;
|
||||
AnnotatedUser user;
|
||||
AuditableBeanWrapper wrapper;
|
||||
|
||||
DateTime time = new DateTime();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
this.user = new AnnotatedUser();
|
||||
this.wrapper = new ReflectionAuditingBeanWrapper(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setsDateTimeFieldCorrectly() {
|
||||
|
||||
wrapper.setCreatedDate(time);
|
||||
assertThat(user.createdDate, is(time));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setsDateFieldCorrectly() {
|
||||
|
||||
wrapper.setLastModifiedDate(time);
|
||||
assertThat(user.lastModifiedDate, is(time.toDate()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setsLongFieldCorrectly() {
|
||||
|
||||
class Sample {
|
||||
|
||||
@CreatedDate
|
||||
Long createdDate;
|
||||
|
||||
@LastModifiedDate
|
||||
long modifiedDate;
|
||||
}
|
||||
|
||||
Sample sample = new Sample();
|
||||
AuditableBeanWrapper wrapper = new ReflectionAuditingBeanWrapper(sample);
|
||||
|
||||
wrapper.setCreatedDate(time);
|
||||
assertThat(sample.createdDate, is(time.getMillis()));
|
||||
|
||||
wrapper.setLastModifiedDate(time);
|
||||
assertThat(sample.modifiedDate, is(time.getMillis()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setsAuditorFieldsCorrectly() {
|
||||
|
||||
Object object = new Object();
|
||||
|
||||
wrapper.setCreatedBy(object);
|
||||
assertThat(user.createdBy, is(object));
|
||||
|
||||
wrapper.setLastModifiedBy(object);
|
||||
assertThat(user.lastModifiedBy, is(object));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user