Harmonize ConditionOutcome messages

Add ConditionMessage class to help build condition messages in a
uniform format and update existing conditions to use it.

Fixes gh-6756
This commit is contained in:
Phillip Webb
2016-09-01 17:49:23 +01:00
parent 41dc53f5dd
commit 7396ccfe04
49 changed files with 1325 additions and 404 deletions

View File

@@ -27,6 +27,7 @@ import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -126,16 +127,18 @@ public class DevToolsDataSourceAutoConfiguration {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
ConditionMessage.Builder message = ConditionMessage
.forCondition("DevTools DataSource Condition");
String[] dataSourceBeanNames = context.getBeanFactory()
.getBeanNamesForType(DataSource.class);
if (dataSourceBeanNames.length != 1) {
return ConditionOutcome
.noMatch("A single DataSource bean was not found in the context");
.noMatch(message.didNotFind("a single DataSource bean").atAll());
}
if (context.getBeanFactory()
.getBeanNamesForType(DataSourceProperties.class).length != 1) {
return ConditionOutcome.noMatch(
"A single DataSourceProperties bean was not found in the context");
message.didNotFind("a single DataSourceProperties bean").atAll());
}
BeanDefinition dataSourceDefinition = context.getRegistry()
.getBeanDefinition(dataSourceBeanNames[0]);
@@ -146,9 +149,11 @@ public class DevToolsDataSourceAutoConfiguration {
.getFactoryMethodMetadata().getDeclaringClassName()
.startsWith(DataSourceAutoConfiguration.class.getPackage()
.getName() + ".DataSourceConfiguration$")) {
return ConditionOutcome.match("Found auto-configured DataSource");
return ConditionOutcome
.match(message.foundExactly("auto-configured DataSource"));
}
return ConditionOutcome.noMatch("DataSource was not auto-configured");
return ConditionOutcome
.noMatch(message.didNotFind("an auto-configured DataSource").atAll());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.boot.devtools.remote.client;
import javax.net.ServerSocketFactory;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.bind.RelaxedPropertyResolver;
@@ -35,6 +36,8 @@ class LocalDebugPortAvailableCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
ConditionMessage.Builder message = ConditionMessage
.forCondition("Local Debug Port Condition");
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
context.getEnvironment(), "spring.devtools.remote.debug.");
Integer port = resolver.getProperty("local-port", Integer.class);
@@ -42,9 +45,9 @@ class LocalDebugPortAvailableCondition extends SpringBootCondition {
port = RemoteDevToolsProperties.Debug.DEFAULT_LOCAL_PORT;
}
if (isPortAvailable(port)) {
return ConditionOutcome.match("Local debug port available");
return ConditionOutcome.match(message.foundExactly("local debug port"));
}
return ConditionOutcome.noMatch("Local debug port unavailable");
return ConditionOutcome.noMatch(message.didNotFind("local debug port").atAll());
}
private boolean isPortAvailable(int port) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.boot.devtools.restart;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.Condition;
@@ -33,14 +34,16 @@ class OnInitializedRestarterCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
ConditionMessage.Builder message = ConditionMessage
.forCondition("Initializer Restarter Condition");
Restarter restarter = getRestarter();
if (restarter == null) {
return ConditionOutcome.noMatch("Restarter unavailable");
return ConditionOutcome.noMatch(message.because("unavailable"));
}
if (restarter.getInitialUrls() == null) {
return ConditionOutcome.noMatch("Restarter initialized without URLs");
return ConditionOutcome.noMatch(message.because("initialized without URLs"));
}
return ConditionOutcome.match("Restarter available and initialized");
return ConditionOutcome.match(message.because("available and initialized"));
}
private Restarter getRestarter() {

View File

@@ -47,7 +47,8 @@ public class LocalDebugPortAvailableConditionTests {
public void portAvailable() throws Exception {
ConditionOutcome outcome = getOutcome();
assertThat(outcome.isMatch()).isTrue();
assertThat(outcome.getMessage()).isEqualTo("Local debug port available");
assertThat(outcome.getMessage())
.isEqualTo("Local Debug Port Condition found local debug port");
}
@Test
@@ -57,7 +58,8 @@ public class LocalDebugPortAvailableConditionTests {
ConditionOutcome outcome = getOutcome();
serverSocket.close();
assertThat(outcome.isMatch()).isFalse();
assertThat(outcome.getMessage()).isEqualTo("Local debug port unavailable");
assertThat(outcome.getMessage())
.isEqualTo("Local Debug Port Condition did not find local debug port");
}
private ConditionOutcome getOutcome() {