Fix conditions report message for same short name
If two auto-configuration classes with the same name are present, the conditions report message now uses the fully qualified name for both instead of the short name. Fixes gh-11710
This commit is contained in:
parent
270ee6dd2c
commit
8e81f0c23d
|
@ -18,17 +18,19 @@ package org.springframework.boot.autoconfigure.logging;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcome;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
|
@ -135,21 +137,28 @@ public class ConditionEvaluationReportMessage {
|
|||
|
||||
private Map<String, ConditionAndOutcomes> orderByName(
|
||||
Map<String, ConditionAndOutcomes> outcomes) {
|
||||
List<String> names = new ArrayList<>();
|
||||
Map<String, String> classNames = new HashMap<>();
|
||||
for (String name : outcomes.keySet()) {
|
||||
String shortName = ClassUtils.getShortName(name);
|
||||
names.add(shortName);
|
||||
classNames.put(shortName, name);
|
||||
}
|
||||
Collections.sort(names);
|
||||
MultiValueMap<String, String> map = mapShortNameToFullyQualifiedNames(outcomes.keySet());
|
||||
List<String> shortNames = new ArrayList<>(map.keySet());
|
||||
Collections.sort(shortNames);
|
||||
Map<String, ConditionAndOutcomes> result = new LinkedHashMap<>();
|
||||
for (String shortName : names) {
|
||||
result.put(shortName, outcomes.get(classNames.get(shortName)));
|
||||
for (String shortName : shortNames) {
|
||||
List<String> fullyQualifiedNames = map.get(shortName);
|
||||
if (fullyQualifiedNames.size() > 1) {
|
||||
fullyQualifiedNames.forEach(k -> result.put(k, outcomes.get(k)));
|
||||
}
|
||||
else {
|
||||
result.put(shortName, outcomes.get(fullyQualifiedNames.get(0)));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private MultiValueMap<String, String> mapShortNameToFullyQualifiedNames(Set<String> keySet) {
|
||||
LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
||||
keySet.forEach(k -> map.add(ClassUtils.getShortName(k), k));
|
||||
return map;
|
||||
}
|
||||
|
||||
private void addMatchLogMessage(StringBuilder message, String source,
|
||||
ConditionAndOutcomes matches) {
|
||||
message.append(String.format("%n %s matched:%n", source));
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcome;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
|
||||
import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportMessage;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
|
@ -241,6 +242,37 @@ public class ConditionEvaluationReportTests {
|
|||
assertThat(sourceOutcomes.get(positiveConfig).isFullMatch()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reportWhenSameShortNamePresentMoreThanOnceShouldUseFullyQualifiedName() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.register(WebMvcAutoConfiguration.class,
|
||||
org.springframework.boot.autoconfigure.condition.config.first.SampleAutoConfiguration.class,
|
||||
org.springframework.boot.autoconfigure.condition.config.second.SampleAutoConfiguration.class);
|
||||
context.refresh();
|
||||
ConditionEvaluationReport report = ConditionEvaluationReport
|
||||
.get(context.getBeanFactory());
|
||||
assertThat(report.getConditionAndOutcomesBySource())
|
||||
.containsKeys("org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration",
|
||||
"org.springframework.boot.autoconfigure.condition.config.first.SampleAutoConfiguration",
|
||||
"org.springframework.boot.autoconfigure.condition.config.second.SampleAutoConfiguration");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reportMessageWhenSameShortNamePresentMoreThanOnceShouldUseFullyQualifiedName() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.register(WebMvcAutoConfiguration.class,
|
||||
org.springframework.boot.autoconfigure.condition.config.first.SampleAutoConfiguration.class,
|
||||
org.springframework.boot.autoconfigure.condition.config.second.SampleAutoConfiguration.class);
|
||||
context.refresh();
|
||||
ConditionEvaluationReport report = ConditionEvaluationReport
|
||||
.get(context.getBeanFactory());
|
||||
String reportMessage = new ConditionEvaluationReportMessage(report).toString();
|
||||
assertThat(reportMessage)
|
||||
.contains("WebMvcAutoConfiguration", "org.springframework.boot.autoconfigure.condition.config.first.SampleAutoConfiguration",
|
||||
"org.springframework.boot.autoconfigure.condition.config.second.SampleAutoConfiguration");
|
||||
assertThat(reportMessage).doesNotContain("org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration");
|
||||
}
|
||||
|
||||
private int getNumberOfOutcomes(ConditionAndOutcomes outcomes) {
|
||||
Iterator<ConditionAndOutcome> iterator = outcomes.iterator();
|
||||
int numberOfOutcomesAdded = 0;
|
||||
|
@ -264,12 +296,12 @@ public class ConditionEvaluationReportTests {
|
|||
}
|
||||
|
||||
@Configuration
|
||||
@Conditional({ ConditionEvaluationReportTests.MatchParseCondition.class,
|
||||
ConditionEvaluationReportTests.NoMatchBeanCondition.class })
|
||||
@Conditional({ConditionEvaluationReportTests.MatchParseCondition.class,
|
||||
ConditionEvaluationReportTests.NoMatchBeanCondition.class})
|
||||
public static class NegativeOuterConfig {
|
||||
|
||||
@Configuration
|
||||
@Conditional({ ConditionEvaluationReportTests.MatchParseCondition.class })
|
||||
@Conditional({ConditionEvaluationReportTests.MatchParseCondition.class})
|
||||
public static class PositiveInnerConfig {
|
||||
|
||||
@Bean
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright 2012-2018 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.condition.config.first;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Sample auto-configuration for {@link ConditionEvaluationReport} tests.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
@Configuration("autoConfigOne")
|
||||
@ConditionalOnProperty("sample.first")
|
||||
public class SampleAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public String one() {
|
||||
return "one";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright 2012-2018 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.condition.config.second;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Sample auto-configuration for {@link ConditionEvaluationReport} tests.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
@Configuration("autoConfigTwo")
|
||||
@ConditionalOnProperty("sample.second")
|
||||
public class SampleAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public String two() {
|
||||
return "two";
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue