DATACMNS-389 - Support for JavaConfig in auditing.

Basic infrastructure for supporting annotation based auditing configuration.

Introduces AuditingBeanDefinitionRegistrarSupport for store specific auditing configuration enhancements. An AuditorAware instance is wired into the Auditing handler automatically if it's present in the ApplicationContext. The auditorAwareRef attribute of the enabling annotation can be used to disambiguate in case multiple AuditorAware instances are present in the configuration.

Original pull requests: #26, #53.
This commit is contained in:
Thomas Darimont
2013-03-07 22:24:11 +08:00
committed by Oliver Gierke
parent e616ec5d05
commit a7d909846c
5 changed files with 436 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2013 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 java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Dummy Annotation to test auditing annotation configuration.
*
* @author Thomas Darimont
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface EnableAuditing {
/**
* @return References a bean of type AuditorAware to represent the current principal.
*/
String auditorAwareRef() default "";
/**
* @return Configures whether the creation and modification dates are set and defaults to {@literal true}.
*/
boolean setDates() default true;
/**
* @return Configures whether the entity shall be marked as modified on creation and defaults to {@literal true}.
*/
boolean modifyOnCreate() default true;
/**
* @return Configures a {@link DateTimeProvider} that allows customizing which DateTime shall be used for setting
* creation and modification dates.
*/
String dateTimeProviderRef() default "";
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2013 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.config;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.lang.annotation.Annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.data.auditing.EnableAuditing;
/**
* @author Ranie Jade Ramiso
* @author Thomas Darimont
*/
@RunWith(MockitoJUnitRunner.class)
public class AuditingBeanDefinitionRegistrarSupportUnitTests {
@Mock private BeanDefinitionRegistry registry;
@Test
public void testRegisterBeanDefinitions() {
AuditingBeanDefinitionRegistrarSupport registrar = new DummyAuditingBeanDefinitionRegistrarSupport();
AnnotationMetadata metadata = new StandardAnnotationMetadata(SampleConfig.class);
registrar.registerBeanDefinitions(metadata, registry);
verify(registry, times(1)).registerBeanDefinition(anyString(), any(BeanDefinition.class));
}
static class SampleConfig {}
static class DummyAuditingBeanDefinitionRegistrarSupport extends AuditingBeanDefinitionRegistrarSupport {
@Override
protected void registerAuditListenerBeanDefinition(BeanDefinition auditingHandlerDefinition,
BeanDefinitionRegistry registry) {}
@Override
protected Class<? extends Annotation> getAnnotation() {
return EnableAuditing.class;
}
@Override
protected AnnotationAuditingConfiguration getConfiguration(AnnotationMetadata annotationMetadata) {
return new AnnotationAuditingConfiguration() {
public String getAuditorAwareRef() {
return "auditor";
}
public boolean isSetDates() {
return true;
}
public String getDateTimeProviderRef() {
return "dateTimeProvider";
}
public boolean isModifyOnCreate() {
return true;
}
};
}
}
}