|
21 | 21 | import java.util.Set; |
22 | 22 |
|
23 | 23 | import com.mongodb.MongoClient; |
24 | | -import org.junit.After; |
25 | 24 | import org.junit.Test; |
26 | 25 |
|
27 | 26 | import org.springframework.beans.factory.BeanCreationException; |
28 | 27 | import org.springframework.boot.autoconfigure.AutoConfigurationPackages; |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
29 | 29 | import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; |
30 | 30 | import org.springframework.boot.autoconfigure.data.mongo.city.City; |
31 | 31 | import org.springframework.boot.autoconfigure.data.mongo.country.Country; |
32 | 32 | import org.springframework.boot.autoconfigure.domain.EntityScan; |
33 | 33 | import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; |
34 | | -import org.springframework.boot.test.util.TestPropertyValues; |
| 34 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
35 | 35 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
36 | 36 | import org.springframework.context.annotation.Bean; |
37 | 37 | import org.springframework.context.annotation.Configuration; |
|
48 | 48 | import org.springframework.test.util.ReflectionTestUtils; |
49 | 49 |
|
50 | 50 | import static org.assertj.core.api.Assertions.assertThat; |
51 | | -import static org.junit.Assert.fail; |
52 | 51 |
|
53 | 52 | /** |
54 | 53 | * Tests for {@link MongoDataAutoConfiguration}. |
|
58 | 57 | */ |
59 | 58 | public class MongoDataAutoConfigurationTests { |
60 | 59 |
|
61 | | - private AnnotationConfigApplicationContext context; |
62 | | - |
63 | | - @After |
64 | | - public void close() { |
65 | | - if (this.context != null) { |
66 | | - this.context.close(); |
67 | | - } |
68 | | - } |
| 60 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 61 | + .withConfiguration(AutoConfigurations.of( |
| 62 | + PropertyPlaceholderAutoConfiguration.class, |
| 63 | + MongoAutoConfiguration.class, MongoDataAutoConfiguration.class)); |
69 | 64 |
|
70 | 65 | @Test |
71 | 66 | public void templateExists() { |
72 | | - this.context = new AnnotationConfigApplicationContext( |
73 | | - PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, |
74 | | - MongoDataAutoConfiguration.class); |
75 | | - assertThat(this.context.getBeanNamesForType(MongoTemplate.class).length) |
76 | | - .isEqualTo(1); |
| 67 | + this.contextRunner |
| 68 | + .run((context) -> assertThat(context).hasSingleBean(MongoTemplate.class)); |
77 | 69 | } |
78 | 70 |
|
79 | 71 | @Test |
80 | 72 | public void gridFsTemplateExists() { |
81 | | - this.context = new AnnotationConfigApplicationContext(); |
82 | | - TestPropertyValues.of("spring.data.mongodb.gridFsDatabase:grid") |
83 | | - .applyTo(this.context); |
84 | | - this.context.register(PropertyPlaceholderAutoConfiguration.class, |
85 | | - MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); |
86 | | - this.context.refresh(); |
87 | | - assertThat(this.context.getBeanNamesForType(GridFsTemplate.class).length) |
88 | | - .isEqualTo(1); |
| 73 | + this.contextRunner.withPropertyValues("spring.data.mongodb.gridFsDatabase:grid") |
| 74 | + .run((context) -> assertThat(context) |
| 75 | + .hasSingleBean(GridFsTemplate.class)); |
89 | 76 | } |
90 | 77 |
|
91 | 78 | @Test |
92 | 79 | public void customConversions() { |
93 | | - this.context = new AnnotationConfigApplicationContext(); |
94 | | - this.context.register(CustomConversionsConfig.class); |
95 | | - this.context.register(PropertyPlaceholderAutoConfiguration.class, |
96 | | - MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); |
97 | | - this.context.refresh(); |
98 | | - MongoTemplate template = this.context.getBean(MongoTemplate.class); |
99 | | - assertThat(template.getConverter().getConversionService() |
100 | | - .canConvert(MongoClient.class, Boolean.class)).isTrue(); |
| 80 | + this.contextRunner.withUserConfiguration(CustomConversionsConfig.class) |
| 81 | + .run((context) -> { |
| 82 | + MongoTemplate template = context.getBean(MongoTemplate.class); |
| 83 | + assertThat(template.getConverter().getConversionService() |
| 84 | + .canConvert(MongoClient.class, Boolean.class)).isTrue(); |
| 85 | + }); |
101 | 86 | } |
102 | 87 |
|
103 | 88 | @Test |
104 | 89 | public void usesAutoConfigurationPackageToPickUpDocumentTypes() { |
105 | | - this.context = new AnnotationConfigApplicationContext(); |
| 90 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
106 | 91 | String cityPackage = City.class.getPackage().getName(); |
107 | | - AutoConfigurationPackages.register(this.context, cityPackage); |
108 | | - this.context.register(MongoAutoConfiguration.class, |
109 | | - MongoDataAutoConfiguration.class); |
110 | | - this.context.refresh(); |
111 | | - assertDomainTypesDiscovered(this.context.getBean(MongoMappingContext.class), |
112 | | - City.class); |
| 92 | + AutoConfigurationPackages.register(context, cityPackage); |
| 93 | + context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); |
| 94 | + try { |
| 95 | + context.refresh(); |
| 96 | + assertDomainTypesDiscovered(context.getBean(MongoMappingContext.class), |
| 97 | + City.class); |
| 98 | + } |
| 99 | + finally { |
| 100 | + context.close(); |
| 101 | + } |
113 | 102 | } |
114 | 103 |
|
115 | 104 | @Test |
116 | 105 | public void defaultFieldNamingStrategy() { |
117 | | - testFieldNamingStrategy(null, PropertyNameFieldNamingStrategy.class); |
| 106 | + this.contextRunner.run((context) -> { |
| 107 | + MongoMappingContext mappingContext = context |
| 108 | + .getBean(MongoMappingContext.class); |
| 109 | + FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils |
| 110 | + .getField(mappingContext, "fieldNamingStrategy"); |
| 111 | + assertThat(fieldNamingStrategy.getClass()) |
| 112 | + .isEqualTo(PropertyNameFieldNamingStrategy.class); |
| 113 | + }); |
118 | 114 | } |
119 | 115 |
|
120 | 116 | @Test |
121 | 117 | public void customFieldNamingStrategy() { |
122 | | - testFieldNamingStrategy(CamelCaseAbbreviatingFieldNamingStrategy.class.getName(), |
123 | | - CamelCaseAbbreviatingFieldNamingStrategy.class); |
| 118 | + this.contextRunner |
| 119 | + .withPropertyValues("spring.data.mongodb.field-naming-strategy:" |
| 120 | + + CamelCaseAbbreviatingFieldNamingStrategy.class.getName()) |
| 121 | + .run((context) -> { |
| 122 | + MongoMappingContext mappingContext = context |
| 123 | + .getBean(MongoMappingContext.class); |
| 124 | + FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils |
| 125 | + .getField(mappingContext, "fieldNamingStrategy"); |
| 126 | + assertThat(fieldNamingStrategy.getClass()) |
| 127 | + .isEqualTo(CamelCaseAbbreviatingFieldNamingStrategy.class); |
| 128 | + }); |
124 | 129 | } |
125 | 130 |
|
126 | 131 | @Test |
127 | 132 | public void interfaceFieldNamingStrategy() { |
128 | | - try { |
129 | | - testFieldNamingStrategy(FieldNamingStrategy.class.getName(), null); |
130 | | - fail("Create FieldNamingStrategy interface should fail"); |
131 | | - } |
132 | | - // We seem to have an inconsistent exception, accept either |
133 | | - catch (BeanCreationException ex) { |
134 | | - // Expected |
135 | | - } |
| 133 | + this.contextRunner |
| 134 | + .withPropertyValues("spring.data.mongodb.field-naming-strategy:" |
| 135 | + + FieldNamingStrategy.class.getName()) |
| 136 | + .run((context) -> assertThat(context).getFailure() |
| 137 | + .isInstanceOf(BeanCreationException.class)); |
136 | 138 | } |
137 | 139 |
|
138 | 140 | @Test |
139 | 141 | @SuppressWarnings("unchecked") |
140 | 142 | public void entityScanShouldSetInitialEntitySet() { |
141 | | - this.context = new AnnotationConfigApplicationContext(); |
142 | | - this.context.register(EntityScanConfig.class, |
143 | | - PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, |
144 | | - MongoDataAutoConfiguration.class); |
145 | | - this.context.refresh(); |
146 | | - MongoMappingContext mappingContext = this.context |
147 | | - .getBean(MongoMappingContext.class); |
148 | | - Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils |
149 | | - .getField(mappingContext, "initialEntitySet"); |
150 | | - assertThat(initialEntitySet).containsOnly(City.class, Country.class); |
| 143 | + this.contextRunner.withUserConfiguration(EntityScanConfig.class) |
| 144 | + .run((context) -> { |
| 145 | + MongoMappingContext mappingContext = context |
| 146 | + .getBean(MongoMappingContext.class); |
| 147 | + Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils |
| 148 | + .getField(mappingContext, "initialEntitySet"); |
| 149 | + assertThat(initialEntitySet).containsOnly(City.class, Country.class); |
| 150 | + }); |
| 151 | + |
151 | 152 | } |
152 | 153 |
|
153 | 154 | @Test |
154 | 155 | public void registersDefaultSimpleTypesWithMappingContext() { |
155 | | - this.context = new AnnotationConfigApplicationContext(); |
156 | | - this.context.register(MongoAutoConfiguration.class, |
157 | | - MongoDataAutoConfiguration.class); |
158 | | - this.context.refresh(); |
159 | | - MongoMappingContext context = this.context.getBean(MongoMappingContext.class); |
160 | | - BasicMongoPersistentEntity<?> entity = context.getPersistentEntity(Sample.class); |
161 | | - MongoPersistentProperty dateProperty = entity.getPersistentProperty("date"); |
162 | | - assertThat(dateProperty.isEntity()).isFalse(); |
163 | | - } |
| 156 | + this.contextRunner.run((context) -> { |
| 157 | + MongoMappingContext mappingContext = context |
| 158 | + .getBean(MongoMappingContext.class); |
| 159 | + BasicMongoPersistentEntity<?> entity = mappingContext |
| 160 | + .getPersistentEntity(Sample.class); |
| 161 | + MongoPersistentProperty dateProperty = entity.getPersistentProperty("date"); |
| 162 | + assertThat(dateProperty.isEntity()).isFalse(); |
| 163 | + }); |
164 | 164 |
|
165 | | - public void testFieldNamingStrategy(String strategy, |
166 | | - Class<? extends FieldNamingStrategy> expectedType) { |
167 | | - this.context = new AnnotationConfigApplicationContext(); |
168 | | - if (strategy != null) { |
169 | | - TestPropertyValues.of("spring.data.mongodb.field-naming-strategy:" + strategy) |
170 | | - .applyTo(this.context); |
171 | | - } |
172 | | - this.context.register(PropertyPlaceholderAutoConfiguration.class, |
173 | | - MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); |
174 | | - this.context.refresh(); |
175 | | - MongoMappingContext mappingContext = this.context |
176 | | - .getBean(MongoMappingContext.class); |
177 | | - FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils |
178 | | - .getField(mappingContext, "fieldNamingStrategy"); |
179 | | - assertThat(fieldNamingStrategy.getClass()).isEqualTo(expectedType); |
180 | 165 | } |
181 | 166 |
|
182 | 167 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
|
0 commit comments