DATACMNS-414 - Removed unnecessary generic types for AuditingHandler.

Removed the unnecessary type arguments for AuditingHandler so that the setter taking an AuditorAware<T> can be successfully wired against implementations other than AuditorAware<Object> with Spring 4. The type argument was superfluous anyway as internally the type wasn't referred to and the handler is not used on a by-type basis.
This commit is contained in:
Oliver Gierke
2014-01-10 16:39:10 +01:00
parent e21a7850f6
commit 05f303c2be
5 changed files with 35 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-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.
@@ -15,6 +15,8 @@
*/
package org.springframework.data.auditing;
import java.util.Calendar;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -29,13 +31,13 @@ import org.springframework.util.Assert;
* @author Oliver Gierke
* @since 1.5
*/
public class AuditingHandler<T> implements InitializingBean {
public class AuditingHandler implements InitializingBean {
private static final Logger LOGGER = LoggerFactory.getLogger(AuditingHandler.class);
private final AuditableBeanWrapperFactory factory = new AuditableBeanWrapperFactory();
private DateTimeProvider dateTimeProvider = CurrentDateTimeProvider.INSTANCE;
private AuditorAware<T> auditorAware;
private AuditorAware<?> auditorAware;
private boolean dateTimeForNow = true;
private boolean modifyOnCreation = true;
@@ -44,7 +46,7 @@ public class AuditingHandler<T> implements InitializingBean {
*
* @param auditorAware the auditorAware to set
*/
public void setAuditorAware(final AuditorAware<T> auditorAware) {
public void setAuditorAware(final AuditorAware<?> auditorAware) {
Assert.notNull(auditorAware);
this.auditorAware = auditorAware;
@@ -106,8 +108,8 @@ public class AuditingHandler<T> implements InitializingBean {
return;
}
T auditor = touchAuditor(wrapper, isNew);
DateTime now = dateTimeForNow ? touchDate(wrapper, isNew) : null;
Object auditor = touchAuditor(wrapper, isNew);
Calendar now = dateTimeForNow ? touchDate(wrapper, isNew) : null;
Object defaultedNow = now == null ? "not set" : now;
Object defaultedAuditor = auditor == null ? "unknown" : auditor;
@@ -121,13 +123,13 @@ public class AuditingHandler<T> implements InitializingBean {
* @param auditable
* @return
*/
private T touchAuditor(AuditableBeanWrapper wrapper, boolean isNew) {
private Object touchAuditor(AuditableBeanWrapper wrapper, boolean isNew) {
if (null == auditorAware) {
return null;
}
T auditor = auditorAware.getCurrentAuditor();
Object auditor = auditorAware.getCurrentAuditor();
if (isNew) {
wrapper.setCreatedBy(auditor);
@@ -146,9 +148,9 @@ public class AuditingHandler<T> implements InitializingBean {
* @param wrapper
* @return
*/
private DateTime touchDate(AuditableBeanWrapper wrapper, boolean isNew) {
private Calendar touchDate(AuditableBeanWrapper wrapper, boolean isNew) {
DateTime now = dateTimeProvider.getDateTime();
Calendar now = dateTimeProvider.getNow();
if (isNew) {
wrapper.setCreatedDate(now);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-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.
@@ -27,7 +27,7 @@ import org.springframework.util.Assert;
* @author Oliver Gierke
* @since 1.5
*/
public class IsNewAwareAuditingHandler<T> extends AuditingHandler<T> {
public class IsNewAwareAuditingHandler extends AuditingHandler {
private final IsNewStrategyFactory isNewStrategyFactory;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2012 the original author or authors.
* Copyright 2008-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.
@@ -31,7 +31,7 @@ import org.springframework.data.domain.AuditorAware;
@SuppressWarnings("unchecked")
public class AuditingHandlerUnitTests {
AuditingHandler<AuditedUser> handler;
AuditingHandler handler;
AuditorAware<AuditedUser> auditorAware;
AuditedUser user;
@@ -46,8 +46,8 @@ public class AuditingHandlerUnitTests {
when(auditorAware.getCurrentAuditor()).thenReturn(user);
}
protected AuditingHandler<AuditedUser> getHandler() {
return new AuditingHandler<AuditedUser>();
protected AuditingHandler getHandler() {
return new AuditingHandler();
}
/**
@@ -149,6 +149,6 @@ public class AuditingHandlerUnitTests {
handler.setDateTimeProvider(provider);
handler.markCreated(user);
verify(provider, times(1)).getDateTime();
verify(provider, times(1)).getNow();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2012 the original author or authors.
* Copyright 2008-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.
@@ -37,10 +37,8 @@ import org.springframework.data.support.IsNewStrategyFactory;
@RunWith(MockitoJUnitRunner.class)
public class IsNewAwareAuditingHandlerUnitTests extends AuditingHandlerUnitTests {
@Mock
IsNewStrategyFactory factory;
@Mock
IsNewStrategy strategy;
@Mock IsNewStrategyFactory factory;
@Mock IsNewStrategy strategy;
@Before
public void init() {
@@ -48,8 +46,8 @@ public class IsNewAwareAuditingHandlerUnitTests extends AuditingHandlerUnitTests
}
@Override
protected IsNewAwareAuditingHandler<AuditedUser> getHandler() {
return new IsNewAwareAuditingHandler<AuditedUser>(factory);
protected IsNewAwareAuditingHandler getHandler() {
return new IsNewAwareAuditingHandler(factory);
}
@Test

View File

@@ -18,6 +18,9 @@ package org.springframework.data.auditing;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
@@ -37,11 +40,14 @@ public class ReflectionAuditingBeanWrapperUnitTests {
AnnotatedUser user;
AuditableBeanWrapper wrapper;
DateTime time = new DateTime();
Calendar calendar = new GregorianCalendar();
DateTime time = new DateTime(calendar);
@Before
public void setUp() {
assertThat(time, is(new DateTime(calendar)));
this.user = new AnnotatedUser();
this.wrapper = new ReflectionAuditingBeanWrapper(user);
}
@@ -49,14 +55,14 @@ public class ReflectionAuditingBeanWrapperUnitTests {
@Test
public void setsDateTimeFieldCorrectly() {
wrapper.setCreatedDate(time);
wrapper.setCreatedDate(calendar);
assertThat(user.createdDate, is(time));
}
@Test
public void setsDateFieldCorrectly() {
wrapper.setLastModifiedDate(time);
wrapper.setLastModifiedDate(calendar);
assertThat(user.lastModifiedDate, is(time.toDate()));
}
@@ -73,10 +79,10 @@ public class ReflectionAuditingBeanWrapperUnitTests {
Sample sample = new Sample();
AuditableBeanWrapper wrapper = new ReflectionAuditingBeanWrapper(sample);
wrapper.setCreatedDate(time);
wrapper.setCreatedDate(calendar);
assertThat(sample.createdDate, is(time.getMillis()));
wrapper.setLastModifiedDate(time);
wrapper.setLastModifiedDate(calendar);
assertThat(sample.modifiedDate, is(time.getMillis()));
}