DATAJPA-9 - Expose flag to disable setting dates in auditing namespace.

Auditing namespace element now has a set-dates attribute that defaults to true, but also allows disabling that in case one wants to use database set values.
This commit is contained in:
Oliver Gierke
2011-01-25 16:42:11 +02:00
parent d108bc9fed
commit c919e2a538
4 changed files with 77 additions and 6 deletions

View File

@@ -38,7 +38,7 @@ import org.w3c.dom.Element;
*/
public class AuditingBeanDefinitionParser implements BeanDefinitionParser {
private static final String AUDITING_ENTITY_LISTENER_CLASS_NAME =
static final String AUDITING_ENTITY_LISTENER_CLASS_NAME =
"org.springframework.data.jpa.domain.support.AuditingEntityListener";
private static final String AUDITING_BFPP_CLASS_NAME =
"org.springframework.data.jpa.domain.support.AuditingBeanFactoryPostProcessor";
@@ -66,6 +66,9 @@ public class AuditingBeanDefinitionParser implements BeanDefinitionParser {
createLazyInitTargetSourceBeanDefinition(auditorAwareRef));
}
builder.addPropertyValue("dateTimeForNow",
element.getAttribute("set-dates"));
registerInfrastructureBeanWithId(builder.getRawBeanDefinition(),
AUDITING_ENTITY_LISTENER_CLASS_NAME, parser, element);

View File

@@ -35,11 +35,6 @@
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="auditing">
<xsd:complexType>
@@ -53,6 +48,7 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="set-dates" default="true" type="xsd:boolean" />
</xsd:complexType>
</xsd:element>

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2008-2011 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.jpa.repository.config;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
* Integration tests for {@link AuditingBeanDefinitionParser}.
*
* @author Oliver Gierke
*/
public class AuditingBeanDefinitionParserTests {
@Test
public void settingDatesIsConfigured() throws Exception {
assertSetDatesIsSetTo("auditing/auditing-namespace-context.xml", "true");
}
@Test
public void notSettingDatesIsConfigured() throws Exception {
assertSetDatesIsSetTo("auditing/auditing-namespace-context2.xml",
"false");
}
private void assertSetDatesIsSetTo(String configFile, String value) {
XmlBeanFactory factory =
new XmlBeanFactory(new ClassPathResource(configFile));
BeanDefinition definition =
factory.getBeanDefinition(AuditingBeanDefinitionParser.AUDITING_ENTITY_LISTENER_CLASS_NAME);
PropertyValue propertyValue =
definition.getPropertyValues().getPropertyValue(
"dateTimeForNow");
assertThat(propertyValue, is(notNullValue()));
assertThat((String) propertyValue.getValue(), is(value));
}
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:auditing set-dates="false" />
</beans>