Skip to content

Commit 05f303c

Browse files
committed
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.
1 parent e21a785 commit 05f303c

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

src/main/java/org/springframework/data/auditing/AuditingHandler.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.auditing;
1717

18+
import java.util.Calendar;
19+
1820
import org.joda.time.DateTime;
1921
import org.slf4j.Logger;
2022
import org.slf4j.LoggerFactory;
@@ -29,13 +31,13 @@
2931
* @author Oliver Gierke
3032
* @since 1.5
3133
*/
32-
public class AuditingHandler<T> implements InitializingBean {
34+
public class AuditingHandler implements InitializingBean {
3335

3436
private static final Logger LOGGER = LoggerFactory.getLogger(AuditingHandler.class);
3537

3638
private final AuditableBeanWrapperFactory factory = new AuditableBeanWrapperFactory();
3739
private DateTimeProvider dateTimeProvider = CurrentDateTimeProvider.INSTANCE;
38-
private AuditorAware<T> auditorAware;
40+
private AuditorAware<?> auditorAware;
3941
private boolean dateTimeForNow = true;
4042
private boolean modifyOnCreation = true;
4143

@@ -44,7 +46,7 @@ public class AuditingHandler<T> implements InitializingBean {
4446
*
4547
* @param auditorAware the auditorAware to set
4648
*/
47-
public void setAuditorAware(final AuditorAware<T> auditorAware) {
49+
public void setAuditorAware(final AuditorAware<?> auditorAware) {
4850

4951
Assert.notNull(auditorAware);
5052
this.auditorAware = auditorAware;
@@ -106,8 +108,8 @@ private void touch(Object target, boolean isNew) {
106108
return;
107109
}
108110

109-
T auditor = touchAuditor(wrapper, isNew);
110-
DateTime now = dateTimeForNow ? touchDate(wrapper, isNew) : null;
111+
Object auditor = touchAuditor(wrapper, isNew);
112+
Calendar now = dateTimeForNow ? touchDate(wrapper, isNew) : null;
111113

112114
Object defaultedNow = now == null ? "not set" : now;
113115
Object defaultedAuditor = auditor == null ? "unknown" : auditor;
@@ -121,13 +123,13 @@ private void touch(Object target, boolean isNew) {
121123
* @param auditable
122124
* @return
123125
*/
124-
private T touchAuditor(AuditableBeanWrapper wrapper, boolean isNew) {
126+
private Object touchAuditor(AuditableBeanWrapper wrapper, boolean isNew) {
125127

126128
if (null == auditorAware) {
127129
return null;
128130
}
129131

130-
T auditor = auditorAware.getCurrentAuditor();
132+
Object auditor = auditorAware.getCurrentAuditor();
131133

132134
if (isNew) {
133135
wrapper.setCreatedBy(auditor);
@@ -146,9 +148,9 @@ private T touchAuditor(AuditableBeanWrapper wrapper, boolean isNew) {
146148
* @param wrapper
147149
* @return
148150
*/
149-
private DateTime touchDate(AuditableBeanWrapper wrapper, boolean isNew) {
151+
private Calendar touchDate(AuditableBeanWrapper wrapper, boolean isNew) {
150152

151-
DateTime now = dateTimeProvider.getDateTime();
153+
Calendar now = dateTimeProvider.getNow();
152154

153155
if (isNew) {
154156
wrapper.setCreatedDate(now);

src/main/java/org/springframework/data/auditing/IsNewAwareAuditingHandler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@
2727
* @author Oliver Gierke
2828
* @since 1.5
2929
*/
30-
public class IsNewAwareAuditingHandler<T> extends AuditingHandler<T> {
30+
public class IsNewAwareAuditingHandler extends AuditingHandler {
3131

3232
private final IsNewStrategyFactory isNewStrategyFactory;
3333

src/test/java/org/springframework/data/auditing/AuditingHandlerUnitTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2012 the original author or authors.
2+
* Copyright 2008-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@
3131
@SuppressWarnings("unchecked")
3232
public class AuditingHandlerUnitTests {
3333

34-
AuditingHandler<AuditedUser> handler;
34+
AuditingHandler handler;
3535
AuditorAware<AuditedUser> auditorAware;
3636

3737
AuditedUser user;
@@ -46,8 +46,8 @@ public void setUp() {
4646
when(auditorAware.getCurrentAuditor()).thenReturn(user);
4747
}
4848

49-
protected AuditingHandler<AuditedUser> getHandler() {
50-
return new AuditingHandler<AuditedUser>();
49+
protected AuditingHandler getHandler() {
50+
return new AuditingHandler();
5151
}
5252

5353
/**
@@ -149,6 +149,6 @@ public void usesDateTimeProviderIfConfigured() {
149149
handler.setDateTimeProvider(provider);
150150
handler.markCreated(user);
151151

152-
verify(provider, times(1)).getDateTime();
152+
verify(provider, times(1)).getNow();
153153
}
154154
}

src/test/java/org/springframework/data/auditing/IsNewAwareAuditingHandlerUnitTests.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2012 the original author or authors.
2+
* Copyright 2008-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,19 +37,17 @@
3737
@RunWith(MockitoJUnitRunner.class)
3838
public class IsNewAwareAuditingHandlerUnitTests extends AuditingHandlerUnitTests {
3939

40-
@Mock
41-
IsNewStrategyFactory factory;
42-
@Mock
43-
IsNewStrategy strategy;
40+
@Mock IsNewStrategyFactory factory;
41+
@Mock IsNewStrategy strategy;
4442

4543
@Before
4644
public void init() {
4745
when(factory.getIsNewStrategy(Mockito.any(Class.class))).thenReturn(strategy);
4846
}
4947

5048
@Override
51-
protected IsNewAwareAuditingHandler<AuditedUser> getHandler() {
52-
return new IsNewAwareAuditingHandler<AuditedUser>(factory);
49+
protected IsNewAwareAuditingHandler getHandler() {
50+
return new IsNewAwareAuditingHandler(factory);
5351
}
5452

5553
@Test

src/test/java/org/springframework/data/auditing/ReflectionAuditingBeanWrapperUnitTests.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
2020

21+
import java.util.Calendar;
22+
import java.util.GregorianCalendar;
23+
2124
import org.joda.time.DateTime;
2225
import org.junit.Before;
2326
import org.junit.Test;
@@ -37,26 +40,29 @@ public class ReflectionAuditingBeanWrapperUnitTests {
3740
AnnotatedUser user;
3841
AuditableBeanWrapper wrapper;
3942

40-
DateTime time = new DateTime();
43+
Calendar calendar = new GregorianCalendar();
44+
DateTime time = new DateTime(calendar);
4145

4246
@Before
4347
public void setUp() {
4448

49+
assertThat(time, is(new DateTime(calendar)));
50+
4551
this.user = new AnnotatedUser();
4652
this.wrapper = new ReflectionAuditingBeanWrapper(user);
4753
}
4854

4955
@Test
5056
public void setsDateTimeFieldCorrectly() {
5157

52-
wrapper.setCreatedDate(time);
58+
wrapper.setCreatedDate(calendar);
5359
assertThat(user.createdDate, is(time));
5460
}
5561

5662
@Test
5763
public void setsDateFieldCorrectly() {
5864

59-
wrapper.setLastModifiedDate(time);
65+
wrapper.setLastModifiedDate(calendar);
6066
assertThat(user.lastModifiedDate, is(time.toDate()));
6167
}
6268

@@ -73,10 +79,10 @@ class Sample {
7379
Sample sample = new Sample();
7480
AuditableBeanWrapper wrapper = new ReflectionAuditingBeanWrapper(sample);
7581

76-
wrapper.setCreatedDate(time);
82+
wrapper.setCreatedDate(calendar);
7783
assertThat(sample.createdDate, is(time.getMillis()));
7884

79-
wrapper.setLastModifiedDate(time);
85+
wrapper.setLastModifiedDate(calendar);
8086
assertThat(sample.modifiedDate, is(time.getMillis()));
8187
}
8288

0 commit comments

Comments
 (0)