[bs-124] Add tests for @ConditionalOnBean

[#49989537] [bs-124] @ConditionalOnBean doesn't work
pull/1/merge
Dave Syer 12 years ago
parent 6d21ff71ba
commit ad2e311f2f

@ -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:/");
}

@ -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<String> beanClassesFound = new ArrayList<String>();
List<String> beanNamesFound = new ArrayList<String>();
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;
}

@ -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;
}

@ -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 {
}
}

@ -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 {
}
}

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="foo" class="java.lang.String">
<constructor-arg value="foo" />
</bean>
</beans>
Loading…
Cancel
Save