Skip to content

Commit 1d943d6

Browse files
christophstroblmp911de
authored andcommitted
Fix build on Java 16.
Make sure to use an initialized MappingContext. Closes: #3749 Original pull request: #3752.
1 parent 7538b1a commit 1d943d6

File tree

8 files changed

+174
-54
lines changed

8 files changed

+174
-54
lines changed

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/EntityOperationsUnitTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.data.mapping.MappingException;
2525
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
2626
import org.springframework.data.mongodb.core.mapping.TimeSeries;
27+
import org.springframework.data.mongodb.test.util.MongoTestMappingContext;
2728

2829
/**
2930
* Unit tests for {@link EntityOperations}.
@@ -32,7 +33,7 @@
3233
*/
3334
class EntityOperationsUnitTests {
3435

35-
EntityOperations operations = new EntityOperations(new MongoMappingContext());
36+
EntityOperations operations = new EntityOperations(MongoTestMappingContext.newTestContext());
3637

3738
@Test // GH-3731
3839
void shouldReportInvalidTimeField() {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public class MongoTemplateTests {
138138

139139
cfg.configureMappingContext(it -> {
140140
it.autocreateIndex(false);
141-
it.intitalEntitySet(AuditablePerson.class);
141+
it.initialEntitySet(AuditablePerson.class);
142142

143143
});
144144

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.test.util;
17+
18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.HashSet;
21+
import java.util.Set;
22+
23+
import org.springframework.lang.Nullable;
24+
25+
/**
26+
* Utility to configure {@link org.springframework.data.mongodb.core.mapping.MongoMappingContext} properties.
27+
*
28+
* @author Christoph Strobl
29+
*/
30+
public class MappingContextConfigurer {
31+
32+
private @Nullable Set<Class<?>> intitalEntitySet;
33+
boolean autocreateIndex = false;
34+
35+
public void autocreateIndex(boolean autocreateIndex) {
36+
this.autocreateIndex = autocreateIndex;
37+
}
38+
39+
public void initialEntitySet(Set<Class<?>> initialEntitySet) {
40+
this.intitalEntitySet = initialEntitySet;
41+
}
42+
43+
public void initialEntitySet(Class<?>... initialEntitySet) {
44+
this.intitalEntitySet = new HashSet<>(Arrays.asList(initialEntitySet));
45+
}
46+
47+
Set<Class<?>> initialEntitySet() {
48+
return intitalEntitySet != null ? intitalEntitySet : Collections.emptySet();
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.test.util;
17+
18+
import java.util.Arrays;
19+
20+
import org.springframework.core.convert.converter.Converter;
21+
import org.springframework.data.convert.CustomConversions;
22+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
23+
24+
/**
25+
* Utility to configure {@link MongoCustomConversions}.
26+
*
27+
* @author Christoph Strobl
28+
*/
29+
public class MongoConverterConfigurer {
30+
31+
CustomConversions customConversions;
32+
33+
public void customConversions(CustomConversions customConversions) {
34+
this.customConversions = customConversions;
35+
}
36+
37+
public void customConverters(Converter<?, ?>... converters) {
38+
customConversions(new MongoCustomConversions(Arrays.asList(converters)));
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.test.util;
17+
18+
import java.util.Collections;
19+
import java.util.function.Consumer;
20+
21+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
22+
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
23+
24+
/**
25+
* @author Christoph Strobl
26+
*/
27+
public class MongoTestMappingContext extends MongoMappingContext {
28+
29+
private MappingContextConfigurer contextConfigurer;
30+
private MongoConverterConfigurer converterConfigurer;
31+
32+
public static MongoTestMappingContext newTestContext() {
33+
return new MongoTestMappingContext(conig -> {}).init();
34+
}
35+
36+
public MongoTestMappingContext(MappingContextConfigurer contextConfig) {
37+
38+
this.contextConfigurer = contextConfig;
39+
this.converterConfigurer = new MongoConverterConfigurer();
40+
}
41+
42+
public MongoTestMappingContext(Consumer<MappingContextConfigurer> contextConfig) {
43+
44+
this(new MappingContextConfigurer());
45+
contextConfig.accept(contextConfigurer);
46+
}
47+
48+
public MongoTestMappingContext customConversions(MongoConverterConfigurer converterConfig) {
49+
50+
this.converterConfigurer = converterConfig;
51+
return this;
52+
}
53+
54+
public MongoTestMappingContext customConversions(Consumer<MongoConverterConfigurer> converterConfig) {
55+
56+
converterConfig.accept(converterConfigurer);
57+
return this;
58+
}
59+
60+
public MongoTestMappingContext init() {
61+
62+
setInitialEntitySet(contextConfigurer.initialEntitySet());
63+
setAutoIndexCreation(contextConfigurer.autocreateIndex);
64+
if (converterConfigurer.customConversions != null) {
65+
setSimpleTypeHolder(converterConfigurer.customConversions.getSimpleTypeHolder());
66+
} else {
67+
setSimpleTypeHolder(new MongoCustomConversions(Collections.emptyList()).getSimpleTypeHolder());
68+
}
69+
70+
super.afterPropertiesSet();
71+
return this;
72+
}
73+
74+
@Override
75+
public void afterPropertiesSet() {
76+
init();
77+
}
78+
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/test/util/MongoTestTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public MongoTestTemplate(MongoClient client, String database, Class<?>... initia
5050
cfg.configureMappingContext(it -> {
5151

5252
it.autocreateIndex(false);
53-
it.intitalEntitySet(initialEntities);
53+
it.initialEntitySet(initialEntities);
5454
});
5555
});
5656
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/test/util/MongoTestTemplateConfiguration.java

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,15 @@
1616
package org.springframework.data.mongodb.test.util;
1717

1818
import java.util.ArrayList;
19-
import java.util.Arrays;
2019
import java.util.Collections;
21-
import java.util.HashSet;
2220
import java.util.List;
23-
import java.util.Set;
2421
import java.util.function.Consumer;
2522
import java.util.function.Function;
2623

2724
import org.springframework.context.ApplicationContext;
2825
import org.springframework.context.ApplicationListener;
2926
import org.springframework.context.ConfigurableApplicationContext;
30-
import org.springframework.core.convert.converter.Converter;
3127
import org.springframework.data.auditing.IsNewAwareAuditingHandler;
32-
import org.springframework.data.convert.CustomConversions;
3328
import org.springframework.data.mapping.context.MappingContext;
3429
import org.springframework.data.mongodb.MongoDatabaseFactory;
3530
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
@@ -115,16 +110,7 @@ ApplicationContext getApplicationContext() {
115110
MongoMappingContext mappingContext() {
116111

117112
if (mappingContext == null) {
118-
119-
mappingContext = new MongoMappingContext();
120-
mappingContext.setInitialEntitySet(mappingContextConfigurer.initialEntitySet());
121-
mappingContext.setAutoIndexCreation(mappingContextConfigurer.autocreateIndex);
122-
if(mongoConverterConfigurer.customConversions != null) {
123-
mappingContext.setSimpleTypeHolder(mongoConverterConfigurer.customConversions.getSimpleTypeHolder());
124-
} else {
125-
mappingContext.setSimpleTypeHolder(new MongoCustomConversions(Collections.emptyList()).getSimpleTypeHolder());
126-
}
127-
mappingContext.afterPropertiesSet();
113+
mappingContext = new MongoTestMappingContext(mappingContextConfigurer).customConversions(mongoConverterConfigurer).init();
128114
}
129115

130116
return mappingContext;
@@ -222,41 +208,6 @@ public void defaultDb(String defaultDatabase) {
222208
}
223209
}
224210

225-
public static class MongoConverterConfigurer {
226-
227-
CustomConversions customConversions;
228-
229-
public void customConversions(CustomConversions customConversions) {
230-
this.customConversions = customConversions;
231-
}
232-
233-
public void customConverters(Converter<?, ?>... converters) {
234-
customConversions(new MongoCustomConversions(Arrays.asList(converters)));
235-
}
236-
}
237-
238-
public static class MappingContextConfigurer {
239-
240-
Set<Class<?>> intitalEntitySet;
241-
boolean autocreateIndex = false;
242-
243-
public void autocreateIndex(boolean autocreateIndex) {
244-
this.autocreateIndex = autocreateIndex;
245-
}
246-
247-
public void intitalEntitySet(Set<Class<?>> intitalEntitySet) {
248-
this.intitalEntitySet = intitalEntitySet;
249-
}
250-
251-
public void intitalEntitySet(Class<?>... initialEntitySet) {
252-
this.intitalEntitySet = new HashSet<>(Arrays.asList(initialEntitySet));
253-
}
254-
255-
Set<Class<?>> initialEntitySet() {
256-
return intitalEntitySet != null ? intitalEntitySet : Collections.emptySet();
257-
}
258-
}
259-
260211
public static class AuditingConfigurer {
261212

262213
Function<MappingContext, IsNewAwareAuditingHandler> auditingHandlerFunction;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/test/util/ReactiveMongoTestTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public ReactiveMongoTestTemplate(MongoClient client, String database, Class<?>..
5656
cfg.configureMappingContext(it -> {
5757

5858
it.autocreateIndex(false);
59-
it.intitalEntitySet(initialEntities);
59+
it.initialEntitySet(initialEntities);
6060
});
6161
});
6262
}

0 commit comments

Comments
 (0)