[bs-22] Add tests for variour @Conditional implementations

[#48127729] [bs-22] Add missing unit tests
This commit is contained in:
Dave Syer 2013-05-21 09:51:49 +01:00
parent c6b4c48181
commit caf1aab9db
6 changed files with 288 additions and 3 deletions

View File

@ -29,7 +29,7 @@ import org.springframework.context.annotation.Conditional;
* @author Dave Syer
*
*/
@Conditional(ExpressionCondition.class)
@Conditional(OnExpressionCondition.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface ConditionalOnExpression {

View File

@ -22,6 +22,7 @@ import org.springframework.beans.factory.config.BeanExpressionResolver;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.expression.StandardBeanExpressionResolver;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.ClassMetadata;
@ -31,9 +32,9 @@ import org.springframework.core.type.ClassMetadata;
* @author Dave Syer
*
*/
public class ExpressionCondition implements Condition {
public class OnExpressionCondition implements Condition {
private static Log logger = LogFactory.getLog(ExpressionCondition.class);
private static Log logger = LogFactory.getLog(OnExpressionCondition.class);
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
@ -61,6 +62,9 @@ public class ExpressionCondition implements Condition {
BeanExpressionResolver resolver = beanFactory.getBeanExpressionResolver();
BeanExpressionContext expressionContext = (beanFactory != null) ? new BeanExpressionContext(
beanFactory, null) : null;
if (resolver == null) {
resolver = new StandardBeanExpressionResolver();
}
Boolean result = (Boolean) resolver.evaluate(value, expressionContext);
if (logger.isDebugEnabled()) {
logger.debug(checking + "Finished matching and result is matches=" + result);

View File

@ -0,0 +1,67 @@
/*
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*
*/
public class NotWebApplicationConditionTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Test
public void testWebApplication() {
this.context.register(BasicConfiguration.class);
this.context.refresh();
assertTrue(this.context.containsBean("foo"));
assertEquals("foo", this.context.getBean("foo"));
}
@Test
public void testNotWebApplication() {
this.context.register(MissingConfiguration.class);
this.context.refresh();
assertFalse(this.context.containsBean("foo"));
}
@Configuration
@ConditionalOnWebApplication
protected static class MissingConfiguration {
@Bean
public String bar() {
return "bar";
}
}
@Configuration
@ConditionalOnNotWebApplication
protected static class BasicConfiguration {
@Bean
public String foo() {
return "foo";
}
}
}

View File

@ -0,0 +1,67 @@
/*
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*
*/
public class OnExpressionConditionTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Test
public void testResourceExists() {
this.context.register(BasicConfiguration.class);
this.context.refresh();
assertTrue(this.context.containsBean("foo"));
assertEquals("foo", this.context.getBean("foo"));
}
@Test
public void testResourceNotExists() {
this.context.register(MissingConfiguration.class);
this.context.refresh();
assertFalse(this.context.containsBean("foo"));
}
@Configuration
@ConditionalOnExpression("false")
protected static class MissingConfiguration {
@Bean
public String bar() {
return "bar";
}
}
@Configuration
@ConditionalOnExpression("true")
protected static class BasicConfiguration {
@Bean
public String foo() {
return "foo";
}
}
}

View File

@ -0,0 +1,77 @@
/*
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*
*/
public class OnMissingClassConditionTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Test
public void testVanillaOnClassCondition() {
this.context.register(BasicConfiguration.class, FooConfiguration.class);
this.context.refresh();
assertFalse(this.context.containsBean("bar"));
assertEquals("foo", this.context.getBean("foo"));
}
@Test
public void testMissingOnClassCondition() {
this.context.register(MissingConfiguration.class, FooConfiguration.class);
this.context.refresh();
assertTrue(this.context.containsBean("bar"));
assertEquals("foo", this.context.getBean("foo"));
}
@Configuration
@ConditionalOnMissingClass("org.springframework.bootstrap.context.annotation.OnMissingClassConditionTests")
protected static class BasicConfiguration {
@Bean
public String bar() {
return "bar";
}
}
@Configuration
@ConditionalOnMissingClass("FOO")
protected static class MissingConfiguration {
@Bean
public String bar() {
return "bar";
}
}
@Configuration
protected static class FooConfiguration {
@Bean
public String foo() {
return "foo";
}
}
}

View File

@ -0,0 +1,70 @@
/*
* 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.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*
*/
public class WebApplicationConditionTests {
private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
@Test
public void testWebApplication() {
this.context.register(BasicConfiguration.class);
this.context.setServletContext(new MockServletContext());
this.context.refresh();
assertTrue(this.context.containsBean("foo"));
assertEquals("foo", this.context.getBean("foo"));
}
@Test
public void testNotWebApplication() {
this.context.register(MissingConfiguration.class);
this.context.setServletContext(new MockServletContext());
this.context.refresh();
assertFalse(this.context.containsBean("foo"));
}
@Configuration
@ConditionalOnNotWebApplication
protected static class MissingConfiguration {
@Bean
public String bar() {
return "bar";
}
}
@Configuration
@ConditionalOnWebApplication
protected static class BasicConfiguration {
@Bean
public String foo() {
return "foo";
}
}
}