|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2016 the original author or authors. |
| 2 | + * Copyright 2012-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.condition;
|
18 | 18 |
|
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
19 | 22 | import org.junit.Test;
|
20 | 23 |
|
21 | 24 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
22 | 25 | import org.springframework.context.annotation.Bean;
|
| 26 | +import org.springframework.context.annotation.ConditionContext; |
23 | 27 | import org.springframework.context.annotation.Configuration;
|
| 28 | +import org.springframework.core.type.AnnotatedTypeMetadata; |
| 29 | +import org.springframework.mock.env.MockEnvironment; |
24 | 30 |
|
25 | 31 | import static org.assertj.core.api.Assertions.assertThat;
|
| 32 | +import static org.mockito.BDDMockito.given; |
| 33 | +import static org.mockito.Mockito.mock; |
26 | 34 |
|
27 | 35 | /**
|
28 | 36 | * Tests for {@link ConditionalOnExpression}.
|
29 | 37 | *
|
30 | 38 | * @author Dave Syer
|
| 39 | + * @author Stephane Nicoll |
31 | 40 | */
|
32 | 41 | public class ConditionalOnExpressionTests {
|
33 | 42 |
|
34 | 43 | private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
35 | 44 |
|
36 | 45 | @Test
|
37 |
| - public void testResourceExists() { |
| 46 | + public void expressionEvaluatesToTrueRegisterBean() { |
38 | 47 | this.context.register(BasicConfiguration.class);
|
39 | 48 | this.context.refresh();
|
40 | 49 | assertThat(this.context.containsBean("foo")).isTrue();
|
41 | 50 | assertThat(this.context.getBean("foo")).isEqualTo("foo");
|
42 | 51 | }
|
43 | 52 |
|
44 | 53 | @Test
|
45 |
| - public void testResourceNotExists() { |
| 54 | + public void expressionEvaluatesToFalseDoesNotRegisterBean() { |
46 | 55 | this.context.register(MissingConfiguration.class);
|
47 | 56 | this.context.refresh();
|
48 | 57 | assertThat(this.context.containsBean("foo")).isFalse();
|
49 | 58 | }
|
50 | 59 |
|
| 60 | + @Test |
| 61 | + public void expressionEvaluationWithNoBeanFactoryDoesNotMatch() { |
| 62 | + OnExpressionCondition condition = new OnExpressionCondition(); |
| 63 | + MockEnvironment environment = new MockEnvironment(); |
| 64 | + ConditionContext conditionContext = mock(ConditionContext.class); |
| 65 | + given(conditionContext.getEnvironment()).willReturn(environment); |
| 66 | + ConditionOutcome outcome = condition.getMatchOutcome(conditionContext, |
| 67 | + mockMetaData("invalid-spel")); |
| 68 | + assertThat(outcome.isMatch()).isFalse(); |
| 69 | + assertThat(outcome.getMessage()).contains("invalid-spel") |
| 70 | + .contains("no BeanFactory available"); |
| 71 | + } |
| 72 | + |
| 73 | + private AnnotatedTypeMetadata mockMetaData(String value) { |
| 74 | + AnnotatedTypeMetadata metadata = mock(AnnotatedTypeMetadata.class); |
| 75 | + Map<String, Object> attributes = new HashMap<String, Object>(); |
| 76 | + attributes.put("value", value); |
| 77 | + given(metadata.getAnnotationAttributes(ConditionalOnExpression.class.getName())) |
| 78 | + .willReturn(attributes); |
| 79 | + return metadata; |
| 80 | + } |
| 81 | + |
51 | 82 | @Configuration
|
52 | 83 | @ConditionalOnExpression("false")
|
53 | 84 | protected static class MissingConfiguration {
|
|
0 commit comments