Merge branch '2.0.x'

pull/13757/head
Madhura Bhave 6 years ago
commit 34ebf1a97c

@ -21,20 +21,17 @@ import java.util.Arrays;
import java.util.Set;
import com.mongodb.MongoClient;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.city.City;
import org.springframework.boot.autoconfigure.data.mongo.country.Country;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -51,7 +48,6 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
/**
* Tests for {@link MongoDataAutoConfiguration}.
@ -61,137 +57,120 @@ import static org.junit.Assert.fail;
*/
public class MongoDataAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@Rule
public ExpectedException thrown = ExpectedException.none();
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PropertyPlaceholderAutoConfiguration.class,
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class));
@Test
public void templateExists() {
this.context = new AnnotationConfigApplicationContext(
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class);
assertThat(this.context.getBeanNamesForType(MongoTemplate.class).length)
.isEqualTo(1);
this.contextRunner
.run((context) -> assertThat(context).hasSingleBean(MongoTemplate.class));
}
@Test
public void gridFsTemplateExists() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.data.mongodb.gridFsDatabase:grid")
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(GridFsTemplate.class).length)
.isEqualTo(1);
this.contextRunner.withPropertyValues("spring.data.mongodb.gridFsDatabase:grid")
.run((context) -> assertThat(context)
.hasSingleBean(GridFsTemplate.class));
}
@Test
public void customConversions() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(CustomConversionsConfig.class);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
this.context.refresh();
MongoTemplate template = this.context.getBean(MongoTemplate.class);
assertThat(template.getConverter().getConversionService()
.canConvert(MongoClient.class, Boolean.class)).isTrue();
this.contextRunner.withUserConfiguration(CustomConversionsConfig.class)
.run((context) -> {
MongoTemplate template = context.getBean(MongoTemplate.class);
assertThat(template.getConverter().getConversionService()
.canConvert(MongoClient.class, Boolean.class)).isTrue();
});
}
@Test
public void usesAutoConfigurationPackageToPickUpDocumentTypes() {
this.context = new AnnotationConfigApplicationContext();
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
String cityPackage = City.class.getPackage().getName();
AutoConfigurationPackages.register(this.context, cityPackage);
this.context.register(MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class);
this.context.refresh();
assertDomainTypesDiscovered(this.context.getBean(MongoMappingContext.class),
City.class);
AutoConfigurationPackages.register(context, cityPackage);
context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
try {
context.refresh();
assertDomainTypesDiscovered(context.getBean(MongoMappingContext.class),
City.class);
}
finally {
context.close();
}
}
@Test
public void defaultFieldNamingStrategy() {
testFieldNamingStrategy(null, PropertyNameFieldNamingStrategy.class);
this.contextRunner.run((context) -> {
MongoMappingContext mappingContext = context
.getBean(MongoMappingContext.class);
FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils
.getField(mappingContext, "fieldNamingStrategy");
assertThat(fieldNamingStrategy.getClass())
.isEqualTo(PropertyNameFieldNamingStrategy.class);
});
}
@Test
public void customFieldNamingStrategy() {
testFieldNamingStrategy(CamelCaseAbbreviatingFieldNamingStrategy.class.getName(),
CamelCaseAbbreviatingFieldNamingStrategy.class);
this.contextRunner
.withPropertyValues("spring.data.mongodb.field-naming-strategy:"
+ CamelCaseAbbreviatingFieldNamingStrategy.class.getName())
.run((context) -> {
MongoMappingContext mappingContext = context
.getBean(MongoMappingContext.class);
FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils
.getField(mappingContext, "fieldNamingStrategy");
assertThat(fieldNamingStrategy.getClass())
.isEqualTo(CamelCaseAbbreviatingFieldNamingStrategy.class);
});
}
@Test
public void interfaceFieldNamingStrategy() {
try {
testFieldNamingStrategy(FieldNamingStrategy.class.getName(), null);
fail("Create FieldNamingStrategy interface should fail");
}
// We seem to have an inconsistent exception, accept either
catch (BeanCreationException ex) {
// Expected
}
this.contextRunner
.withPropertyValues("spring.data.mongodb.field-naming-strategy:"
+ FieldNamingStrategy.class.getName())
.run((context) -> assertThat(context).getFailure()
.isInstanceOf(BeanCreationException.class));
}
@Test
@SuppressWarnings("unchecked")
public void entityScanShouldSetInitialEntitySet() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(EntityScanConfig.class,
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class);
this.context.refresh();
MongoMappingContext mappingContext = this.context
.getBean(MongoMappingContext.class);
Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils
.getField(mappingContext, "initialEntitySet");
assertThat(initialEntitySet).containsOnly(City.class, Country.class);
this.contextRunner.withUserConfiguration(EntityScanConfig.class)
.run((context) -> {
MongoMappingContext mappingContext = context
.getBean(MongoMappingContext.class);
Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils
.getField(mappingContext, "initialEntitySet");
assertThat(initialEntitySet).containsOnly(City.class, Country.class);
});
}
@Test
public void registersDefaultSimpleTypesWithMappingContext() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class);
this.context.refresh();
MongoMappingContext context = this.context.getBean(MongoMappingContext.class);
BasicMongoPersistentEntity<?> entity = context.getPersistentEntity(Sample.class);
MongoPersistentProperty dateProperty = entity.getPersistentProperty("date");
assertThat(dateProperty.isEntity()).isFalse();
this.contextRunner.run((context) -> {
MongoMappingContext mappingContext = context
.getBean(MongoMappingContext.class);
BasicMongoPersistentEntity<?> entity = mappingContext
.getPersistentEntity(Sample.class);
MongoPersistentProperty dateProperty = entity.getPersistentProperty("date");
assertThat(dateProperty.isEntity()).isFalse();
});
}
@Test
public void backsOffIfMongoClientBeanIsNotPresent() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(MongoDataAutoConfiguration.class);
this.context.refresh();
this.thrown.expect(NoSuchBeanDefinitionException.class);
assertThat(this.context.getBean(MongoDataAutoConfiguration.class)).isNull();
}
public void testFieldNamingStrategy(String strategy,
Class<? extends FieldNamingStrategy> expectedType) {
this.context = new AnnotationConfigApplicationContext();
if (strategy != null) {
TestPropertyValues.of("spring.data.mongodb.field-naming-strategy:" + strategy)
.applyTo(this.context);
}
this.context.register(PropertyPlaceholderAutoConfiguration.class,
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
this.context.refresh();
MongoMappingContext mappingContext = this.context
.getBean(MongoMappingContext.class);
FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils
.getField(mappingContext, "fieldNamingStrategy");
assertThat(fieldNamingStrategy.getClass()).isEqualTo(expectedType);
ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(MongoDataAutoConfiguration.class));
runner.run((context) -> assertThat(context)
.doesNotHaveBean(MongoDataAutoConfiguration.class));
}
@SuppressWarnings({ "unchecked", "rawtypes" })

@ -16,15 +16,12 @@
package org.springframework.boot.autoconfigure.data.mongo;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import static org.assertj.core.api.Assertions.assertThat;
@ -36,35 +33,26 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class MongoReactiveDataAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@Rule
public ExpectedException thrown = ExpectedException.none();
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
MongoReactiveAutoConfiguration.class,
MongoReactiveDataAutoConfiguration.class));
@Test
public void templateExists() {
this.context = new AnnotationConfigApplicationContext(
PropertyPlaceholderAutoConfiguration.class,
MongoReactiveAutoConfiguration.class,
MongoReactiveDataAutoConfiguration.class);
assertThat(this.context.getBeanNamesForType(ReactiveMongoTemplate.class))
.hasSize(1);
this.contextRunner.run((context) -> assertThat(context)
.hasSingleBean(ReactiveMongoTemplate.class));
}
@Test
public void backsOffIfMongoClientBeanIsNotPresent() {
this.context = new AnnotationConfigApplicationContext(
PropertyPlaceholderAutoConfiguration.class,
MongoReactiveDataAutoConfiguration.class);
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.context.getBean(MongoReactiveDataAutoConfiguration.class);
ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
MongoReactiveDataAutoConfiguration.class));
runner.run((context) -> assertThat(context)
.doesNotHaveBean(MongoReactiveDataAutoConfiguration.class));
}
}

Loading…
Cancel
Save