Polishing.
Move auto index create lookup flag to dedicated method and add issue reference to tests. Original Pull Request: #4275
This commit is contained in:
@@ -59,6 +59,7 @@ import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventL
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
import org.w3c.dom.Element;
|
||||
@@ -94,8 +95,7 @@ public class MappingMongoConverterParser implements BeanDefinitionParser {
|
||||
String id = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE);
|
||||
id = StringUtils.hasText(id) ? id : DEFAULT_CONVERTER_BEAN_NAME;
|
||||
|
||||
String autoIndexCreation = element.getAttribute("auto-index-creation");
|
||||
boolean autoIndexCreationEnabled = StringUtils.hasText(autoIndexCreation) && Boolean.parseBoolean(autoIndexCreation);
|
||||
boolean autoIndexCreationEnabled = isAutoIndexCreationEnabled(element);
|
||||
|
||||
parserContext.pushContainingComponent(new CompositeComponentDefinition("Mapping Mongo Converter", element));
|
||||
|
||||
@@ -196,11 +196,33 @@ public class MappingMongoConverterParser implements BeanDefinitionParser {
|
||||
return new RuntimeBeanReference(validatorName);
|
||||
}
|
||||
|
||||
private static boolean isAutoIndexCreationEnabled(Element element) {
|
||||
|
||||
String autoIndexCreation = element.getAttribute("auto-index-creation");
|
||||
return StringUtils.hasText(autoIndexCreation) && Boolean.parseBoolean(autoIndexCreation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and register the {@link BeanDefinition} for a {@link MongoMappingContext} if not explicitly referenced by a
|
||||
* given {@literal mapping-context-ref} {@link Element#getAttribute(String) attribuite}.
|
||||
*
|
||||
* @return the mapping context bean name.
|
||||
* @deprecated since 4.3. Use
|
||||
* {@link #potentiallyCreateMappingContext(Element, ParserContext, BeanDefinition, String, boolean)}
|
||||
* instead.
|
||||
*/
|
||||
@Deprecated(since = "4.3", forRemoval = true)
|
||||
public static String potentiallyCreateMappingContext(Element element, ParserContext parserContext,
|
||||
@Nullable BeanDefinition conversionsDefinition, @Nullable String converterId) {
|
||||
return potentiallyCreateMappingContext(element, parserContext, conversionsDefinition, converterId, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and register the {@link BeanDefinition} for a {@link MongoMappingContext} if not explicitly referenced by a
|
||||
* given {@literal mapping-context-ref} {@link Element#getAttribute(String) attribuite}.
|
||||
*
|
||||
* @return the mapping context bean name.
|
||||
*/
|
||||
public static String potentiallyCreateMappingContext(Element element, ParserContext parserContext,
|
||||
@Nullable BeanDefinition conversionsDefinition, @Nullable String converterId, boolean autoIndexCreation) {
|
||||
|
||||
@@ -280,8 +302,10 @@ public class MappingMongoConverterParser implements BeanDefinitionParser {
|
||||
ManagedList<BeanMetadataElement> converterBeans = new ManagedList<>();
|
||||
List<Element> converterElements = DomUtils.getChildElementsByTagName(customerConvertersElement, "converter");
|
||||
|
||||
for (Element listenerElement : converterElements) {
|
||||
converterBeans.add(parseConverter(listenerElement, parserContext));
|
||||
if (!ObjectUtils.isEmpty(converterElements)) {
|
||||
for (Element listenerElement : converterElements) {
|
||||
converterBeans.add(parseConverter(listenerElement, parserContext));
|
||||
}
|
||||
}
|
||||
|
||||
// Scan for Converter and GenericConverter beans in the given base-package
|
||||
|
||||
@@ -22,7 +22,6 @@ import java.util.Set;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanReference;
|
||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
||||
@@ -63,22 +62,24 @@ public class MappingMongoConverterParserIntegrationTests {
|
||||
factory.getBean("converter");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // GH-4275
|
||||
void defaultsToFalseForAutoIndexCreation() {
|
||||
|
||||
loadValidConfiguration();
|
||||
MongoMappingContext mongoMappingContext = factory.getBean("converter.mongoMappingContext",
|
||||
MongoMappingContext.class);
|
||||
assertThat(mongoMappingContext.isAutoIndexCreation()).isFalse();
|
||||
}
|
||||
|
||||
@Test // GH-4275
|
||||
void allowsToOverrideAutoIndexCreation() {
|
||||
|
||||
loadValidConfiguration();
|
||||
MongoMappingContext mongoMappingContext = factory.getBean("autoIndexCreationConverter.mongoMappingContext", MongoMappingContext.class);
|
||||
MongoMappingContext mongoMappingContext = factory.getBean("autoIndexCreationConverter.mongoMappingContext",
|
||||
MongoMappingContext.class);
|
||||
assertThat(mongoMappingContext.isAutoIndexCreation()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDefaultValueForAutoIndexCreation() {
|
||||
|
||||
loadValidConfiguration();
|
||||
MongoMappingContext mongoMappingContext = factory.getBean("converter.mongoMappingContext", MongoMappingContext.class);
|
||||
assertThat(mongoMappingContext.isAutoIndexCreation()).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-725
|
||||
void hasCustomTypeMapper() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user