下面列出了怎么用org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void createsTracingPostProcessorWhenAutoConfigured() {
final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.data.mongodb.port=27017") // Otherwise a random embedded mongo port is used
.withConfiguration(AutoConfigurations.of(
MongoTracingAutoConfiguration.class,
TracerAutoConfiguration.class,
MongoAutoConfiguration.class,
EmbeddedMongoAutoConfiguration.class
));
contextRunner.run(context -> Assertions.assertThat(context).hasSingleBean(TracingMongoClientPostProcessor.class));
}
@Test
public void shouldDoNothingIfDisabled() {
TestPropertyValues.of("mongeez.enabled:false").applyTo(this.context);
registerAndRefresh(MongoAutoConfiguration.class, MongeezAutoConfiguration.class);
assumeThat(this.context.getBeanNamesForType(Mongo.class), not(emptyArray()));
assertThat(this.context.getBeanNamesForType(Mongeez.class), emptyArray());
}
@Test
public void shouldUseDatabaseFromMongoProperties() {
String database = "foo";
TestPropertyValues.of("spring.data.mongodb.database:" + database).applyTo(this.context);
registerAndRefresh(DoNotExecuteMongeezPostProcessor.class,
MongoAutoConfiguration.class, MongeezAutoConfiguration.class);
Mongeez mongeez = this.context.getBean(Mongeez.class);
Object mongeezDatabase = ReflectionTestUtils.getField(mongeez, "dbName");
assertThat(mongeezDatabase.toString(), equalTo(database));
}
@Test
public void shouldUseDatabaseOverrideFromMongeezProperties() {
String mongoDatabase = "foo";
String mongeezOverrideDatabase = "bar";
TestPropertyValues.of("spring.data.mongodb.database:" + mongoDatabase)
.and("mongeez.database:" + mongeezOverrideDatabase)
.applyTo(this.context);
registerAndRefresh(DoNotExecuteMongeezPostProcessor.class,
MongoAutoConfiguration.class, MongeezAutoConfiguration.class);
Mongeez mongeez = this.context.getBean(Mongeez.class);
Object mongeezActualDatabase = ReflectionTestUtils.getField(mongeez, "dbName");
assertThat(mongeezActualDatabase.toString(), equalTo(mongeezOverrideDatabase));
}
@Test
public void shouldUseAuthenticationDatabaseFromMongoProperties() {
String database = "foo";
TestPropertyValues.of("spring.data.mongodb.authenticationDatabase:" + database)
.and("mongeez.username:user")
.and("mongeez.password:pass")
.applyTo(this.context);
registerAndRefresh(DoNotExecuteMongeezPostProcessor.class,
MongoAutoConfiguration.class, MongeezAutoConfiguration.class);
Mongeez mongeez = this.context.getBean(Mongeez.class);
MongoAuth auth = (MongoAuth) ReflectionTestUtils.getField(mongeez, "auth");
assertThat(auth.getAuthDb(), equalTo(database));
}
@Test
public void shouldUseAuthenticationDatabaseOverrideFromMongeezProperties() {
String database = "foo";
String mongeezOverrideDatabase = "bar";
TestPropertyValues.of("spring.data.mongodb.authenticationDatabase:" + database)
.and("mongeez.authenticationDatabase:" + mongeezOverrideDatabase)
.and("mongeez.username:user")
.and("mongeez.password:pass")
.applyTo(this.context);
registerAndRefresh(DoNotExecuteMongeezPostProcessor.class,
MongoAutoConfiguration.class, MongeezAutoConfiguration.class);
Mongeez mongeez = this.context.getBean(Mongeez.class);
MongoAuth auth = (MongoAuth) ReflectionTestUtils.getField(mongeez, "auth");
assertThat(auth.getAuthDb(), equalTo(mongeezOverrideDatabase));
}
@Test(expected = BeanCreationException.class)
public void shouldFailIfOnlyMongoCredentialsProvided() {
String mongoUsername = "foo";
String mongoPassword = "bar";
TestPropertyValues.of("spring.data.mongodb.username:" + mongoUsername)
.and("spring.data.mongodb.password:" + mongoPassword)
.applyTo(this.context);
registerAndRefresh(DoNotExecuteMongeezPostProcessor.class,
MongoAutoConfiguration.class, MongeezAutoConfiguration.class);
}
@Test(expected = BeanCreationException.class)
public void shouldFailIfLocationDoesNotExist() {
TestPropertyValues.of("mongeez.location:does/not/exist").applyTo(this.context);
registerAndRefresh(DoNotExecuteMongeezPostProcessor.class,
MongoAutoConfiguration.class, MongeezAutoConfiguration.class);
}