Polish "Add auto-configuration for WebServiceTemplate"

Closes gh-1270
pull/13338/merge
Stephane Nicoll 7 years ago
parent ef9c1a8e35
commit 8bcea0d8cc

@ -37,10 +37,10 @@ import org.springframework.ws.client.core.WebServiceTemplate;
* {@link EnableAutoConfiguration Auto-configuration} for {@link WebServiceTemplate}.
*
* @author Dmytro Nosan
* @since 2.1.0
*/
@Configuration
@ConditionalOnClass({ WebServiceTemplateBuilder.class, WebServiceTemplate.class,
Unmarshaller.class, Marshaller.class })
@ConditionalOnClass({ WebServiceTemplate.class, Unmarshaller.class, Marshaller.class })
public class WebServiceTemplateAutoConfiguration {
private final ObjectProvider<List<WebServiceTemplateCustomizer>> webServiceTemplateCustomizers;
@ -59,7 +59,7 @@ public class WebServiceTemplateAutoConfiguration {
if (!CollectionUtils.isEmpty(customizers)) {
customizers = new ArrayList<>(customizers);
AnnotationAwareOrderComparator.sort(customizers);
builder = builder.setCustomizers(customizers);
builder = builder.customizers(customizers);
}
return builder;
}

@ -16,82 +16,94 @@
package org.springframework.boot.autoconfigure.webservices.client;
import org.junit.After;
import java.util.function.Consumer;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
import org.springframework.boot.webservices.client.WebServiceTemplateCustomizer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.transport.WebServiceMessageSender;
import org.springframework.ws.transport.http.ClientHttpRequestMessageSender;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WebServiceTemplateAutoConfiguration
* WebServiceTemplateAutoConfiguration}.
* Tests for {@link WebServiceTemplateAutoConfiguration}.
*
* @author Stephane Nicoll
* @author Dmytro Nosan
*/
public class WebServiceTemplateAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(WebServiceTemplateAutoConfiguration.class));
@After
public void close() {
if (this.context != null) {
this.context.close();
}
@Test
public void autoConfiguredBuilderShouldNotHaveMarshallerAndUnmarshaller() {
this.contextRunner.run(assertWebServiceTemplateBuilder((builder) -> {
WebServiceTemplate webServiceTemplate = builder.build();
assertThat(webServiceTemplate.getUnmarshaller()).isNull();
assertThat(webServiceTemplate.getMarshaller()).isNull();
}));
}
@Test
public void webServiceTemplateShouldNotHaveMarshallerAndUnmarshaller() {
load(WebServiceTemplateConfig.class);
WebServiceTemplate webServiceTemplate = this.context
.getBean(WebServiceTemplate.class);
assertThat(webServiceTemplate.getUnmarshaller()).isNull();
assertThat(webServiceTemplate.getMarshaller()).isNull();
public void autoConfiguredBuilderShouldHaveHttpMessageSenderByDefault() {
this.contextRunner.run(assertWebServiceTemplateBuilder((builder) -> {
WebServiceTemplate webServiceTemplate = builder.build();
assertThat(webServiceTemplate.getMessageSenders()).hasSize(1);
WebServiceMessageSender messageSender = webServiceTemplate
.getMessageSenders()[0];
assertThat(messageSender).isInstanceOf(ClientHttpRequestMessageSender.class);
}));
}
@Test
public void webServiceTemplateShouldUserCustomBuilder() {
load(CustomWebServiceTemplateBuilderConfig.class, WebServiceTemplateConfig.class);
WebServiceTemplate webServiceTemplate = this.context
.getBean(WebServiceTemplate.class);
assertThat(webServiceTemplate.getMarshaller()).isNotNull();
public void webServiceTemplateWhenHasCustomBuilderShouldUseCustomBuilder() {
this.contextRunner
.withUserConfiguration(CustomWebServiceTemplateBuilderConfig.class)
.run(assertWebServiceTemplateBuilder((builder) -> {
WebServiceTemplate webServiceTemplate = builder.build();
assertThat(webServiceTemplate.getMarshaller())
.isSameAs(CustomWebServiceTemplateBuilderConfig.marshaller);
}));
}
@Test
public void webServiceTemplateShouldApplyCustomizer() {
load(WebServiceTemplateCustomizerConfig.class, WebServiceTemplateConfig.class);
WebServiceTemplate webServiceTemplate = this.context
.getBean(WebServiceTemplate.class);
assertThat(webServiceTemplate.getUnmarshaller()).isNotNull();
this.contextRunner.withUserConfiguration(WebServiceTemplateCustomizerConfig.class)
.run(assertWebServiceTemplateBuilder((builder) -> {
WebServiceTemplate webServiceTemplate = builder.build();
assertThat(webServiceTemplate.getUnmarshaller())
.isSameAs(WebServiceTemplateCustomizerConfig.unmarshaller);
}));
}
@Test
public void builderShouldBeFreshForEachUse() {
load(DirtyWebServiceTemplateConfig.class);
this.contextRunner.withUserConfiguration(DirtyWebServiceTemplateConfig.class)
.run((context) -> {
assertThat(context).hasNotFailed();
});
}
private void load(Class<?>... config) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(config);
ctx.register(WebServiceTemplateAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}
@Configuration
static class WebServiceTemplateConfig {
@Bean
public WebServiceTemplate webServiceTemplate(WebServiceTemplateBuilder builder) {
return builder.build();
}
private ContextConsumer<AssertableApplicationContext> assertWebServiceTemplateBuilder(
Consumer<WebServiceTemplateBuilder> builder) {
return (context) -> {
assertThat(context).hasSingleBean(WebServiceTemplateBuilder.class);
builder.accept(context.getBean(WebServiceTemplateBuilder.class));
};
}
@Configuration
@ -120,7 +132,7 @@ public class WebServiceTemplateAutoConfigurationTests {
}
private void breakBuilderOnNextCall(WebServiceTemplateBuilder builder) {
builder.addCustomizers((webServiceTemplate) -> {
builder.additionalCustomizers((webServiceTemplate) -> {
throw new IllegalStateException();
});
}
@ -130,9 +142,11 @@ public class WebServiceTemplateAutoConfigurationTests {
@Configuration
static class CustomWebServiceTemplateBuilderConfig {
private static final Marshaller marshaller = new Jaxb2Marshaller();
@Bean
public WebServiceTemplateBuilder webServiceTemplateBuilder() {
return new WebServiceTemplateBuilder().setMarshaller(new Jaxb2Marshaller());
return new WebServiceTemplateBuilder().setMarshaller(marshaller);
}
}
@ -140,9 +154,11 @@ public class WebServiceTemplateAutoConfigurationTests {
@Configuration
static class WebServiceTemplateCustomizerConfig {
private static final Unmarshaller unmarshaller = new Jaxb2Marshaller();
@Bean
public WebServiceTemplateCustomizer webServiceTemplateCustomizer() {
return (ws) -> ws.setUnmarshaller(new Jaxb2Marshaller());
return (ws) -> ws.setUnmarshaller(unmarshaller);
}
}

@ -5634,6 +5634,7 @@ The following code shows a typical example:
----
[[boot-features-webclient-customization]]
=== WebClient Customization
There are three main approaches to `WebClient` customization, depending on how broadly you
@ -5652,35 +5653,6 @@ the point of injection.
Finally, you can fall back to the original API and use `WebClient.create()`. In that case,
no auto-configuration or `WebClientCustomizer` is applied.
[[boot-features-webservicetemplate]]
== Calling Web Services with `WebServiceTemplate`
If you need to call remote WEB services from your application, you can use the Spring
Framework's {spring-webservices-reference}#client-web-service-template[`WebServiceTemplate`] class. Since
`WebServiceTemplate` instances often need to be customized before being used, Spring Boot does
not provide any single auto-configured `WebServiceTemplate` bean. It does, however,
auto-configure a `WebServiceTemplateBuilder`, which can be used to create `WebServiceTemplate`
instances when needed.
The following code shows a typical example:
[source,java,indent=0]
----
@Service
public class MyService {
private final WebServiceTemplate webServiceTemplate;
public MyService(WebServiceTemplateBuilder webServiceTemplateBuilder) {
this.webServiceTemplate = webServiceTemplateBuilder.build();
}
public DetailsResp someCall(DetailsReq detailsReq) {
return (DetailsResp) this.webServiceTemplate.marshalSendAndReceive(detailsReq, new SoapActionCallback(ACTION));
}
}
----
[[boot-features-validation]]
@ -7416,6 +7388,51 @@ following example:
[[boot-features-webservices-template]]
== Calling Web Services with `WebServiceTemplate`
If you need to call remote Web services from your application, you can use the
{spring-webservices-reference}#client-web-service-template[`WebServiceTemplate`] class.
Since `WebServiceTemplate` instances often need to be customized before being used, Spring
Boot does not provide any single auto-configured `WebServiceTemplate` bean. It does,
however, auto-configure a `WebServiceTemplateBuilder`, which can be used to create
`WebServiceTemplate` instances when needed.
The following code shows a typical example:
[source,java,indent=0]
----
@Service
public class MyService {
private final WebServiceTemplate webServiceTemplate;
public MyService(WebServiceTemplateBuilder webServiceTemplateBuilder) {
this.webServiceTemplate = webServiceTemplateBuilder.build();
}
public DetailsResp someWsCall(DetailsReq detailsReq) {
return (DetailsResp) this.webServiceTemplate.marshalSendAndReceive(detailsReq, new SoapActionCallback(ACTION));
}
}
----
By default, `WebServiceTemplateBuilder` detects a suitable HTTP-based
`WebServiceMessageSender` using the available HTTP client libraries on the classpath. You
can also customize read and connection timeouts as follows:
[source,java,indent=0]
----
@Bean
public WebServiceTemplate webServiceTemplate(WebServiceTemplateBuilder builder) {
return builder.messageSenders(new HttpWebServiceMessageSenderBuilder()
.setReadTimeout(5000).setConnectionTimeout(2000).build()).build();
}
----
[[boot-features-developing-auto-configuration]]
== Creating Your Own Auto-configuration
If you work in a company that develops shared libraries, or if you work on an open-source

@ -245,11 +245,6 @@
<artifactId>spring-orm</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
@ -280,6 +275,11 @@
<artifactId>spring-security-web</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>

@ -0,0 +1,124 @@
/*
* Copyright 2012-2018 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.boot.webservices.client;
import java.lang.reflect.Method;
import java.util.function.Supplier;
import org.springframework.boot.web.client.ClientHttpRequestFactorySupplier;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.ws.transport.WebServiceMessageSender;
import org.springframework.ws.transport.http.ClientHttpRequestMessageSender;
/**
* {@link WebServiceMessageSender} builder that can detect a suitable HTTP library based
* on the classpath.
*
* @author Stephane Nicoll
* @since 2.1.0
*/
public class HttpWebServiceMessageSenderBuilder {
private Integer connectionTimeout;
private Integer readTimeout;
private Supplier<ClientHttpRequestFactory> requestFactorySupplier;
/**
* Set the connection timeout in milliseconds.
* @param connectionTimeout the connection timeout in milliseconds
* @return a new builder instance
*/
public HttpWebServiceMessageSenderBuilder setConnectionTimeout(
int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
return this;
}
/**
* Set the read timeout in milliseconds.
* @param readTimeout the read timeout in milliseconds
* @return a new builder instance
*/
public HttpWebServiceMessageSenderBuilder setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
/**
* Set the {@code Supplier} of {@link ClientHttpRequestFactory} that should be called
* to create the HTTP-based {@link WebServiceMessageSender}.
* @param requestFactorySupplier the supplier for the request factory
* @return a new builder instance
*/
public HttpWebServiceMessageSenderBuilder requestFactory(
Supplier<ClientHttpRequestFactory> requestFactorySupplier) {
Assert.notNull(requestFactorySupplier,
"RequestFactory Supplier must not be null");
this.requestFactorySupplier = requestFactorySupplier;
return this;
}
public WebServiceMessageSender build() {
ClientHttpRequestFactory requestFactory = (this.requestFactorySupplier != null
? this.requestFactorySupplier.get()
: new ClientHttpRequestFactorySupplier().get());
if (this.connectionTimeout != null) {
new TimeoutRequestFactoryCustomizer(this.connectionTimeout,
"setConnectTimeout").customize(requestFactory);
}
if (this.readTimeout != null) {
new TimeoutRequestFactoryCustomizer(this.readTimeout, "setReadTimeout")
.customize(requestFactory);
}
return new ClientHttpRequestMessageSender(requestFactory);
}
/**
* {@link ClientHttpRequestFactory} customizer to call a "set timeout" method.
*/
private static class TimeoutRequestFactoryCustomizer {
private final int timeout;
private final String methodName;
TimeoutRequestFactoryCustomizer(int timeout, String methodName) {
this.timeout = timeout;
this.methodName = methodName;
}
public void customize(ClientHttpRequestFactory factory) {
ReflectionUtils.invokeMethod(findMethod(factory), factory, this.timeout);
}
private Method findMethod(ClientHttpRequestFactory factory) {
Method method = ReflectionUtils.findMethod(factory.getClass(),
this.methodName, int.class);
if (method != null) {
return method;
}
throw new IllegalStateException("Request factory " + factory.getClass()
+ " does not have a " + this.methodName + "(int) method");
}
}
}

@ -22,6 +22,7 @@ import org.springframework.ws.client.core.WebServiceTemplate;
* Callback interface that can be used to customize a {@link WebServiceTemplate}.
*
* @author Dmytro Nosan
* @since 2.1.0
*/
public interface WebServiceTemplateCustomizer {

@ -17,5 +17,4 @@
/**
* Web Services client utilities.
*/
package org.springframework.boot.webservices.client;

@ -22,61 +22,52 @@ import org.junit.runner.RunWith;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.transport.WebServiceMessageSender;
import org.springframework.ws.transport.http.ClientHttpRequestMessageSender;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WebServiceTemplateBuilder}. This test class check that builder will
* create ClientHttpRequestMessageSender (OkHttp3ClientHttpRequestFactory) if apache
* client is not present in the classpath
* Tests for {@link HttpWebServiceMessageSenderBuilder} when Http Components is not
* available.
*
* @author Dmytro Nosan
* @author Stephane Nicoll
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions("httpclient-*.jar")
public class WebServiceTemplateBuilderOkHttp3ClientHttpRequestFactoryTests {
public class HttpWebServiceMessageSenderBuilderOkHttp3IntegrationTests {
private WebServiceTemplateBuilder builder = new WebServiceTemplateBuilder();
private final HttpWebServiceMessageSenderBuilder builder = new HttpWebServiceMessageSenderBuilder();
@Test
public void build() {
WebServiceTemplate webServiceTemplate = this.builder.build();
assertThat(webServiceTemplate.getMessageSenders()).hasSize(1);
assertThat(webServiceTemplate.getMessageSenders()[0])
.isInstanceOf(ClientHttpRequestMessageSender.class);
ClientHttpRequestMessageSender sender = (ClientHttpRequestMessageSender) webServiceTemplate
.getMessageSenders()[0];
assertThat(sender.getRequestFactory())
.isInstanceOf(OkHttp3ClientHttpRequestFactory.class);
public void buildUseOkHttp3ByDefault() {
WebServiceMessageSender messageSender = this.builder.build();
assertOkHttp3RequestFactory(messageSender);
}
@Test
public void setTimeout() {
OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory();
ClientHttpRequestMessageSender sender = new ClientHttpRequestMessageSender(
new BufferingClientHttpRequestFactory(factory));
this.builder.setConnectionTimeout(5000).setReadTimeout(2000)
.setWebServiceMessageSender(() -> sender).build();
public void buildWithCustomTimeouts() {
WebServiceMessageSender messageSender = this.builder.setConnectionTimeout(5000)
.setReadTimeout(2000).build();
OkHttp3ClientHttpRequestFactory factory = assertOkHttp3RequestFactory(
messageSender);
OkHttpClient client = (OkHttpClient) ReflectionTestUtils.getField(factory,
"client");
assertThat(client).isNotNull();
assertThat(client.connectTimeoutMillis()).isEqualTo(5000);
assertThat(client.readTimeoutMillis()).isEqualTo(2000);
}
private OkHttp3ClientHttpRequestFactory assertOkHttp3RequestFactory(
WebServiceMessageSender messageSender) {
assertThat(messageSender).isInstanceOf(ClientHttpRequestMessageSender.class);
ClientHttpRequestMessageSender sender = (ClientHttpRequestMessageSender) messageSender;
ClientHttpRequestFactory requestFactory = sender.getRequestFactory();
assertThat(requestFactory).isInstanceOf(OkHttp3ClientHttpRequestFactory.class);
return (OkHttp3ClientHttpRequestFactory) requestFactory;
}
}

@ -0,0 +1,71 @@
/*
* Copyright 2012-2018 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.boot.webservices.client;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.ws.transport.WebServiceMessageSender;
import org.springframework.ws.transport.http.ClientHttpRequestMessageSender;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link HttpWebServiceMessageSenderBuilder} when no preferred HTTP clients are
* available
*
* @author Stephane Nicoll
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions({ "httpclient-*.jar", "okhttp*.jar" })
public class HttpWebServiceMessageSenderBuilderSimpleIntegrationTests {
private final HttpWebServiceMessageSenderBuilder builder = new HttpWebServiceMessageSenderBuilder();
@Test
public void buildUseUseSimpleClientByDefault() {
WebServiceMessageSender messageSender = this.builder.build();
assertSimpleClientRequestFactory(messageSender);
}
@Test
public void buildWithCustomTimeouts() {
WebServiceMessageSender messageSender = this.builder.setConnectionTimeout(5000)
.setReadTimeout(2000).build();
SimpleClientHttpRequestFactory requestFactory = assertSimpleClientRequestFactory(
messageSender);
assertThat(ReflectionTestUtils.getField(requestFactory, "connectTimeout"))
.isEqualTo(5000);
assertThat(ReflectionTestUtils.getField(requestFactory, "readTimeout"))
.isEqualTo(2000);
}
private SimpleClientHttpRequestFactory assertSimpleClientRequestFactory(
WebServiceMessageSender messageSender) {
assertThat(messageSender).isInstanceOf(ClientHttpRequestMessageSender.class);
ClientHttpRequestMessageSender sender = (ClientHttpRequestMessageSender) messageSender;
ClientHttpRequestFactory requestFactory = sender.getRequestFactory();
assertThat(requestFactory).isInstanceOf(SimpleClientHttpRequestFactory.class);
return (SimpleClientHttpRequestFactory) requestFactory;
}
}

@ -0,0 +1,84 @@
/*
* Copyright 2012-2018 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.boot.webservices.client;
import org.apache.http.client.config.RequestConfig;
import org.junit.Test;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.ws.transport.WebServiceMessageSender;
import org.springframework.ws.transport.http.ClientHttpRequestMessageSender;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link HttpWebServiceMessageSenderBuilder}.
*
* @author Stephane Nicoll
*/
public class HttpWebServiceMessageSenderBuilderTests {
@Test
public void buildWithRequestFactorySupplier() {
ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
ClientHttpRequestMessageSender messageSender = build(
new HttpWebServiceMessageSenderBuilder()
.requestFactory(() -> requestFactory));
assertThat(messageSender.getRequestFactory()).isSameAs(requestFactory);
}
@Test
public void buildWithReadAndConnectTimeout() {
ClientHttpRequestMessageSender messageSender = build(
new HttpWebServiceMessageSenderBuilder()
.requestFactory(SimpleClientHttpRequestFactory::new)
.setConnectionTimeout(5000).setReadTimeout(2000));
SimpleClientHttpRequestFactory requestFactory = (SimpleClientHttpRequestFactory) messageSender
.getRequestFactory();
assertThat(ReflectionTestUtils.getField(requestFactory, "connectTimeout"))
.isEqualTo(5000);
assertThat(ReflectionTestUtils.getField(requestFactory, "readTimeout"))
.isEqualTo(2000);
}
@Test
public void buildUsesHttpComponentsBydefault() {
ClientHttpRequestMessageSender messageSender = build(
new HttpWebServiceMessageSenderBuilder().setConnectionTimeout(5000)
.setReadTimeout(2000));
ClientHttpRequestFactory requestFactory = messageSender.getRequestFactory();
assertThat(requestFactory)
.isInstanceOf(HttpComponentsClientHttpRequestFactory.class);
RequestConfig requestConfig = (RequestConfig) ReflectionTestUtils
.getField(requestFactory, "requestConfig");
assertThat(requestConfig).isNotNull();
assertThat(requestConfig.getConnectTimeout()).isEqualTo(5000);
assertThat(requestConfig.getSocketTimeout()).isEqualTo(2000);
}
private ClientHttpRequestMessageSender build(
HttpWebServiceMessageSenderBuilder builder) {
WebServiceMessageSender messageSender = builder.build();
assertThat(messageSender).isInstanceOf(ClientHttpRequestMessageSender.class);
return ((ClientHttpRequestMessageSender) messageSender);
}
}

@ -1,93 +0,0 @@
/*
* Copyright 2012-2018 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.boot.webservices.client;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.ws.transport.WebServiceMessageSender;
import org.springframework.ws.transport.http.ClientHttpRequestMessageSender;
/**
* Tests for
* {@link org.springframework.boot.webservices.client.WebServiceTemplateBuilder}.
*
* @author Dmytro Nosan
*/
public class WebServiceTemplateBuilderCustomsMessageSenderTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private WebServiceTemplateBuilder builder = new WebServiceTemplateBuilder();
@Test
public void unknownSenderReadTimeout() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("with 'readTimeout'. Please use a custom customizer.");
this.thrown.expectMessage("There is no way to customize");
this.builder.setReadTimeout(3000).setWebServiceMessageSender(
() -> Mockito.mock(WebServiceMessageSender.class)).build();
}
@Test
public void unknownSenderConnectionTimeout() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage(
"with 'connectionTimeout'. Please use a custom customizer.");
this.thrown.expectMessage("There is no way to customize");
this.builder.setConnectionTimeout(3000).setWebServiceMessageSender(
() -> Mockito.mock(WebServiceMessageSender.class)).build();
}
@Test
public void unknownRequestFactoryReadTimeout() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("with 'readTimeout'. Please use a custom customizer.");
this.thrown.expectMessage("There is no way to customize");
this.builder.setReadTimeout(3000)
.setWebServiceMessageSender(() -> new ClientHttpRequestMessageSender(
Mockito.mock(ClientHttpRequestFactory.class)))
.build();
}
@Test
public void unknownRequestFactoryConnectionTimeout() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage(
"with 'connectionTimeout'. Please use a custom customizer.");
this.thrown.expectMessage("There is no way to customize");
this.builder.setConnectionTimeout(3000)
.setWebServiceMessageSender(() -> new ClientHttpRequestMessageSender(
Mockito.mock(ClientHttpRequestFactory.class)))
.build();
}
@Test
public void shouldBuildWithoutTimeouts() {
this.builder.setWebServiceMessageSender(
() -> Mockito.mock(WebServiceMessageSender.class)).build();
}
}

@ -1,55 +0,0 @@
/*
* Copyright 2012-2018 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.boot.webservices.client;
import org.apache.http.client.config.RequestConfig;
import org.junit.Test;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.ws.transport.http.ClientHttpRequestMessageSender;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WebServiceTemplateBuilder}.
*
* @author Dmytro Nosan
*/
public class WebServiceTemplateBuilderHttpComponentsClientHttpRequestFactoryTests {
private WebServiceTemplateBuilder builder = new WebServiceTemplateBuilder();
@Test
public void setTimeout() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
ClientHttpRequestMessageSender sender = new ClientHttpRequestMessageSender(
new BufferingClientHttpRequestFactory(factory));
this.builder.setConnectionTimeout(5000).setReadTimeout(2000)
.setWebServiceMessageSender(() -> sender).build();
RequestConfig requestConfig = (RequestConfig) ReflectionTestUtils
.getField(factory, "requestConfig");
assertThat(requestConfig).isNotNull();
assertThat(requestConfig.getConnectTimeout()).isEqualTo(5000);
assertThat(requestConfig.getSocketTimeout()).isEqualTo(2000);
}
}

@ -1,67 +0,0 @@
/*
* Copyright 2012-2018 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.boot.webservices.client;
import org.apache.http.client.HttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.junit.Test;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.transport.http.HttpComponentsMessageSender;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for
* {@link org.springframework.boot.webservices.client.WebServiceTemplateBuilder}. This
* test class check that builder will create HttpComponents by default if apache client is
* present in the classpath.
*
* @author Dmytro Nosan
*/
@SuppressWarnings("deprecation")
public class WebServiceTemplateBuilderHttpComponentsMessageSenderTests {
private WebServiceTemplateBuilder builder = new WebServiceTemplateBuilder();
@Test
public void build() {
WebServiceTemplate webServiceTemplate = new WebServiceTemplateBuilder().build();
assertThat(webServiceTemplate.getMessageSenders()).hasSize(1);
assertThat(webServiceTemplate.getMessageSenders()[0])
.isInstanceOf(HttpComponentsMessageSender.class);
}
@Test
public void setTimeout() {
HttpComponentsMessageSender sender = new HttpComponentsMessageSender();
HttpClient httpClient = sender.getHttpClient();
this.builder.setConnectionTimeout(5000).setReadTimeout(2000)
.setWebServiceMessageSender(() -> sender).build();
assertThat(HttpConnectionParams.getConnectionTimeout(httpClient.getParams()))
.isEqualTo(5000);
assertThat(HttpConnectionParams.getSoTimeout(httpClient.getParams()))
.isEqualTo(2000);
}
}

@ -1,70 +0,0 @@
/*
* Copyright 2012-2018 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.boot.webservices.client;
import java.time.Duration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.transport.http.HttpUrlConnectionMessageSender;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WebServiceTemplateBuilder}. This test class check that builder will
* create HttpUrlConnectionMessageSender If Ok-http and Apache client are not present in
* the classpath.
*
* @author Dmytro Nosan
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions({ "httpclient-*.jar", "okhttp-*.jar" })
public class WebServiceTemplateBuilderHttpUrlConnectionMessageSenderTests {
private WebServiceTemplateBuilder builder = new WebServiceTemplateBuilder();
@Test
public void build() {
WebServiceTemplate webServiceTemplate = this.builder.build();
assertThat(webServiceTemplate.getMessageSenders()).hasSize(1);
assertThat(webServiceTemplate.getMessageSenders()[0])
.isInstanceOf(HttpUrlConnectionMessageSender.class);
}
@Test
public void setTimeout() {
HttpUrlConnectionMessageSender sender = new HttpUrlConnectionMessageSender();
this.builder.setConnectionTimeout(5000).setReadTimeout(2000)
.setWebServiceMessageSender(() -> sender).build();
assertThat(ReflectionTestUtils.getField(sender, "connectionTimeout"))
.isEqualTo(Duration.ofMillis(5000));
assertThat(ReflectionTestUtils.getField(sender, "readTimeout"))
.isEqualTo(Duration.ofMillis(2000));
}
}

@ -1,52 +0,0 @@
/*
* Copyright 2012-2018 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.boot.webservices.client;
import org.junit.Test;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.ws.transport.http.ClientHttpRequestMessageSender;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WebServiceTemplateBuilder}.
*
* @author Dmytro Nosan
*/
public class WebServiceTemplateBuilderSimpleClientHttpRequestFactoryTests {
private WebServiceTemplateBuilder builder = new WebServiceTemplateBuilder();
@Test
public void setTimeout() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
ClientHttpRequestMessageSender sender = new ClientHttpRequestMessageSender(
new BufferingClientHttpRequestFactory(factory));
this.builder.setConnectionTimeout(5000).setReadTimeout(2000)
.setWebServiceMessageSender(() -> sender).build();
assertThat(ReflectionTestUtils.getField(factory, "connectTimeout"))
.isEqualTo(5000);
assertThat(ReflectionTestUtils.getField(factory, "readTimeout")).isEqualTo(2000);
}
}

@ -19,502 +19,362 @@ package org.springframework.boot.webservices.client;
import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Supplier;
import java.util.Set;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.core.FaultMessageResolver;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.client.support.destination.DestinationProvider;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.soap.client.core.SoapFaultMessageResolver;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import org.springframework.ws.transport.WebServiceMessageSender;
import org.springframework.ws.transport.http.ClientHttpRequestMessageSender;
import org.springframework.ws.transport.http.HttpUrlConnectionMessageSender;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Tests for {@link WebServiceTemplateBuilder}.
*
* @author Stephane Nicoll
* @author Dmytro Nosan
*/
public class WebServiceTemplateBuilderTests {
private WebServiceTemplateBuilder builder = new WebServiceTemplateBuilder();
private final WebServiceTemplateBuilder builder = new WebServiceTemplateBuilder();
@Test
public void addInterceptors() {
ClientInterceptor f1 = Mockito.mock(ClientInterceptor.class);
ClientInterceptor f2 = Mockito.mock(ClientInterceptor.class);
@Rule
public ExpectedException thrown = ExpectedException.none();
WebServiceTemplate webServiceTemplate = this.builder.addInterceptors(f1)
.addInterceptors(f2).build();
@Mock
private WebServiceMessageSender messageSender;
assertThat(webServiceTemplate.getInterceptors()).containsExactlyInAnyOrder(f1,
f2);
}
@Test
public void addInterceptorsCollection() {
ClientInterceptor f1 = Mockito.mock(ClientInterceptor.class);
ClientInterceptor f2 = Mockito.mock(ClientInterceptor.class);
WebServiceTemplate webServiceTemplate = this.builder
.addInterceptors(Collections.singletonList(f1))
.addInterceptors(Collections.singleton(f2)).build();
assertThat(webServiceTemplate.getInterceptors()).containsExactlyInAnyOrder(f1,
f2);
@Mock
private ClientInterceptor interceptor;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void setInterceptors() {
ClientInterceptor f1 = Mockito.mock(ClientInterceptor.class);
ClientInterceptor f2 = Mockito.mock(ClientInterceptor.class);
WebServiceTemplate webServiceTemplate = this.builder.setInterceptors(f1)
.setInterceptors(f2).build();
assertThat(webServiceTemplate.getInterceptors()).doesNotContain(f1).contains(f2);
}
@Test
public void setInterceptorsCollection() {
ClientInterceptor f1 = Mockito.mock(ClientInterceptor.class);
ClientInterceptor f2 = Mockito.mock(ClientInterceptor.class);
WebServiceTemplate webServiceTemplate = this.builder
.setInterceptors(Collections.singletonList(f1))
.setInterceptors(Collections.singleton(f2)).build();
assertThat(webServiceTemplate.getInterceptors()).doesNotContain(f1).contains(f2);
public void createWithCustomizersShouldApplyCustomizers() {
WebServiceTemplateCustomizer customizer = mock(
WebServiceTemplateCustomizer.class);
WebServiceTemplate template = new WebServiceTemplateBuilder(customizer).build();
verify(customizer).customize(template);
}
@Test
public void addCustomizers() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
WebServiceTemplateCustomizer customizer = (ws) -> ws
.setMarshaller(jaxb2Marshaller);
WebServiceTemplateCustomizer customizer1 = (ws) -> ws
.setUnmarshaller(jaxb2Marshaller);
WebServiceTemplate webServiceTemplate = this.builder.addCustomizers(customizer)
.addCustomizers(customizer1).build();
assertThat(webServiceTemplate.getMarshaller()).isEqualTo(jaxb2Marshaller);
assertThat(webServiceTemplate.getUnmarshaller()).isEqualTo(jaxb2Marshaller);
public void buildShouldDetectHttpMessageSender() {
WebServiceTemplate webServiceTemplate = this.builder.build();
assertThat(webServiceTemplate.getMessageSenders()).hasSize(1);
WebServiceMessageSender messageSender = webServiceTemplate.getMessageSenders()[0];
assertHttpComponentsRequestFactory(messageSender);
}
@Test
public void addCustomizersCollection() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
WebServiceTemplateCustomizer customizer = (ws) -> ws
.setMarshaller(jaxb2Marshaller);
WebServiceTemplateCustomizer customizer1 = (ws) -> ws
.setUnmarshaller(jaxb2Marshaller);
public void detectHttpMessageSenderWhenFalseShouldDisableDetection() {
WebServiceTemplate webServiceTemplate = this.builder
.addCustomizers(Collections.singleton(customizer))
.addCustomizers(Collections.singletonList(customizer1)).build();
assertThat(webServiceTemplate.getMarshaller()).isEqualTo(jaxb2Marshaller);
assertThat(webServiceTemplate.getUnmarshaller()).isEqualTo(jaxb2Marshaller);
.detectHttpMessageSender(false).build();
assertThat(webServiceTemplate.getMessageSenders()).hasSize(1);
assertThat(webServiceTemplate.getMessageSenders()[0])
.isInstanceOf(HttpUrlConnectionMessageSender.class);
}
@Test
public void setCustomizers() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
WebServiceTemplateCustomizer customizer = (ws) -> ws
.setMarshaller(jaxb2Marshaller);
WebServiceTemplateCustomizer customizer1 = (ws) -> ws
.setUnmarshaller(jaxb2Marshaller);
WebServiceTemplate webServiceTemplate = this.builder.setCustomizers(customizer)
.setCustomizers(customizer1).build();
assertThat(webServiceTemplate.getMarshaller()).isNull();
assertThat(webServiceTemplate.getUnmarshaller()).isEqualTo(jaxb2Marshaller);
public void messageSendersWhenSendersAreAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("MessageSenders must not be null");
this.builder.messageSenders((WebServiceMessageSender[]) null);
}
@Test
public void setCustomizersCollection() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
WebServiceTemplateCustomizer customizer = (ws) -> ws
.setMarshaller(jaxb2Marshaller);
WebServiceTemplateCustomizer customizer1 = (ws) -> ws
.setUnmarshaller(jaxb2Marshaller);
WebServiceTemplate webServiceTemplate = this.builder
.setCustomizers(Collections.singleton(customizer))
.setCustomizers(Collections.singletonList(customizer1)).build();
assertThat(webServiceTemplate.getMarshaller()).isNull();
assertThat(webServiceTemplate.getUnmarshaller()).isEqualTo(jaxb2Marshaller);
public void messageSendersCollectionWhenSendersAreAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("MessageSenders must not be null");
this.builder.messageSenders((Collection<? extends WebServiceMessageSender>) null);
}
@Test
public void addWebServiceMessageSenders() {
WebServiceMessageSender sender = Mockito.mock(WebServiceMessageSender.class);
WebServiceMessageSender sender1 = Mockito.mock(WebServiceMessageSender.class);
WebServiceTemplate webServiceTemplate = this.builder
.addWebServiceMessageSenders(Collections.singleton(() -> sender))
.addWebServiceMessageSenders(Collections.singletonList(() -> sender1))
public void messageSendersShouldApply() {
WebServiceTemplate template = this.builder.messageSenders(this.messageSender)
.build();
assertThat(webServiceTemplate.getMessageSenders())
.containsExactlyInAnyOrder(sender, sender1);
assertThat(template.getMessageSenders()).containsOnly(this.messageSender);
}
@Test
public void setWebServiceMessageSenders() {
WebServiceMessageSender sender = Mockito.mock(WebServiceMessageSender.class);
WebServiceMessageSender sender1 = Mockito.mock(WebServiceMessageSender.class);
WebServiceTemplate webServiceTemplate = this.builder
.setWebServiceMessageSenders(Collections.singleton(() -> sender))
.setWebServiceMessageSenders(Collections.singletonList(() -> sender1))
.build();
assertThat(webServiceTemplate.getMessageSenders()).doesNotContain(sender)
.contains(sender1);
public void messageSendersShouldReplaceExisting() {
WebServiceTemplate template = this.builder
.messageSenders(new ClientHttpRequestMessageSender())
.messageSenders(this.messageSender).build();
assertThat(template.getMessageSenders()).containsOnly(this.messageSender);
}
@Test
public void addWebServiceMessageSenderClass() {
WebServiceTemplate webServiceTemplate = this.builder
.addWebServiceMessageSender(ClientHttpRequestMessageSender.class)
.addWebServiceMessageSender(HttpUrlConnectionMessageSender.class).build();
assertThat(webServiceTemplate.getMessageSenders()).hasSize(2);
assertThat(webServiceTemplate.getMessageSenders()[0])
.isInstanceOf(ClientHttpRequestMessageSender.class);
assertThat(webServiceTemplate.getMessageSenders()[1])
.isInstanceOf(HttpUrlConnectionMessageSender.class);
public void additionalMessageSendersWhenSendersAreAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("MessageSenders must not be null");
this.builder.additionalMessageSenders((WebServiceMessageSender[]) null);
}
@Test
public void setWebServiceMessageSenderClass() {
WebServiceTemplate webServiceTemplate = this.builder
.setWebServiceMessageSender(ClientHttpRequestMessageSender.class)
.setWebServiceMessageSender(HttpUrlConnectionMessageSender.class).build();
assertThat(webServiceTemplate.getMessageSenders()).hasSize(1);
assertThat(webServiceTemplate.getMessageSenders()[0])
.isInstanceOf(HttpUrlConnectionMessageSender.class);
public void additionalMessageSendersCollectionWhenSendersAreAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("MessageSenders must not be null");
this.builder.additionalMessageSenders(
(Collection<? extends WebServiceMessageSender>) null);
}
@Test
public void addWebServiceMessageSender() {
WebServiceMessageSender sender = Mockito.mock(WebServiceMessageSender.class);
WebServiceMessageSender sender1 = Mockito.mock(WebServiceMessageSender.class);
WebServiceTemplate webServiceTemplate = this.builder
.addWebServiceMessageSender(() -> sender)
.addWebServiceMessageSender(() -> sender1).build();
assertThat(webServiceTemplate.getMessageSenders())
.containsExactlyInAnyOrder(sender, sender1);
public void additionalMessageSendersShouldAddToExisting() {
ClientHttpRequestMessageSender httpMessageSender = new ClientHttpRequestMessageSender();
WebServiceTemplate template = this.builder.messageSenders(httpMessageSender)
.additionalMessageSenders(this.messageSender).build();
assertThat(template.getMessageSenders()).containsOnly(httpMessageSender,
this.messageSender);
}
@Test
public void setWebServiceMessageSender() {
WebServiceMessageSender sender = Mockito.mock(WebServiceMessageSender.class);
WebServiceMessageSender sender1 = Mockito.mock(WebServiceMessageSender.class);
WebServiceTemplate webServiceTemplate = this.builder
.setWebServiceMessageSender(() -> sender)
.setWebServiceMessageSender(() -> sender1).build();
assertThat(webServiceTemplate.getMessageSenders()).doesNotContain(sender)
.contains(sender1);
public void additionalMessageSendersShouldKeepDetectedHttpMessageSender() {
WebServiceTemplate template = this.builder
.additionalMessageSenders(this.messageSender).build();
assertThat(template.getMessageSenders()).contains(this.messageSender);
assertThat(template.getMessageSenders()).hasSize(2);
}
@Test
public void setCheckConnectionForFault() {
MockWebServiceTemplate webServiceTemplate = this.builder
.setCheckConnectionForFault(false).build(MockWebServiceTemplate.class);
assertThat(webServiceTemplate.isCheckConnectionForFault()).isFalse();
public void interceptorsWhenInterceptorsAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Interceptors must not be null");
this.builder.interceptors((ClientInterceptor[]) null);
}
@Test
public void setCheckConnectionForError() {
MockWebServiceTemplate webServiceTemplate = this.builder
.setCheckConnectionForError(false).build(MockWebServiceTemplate.class);
assertThat(webServiceTemplate.isCheckConnectionForError()).isFalse();
public void interceptorsCollectionWhenInterceptorsAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Interceptors must not be null");
this.builder.interceptors((Collection<? extends ClientInterceptor>) null);
}
@Test
public void setTransformerFactoryClass() {
MockWebServiceTemplate webServiceTemplate = this.builder
.setTransformerFactoryClass(SAXTransformerFactory.class)
.build(MockWebServiceTemplate.class);
assertThat(webServiceTemplate.getTransformerFactoryClass())
.isEqualTo(SAXTransformerFactory.class);
public void interceptorsShouldApply() {
WebServiceTemplate template = this.builder.interceptors(this.interceptor).build();
assertThat(template.getInterceptors()).containsOnly(this.interceptor);
}
@Test
public void setWebServiceMessageFactory() {
SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
WebServiceTemplate webServiceTemplate = this.builder
.setWebServiceMessageFactory(messageFactory).build();
assertThat(webServiceTemplate.getMessageFactory()).isEqualTo(messageFactory);
public void interceptorsShouldReplaceExisting() {
WebServiceTemplate template = this.builder
.interceptors(mock(ClientInterceptor.class))
.interceptors(Collections.singleton(this.interceptor)).build();
assertThat(template.getInterceptors()).containsOnly(this.interceptor);
}
@Test
public void setMarshaller() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
WebServiceTemplate webServiceTemplate = this.builder
.setMarshaller(jaxb2Marshaller).build();
assertThat(webServiceTemplate.getMarshaller()).isEqualTo(jaxb2Marshaller);
public void additionalInterceptorsWhenInterceptorsAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Interceptors must not be null");
this.builder.additionalInterceptors((ClientInterceptor[]) null);
}
@Test
public void setUnmarshaller() {
Jaxb2Marshaller jaxb2Unmarshaller = new Jaxb2Marshaller();
WebServiceTemplate webServiceTemplate = this.builder
.setUnmarshaller(jaxb2Unmarshaller).build();
assertThat(webServiceTemplate.getUnmarshaller()).isEqualTo(jaxb2Unmarshaller);
public void additionalInterceptorsCollectionWhenInterceptorsAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Interceptors must not be null");
this.builder.additionalInterceptors((Set<ClientInterceptor>) null);
}
@Test
public void setFaultMessageResolver() {
FaultMessageResolver faultMessageResolver = new SoapFaultMessageResolver();
WebServiceTemplate webServiceTemplate = this.builder
.setFaultMessageResolver(faultMessageResolver).build();
assertThat(webServiceTemplate.getFaultMessageResolver())
.isEqualTo(faultMessageResolver);
public void additionalInterceptorsShouldAddToExisting() {
ClientInterceptor interceptor = mock(ClientInterceptor.class);
WebServiceTemplate template = this.builder.interceptors(interceptor)
.additionalInterceptors(this.interceptor).build();
assertThat(template.getInterceptors()).containsOnly(interceptor,
this.interceptor);
}
@Test
public void setDefaultUri() {
URI uri = URI.create("http://localhost:8080");
WebServiceTemplate webServiceTemplate = this.builder.setDefaultUri(uri.toString())
.build();
assertThat(webServiceTemplate.getDestinationProvider().getDestination())
.isEqualTo(uri);
}
@Test
public void setDestinationProvider() {
URI uri = URI.create("http://localhost:8080");
WebServiceTemplate webServiceTemplate = this.builder
.setDestinationProvider(() -> uri).build();
assertThat(webServiceTemplate.getDestinationProvider().getDestination())
.isEqualTo(uri);
}
@Test
public void shouldNotOverrideDefaultSender() {
WebServiceMessageSender sender = Mockito.mock(WebServiceMessageSender.class);
WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
webServiceTemplate.setMessageSender(sender);
this.builder.detectWebServiceMessageSender(false).configure(webServiceTemplate);
assertThat(webServiceTemplate.getMessageSenders()).hasSize(1).contains(sender);
}
@Test
public void addInterceptorsToExistingWebServiceTemplate() {
public void additionalInterceptorsShouldAddToExistingWebServiceTemplate() {
ClientInterceptor f1 = Mockito.mock(ClientInterceptor.class);
ClientInterceptor f2 = Mockito.mock(ClientInterceptor.class);
WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
webServiceTemplate.setInterceptors(new ClientInterceptor[] { f1 });
this.builder.addInterceptors(f2).configure(webServiceTemplate);
this.builder.additionalInterceptors(f2).configure(webServiceTemplate);
assertThat(webServiceTemplate.getInterceptors()).containsExactlyInAnyOrder(f2,
f1);
}
@Test(expected = IllegalArgumentException.class)
public void setInterceptorsArrayNull() {
this.builder.setInterceptors((ClientInterceptor[]) null).build();
}
@Test(expected = IllegalArgumentException.class)
public void setInterceptorsCollectionNull() {
this.builder.setInterceptors((Collection<? extends ClientInterceptor>) null)
.build();
}
@Test(expected = IllegalArgumentException.class)
public void addInterceptorsArrayNull() {
this.builder.addInterceptors((ClientInterceptor[]) null).build();
}
@Test(expected = IllegalArgumentException.class)
public void addInterceptorsCollectionNull() {
this.builder.addInterceptors((Collection<? extends ClientInterceptor>) null)
.build();
}
@Test(expected = IllegalArgumentException.class)
public void setCustomizersArrayNull() {
this.builder.setCustomizers((WebServiceTemplateCustomizer[]) null).build();
@Test
public void customizersWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Customizers must not be null");
this.builder.customizers((WebServiceTemplateCustomizer[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void setCustomizersCollectionNull() {
@Test
public void customizersCollectionWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Customizers must not be null");
this.builder
.setCustomizers((Collection<? extends WebServiceTemplateCustomizer>) null)
.build();
.customizers((Collection<? extends WebServiceTemplateCustomizer>) null);
}
@Test(expected = IllegalArgumentException.class)
public void addCustomizersArrayNull() {
this.builder
.addCustomizers((Collection<? extends WebServiceTemplateCustomizer>) null)
.build();
@Test
public void customizersShouldApply() {
WebServiceTemplateCustomizer customizer = mock(
WebServiceTemplateCustomizer.class);
WebServiceTemplate template = this.builder.customizers(customizer).build();
verify(customizer).customize(template);
}
@Test(expected = IllegalArgumentException.class)
public void addCustomizersCollectionNull() {
@Test
public void customizersShouldBeAppliedLast() {
WebServiceTemplate template = spy(new WebServiceTemplate());
this.builder
.addCustomizers((Collection<? extends WebServiceTemplateCustomizer>) null)
.build();
.additionalCustomizers(((webServiceTemplate) -> verify(webServiceTemplate)
.setMessageSenders(any())));
this.builder.configure(template);
}
@Test(expected = IllegalArgumentException.class)
public void setWebServiceMessageSendersNull() {
this.builder.setWebServiceMessageSenders(null).build();
@Test
public void customizersShouldReplaceExisting() {
WebServiceTemplateCustomizer customizer1 = mock(
WebServiceTemplateCustomizer.class);
WebServiceTemplateCustomizer customizer2 = mock(
WebServiceTemplateCustomizer.class);
WebServiceTemplate template = this.builder.customizers(customizer1)
.customizers(Collections.singleton(customizer2)).build();
verifyZeroInteractions(customizer1);
verify(customizer2).customize(template);
}
@Test(expected = IllegalArgumentException.class)
public void addWebServiceMessageSendersNull() {
this.builder.addWebServiceMessageSenders(null).build();
@Test
public void additionalCustomizersWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Customizers must not be null");
this.builder.additionalCustomizers((WebServiceTemplateCustomizer[]) null);
}
@Test(expected = IllegalArgumentException.class)
public void setWebServiceMessageSenderClassNull() {
this.builder.setWebServiceMessageSender(
(Class<? extends WebServiceMessageSender>) null).build();
@Test
public void additionalCustomizersCollectionWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Customizers must not be null");
this.builder.additionalCustomizers(
(Collection<? extends WebServiceTemplateCustomizer>) null);
}
@Test(expected = IllegalArgumentException.class)
public void addWebServiceMessageSenderClassNull() {
this.builder.addWebServiceMessageSender(
(Class<? extends WebServiceMessageSender>) null).build();
@Test
public void additionalCustomizersShouldAddToExisting() {
WebServiceTemplateCustomizer customizer1 = mock(
WebServiceTemplateCustomizer.class);
WebServiceTemplateCustomizer customizer2 = mock(
WebServiceTemplateCustomizer.class);
WebServiceTemplate template = this.builder.customizers(customizer1)
.additionalCustomizers(customizer2).build();
verify(customizer1).customize(template);
verify(customizer2).customize(template);
}
@Test(expected = IllegalArgumentException.class)
public void setWebServiceMessageSenderSupplierNull() {
this.builder.setWebServiceMessageSender(
(Supplier<? extends WebServiceMessageSender>) null).build();
@Test
public void setCheckConnectionForFault() {
WebServiceTemplate template = mock(WebServiceTemplate.class);
this.builder.setCheckConnectionForFault(false).configure(template);
verify(template).setCheckConnectionForFault(false);
}
@Test(expected = IllegalArgumentException.class)
public void addWebServiceMessageSenderSupplierNull() {
this.builder.addWebServiceMessageSender(
(Supplier<? extends WebServiceMessageSender>) null).build();
}
@Test
public void setCheckConnectionForError() {
WebServiceTemplate template = mock(WebServiceTemplate.class);
this.builder.setCheckConnectionForError(false).configure(template);
verify(template).setCheckConnectionForError(false);
@Test(expected = IllegalArgumentException.class)
public void setWebServiceMessageFactoryNull() {
this.builder.setWebServiceMessageFactory(null).build();
}
@Test(expected = IllegalArgumentException.class)
public void setUnmarshallerNull() {
this.builder.setUnmarshaller(null).build();
@Test
public void setTransformerFactoryClass() {
WebServiceTemplate template = mock(WebServiceTemplate.class);
this.builder.setTransformerFactoryClass(SAXTransformerFactory.class)
.configure(template);
verify(template).setTransformerFactoryClass(SAXTransformerFactory.class);
}
@Test(expected = IllegalArgumentException.class)
public void setMarshallerNull() {
this.builder.setMarshaller(null).build();
@Test
public void setWebServiceMessageFactory() {
WebServiceMessageFactory messageFactory = mock(WebServiceMessageFactory.class);
WebServiceTemplate template = this.builder
.setWebServiceMessageFactory(messageFactory).build();
assertThat(template.getMessageFactory()).isEqualTo(messageFactory);
}
@Test(expected = IllegalArgumentException.class)
public void setTransformerFactoryClassNull() {
this.builder.setTransformerFactoryClass(null).build();
@Test
public void setMarshaller() {
Marshaller marshaller = mock(Marshaller.class);
WebServiceTemplate template = this.builder.setMarshaller(marshaller).build();
assertThat(template.getMarshaller()).isEqualTo(marshaller);
}
@Test(expected = IllegalArgumentException.class)
public void setDefaultUriNull() {
this.builder.setDefaultUri(null).build();
@Test
public void setUnmarshaller() {
Unmarshaller unmarshaller = mock(Unmarshaller.class);
WebServiceTemplate webServiceTemplate = this.builder.setUnmarshaller(unmarshaller)
.build();
assertThat(webServiceTemplate.getUnmarshaller()).isEqualTo(unmarshaller);
}
@Test(expected = IllegalArgumentException.class)
public void setDestinationProviderNull() {
this.builder.setDestinationProvider(null).build();
@Test
public void setFaultMessageResolver() {
FaultMessageResolver faultMessageResolver = mock(FaultMessageResolver.class);
WebServiceTemplate webServiceTemplate = this.builder
.setFaultMessageResolver(faultMessageResolver).build();
assertThat(webServiceTemplate.getFaultMessageResolver())
.isEqualTo(faultMessageResolver);
}
private static class MockWebServiceTemplate extends WebServiceTemplate {
private boolean checkConnectionForError;
private boolean checkConnectionForFault;
private Class<? extends TransformerFactory> transformerFactoryClass;
boolean isCheckConnectionForError() {
return this.checkConnectionForError;
}
@Override
public void setCheckConnectionForError(boolean checkConnectionForError) {
this.checkConnectionForError = checkConnectionForError;
}
boolean isCheckConnectionForFault() {
return this.checkConnectionForFault;
}
@Override
public void setCheckConnectionForFault(boolean checkConnectionForFault) {
this.checkConnectionForFault = checkConnectionForFault;
}
Class<? extends TransformerFactory> getTransformerFactoryClass() {
return this.transformerFactoryClass;
}
@Override
public void setTransformerFactoryClass(
Class<? extends TransformerFactory> transformerFactoryClass) {
this.transformerFactoryClass = transformerFactoryClass;
}
@Test
public void setDefaultUri() {
URI uri = URI.create("http://localhost:8080");
WebServiceTemplate webServiceTemplate = this.builder.setDefaultUri(uri.toString())
.build();
assertThat(webServiceTemplate.getDestinationProvider().getDestination())
.isEqualTo(uri);
}
@Test
public void setDestinationProvider() {
DestinationProvider destinationProvider = () -> URI
.create("http://localhost:8080");
WebServiceTemplate webServiceTemplate = this.builder
.setDestinationProvider(destinationProvider).build();
assertThat(webServiceTemplate.getDestinationProvider())
.isEqualTo(destinationProvider);
}
private void assertHttpComponentsRequestFactory(
WebServiceMessageSender messageSender) {
assertThat(messageSender).isInstanceOf(ClientHttpRequestMessageSender.class);
ClientHttpRequestMessageSender sender = (ClientHttpRequestMessageSender) messageSender;
ClientHttpRequestFactory requestFactory = sender.getRequestFactory();
assertThat(requestFactory)
.isInstanceOf(HttpComponentsClientHttpRequestFactory.class);
}
}

Loading…
Cancel
Save