DATACMNS-179 - Defer adding initial entity set to AbstractMappingContext.

So far the domain classes to be added to the mapping context initially were added in afterPropertiesSet() of InitializingBean. This had the drawback that although the MappingContext is already set up properly the actual listeners of the events fired (e.g. index creating ones) might not have been set up properly. We list to the ContextRefreshedEvent indicating the entire ApplicationContext being set up completely. Thus all infrastructure components are set up properly and the timing issues doesn't exist anymore.
This commit is contained in:
Oliver Gierke
2012-06-04 12:24:10 +02:00
parent 31926a6299
commit e5c6af6b24
4 changed files with 83 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2011-2012 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.
@@ -26,6 +26,11 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentProperty;
@@ -42,9 +47,12 @@ import org.springframework.data.util.TypeInformation;
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class ConfigurableTypeInformationMapperUnitTests<T extends PersistentProperty<T>> {
ConfigurableTypeInformationMapper mapper;
@Mock
ApplicationContext context;
@Before
public void setUp() {
@@ -93,8 +101,11 @@ public class ConfigurableTypeInformationMapperUnitTests<T extends PersistentProp
}
};
ContextRefreshedEvent event = new ContextRefreshedEvent(context);
mappingContext.setInitialEntitySet(Collections.singleton(Entity.class));
mappingContext.afterPropertiesSet();
mappingContext.setApplicationContext(context);
mappingContext.onApplicationEvent(event);
mapper = new ConfigurableTypeInformationMapper(mappingContext);

View File

@@ -44,6 +44,7 @@ public class MappingMetadataTests {
}
@Test
@SuppressWarnings("deprecation")
public void testPojoWithId() {
ctx.setInitialEntitySet(Collections.singleton(PersonWithId.class));
@@ -55,6 +56,7 @@ public class MappingMetadataTests {
}
@Test
@SuppressWarnings("deprecation")
public void testAssociations() {
ctx.setInitialEntitySet(Collections.singleton(PersonWithChildren.class));
@@ -101,4 +103,4 @@ public class MappingMetadataTests {
return new Association<MappingMetadataTests.SampleProperty>(this, null);
}
}
}
}

View File

@@ -1,13 +1,19 @@
package org.springframework.data.mapping.context;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.mapping.model.AbstractPersistentProperty;
@@ -54,6 +60,24 @@ public class AbstractMappingContextUnitTests {
context.getPersistentEntity(Unsupported.class);
}
@Test
@SuppressWarnings("deprecation")
public void registersEntitiesOnContextRefreshedEvent() {
ApplicationContext context = mock(ApplicationContext.class);
DummyMappingContext mappingContext = new DummyMappingContext();
mappingContext.setInitialEntitySet(Collections.singleton(Person.class));
mappingContext.setApplicationContext(context);
mappingContext.setApplicationEventPublisher(context);
mappingContext.afterPropertiesSet();
verify(context, times(0)).publishEvent(Mockito.any(ApplicationEvent.class));
mappingContext.onApplicationEvent(new ContextRefreshedEvent(context));
verify(context, times(1)).publishEvent(Mockito.any(ApplicationEvent.class));
}
class Person {
String name;
}