Commit 2aa68aaf authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #8128 from tinexw:master

* pr/8128:
  Polish "Handle null value in OnExpressionCondition"
  Handle null value in OnExpressionCondition
parents 70f57282 5114c9d6
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -51,8 +51,9 @@ class OnExpressionCondition extends SpringBootCondition { ...@@ -51,8 +51,9 @@ class OnExpressionCondition extends SpringBootCondition {
if (resolver == null) { if (resolver == null) {
resolver = new StandardBeanExpressionResolver(); resolver = new StandardBeanExpressionResolver();
} }
boolean result = (Boolean) resolver.evaluate(expression, expressionContext); Object result = resolver.evaluate(expression, expressionContext);
return new ConditionOutcome(result, ConditionMessage boolean match = result != null && (boolean) result;
return new ConditionOutcome(match, ConditionMessage
.forCondition(ConditionalOnExpression.class, "(" + rawExpression + ")") .forCondition(ConditionalOnExpression.class, "(" + rawExpression + ")")
.resultedIn(result)); .resultedIn(result));
} }
......
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -28,13 +28,14 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -28,13 +28,14 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link ConditionalOnExpression}. * Tests for {@link ConditionalOnExpression}.
* *
* @author Dave Syer * @author Dave Syer
* @author Stephane Nicoll
*/ */
public class ConditionalOnExpressionTests { public class ConditionalOnExpressionTests {
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Test @Test
public void testResourceExists() { public void expressionIsTrue() {
this.context.register(BasicConfiguration.class); this.context.register(BasicConfiguration.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.containsBean("foo")).isTrue(); assertThat(this.context.containsBean("foo")).isTrue();
...@@ -42,12 +43,19 @@ public class ConditionalOnExpressionTests { ...@@ -42,12 +43,19 @@ public class ConditionalOnExpressionTests {
} }
@Test @Test
public void testResourceNotExists() { public void expressionIsFalse() {
this.context.register(MissingConfiguration.class); this.context.register(MissingConfiguration.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.containsBean("foo")).isFalse(); assertThat(this.context.containsBean("foo")).isFalse();
} }
@Test
public void expressionIsNull() {
this.context.register(NullConfiguration.class);
this.context.refresh();
assertThat(this.context.containsBean("foo")).isFalse();
}
@Configuration @Configuration
@ConditionalOnExpression("false") @ConditionalOnExpression("false")
protected static class MissingConfiguration { protected static class MissingConfiguration {
...@@ -70,4 +78,15 @@ public class ConditionalOnExpressionTests { ...@@ -70,4 +78,15 @@ public class ConditionalOnExpressionTests {
} }
@Configuration
@ConditionalOnExpression("true ? null : false")
protected static class NullConfiguration {
@Bean
public String foo() {
return "foo";
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment