Skip to content

Commit 80358f7

Browse files
eneiascssnicoll
authored andcommitted
Fix WSDL locations condition to work with a list
See gh-14285
1 parent 6078865 commit 80358f7

File tree

6 files changed

+203
-28
lines changed

6 files changed

+203
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2012-2018 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+
* http://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.boot.autoconfigure.condition;
18+
19+
import java.util.List;
20+
21+
import org.springframework.boot.context.properties.bind.BindResult;
22+
import org.springframework.boot.context.properties.bind.Bindable;
23+
import org.springframework.boot.context.properties.bind.Binder;
24+
import org.springframework.context.annotation.ConditionContext;
25+
import org.springframework.core.type.AnnotatedTypeMetadata;
26+
27+
/**
28+
* Abstract base class for list conditions.
29+
*
30+
* @author Eneias Silva
31+
*
32+
*/
33+
public abstract class AbstractListCondition extends SpringBootCondition {
34+
35+
private static final Bindable<List<String>> STRING_LIST = Bindable
36+
.listOf(String.class);
37+
38+
@Override
39+
public ConditionOutcome getMatchOutcome(ConditionContext context,
40+
AnnotatedTypeMetadata metadata) {
41+
BindResult<?> property = Binder.get(context.getEnvironment())
42+
.bind(getPropertyName(), STRING_LIST);
43+
if (property.isBound()) {
44+
return ConditionOutcome.match(ConditionMessage.forCondition(getClassName())
45+
.found("property").items(getPropertyName()));
46+
}
47+
return ConditionOutcome.noMatch(ConditionMessage.forCondition(getClassName())
48+
.didNotFind("property").items(getPropertyName()));
49+
}
50+
51+
52+
protected abstract String getPropertyName();
53+
54+
55+
protected abstract String getClassName();
56+
57+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/OnBootstrapHostsCondition.java

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,25 @@
1616

1717
package org.springframework.boot.autoconfigure.couchbase;
1818

19-
import java.util.List;
20-
21-
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
22-
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
23-
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
24-
import org.springframework.boot.context.properties.bind.BindResult;
25-
import org.springframework.boot.context.properties.bind.Bindable;
26-
import org.springframework.boot.context.properties.bind.Binder;
27-
import org.springframework.context.annotation.ConditionContext;
28-
import org.springframework.core.type.AnnotatedTypeMetadata;
19+
import org.springframework.boot.autoconfigure.condition.AbstractListCondition;
2920

3021
/**
3122
* Condition to determine if {@code spring.couchbase.bootstrap-hosts} is specified.
3223
*
3324
* @author Stephane Nicoll
3425
* @author Madhura Bhave
26+
* @author Eneias Silva
3527
*/
36-
class OnBootstrapHostsCondition extends SpringBootCondition {
28+
class OnBootstrapHostsCondition extends AbstractListCondition {
3729

38-
private static final Bindable<List<String>> STRING_LIST = Bindable
39-
.listOf(String.class);
30+
@Override
31+
protected String getPropertyName() {
32+
return "spring.couchbase.bootstrap-hosts";
33+
}
4034

4135
@Override
42-
public ConditionOutcome getMatchOutcome(ConditionContext context,
43-
AnnotatedTypeMetadata metadata) {
44-
String name = "spring.couchbase.bootstrap-hosts";
45-
BindResult<?> property = Binder.get(context.getEnvironment()).bind(name,
46-
STRING_LIST);
47-
if (property.isBound()) {
48-
return ConditionOutcome.match(ConditionMessage
49-
.forCondition(OnBootstrapHostsCondition.class.getName())
50-
.found("property").items(name));
51-
}
52-
return ConditionOutcome.noMatch(
53-
ConditionMessage.forCondition(OnBootstrapHostsCondition.class.getName())
54-
.didNotFind("property").items(name));
36+
protected String getClassName() {
37+
return OnBootstrapHostsCondition.class.getName();
5538
}
5639

5740
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2017 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+
* http://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.boot.autoconfigure.webservices;
18+
19+
import org.springframework.boot.autoconfigure.condition.AbstractListCondition;
20+
21+
/**
22+
* Condition to determine if {@code spring.webservices.wsdl-locations} is specified.
23+
*
24+
* @author Eneias Silva
25+
*/
26+
class OnWsdlLocationsCondition extends AbstractListCondition {
27+
28+
@Override
29+
protected String getPropertyName() {
30+
return "spring.webservices.wsdl-locations";
31+
}
32+
33+
@Override
34+
protected String getClassName() {
35+
return OnWsdlLocationsCondition.class.getName();
36+
}
37+
38+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3131
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3232
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
33-
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3433
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
3534
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
3635
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
@@ -41,6 +40,7 @@
4140
import org.springframework.context.ApplicationContext;
4241
import org.springframework.context.ApplicationContextAware;
4342
import org.springframework.context.annotation.Bean;
43+
import org.springframework.context.annotation.Conditional;
4444
import org.springframework.context.annotation.Configuration;
4545
import org.springframework.core.io.Resource;
4646
import org.springframework.util.StringUtils;
@@ -55,6 +55,7 @@
5555
*
5656
* @author Vedran Pavic
5757
* @author Stephane Nicoll
58+
* @author Eneias Silva
5859
* @since 1.4.0
5960
*/
6061
@Configuration
@@ -87,7 +88,7 @@ public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServle
8788
}
8889

8990
@Bean
90-
@ConditionalOnProperty(prefix = "spring.webservices", name = "wsdl-locations")
91+
@Conditional(OnWsdlLocationsCondition.class)
9192
public static WsdlDefinitionBeanFactoryPostProcessor wsdlDefinitionBeanFactoryPostProcessor() {
9293
return new WsdlDefinitionBeanFactoryPostProcessor();
9394
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2012-2017 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+
* http://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.boot.autoconfigure.webservices;
18+
19+
import org.junit.After;
20+
import org.junit.Test;
21+
22+
import org.springframework.boot.test.util.TestPropertyValues;
23+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Conditional;
26+
import org.springframework.context.annotation.Configuration;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link OnWsdlLocationsCondition}.
32+
*
33+
* @author Eneias Silva
34+
*/
35+
public class OnWsdlLocationsConditionTests {
36+
37+
private AnnotationConfigApplicationContext context;
38+
39+
@After
40+
public void tearDown() {
41+
if (this.context != null) {
42+
this.context.close();
43+
}
44+
}
45+
46+
@Test
47+
public void wsdlLocationsNotDefined() {
48+
load(TestConfig.class);
49+
assertThat(this.context.containsBean("foo")).isFalse();
50+
}
51+
52+
@Test
53+
public void wsdlLocationsDefinedAsCommaSeparated() {
54+
load(TestConfig.class, "spring.webservices.wsdl-locations=value1");
55+
assertThat(this.context.containsBean("foo")).isTrue();
56+
}
57+
58+
@Test
59+
public void wsdlLocationsDefinedAsList() {
60+
load(TestConfig.class, "spring.webservices.wsdl-locations[0]=value1");
61+
assertThat(this.context.containsBean("foo")).isTrue();
62+
}
63+
64+
private void load(Class<?> config, String... environment) {
65+
this.context = new AnnotationConfigApplicationContext();
66+
TestPropertyValues.of(environment).applyTo(this.context);
67+
this.context.register(config);
68+
this.context.refresh();
69+
}
70+
71+
@Configuration
72+
@Conditional(OnWsdlLocationsCondition.class)
73+
protected static class TestConfig {
74+
75+
@Bean
76+
public String foo() {
77+
return "foo";
78+
}
79+
80+
}
81+
82+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Vedran Pavic
3838
* @author Stephane Nicoll
3939
* @author Andy Wilkinson
40+
* @author Eneias Silva
4041
*/
4142
public class WebServicesAutoConfigurationTests {
4243

@@ -104,6 +105,19 @@ public void withWsdlBeans() {
104105
});
105106
}
106107

108+
@Test
109+
public void withWsdlBeansAsList() {
110+
this.contextRunner
111+
.withPropertyValues(
112+
"spring.webservices.wsdl-locations[0]=classpath:/wsdl")
113+
.run((context) -> {
114+
assertThat(context.getBeansOfType(SimpleWsdl11Definition.class))
115+
.hasSize(1).containsKey("service");
116+
assertThat(context.getBeansOfType(SimpleXsdSchema.class)).hasSize(1)
117+
.containsKey("types");
118+
});
119+
}
120+
107121
private Collection<String> getUrlMappings(ApplicationContext context) {
108122
return getServletRegistrationBean(context).getUrlMappings();
109123
}

0 commit comments

Comments
 (0)