From caf1aab9db8f40d325b86d41de837e3ebae8def6 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 21 May 2013 09:51:49 +0100 Subject: [PATCH] [bs-22] Add tests for variour @Conditional implementations [#48127729] [bs-22] Add missing unit tests --- .../annotation/ConditionalOnExpression.java | 2 +- ...dition.java => OnExpressionCondition.java} | 8 +- .../NotWebApplicationConditionTests.java | 67 ++++++++++++++++ .../OnExpressionConditionTests.java | 67 ++++++++++++++++ .../OnMissingClassConditionTests.java | 77 +++++++++++++++++++ .../WebApplicationConditionTests.java | 70 +++++++++++++++++ 6 files changed, 288 insertions(+), 3 deletions(-) rename spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/{ExpressionCondition.java => OnExpressionCondition.java} (90%) create mode 100644 spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/NotWebApplicationConditionTests.java create mode 100644 spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnExpressionConditionTests.java create mode 100644 spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnMissingClassConditionTests.java create mode 100644 spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/WebApplicationConditionTests.java diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ConditionalOnExpression.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ConditionalOnExpression.java index 216f8bbc9f..9b3454a586 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ConditionalOnExpression.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ConditionalOnExpression.java @@ -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 { diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ExpressionCondition.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnExpressionCondition.java similarity index 90% rename from spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ExpressionCondition.java rename to spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnExpressionCondition.java index c862d995d5..044d54a1a9 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ExpressionCondition.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnExpressionCondition.java @@ -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); diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/NotWebApplicationConditionTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/NotWebApplicationConditionTests.java new file mode 100644 index 0000000000..07b92a78a8 --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/NotWebApplicationConditionTests.java @@ -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"; + } + } +} diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnExpressionConditionTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnExpressionConditionTests.java new file mode 100644 index 0000000000..9df5ccebe3 --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnExpressionConditionTests.java @@ -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"; + } + } +} diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnMissingClassConditionTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnMissingClassConditionTests.java new file mode 100644 index 0000000000..a5acea274e --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnMissingClassConditionTests.java @@ -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"; + } + } + +} diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/WebApplicationConditionTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/WebApplicationConditionTests.java new file mode 100644 index 0000000000..40579703e7 --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/WebApplicationConditionTests.java @@ -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"; + } + } +}