/*
 * Copyright 2002-2015 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.test.web.servlet.htmlunit.webdriver;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import org.springframework.util.Assert;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection;

/**
 * {@code WebConnectionHtmlUnitDriver} enables configuration of the
 * {@link WebConnection} for an {@link HtmlUnitDriver} instance.
 *
 * <p>This is useful because it allows a
 * {@link org.springframework.test.web.servlet.htmlunit.MockMvcWebConnection
 * MockMvcWebConnection} to be injected.
 *
 * @author Rob Winch
 * @author Sam Brannen
 * @since 4.2
 * @see MockMvcHtmlUnitDriverBuilder
 */
public class WebConnectionHtmlUnitDriver extends HtmlUnitDriver {

	private WebClient webClient;


	public WebConnectionHtmlUnitDriver(BrowserVersion browserVersion) {
		super(browserVersion);
	}

	public WebConnectionHtmlUnitDriver() {
	}

	public WebConnectionHtmlUnitDriver(boolean enableJavascript) {
		super(enableJavascript);
	}

	public WebConnectionHtmlUnitDriver(Capabilities capabilities) {
		super(capabilities);
	}

	/**
	 * Modify the supplied {@link WebClient}, {@linkplain #configureWebClient
	 * configure} it, and retain a reference to it so that its {@link WebConnection}
	 * is {@linkplain #getWebConnection accessible} for later use.
	 * @param webClient the client to modify
	 * @return the modified client
	 * @see org.openqa.selenium.htmlunit.HtmlUnitDriver#modifyWebClient(WebClient)
	 */
	@Override
	protected final WebClient modifyWebClient(WebClient webClient) {
		this.webClient = super.modifyWebClient(webClient);
		this.webClient = configureWebClient(this.webClient);
		return this.webClient;
	}

	/**
	 * Configure the supplied {@link WebClient}.
	 * <p>The default implementation simply returns the supplied client
	 * unmodified.
	 * <p>Subclasses can override this method to customize the {@code WebClient}
	 * that the {@link HtmlUnitDriver} driver uses.
	 * @param client the client to configure
	 * @return the configured client
	 */
	protected WebClient configureWebClient(WebClient client) {
		return client;
	}

	/**
	 * Access the current {@link WebConnection} for the {@link WebClient}.
	 * @return the current {@code WebConnection}
	 */
	public WebConnection getWebConnection() {
		return this.webClient.getWebConnection();
	}

	/**
	 * Set the {@link WebConnection} to be used with the {@link WebClient}.
	 * @param webConnection the {@code WebConnection} to use; never {@code null}
	 */
	public void setWebConnection(WebConnection webConnection) {
		Assert.notNull(webConnection, "WebConnection must not be null");
		this.webClient.setWebConnection(webConnection);
	}

}
@@ -1849,60 +1849,8 @@
 ent%7D
-, %7B@linkplain #configureWebClient%0A%09 * configure%7D it,
  and
@@ -1874,16 +1874,20 @@
 ce to it
+%0A%09 *
  so that
@@ -1912,20 +1912,16 @@
 nection%7D
-%0A%09 *
  is %7B@li
@@ -1945,16 +1945,20 @@
 nnection
+%0A%09 *
  accessi
@@ -1977,16 +1977,193 @@
 er use.%0A
+%09 * %3Cp%3EDelegates to %7B@link HtmlUnitDriver#modifyWebClient(WebClient)%7D%0A%09 * for default behavior and to %7B@link #modifyWebClientInternal(WebClient)%7D%0A%09 * for further customization.%0A
 %09 * @par
@@ -2241,37 +2241,8 @@
 see 
-org.openqa.selenium.htmlunit.
 Html
@@ -2279,16 +2279,61 @@
 Client)%0A
+%09 * @see #modifyWebClientInternal(WebClient)%0A
 %09 */%0A%09@O
@@ -2478,25 +2478,22 @@
 lient = 
-configure
+modify
 WebClien
@@ -2493,16 +2493,24 @@
 ebClient
+Internal
 (this.we
@@ -2561,17 +2561,14 @@
 %09 * 
-Configure
+Modify
  the
@@ -2798,15 +2798,8 @@
 ver%7D
- driver
  use
@@ -2812,17 +2812,20 @@
  @param 
-c
+webC
 lient th
@@ -2836,25 +2836,22 @@
 ient to 
-configure
+modify
 %0A%09 * @re
@@ -2859,24 +2859,22 @@
 urn the 
-configur
+modifi
 ed clien
@@ -2905,17 +2905,14 @@
 ent 
-configure
+modify
 WebC
@@ -2908,32 +2908,40 @@
  modifyWebClient
+Internal
 (WebClient clien
@@ -2935,17 +2935,20 @@
 bClient 
-c
+webC
 lient) %7B
@@ -2957,17 +2957,20 @@
 %09return 
-c
+webC
 lient;%0A%09

Rename configureWebClient() to modifyWebClientInternal() in HtmlUnit support