下面列出了怎么用org.springframework.boot.context.annotation.UserConfigurations的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
@SuppressWarnings("deprecation")
public void springVaultClientConfigurationIsAProxy() {
new WebApplicationContextRunner()
.withPropertyValues("spring.profiles.active=vault")
.withConfiguration(UserConfigurations.of(ConfigServerConfiguration.class))
.withConfiguration(
AutoConfigurations.of(ConfigServerAutoConfiguration.class))
.run(context -> {
assertThat(context).getBean(SpringVaultClientConfiguration.class)
.isNotNull()
.matches(svcc -> ClassUtils
.isCglibProxyClassName(svcc.getClass().getName()),
"is a proxy");
});
}
@Test
void receiveMessage_methodWithMessageAsParameter_parameterIsConverted() {
new ApplicationContextRunner()
.withConfiguration(UserConfigurations
.of(QueueMessageHandlerWithJacksonMappingConfiguration.class))
.withBean(IncomingMessageHandlerWithMessageParameter.class)
.run((context) -> {
DummyKeyValueHolder messagePayload = new DummyKeyValueHolder("myKey",
"A value");
MappingJackson2MessageConverter jsonMapper = context
.getBean(MappingJackson2MessageConverter.class);
Message<?> message = jsonMapper.toMessage(messagePayload,
new MessageHeaders(Collections.singletonMap(
QueueMessageHandler.LOGICAL_RESOURCE_ID,
"testQueue")));
MessageHandler messageHandler = context.getBean(MessageHandler.class);
messageHandler.handleMessage(message);
IncomingMessageHandlerWithMessageParameter messageListener = context
.getBean(IncomingMessageHandlerWithMessageParameter.class);
assertThat(messageListener.getLastReceivedMessage()).isNotNull();
assertThat(messageListener.getLastReceivedMessage().getPayload())
.isEqualTo(messagePayload);
});
}
@Test
public void createsTracingPostProcessor() {
final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.data.mongodb.port=27017") // Otherwise a random embedded mongo port is used
.withConfiguration(UserConfigurations.of(TracerConfig.class, MongoConfig.class))
.withConfiguration(AutoConfigurations.of(
MongoTracingAutoConfiguration.class,
EmbeddedMongoAutoConfiguration.class
));
contextRunner.run(context -> Assertions.assertThat(context).hasSingleBean(TracingMongoClientPostProcessor.class));
}
@Test
public void doesNotCreateTracingPostProcessorWhenNoTracer() {
final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(UserConfigurations.of(MongoConfig.class))
.withConfiguration(AutoConfigurations.of(MongoTracingAutoConfiguration.class));
contextRunner.run(context -> Assertions.assertThat(context).doesNotHaveBean(TracingMongoClientPostProcessor.class));
}
@Test
public void doesNotCreateTracingPostProcessorWhenNoMongoClient() {
final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(UserConfigurations.of(TracerConfig.class))
.withConfiguration(AutoConfigurations.of(MongoTracingAutoConfiguration.class));
contextRunner.run(context -> Assertions.assertThat(context).doesNotHaveBean(TracingMongoClientPostProcessor.class));
}
@Test
public void doesNotCreateTracingPostProcessorWhenDisabled() {
final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("opentracing.spring.cloud.mongo.enabled=false")
.withConfiguration(UserConfigurations.of(TracerConfig.class, MongoConfig.class))
.withConfiguration(AutoConfigurations.of(MongoTracingAutoConfiguration.class));
contextRunner.run(context -> Assertions.assertThat(context).doesNotHaveBean(TracingMongoClientPostProcessor.class));
}
@Test
public void should_return_management_context_with_context_path() throws Exception {
contextRunner
.withConfiguration(
UserConfigurations.of(ManagementContextAutoConfiguration.class,
ServerPropertiesConfig.class))
.withPropertyValues("management.server.servlet.context-path=foo")
.run(context -> {
then(extractAllPatterns(context)).containsExactlyInAnyOrder(
"/actuator(/|/(health|health/.*|info|info/.*))?", "foo.*",
SleuthWebProperties.DEFAULT_SKIP_PATTERN);
});
}
@Test
public void should_return_endpoints_without_context_path() {
contextRunner
.withConfiguration(UserConfigurations.of(ServerPropertiesConfig.class))
.run(context -> {
then(extractAllPatterns(context)).containsExactlyInAnyOrder(
"/actuator(/|/(health|health/.*|info|info/.*))?",
SleuthWebProperties.DEFAULT_SKIP_PATTERN);
});
}
@Test
public void should_return_endpoints_with_context_path() {
contextRunner
.withConfiguration(UserConfigurations.of(ServerPropertiesConfig.class))
.withPropertyValues("server.servlet.context-path=foo").run(context -> {
then(extractAllPatterns(context)).containsExactlyInAnyOrder(
"foo/actuator(/|/(health|health/.*|info|info/.*))?",
SleuthWebProperties.DEFAULT_SKIP_PATTERN);
});
}
@Test
public void should_return_endpoints_without_context_path_and_base_path_set_to_root() {
contextRunner
.withConfiguration(UserConfigurations.of(ServerPropertiesConfig.class))
.withPropertyValues("management.endpoints.web.base-path=/")
.run(context -> {
then(extractAllPatterns(context)).containsExactlyInAnyOrder(
"/(health|health/.*|info|info/.*)",
SleuthWebProperties.DEFAULT_SKIP_PATTERN);
});
}
@Test
public void should_return_endpoints_with_context_path_and_base_path_set_to_root() {
contextRunner
.withConfiguration(UserConfigurations.of(ServerPropertiesConfig.class))
.withPropertyValues("management.endpoints.web.base-path=/",
"server.servlet.context-path=foo")
.run(context -> {
then(extractAllPatterns(context)).containsExactlyInAnyOrder(
"foo(/|/(health|health/.*|info|info/.*))?",
SleuthWebProperties.DEFAULT_SKIP_PATTERN);
});
}
@Test
public void should_return_endpoints_with_context_path_and_base_path_set_to_root_different_port() {
contextRunner
.withConfiguration(UserConfigurations.of(ServerPropertiesConfig.class))
.withPropertyValues("management.endpoints.web.base-path=/",
"management.server.port=0", "server.servlet.context-path=foo")
.run(context -> {
then(extractAllPatterns(context)).containsExactlyInAnyOrder(
"/(health|health/.*|info|info/.*)",
SleuthWebProperties.DEFAULT_SKIP_PATTERN);
});
}
@Test
public void should_return_endpoints_with_actuator_context_path_only() {
contextRunner
.withConfiguration(UserConfigurations.of(ServerPropertiesConfig.class))
.withPropertyValues("management.endpoints.web.base-path=/mgt",
"server.servlet.context-path=foo")
.run(context -> {
then(extractAllPatterns(context)).containsExactlyInAnyOrder(
"foo/mgt(/|/(health|health/.*|info|info/.*))?",
SleuthWebProperties.DEFAULT_SKIP_PATTERN);
});
}
@Test
public void should_return_endpoints_with_actuator_default_context_path_different_port() {
contextRunner
.withConfiguration(UserConfigurations.of(ServerPropertiesConfig.class))
.withPropertyValues("management.server.port=0",
"server.servlet.context-path=foo")
.run(context -> {
then(extractAllPatterns(context)).containsExactlyInAnyOrder(
"/actuator(/|/(health|health/.*|info|info/.*))?",
SleuthWebProperties.DEFAULT_SKIP_PATTERN);
});
}
@Test
public void should_return_endpoints_with_actuator_context_path_only_different_port() {
contextRunner
.withConfiguration(UserConfigurations.of(ServerPropertiesConfig.class))
.withPropertyValues("management.endpoints.web.base-path=/mgt",
"management.server.port=0", "server.servlet.context-path=foo")
.run(context -> {
then(extractAllPatterns(context)).containsExactlyInAnyOrder(
"/mgt(/|/(health|health/.*|info|info/.*))?",
SleuthWebProperties.DEFAULT_SKIP_PATTERN);
});
}
@Test
public void should_return_endpoints_with_context_path_different_port() {
contextRunner
.withConfiguration(UserConfigurations.of(ServerPropertiesConfig.class))
.withPropertyValues("management.server.port=0",
"server.servlet.context-path=foo")
.run(context -> {
then(extractAllPatterns(context)).containsExactlyInAnyOrder(
"/actuator(/|/(health|health/.*|info|info/.*))?",
SleuthWebProperties.DEFAULT_SKIP_PATTERN);
});
}
@Test
void receiveMessage_methodWithCustomObjectAsParameter_parameterIsConverted() {
new ApplicationContextRunner()
.withConfiguration(UserConfigurations
.of(QueueMessageHandlerWithJacksonMappingConfiguration.class))
.withBean(IncomingMessageHandlerWithCustomParameter.class)
.run((context) -> {
DummyKeyValueHolder messagePayload = new DummyKeyValueHolder("myKey",
"A value");
MappingJackson2MessageConverter jsonMapper = context
.getBean(MappingJackson2MessageConverter.class);
Message<?> message = jsonMapper.toMessage(messagePayload,
new MessageHeaders(Collections.singletonMap(
QueueMessageHandler.LOGICAL_RESOURCE_ID,
"testQueue")));
MessageHandler messageHandler = context.getBean(MessageHandler.class);
messageHandler.handleMessage(message);
IncomingMessageHandlerWithCustomParameter messageListener = context
.getBean(IncomingMessageHandlerWithCustomParameter.class);
assertThat(messageListener.getLastReceivedMessage()).isNotNull();
assertThat(messageListener.getLastReceivedMessage().getKey())
.isEqualTo("myKey");
assertThat(messageListener.getLastReceivedMessage().getValue())
.isEqualTo("A value");
});
}