Skip to content

Commit

Permalink
Fix conditions report message for same short name
Browse files Browse the repository at this point in the history
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 spring-projectsgh-11710
  • Loading branch information
mbhave committed Apr 2, 2018
1 parent 270ee6d commit 8e81f0c
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@

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;

/**
Expand Down Expand Up @@ -135,21 +137,28 @@ private void logUnconditionalClasses(ConditionEvaluationReport report,

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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -241,6 +242,37 @@ public void negativeOuterPositiveInnerBean() {
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;
Expand All @@ -264,12 +296,12 @@ static class DuplicateConfig {
}

@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
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}

}

Original file line number Diff line number Diff line change
@@ -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";
}

}

0 comments on commit 8e81f0c

Please sign in to comment.