Fix build on Java 16.

Make sure to use an initialized MappingContext.

Closes: #3749
Original pull request: #3752.
This commit is contained in:
Christoph Strobl
2021-07-26 13:10:15 +02:00
committed by Mark Paluch
parent 7538b1a1a5
commit 1d943d62a3
8 changed files with 174 additions and 54 deletions

View File

@@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.TimeSeries;
import org.springframework.data.mongodb.test.util.MongoTestMappingContext;
/**
* Unit tests for {@link EntityOperations}.
@@ -32,7 +33,7 @@ import org.springframework.data.mongodb.core.mapping.TimeSeries;
*/
class EntityOperationsUnitTests {
EntityOperations operations = new EntityOperations(new MongoMappingContext());
EntityOperations operations = new EntityOperations(MongoTestMappingContext.newTestContext());
@Test // GH-3731
void shouldReportInvalidTimeField() {

View File

@@ -138,7 +138,7 @@ public class MongoTemplateTests {
cfg.configureMappingContext(it -> {
it.autocreateIndex(false);
it.intitalEntitySet(AuditablePerson.class);
it.initialEntitySet(AuditablePerson.class);
});

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2021 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
*
* https://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.mongodb.test.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.springframework.lang.Nullable;
/**
* Utility to configure {@link org.springframework.data.mongodb.core.mapping.MongoMappingContext} properties.
*
* @author Christoph Strobl
*/
public class MappingContextConfigurer {
private @Nullable Set<Class<?>> intitalEntitySet;
boolean autocreateIndex = false;
public void autocreateIndex(boolean autocreateIndex) {
this.autocreateIndex = autocreateIndex;
}
public void initialEntitySet(Set<Class<?>> initialEntitySet) {
this.intitalEntitySet = initialEntitySet;
}
public void initialEntitySet(Class<?>... initialEntitySet) {
this.intitalEntitySet = new HashSet<>(Arrays.asList(initialEntitySet));
}
Set<Class<?>> initialEntitySet() {
return intitalEntitySet != null ? intitalEntitySet : Collections.emptySet();
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2021 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
*
* https://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.mongodb.test.util;
import java.util.Arrays;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
/**
* Utility to configure {@link MongoCustomConversions}.
*
* @author Christoph Strobl
*/
public class MongoConverterConfigurer {
CustomConversions customConversions;
public void customConversions(CustomConversions customConversions) {
this.customConversions = customConversions;
}
public void customConverters(Converter<?, ?>... converters) {
customConversions(new MongoCustomConversions(Arrays.asList(converters)));
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2021 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
*
* https://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.mongodb.test.util;
import java.util.Collections;
import java.util.function.Consumer;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
/**
* @author Christoph Strobl
*/
public class MongoTestMappingContext extends MongoMappingContext {
private MappingContextConfigurer contextConfigurer;
private MongoConverterConfigurer converterConfigurer;
public static MongoTestMappingContext newTestContext() {
return new MongoTestMappingContext(conig -> {}).init();
}
public MongoTestMappingContext(MappingContextConfigurer contextConfig) {
this.contextConfigurer = contextConfig;
this.converterConfigurer = new MongoConverterConfigurer();
}
public MongoTestMappingContext(Consumer<MappingContextConfigurer> contextConfig) {
this(new MappingContextConfigurer());
contextConfig.accept(contextConfigurer);
}
public MongoTestMappingContext customConversions(MongoConverterConfigurer converterConfig) {
this.converterConfigurer = converterConfig;
return this;
}
public MongoTestMappingContext customConversions(Consumer<MongoConverterConfigurer> converterConfig) {
converterConfig.accept(converterConfigurer);
return this;
}
public MongoTestMappingContext init() {
setInitialEntitySet(contextConfigurer.initialEntitySet());
setAutoIndexCreation(contextConfigurer.autocreateIndex);
if (converterConfigurer.customConversions != null) {
setSimpleTypeHolder(converterConfigurer.customConversions.getSimpleTypeHolder());
} else {
setSimpleTypeHolder(new MongoCustomConversions(Collections.emptyList()).getSimpleTypeHolder());
}
super.afterPropertiesSet();
return this;
}
@Override
public void afterPropertiesSet() {
init();
}
}

View File

@@ -50,7 +50,7 @@ public class MongoTestTemplate extends MongoTemplate {
cfg.configureMappingContext(it -> {
it.autocreateIndex(false);
it.intitalEntitySet(initialEntities);
it.initialEntitySet(initialEntities);
});
});
}

View File

@@ -16,20 +16,15 @@
package org.springframework.data.mongodb.test.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.auditing.IsNewAwareAuditingHandler;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
@@ -115,16 +110,7 @@ public class MongoTestTemplateConfiguration {
MongoMappingContext mappingContext() {
if (mappingContext == null) {
mappingContext = new MongoMappingContext();
mappingContext.setInitialEntitySet(mappingContextConfigurer.initialEntitySet());
mappingContext.setAutoIndexCreation(mappingContextConfigurer.autocreateIndex);
if(mongoConverterConfigurer.customConversions != null) {
mappingContext.setSimpleTypeHolder(mongoConverterConfigurer.customConversions.getSimpleTypeHolder());
} else {
mappingContext.setSimpleTypeHolder(new MongoCustomConversions(Collections.emptyList()).getSimpleTypeHolder());
}
mappingContext.afterPropertiesSet();
mappingContext = new MongoTestMappingContext(mappingContextConfigurer).customConversions(mongoConverterConfigurer).init();
}
return mappingContext;
@@ -222,41 +208,6 @@ public class MongoTestTemplateConfiguration {
}
}
public static class MongoConverterConfigurer {
CustomConversions customConversions;
public void customConversions(CustomConversions customConversions) {
this.customConversions = customConversions;
}
public void customConverters(Converter<?, ?>... converters) {
customConversions(new MongoCustomConversions(Arrays.asList(converters)));
}
}
public static class MappingContextConfigurer {
Set<Class<?>> intitalEntitySet;
boolean autocreateIndex = false;
public void autocreateIndex(boolean autocreateIndex) {
this.autocreateIndex = autocreateIndex;
}
public void intitalEntitySet(Set<Class<?>> intitalEntitySet) {
this.intitalEntitySet = intitalEntitySet;
}
public void intitalEntitySet(Class<?>... initialEntitySet) {
this.intitalEntitySet = new HashSet<>(Arrays.asList(initialEntitySet));
}
Set<Class<?>> initialEntitySet() {
return intitalEntitySet != null ? intitalEntitySet : Collections.emptySet();
}
}
public static class AuditingConfigurer {
Function<MappingContext, IsNewAwareAuditingHandler> auditingHandlerFunction;

View File

@@ -56,7 +56,7 @@ public class ReactiveMongoTestTemplate extends ReactiveMongoTemplate {
cfg.configureMappingContext(it -> {
it.autocreateIndex(false);
it.intitalEntitySet(initialEntities);
it.initialEntitySet(initialEntities);
});
});
}