From db3f488d5a5809b5b65f925eb9230cba00c82d0b Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 11 Oct 2016 17:54:00 -0700 Subject: [PATCH] Polish --- .../EmbeddedDataSourceConfigurationTests.java | 5 +- .../mock/mockito/MockitoPostProcessor.java | 5 +- ...eanCurrentlyInCreationFailureAnalyzer.java | 56 +++++++++++-------- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfigurationTests.java index c1b6cc28ad..b5c141e8bb 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfigurationTests.java @@ -56,14 +56,13 @@ public class EmbeddedDataSourceConfigurationTests { @Test public void generateUniqueName() throws Exception { this.context = load("spring.datasource.generate-unique-name=true"); - AnnotationConfigApplicationContext context2 = - load("spring.datasource.generate-unique-name=true"); + AnnotationConfigApplicationContext context2 = load( + "spring.datasource.generate-unique-name=true"); try { DataSource dataSource = this.context.getBean(DataSource.class); DataSource dataSource2 = context2.getBean(DataSource.class); assertThat(getDatabaseName(dataSource)) .isNotEqualTo(getDatabaseName(dataSource2)); - System.out.println(dataSource2); } finally { context2.close(); diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java b/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java index 90145d27bb..a95be45f54 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java @@ -478,8 +478,11 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda } @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof FactoryBean) { + return bean; + } return createSpyIfNecessary(bean, beanName); } diff --git a/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java b/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java index cbec191884..47d803ccd2 100644 --- a/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java +++ b/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzer.java @@ -39,40 +39,42 @@ class BeanCurrentlyInCreationFailureAnalyzer @Override protected FailureAnalysis analyze(Throwable rootFailure, BeanCurrentlyInCreationException cause) { - List beansInCycle = new ArrayList(); + List cycle = new ArrayList(); Throwable candidate = rootFailure; int cycleStart = -1; while (candidate != null) { - if (candidate instanceof BeanCreationException) { - BeanCreationException creationEx = (BeanCreationException) candidate; - if (StringUtils.hasText(creationEx.getBeanName())) { - BeanInCycle beanInCycle = new BeanInCycle(creationEx); - int index = beansInCycle.indexOf(beanInCycle); - if (index == -1) { - beansInCycle.add(beanInCycle); - } - else { - cycleStart = index; - } + BeanInCycle beanInCycle = BeanInCycle.get(candidate); + if (beanInCycle != null) { + int index = cycle.indexOf(beanInCycle); + if (index == -1) { + cycle.add(beanInCycle); } + cycleStart = (cycleStart == -1 ? index : cycleStart); } candidate = candidate.getCause(); } + String message = buildMessage(cycle, cycleStart); + return new FailureAnalysis(message, null, cause); + } + + private String buildMessage(List beansInCycle, int cycleStart) { StringBuilder message = new StringBuilder(); message.append(String.format("The dependencies of some of the beans in the " + "application context form a cycle:%n%n")); for (int i = 0; i < beansInCycle.size(); i++) { + BeanInCycle beanInCycle = beansInCycle.get(i); if (i == cycleStart) { message.append(String.format("┌─────┐%n")); } else if (i > 0) { - message.append(String.format("%s ↓%n", i < cycleStart ? " " : "↑")); + String leftSide = (i < cycleStart ? " " : "↑"); + message.append(String.format("%s ↓%n", leftSide)); } - message.append(String.format("%s %s%n", i < cycleStart ? " " : "|", - beansInCycle.get(i))); + String leftSide = i < cycleStart ? " " : "|"; + message.append(String.format("%s %s%n", leftSide, beanInCycle)); } message.append(String.format("└─────┘%n")); - return new FailureAnalysis(message.toString(), null, cause); + return message.toString(); } private static final class BeanInCycle { @@ -114,14 +116,10 @@ class BeanCurrentlyInCreationFailureAnalyzer if (this == obj) { return true; } - if (obj == null) { + if (obj == null || getClass() != obj.getClass()) { return false; } - if (getClass() != obj.getClass()) { - return false; - } - BeanInCycle other = (BeanInCycle) obj; - return this.name.equals(other.name); + return this.name.equals(((BeanInCycle) obj).name); } @Override @@ -129,6 +127,20 @@ class BeanCurrentlyInCreationFailureAnalyzer return this.name + this.description; } + public static BeanInCycle get(Throwable ex) { + if (ex instanceof BeanCreationException) { + return get((BeanCreationException) ex); + } + return null; + } + + private static BeanInCycle get(BeanCreationException ex) { + if (StringUtils.hasText(ex.getBeanName())) { + return new BeanInCycle(ex); + } + return null; + } + } }