Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Commit 6e3f32d

Browse files
bsamartinsmaciejwalkowiak
authored andcommitted
Allow ConditionalOnAwsCloudEnvironment to be used on method level.
Fixes gh-389 Closes gh-424
1 parent 4c11014 commit 6e3f32d

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

spring-cloud-aws-context/src/main/java/org/springframework/cloud/aws/context/annotation/OnAwsCloudEnvironmentCondition.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
/**
2525
* @author Agim Emruli
2626
* @author Gleb Schukin
27+
* @author Bernardo Martins
2728
*/
2829
public class OnAwsCloudEnvironmentCondition implements ConfigurationCondition {
2930

3031
@Override
3132
public ConfigurationPhase getConfigurationPhase() {
32-
return ConfigurationPhase.PARSE_CONFIGURATION;
33+
return ConfigurationPhase.REGISTER_BEAN;
3334
}
3435

3536
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2013-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.aws.context.annotation;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* @author Bernardo Martins
29+
*/
30+
public class OnAwsCloudEnvironmentConditionTest {
31+
32+
@Test
33+
public void condition_methodAnnotation_shouldNotCreateBeanFoo() {
34+
// Arrange & Act
35+
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
36+
ConfigWithConditionalOnAwsCloudEnvironmentAnnotatedBean.class);
37+
38+
// Assert
39+
assertThat(applicationContext.containsBean("foo")).isFalse();
40+
}
41+
42+
@Test
43+
public void condition_typeAnnotation_shouldNotCreateBeanFoo() {
44+
// Arrange & Act
45+
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
46+
ConfigWithConditionalOnAwsCloudEnvironmentAnnotation.class);
47+
48+
// Assert
49+
assertThat(applicationContext.containsBean("foo")).isFalse();
50+
}
51+
52+
@Test
53+
public void condition_methodAnnotation_shouldCreateBeanFoo() {
54+
// Arrange & Act
55+
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
56+
ConfigWithConditionalOnMissingAwsCloudEnvironmentAnnotatedBean.class);
57+
58+
// Assert
59+
assertThat(applicationContext.containsBean("foo")).isTrue();
60+
}
61+
62+
@Test
63+
public void condition_typeAnnotation_shouldCreateBeanFoo() {
64+
// Arrange & Act
65+
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
66+
ConfigWithConditionalOnMissingAwsCloudEnvironmentAnnotation.class);
67+
68+
// Assert
69+
assertThat(applicationContext.containsBean("foo")).isTrue();
70+
}
71+
72+
@Configuration
73+
protected static class ConfigWithConditionalOnAwsCloudEnvironmentAnnotatedBean {
74+
75+
@Bean
76+
@ConditionalOnAwsCloudEnvironment
77+
public String foo() {
78+
return "foo";
79+
}
80+
81+
}
82+
83+
@Configuration
84+
@ConditionalOnAwsCloudEnvironment
85+
protected static class ConfigWithConditionalOnAwsCloudEnvironmentAnnotation {
86+
87+
@Bean
88+
public String foo() {
89+
return "foo";
90+
}
91+
92+
}
93+
94+
@Configuration
95+
protected static class ConfigWithConditionalOnMissingAwsCloudEnvironmentAnnotatedBean {
96+
97+
@Bean
98+
@ConditionalOnMissingAwsCloudEnvironment
99+
public String foo() {
100+
return "foo";
101+
}
102+
103+
}
104+
105+
@Configuration
106+
@ConditionalOnMissingAwsCloudEnvironment
107+
protected static class ConfigWithConditionalOnMissingAwsCloudEnvironmentAnnotation {
108+
109+
@Bean
110+
public String foo() {
111+
return "foo";
112+
}
113+
114+
}
115+
116+
}

0 commit comments

Comments
 (0)