DATAJPA-265 - Support for auditing configuration via JavaConfig.

Introduces the necessary infrastructure to configure auditing with JPA via JavaConfig using @EnableJpaAuditing.

Original pull request: #50.
This commit is contained in:
Thomas Darimont
2013-10-28 11:00:45 +01:00
committed by Oliver Gierke
parent 23e27f3332
commit 44806cb7f1
13 changed files with 477 additions and 44 deletions

View File

@@ -33,21 +33,20 @@ import org.springframework.data.jpa.domain.AbstractAuditable;
*/
@Entity
@NamedQuery(name = "AuditableUser.findByFirstname", query = "SELECT u FROM AuditableUser u WHERE u.firstname = ?1")
public class AuditableUser extends AbstractAuditable<AuditableUser, Long> {
public class AuditableUser extends AbstractAuditable<AuditableUser, Integer> {
private static final long serialVersionUID = 7409344446795693011L;
private String firstname;
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Set<AuditableRole> roles = new HashSet<AuditableRole>();
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) private Set<AuditableRole> roles = new HashSet<AuditableRole>();
public AuditableUser() {
this(null);
}
public AuditableUser(Long id) {
public AuditableUser(Integer id) {
this.setId(id);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2011 the original author or authors.
* Copyright 2008-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.
@@ -15,13 +15,11 @@
*/
package org.springframework.data.jpa.domain.support;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
@@ -30,6 +28,7 @@ import org.springframework.core.io.ClassPathResource;
* Unit test for {@link AuditingBeanFactoryPostProcessor}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class AuditingBeanFactoryPostProcessorUnitTests {
@@ -52,12 +51,10 @@ public class AuditingBeanFactoryPostProcessorUnitTests {
}
@Test
public void testname() throws Exception {
public void beanConfigurerAspectShouldBeConfiguredAfterPostProcessing() throws Exception {
processor.postProcessBeanFactory(beanFactory);
BeanDefinition definition = beanFactory.getBeanDefinition("entityManagerFactory");
assertTrue(Arrays.asList(definition.getDependsOn()).contains(
AuditingBeanFactoryPostProcessor.BEAN_CONFIGURER_ASPECT_BEAN_NAME));
assertThat(beanFactory.isBeanNameInUse(AuditingBeanFactoryPostProcessor.BEAN_CONFIGURER_ASPECT_BEAN_NAME), is(true));
}
}

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.jpa.repository.config;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.domain.sample.AuditableUser;
import org.springframework.data.jpa.repository.sample.AuditableUserRepository;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Integration tests for auditing via Java config.
*
* @author Thomas Darimont
*/
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public abstract class AbstractAuditingViaJavaConfigRepositoriesTests {
@Autowired AuditableUserRepository auditableUserRepository;
@Autowired AuditorAware<AuditableUser> auditorAware;
AuditableUser auditor;
@Configuration
@Import(InfrastructureConfig.class)
@EnableJpaRepositories(basePackageClasses = AuditableUserRepository.class)
static class TestConfig {}
@Before
public void setup() {
AuditableUser auditor = new AuditableUser(null);
auditor.setFirstname("auditor");
this.auditor = this.auditableUserRepository.save(auditor);
doReturn(this.auditor).when(this.auditorAware).getCurrentAuditor();
}
@After
public void teardown() {
doReturn(null).when(this.auditorAware).getCurrentAuditor();
}
@Test
@Transactional
public void basicAuditing() throws Exception {
AuditableUser user = new AuditableUser(null);
user.setFirstname("user");
AuditableUser savedUser = auditableUserRepository.save(user);
TimeUnit.MILLISECONDS.sleep(10);
assertThat(savedUser.getCreatedDate(), is(notNullValue()));
assertThat(savedUser.getCreatedDate().isBeforeNow(), is(true));
AuditableUser createdBy = savedUser.getCreatedBy();
assertThat(createdBy, is(notNullValue()));
assertThat(createdBy.getFirstname(), is(this.auditor.getFirstname()));
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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.jpa.repository.config;
import static org.mockito.Mockito.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.domain.sample.AuditableUser;
import org.springframework.test.context.ContextConfiguration;
/**
* Integration tests for auditing via Java config with default configuration.
*
* @author Thomas Darimont
*/
@ContextConfiguration
public class DefaultAuditingViaJavaConfigRepositoriesTests extends AbstractAuditingViaJavaConfigRepositoriesTests {
@Configuration
@EnableJpaAuditing
@Import(TestConfig.class)
static class Config {
@Bean
public AuditorAware<AuditableUser> auditorProvider() {
return mock(AuditorAware.class);
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.jpa.repository.config;
import static org.mockito.Mockito.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.domain.sample.AuditableUser;
import org.springframework.test.context.ContextConfiguration;
/**
* Integration tests for auditing via Java config with explicit configuration.
*
* @author Thomas Darimont
*/
@ContextConfiguration
public class ExplicitAuditingViaJavaConfigRepositoriesTests extends AbstractAuditingViaJavaConfigRepositoriesTests {
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
@Import(TestConfig.class)
static class Config {
@Bean
public AuditorAware<AuditableUser> auditorProvider() {
return mock(AuditorAware.class);
}
}
}

View File

@@ -0,0 +1,70 @@
/*
* 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.jpa.repository.config;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* Reusable config class for testing pure java based Spring configuration.
*
* @author Thomas Darimont
*/
@Configuration
@EnableTransactionManagement
class InfrastructureConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setName("data").setType(EmbeddedDatabaseType.HSQL).build();
}
@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
@Bean
public HibernateJpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setGenerateDdl(true);
adapter.setDatabase(Database.HSQL);
return adapter;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setJpaVendorAdapter(jpaVendorAdapter());
em.setPackagesToScan("purejavaconfig");
return em;
}
}