@PostConstruct public void setupAutoConfigurationReport() { ConditionEvaluationReport report = ConditionEvaluationReport .get(this.context.getBeanFactory()); report.recordConditionEvaluation("a", mock(Condition.class), mock(ConditionOutcome.class)); report.recordExclusions(Arrays.asList("com.foo.Bar")); }
public void logAutoConfigurationReport(boolean isCrashReport) { if (this.report == null) { if (this.applicationContext == null) { this.logger.info("Unable to provide auto-configuration report " + "due to missing ApplicationContext"); return; } this.report = ConditionEvaluationReport .get(this.applicationContext.getBeanFactory()); } if (!this.report.getConditionAndOutcomesBySource().isEmpty()) { if (isCrashReport && this.logger.isInfoEnabled() && !this.logger.isDebugEnabled()) { this.logger.info(String.format("%n%nError starting ApplicationContext. " + "To display the auto-configuration report enable " + "debug logging (start with --debug)%n%n")); } if (this.logger.isDebugEnabled()) { this.logger.debug(new ConditionEvaluationReportMessage(this.report)); } } }
@Test public void logsOutput() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.initializer.initialize(context); context.register(Config.class); ConditionEvaluationReport.get(context.getBeanFactory()) .recordExclusions(Arrays.asList("com.foo.Bar")); context.refresh(); this.initializer.onApplicationEvent(new ContextRefreshedEvent(context)); for (String message : this.debugLog) { System.out.println(message); } // Just basic sanity check, test is for visual inspection String l = this.debugLog.get(0); assertThat(l).contains("not a web application (OnWebApplicationCondition)"); }
public void logAutoConfigurationReport(boolean isCrashReport) { if (this.report == null) { if (this.applicationContext == null) { this.logger.info("Unable to provide auto-configuration report " + "due to missing ApplicationContext"); return; } this.report = ConditionEvaluationReport .get(this.applicationContext.getBeanFactory()); } if (this.report.getConditionAndOutcomesBySource().size() > 0) { if (isCrashReport && this.logger.isInfoEnabled() && !this.logger.isDebugEnabled()) { this.logger.info("\n\nError starting ApplicationContext. " + "To display the auto-configuration report enable " + "debug logging (start with --debug)\n\n"); } if (this.logger.isDebugEnabled()) { this.logger.debug(getLogMessage(this.report)); } } }
@Test public void logsOutput() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.initializer.initialize(context); context.register(Config.class); ConditionEvaluationReport.get(context.getBeanFactory()) .recordExclusions(Arrays.asList("com.foo.Bar")); context.refresh(); this.initializer.onApplicationEvent(new ContextRefreshedEvent(context)); for (String message : this.debugLog) { System.out.println(message); } // Just basic sanity check, test is for visual inspection String l = this.debugLog.get(0); assertThat(l, containsString("not a web application (OnWebApplicationCondition)")); }
@Test public void test01() { ConditionEvaluationReport conditionEvaluationReport = beanFactory.getBean("autoConfigurationReport", ConditionEvaluationReport.class); Map<String, ConditionEvaluationReport.ConditionAndOutcomes> result = conditionEvaluationReport.getConditionAndOutcomesBySource(); for(String key : result.keySet()) { ConditionEvaluationReport.ConditionAndOutcomes conditionAndOutcomes = result.get(key); Iterator<ConditionEvaluationReport.ConditionAndOutcome> iterator = conditionAndOutcomes.iterator(); while(iterator.hasNext()) { ConditionEvaluationReport.ConditionAndOutcome conditionAndOutcome = iterator.next(); System.out.println(key + " -- " + conditionAndOutcome.getCondition().getClass().getSimpleName() + " -- " + conditionAndOutcome.getOutcome()); } } }
@RequestMapping("/conditional") public String conditonal() { StringBuilder sb = new StringBuilder(); ConditionEvaluationReport conditionEvaluationReport = beanFactory.getBean("autoConfigurationReport", ConditionEvaluationReport.class); Map<String, ConditionEvaluationReport.ConditionAndOutcomes> result = conditionEvaluationReport.getConditionAndOutcomesBySource(); for(String key : result.keySet()) { ConditionEvaluationReport.ConditionAndOutcomes conditionAndOutcomes = result.get(key); Iterator<ConditionEvaluationReport.ConditionAndOutcome> iterator = conditionAndOutcomes.iterator(); while(iterator.hasNext()) { ConditionEvaluationReport.ConditionAndOutcome conditionAndOutcome = iterator.next(); sb.append(key + " -- " + conditionAndOutcome.getCondition().getClass().getSimpleName() + " -- " + conditionAndOutcome.getOutcome() + "<br/>"); } } return sb.toString(); }
@Override public void prepareTestInstance(TestContext testContext) throws Exception { try { this.delegate.prepareTestInstance(testContext); } catch (Exception ex) { ApplicationContext context = testContext.getApplicationContext(); if (context instanceof ConfigurableApplicationContext) { ConditionEvaluationReport report = ConditionEvaluationReport .get(((ConfigurableApplicationContext) context).getBeanFactory()); System.err.println(new ConditionEvaluationReportMessage(report)); } throw ex; } }
@Override public boolean matches(ApplicationContext context) { ConditionEvaluationReport report = ConditionEvaluationReport .get((ConfigurableListableBeanFactory) context .getAutowireCapableBeanFactory()); return report.getConditionAndOutcomesBySource().keySet() .contains(this.autoConfigurationClass.getName()); }
public Report(ConditionEvaluationReport report) { this.positiveMatches = new LinkedMultiValueMap<String, MessageAndCondition>(); this.negativeMatches = new LinkedMultiValueMap<String, MessageAndCondition>(); this.exclusions = report.getExclusions(); for (Map.Entry<String, ConditionAndOutcomes> entry : report .getConditionAndOutcomesBySource().entrySet()) { add(entry.getValue().isFullMatch() ? this.positiveMatches : this.negativeMatches, entry.getKey(), entry.getValue()); } if (report.getParent() != null) { this.parent = new Report(report.getParent()); } }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { this.applicationContext = applicationContext; applicationContext.addApplicationListener(new AutoConfigurationReportListener()); if (applicationContext instanceof GenericApplicationContext) { // Get the report early in case the context fails to load this.report = ConditionEvaluationReport .get(this.applicationContext.getBeanFactory()); } }
private void recordWithConditionEvaluationReport(List<String> configurations, Collection<String> exclusions) throws IOException { ConditionEvaluationReport report = ConditionEvaluationReport .get(getBeanFactory()); report.recordEvaluationCandidates(configurations); report.recordExclusions(exclusions); }
@Test public void canBeUsedInApplicationContext() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(Config.class); new AutoConfigurationReportLoggingInitializer().initialize(context); context.refresh(); assertThat(context.getBean(ConditionEvaluationReport.class)).isNotNull(); }
@Test public void canBeUsedInNonGenericApplicationContext() throws Exception { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setServletContext(new MockServletContext()); context.register(Config.class); new AutoConfigurationReportLoggingInitializer().initialize(context); context.refresh(); assertThat(context.getBean(ConditionEvaluationReport.class)).isNotNull(); }
@Test public void importsAreSelected() { configureExclusions(new String[0], new String[0], new String[0]); String[] imports = this.importSelector.selectImports(this.annotationMetadata); assertThat(imports).hasSameSizeAs(SpringFactoriesLoader.loadFactoryNames( EnableAutoConfiguration.class, getClass().getClassLoader())); assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions()) .isEmpty(); }
@Test public void classExclusionsAreApplied() { configureExclusions(new String[] { FreeMarkerAutoConfiguration.class.getName() }, new String[0], new String[0]); String[] imports = this.importSelector.selectImports(this.annotationMetadata); assertThat(imports).hasSize(getAutoConfigurationClassNames().size() - 1); assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions()) .contains(FreeMarkerAutoConfiguration.class.getName()); }
@Test public void classNamesExclusionsAreApplied() { configureExclusions(new String[0], new String[] { VelocityAutoConfiguration.class.getName() }, new String[0]); String[] imports = this.importSelector.selectImports(this.annotationMetadata); assertThat(imports).hasSize(getAutoConfigurationClassNames().size() - 1); assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions()) .contains(VelocityAutoConfiguration.class.getName()); }
@Test public void propertyExclusionsAreApplied() { configureExclusions(new String[0], new String[0], new String[] { FreeMarkerAutoConfiguration.class.getName() }); String[] imports = this.importSelector.selectImports(this.annotationMetadata); assertThat(imports).hasSize(getAutoConfigurationClassNames().size() - 1); assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions()) .contains(FreeMarkerAutoConfiguration.class.getName()); }
@Test public void severalPropertyExclusionsAreApplied() { configureExclusions(new String[0], new String[0], new String[] { FreeMarkerAutoConfiguration.class.getName(), VelocityAutoConfiguration.class.getName() }); String[] imports = this.importSelector.selectImports(this.annotationMetadata); assertThat(imports).hasSize(getAutoConfigurationClassNames().size() - 2); assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions()) .contains(FreeMarkerAutoConfiguration.class.getName(), VelocityAutoConfiguration.class.getName()); }
@Test public void severalPropertyYamlExclusionsAreApplied() { configureExclusions(new String[0], new String[0], new String[0]); this.environment.setProperty("spring.autoconfigure.exclude[0]", FreeMarkerAutoConfiguration.class.getName()); this.environment.setProperty("spring.autoconfigure.exclude[1]", VelocityAutoConfiguration.class.getName()); String[] imports = this.importSelector.selectImports(this.annotationMetadata); assertThat(imports).hasSize(getAutoConfigurationClassNames().size() - 2); assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions()) .contains(FreeMarkerAutoConfiguration.class.getName(), VelocityAutoConfiguration.class.getName()); }
@Test public void combinedExclusionsAreApplied() { configureExclusions(new String[] { VelocityAutoConfiguration.class.getName() }, new String[] { FreeMarkerAutoConfiguration.class.getName() }, new String[] { ThymeleafAutoConfiguration.class.getName() }); String[] imports = this.importSelector.selectImports(this.annotationMetadata); assertThat(imports).hasSize(getAutoConfigurationClassNames().size() - 3); assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions()) .contains(FreeMarkerAutoConfiguration.class.getName(), VelocityAutoConfiguration.class.getName(), ThymeleafAutoConfiguration.class.getName()); }