001/*
002 * Copyright 2002-2017 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.webdriver;
018
019import com.gargoylesoftware.htmlunit.BrowserVersion;
020import com.gargoylesoftware.htmlunit.WebClient;
021import com.gargoylesoftware.htmlunit.WebConnection;
022import org.openqa.selenium.Capabilities;
023import org.openqa.selenium.htmlunit.HtmlUnitDriver;
024
025import org.springframework.lang.Nullable;
026import org.springframework.util.Assert;
027
028/**
029 * {@code WebConnectionHtmlUnitDriver} enables configuration of the
030 * {@link WebConnection} for an {@link HtmlUnitDriver} instance.
031 *
032 * <p>This is useful because it allows a
033 * {@link org.springframework.test.web.servlet.htmlunit.MockMvcWebConnection
034 * MockMvcWebConnection} to be injected.
035 *
036 * @author Rob Winch
037 * @author Sam Brannen
038 * @since 4.2
039 * @see MockMvcHtmlUnitDriverBuilder
040 */
041public class WebConnectionHtmlUnitDriver extends HtmlUnitDriver {
042
043        @Nullable
044        private WebClient webClient;
045
046
047        public WebConnectionHtmlUnitDriver() {
048        }
049
050        public WebConnectionHtmlUnitDriver(BrowserVersion browserVersion) {
051                super(browserVersion);
052        }
053
054        public WebConnectionHtmlUnitDriver(boolean enableJavascript) {
055                super(enableJavascript);
056        }
057
058        public WebConnectionHtmlUnitDriver(Capabilities capabilities) {
059                super(capabilities);
060        }
061
062
063        /**
064         * Modify the supplied {@link WebClient} and retain a reference to it so that its
065         * {@link WebConnection} is {@linkplain #getWebConnection accessible} for later use.
066         * <p>Delegates to {@link HtmlUnitDriver#modifyWebClient} for default behavior
067         * and to {@link #modifyWebClientInternal} for further customization.
068         * @param webClient the client to modify
069         * @return the modified client
070         * @see HtmlUnitDriver#modifyWebClient(WebClient)
071         * @see #modifyWebClientInternal(WebClient)
072         */
073        @Override
074        protected final WebClient modifyWebClient(WebClient webClient) {
075                this.webClient = super.modifyWebClient(webClient);
076                this.webClient = modifyWebClientInternal(this.webClient);
077                return this.webClient;
078        }
079
080        /**
081         * Modify the supplied {@link WebClient}.
082         * <p>The default implementation simply returns the supplied client unmodified.
083         * <p>Subclasses can override this method to customize the {@code WebClient}
084         * that the {@link HtmlUnitDriver} uses.
085         * @param webClient the client to modify
086         * @return the modified client
087         */
088        protected WebClient modifyWebClientInternal(WebClient webClient) {
089                return webClient;
090        }
091
092        /**
093         * Return the current {@link WebClient}.
094         * @since 4.3
095         */
096        @Override
097        public WebClient getWebClient() {
098                Assert.state(this.webClient != null, "No WebClient set");
099                return this.webClient;
100        }
101
102        /**
103         * Set the {@link WebConnection} to be used with the {@link WebClient}.
104         * @param webConnection the {@code WebConnection} to use
105         */
106        public void setWebConnection(WebConnection webConnection) {
107                Assert.notNull(webConnection, "WebConnection must not be null");
108                getWebClient().setWebConnection(webConnection);
109        }
110
111        /**
112         * Access the current {@link WebConnection} for the {@link WebClient}.
113         * @return the current {@code WebConnection}
114         */
115        public WebConnection getWebConnection() {
116                return getWebClient().getWebConnection();
117        }
118
119}