From ad2e311f2f150cdb533e7b582c777136fed4a060 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 16 May 2013 12:14:11 +0100 Subject: [PATCH] [bs-124] Add tests for @ConditionalOnBean [#49989537] [bs-124] @ConditionalOnBean doesn't work --- .../web/WebMvcAutoConfiguration.java | 1 + .../annotation/AbstractOnBeanCondition.java | 19 +-- .../context/annotation/OnClassCondition.java | 4 +- .../annotation/OnBeanConditionTests.java | 114 ++++++++++++++++++ .../annotation/OnClassConditionTests.java | 105 ++++++++++++++++ .../bootstrap/context/annotation/foo.xml | 10 ++ 6 files changed, 243 insertions(+), 10 deletions(-) create mode 100644 spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnBeanConditionTests.java create mode 100644 spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnClassConditionTests.java create mode 100644 spring-bootstrap/src/test/resources/org/springframework/bootstrap/context/annotation/foo.xml diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/web/WebMvcAutoConfiguration.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/web/WebMvcAutoConfiguration.java index df0282fb8b1..dd2ece7a3ac 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/web/WebMvcAutoConfiguration.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/web/WebMvcAutoConfiguration.java @@ -91,6 +91,7 @@ public class WebMvcAutoConfiguration { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("/") + .addResourceLocations("classpath:/META-INF/resources/") .addResourceLocations("classpath:/static") .addResourceLocations("classpath:/"); } diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/AbstractOnBeanCondition.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/AbstractOnBeanCondition.java index 6e29394b2dd..b2ef7ee1108 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/AbstractOnBeanCondition.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/AbstractOnBeanCondition.java @@ -33,6 +33,7 @@ import org.springframework.util.MultiValueMap; * Base for {@link OnBeanCondition} and {@link OnMissingBeanCondition}. * * @author Phillip Webb + * @author Dave Syer */ abstract class AbstractOnBeanCondition implements Condition { @@ -53,9 +54,6 @@ abstract class AbstractOnBeanCondition implements Condition { List beanClassesFound = new ArrayList(); List beanNamesFound = new ArrayList(); - if (this.logger.isDebugEnabled()) { - this.logger.debug("Looking for beans with class: " + beanClasses); - } for (String beanClass : beanClasses) { try { // eagerInit set to false to prevent early instantiation (some @@ -71,10 +69,6 @@ abstract class AbstractOnBeanCondition implements Condition { } catch (ClassNotFoundException ex) { } } - - if (this.logger.isDebugEnabled()) { - this.logger.debug("Looking for beans with names: " + beanNames); - } for (String beanName : beanNames) { if (context.getBeanFactory().containsBeanDefinition(beanName)) { beanNamesFound.add(beanName); @@ -83,7 +77,16 @@ abstract class AbstractOnBeanCondition implements Condition { boolean result = evaluate(beanClassesFound, beanNamesFound); if (this.logger.isDebugEnabled()) { - this.logger.debug("Finished matching and result is matches: " + result); + if (!beanClasses.isEmpty()) { + this.logger.debug("Looking for beans with class: " + beanClasses); + this.logger.debug("Found beans with classes: " + beanClassesFound); + } + + if (!beanNames.isEmpty()) { + this.logger.debug("Looking for beans with names: " + beanNames); + this.logger.debug("Found beans with names: " + beanNamesFound); + } + this.logger.debug("Match result is: " + result); } return result; } diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnClassCondition.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnClassCondition.java index fd00c16bed4..7b8e5fd4a40 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnClassCondition.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnClassCondition.java @@ -54,7 +54,7 @@ class OnClassCondition implements Condition { } if (!ClassUtils.isPresent(className, context.getClassLoader())) { if (logger.isDebugEnabled()) { - logger.debug("Found class: " + className + logger.debug("Class not found: " + className + " (search terminated with matches=false)"); } return false; @@ -62,7 +62,7 @@ class OnClassCondition implements Condition { } } if (logger.isDebugEnabled()) { - logger.debug("Classes not found (search terminated with matches=true)"); + logger.debug("All classes found (search terminated with matches=true)"); } return true; } diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnBeanConditionTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnBeanConditionTests.java new file mode 100644 index 00000000000..061d3f53bac --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnBeanConditionTests.java @@ -0,0 +1,114 @@ +/* + * Copyright 2012-2013 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.bootstrap.context.annotation; + +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.ImportResource; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author Dave Syer + * + */ +@Ignore +public class OnBeanConditionTests { + + private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + + @Test + public void testNameOnBeanCondition() { + this.context.register(FooConfiguration.class, OnBeanNameConfiguration.class); + this.context.refresh(); + assertTrue(this.context.containsBean("bar")); + assertEquals("bar", this.context.getBean("bar")); + } + + @Test + public void testNameOnBeanConditionReverseOrder() { + this.context.register(OnBeanNameConfiguration.class, FooConfiguration.class); + this.context.refresh(); + assertTrue(this.context.containsBean("bar")); + assertEquals("bar", this.context.getBean("bar")); + } + + @Test + public void testClassOnBeanCondition() { + this.context.register(OnBeanClassConfiguration.class, FooConfiguration.class); + this.context.refresh(); + assertTrue(this.context.containsBean("bar")); + assertEquals("bar", this.context.getBean("bar")); + } + + @Test + public void testOnBeanConditionWithXml() { + this.context.register(OnBeanNameConfiguration.class, XmlConfiguration.class); + this.context.refresh(); + assertTrue(this.context.containsBean("bar")); + assertEquals("bar", this.context.getBean("bar")); + } + + @Test + public void testOnBeanConditionWithCombinedXml() { + this.context.register(CombinedXmlConfiguration.class); + this.context.refresh(); + assertTrue(this.context.containsBean("bar")); + assertEquals("bar", this.context.getBean("bar")); + } + + @Configuration + @ConditionalOnBean(name = "foo") + protected static class OnBeanNameConfiguration { + @Bean + public String bar() { + return "bar"; + } + } + + @Configuration + @ConditionalOnBean(String.class) + protected static class OnBeanClassConfiguration { + @Bean + public String bar() { + return "bar"; + } + } + + @Configuration + protected static class FooConfiguration { + @Bean + public String foo() { + return "foo"; + } + } + + @Configuration + @ImportResource("org/springframework/bootstrap/context/annotation/foo.xml") + protected static class XmlConfiguration { + } + + @Configuration + @Import(OnBeanNameConfiguration.class) + @ImportResource("org/springframework/bootstrap/context/annotation/foo.xml") + protected static class CombinedXmlConfiguration { + } +} diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnClassConditionTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnClassConditionTests.java new file mode 100644 index 00000000000..3e4a95eda98 --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnClassConditionTests.java @@ -0,0 +1,105 @@ +/* + * Copyright 2012-2013 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.bootstrap.context.annotation; + +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.ImportResource; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Dave Syer + * + */ +public class OnClassConditionTests { + + private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + + @Test + public void testVanillaOnClassCondition() { + this.context.register(BasicConfiguration.class, FooConfiguration.class); + this.context.refresh(); + assertTrue(this.context.containsBean("bar")); + assertEquals("bar", this.context.getBean("bar")); + } + + @Test + public void testMissingOnClassCondition() { + this.context.register(MissingConfiguration.class, FooConfiguration.class); + this.context.refresh(); + assertFalse(this.context.containsBean("bar")); + assertEquals("foo", this.context.getBean("foo")); + } + + @Test + public void testOnClassConditionWithXml() { + this.context.register(BasicConfiguration.class, XmlConfiguration.class); + this.context.refresh(); + assertTrue(this.context.containsBean("bar")); + assertEquals("bar", this.context.getBean("bar")); + } + + @Test + public void testOnClassConditionWithCombinedXml() { + this.context.register(CombinedXmlConfiguration.class); + this.context.refresh(); + assertTrue(this.context.containsBean("bar")); + assertEquals("bar", this.context.getBean("bar")); + } + + @Configuration + @ConditionalOnClass(OnClassConditionTests.class) + protected static class BasicConfiguration { + @Bean + public String bar() { + return "bar"; + } + } + + @Configuration + @ConditionalOnClass(name = "FOO") + protected static class MissingConfiguration { + @Bean + public String bar() { + return "bar"; + } + } + + @Configuration + protected static class FooConfiguration { + @Bean + public String foo() { + return "foo"; + } + } + + @Configuration + @ImportResource("org/springframework/bootstrap/context/annotation/foo.xml") + protected static class XmlConfiguration { + } + + @Configuration + @Import(BasicConfiguration.class) + @ImportResource("org/springframework/bootstrap/context/annotation/foo.xml") + protected static class CombinedXmlConfiguration { + } +} diff --git a/spring-bootstrap/src/test/resources/org/springframework/bootstrap/context/annotation/foo.xml b/spring-bootstrap/src/test/resources/org/springframework/bootstrap/context/annotation/foo.xml new file mode 100644 index 00000000000..735b571fc78 --- /dev/null +++ b/spring-bootstrap/src/test/resources/org/springframework/bootstrap/context/annotation/foo.xml @@ -0,0 +1,10 @@ + + + + + + + +