001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.web.servlet.htmlunit;
018
019import com.gargoylesoftware.htmlunit.WebClient;
020
021import org.springframework.test.web.servlet.MockMvc;
022import org.springframework.test.web.servlet.setup.MockMvcConfigurer;
023import org.springframework.util.Assert;
024import org.springframework.web.context.WebApplicationContext;
025
026/**
027 * {@code MockMvcWebClientBuilder} simplifies the creation of an HtmlUnit
028 * {@link WebClient} that delegates to a {@link MockMvc} instance.
029 *
030 * <p>The {@code MockMvc} instance used by the builder may be
031 * {@linkplain #mockMvcSetup supplied directly} or created transparently
032 * from a {@link #webAppContextSetup WebApplicationContext}.
033 *
034 * @author Rob Winch
035 * @author Sam Brannen
036 * @since 4.2
037 * @see #mockMvcSetup(MockMvc)
038 * @see #webAppContextSetup(WebApplicationContext)
039 * @see #webAppContextSetup(WebApplicationContext, MockMvcConfigurer)
040 * @see #withDelegate(WebClient)
041 * @see #build()
042 */
043public class MockMvcWebClientBuilder extends MockMvcWebConnectionBuilderSupport<MockMvcWebClientBuilder> {
044
045        private WebClient webClient;
046
047
048        protected MockMvcWebClientBuilder(MockMvc mockMvc) {
049                super(mockMvc);
050        }
051
052        protected MockMvcWebClientBuilder(WebApplicationContext context) {
053                super(context);
054        }
055
056        protected MockMvcWebClientBuilder(WebApplicationContext context, MockMvcConfigurer configurer) {
057                super(context, configurer);
058        }
059
060
061        /**
062         * Create a new {@code MockMvcWebClientBuilder} based on the supplied
063         * {@link MockMvc} instance.
064         * @param mockMvc the {@code MockMvc} instance to use; never {@code null}
065         * @return the MockMvcWebClientBuilder to customize
066         */
067        public static MockMvcWebClientBuilder mockMvcSetup(MockMvc mockMvc) {
068                Assert.notNull(mockMvc, "MockMvc must not be null");
069                return new MockMvcWebClientBuilder(mockMvc);
070        }
071
072        /**
073         * Create a new {@code MockMvcWebClientBuilder} based on the supplied
074         * {@link WebApplicationContext}.
075         * @param context the {@code WebApplicationContext} to create a {@link MockMvc}
076         * instance from; never {@code null}
077         * @return the MockMvcWebClientBuilder to customize
078         */
079        public static MockMvcWebClientBuilder webAppContextSetup(WebApplicationContext context) {
080                Assert.notNull(context, "WebApplicationContext must not be null");
081                return new MockMvcWebClientBuilder(context);
082        }
083
084        /**
085         * Create a new {@code MockMvcWebClientBuilder} based on the supplied
086         * {@link WebApplicationContext} and {@link MockMvcConfigurer}.
087         * @param context the {@code WebApplicationContext} to create a {@link MockMvc}
088         * instance from; never {@code null}
089         * @param configurer the {@code MockMvcConfigurer} to apply; never {@code null}
090         * @return the MockMvcWebClientBuilder to customize
091         */
092        public static MockMvcWebClientBuilder webAppContextSetup(WebApplicationContext context, MockMvcConfigurer configurer) {
093                Assert.notNull(context, "WebApplicationContext must not be null");
094                Assert.notNull(configurer, "MockMvcConfigurer must not be null");
095                return new MockMvcWebClientBuilder(context, configurer);
096        }
097
098        /**
099         * Supply the {@code WebClient} that the client {@linkplain #build built}
100         * by this builder should delegate to when processing
101         * non-{@linkplain WebRequestMatcher matching} requests.
102         * @param webClient the {@code WebClient} to delegate to for requests
103         * that do not match; never {@code null}
104         * @return this builder for further customization
105         * @see #build()
106         */
107        public MockMvcWebClientBuilder withDelegate(WebClient webClient) {
108                Assert.notNull(webClient, "WebClient must not be null");
109                webClient.setWebConnection(createConnection(webClient));
110                this.webClient = webClient;
111                return this;
112        }
113
114        /**
115         * Build the {@link WebClient} configured via this builder.
116         * <p>The returned client will use the configured {@link MockMvc} instance
117         * for processing any {@linkplain WebRequestMatcher matching} requests
118         * and a delegate {@code WebClient} for all other requests.
119         * <p>If a {@linkplain #withDelegate delegate} has been explicitly configured,
120         * it will be used; otherwise, a default {@code WebClient} will be configured
121         * as the delegate.
122         * @return the {@code WebClient} to use
123         * @see #withDelegate(WebClient)
124         */
125        public WebClient build() {
126                return (this.webClient != null ? this.webClient : withDelegate(new WebClient()).build());
127        }
128
129}