MockMvcHtmlUnitDriverBuilderTests.java 4.2 KB
Newer Older
1 2 3 4 5 6 7
/*
 * 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
 *
S
Sam Brannen 已提交
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15
 *
 * 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.
 */
S
Sam Brannen 已提交
16

17 18 19
package org.springframework.test.web.servlet.htmlunit.webdriver;

import java.io.IOException;
S
Sam Brannen 已提交
20

21 22 23 24 25
import javax.servlet.http.HttpServletRequest;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
S
Sam Brannen 已提交
26

27 28 29 30 31 32 33 34 35
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
36 37
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
38 39 40 41 42
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

S
Sam Brannen 已提交
43 44 45
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

46
/**
S
Sam Brannen 已提交
47 48
 * Integration tests for {@link MockMvcHtmlUnitDriverBuilder}.
 *
49
 * @author Rob Winch
S
Sam Brannen 已提交
50
 * @author Sam Brannen
S
Sam Brannen 已提交
51
 * @since 4.2
52 53 54 55 56 57
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class MockMvcHtmlUnitDriverBuilderTests {

S
Sam Brannen 已提交
58
	private static final String EXPECTED_BODY = "MockMvcHtmlUnitDriverBuilderTests mvc";
59 60

	@Autowired
S
Sam Brannen 已提交
61
	private WebApplicationContext wac;
62

S
Sam Brannen 已提交
63
	private MockMvc mockMvc;
64

S
Sam Brannen 已提交
65
	private HtmlUnitDriver driver;
66 67 68

	@Before
	public void setup() {
S
Sam Brannen 已提交
69
		this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
70 71 72 73 74 75 76 77 78 79 80 81 82
	}

	@Test(expected = IllegalArgumentException.class)
	public void mockMvcSetupNull() {
		MockMvcHtmlUnitDriverBuilder.mockMvcSetup(null);
	}

	@Test(expected = IllegalArgumentException.class)
	public void webAppContextSetupNull() {
		MockMvcHtmlUnitDriverBuilder.webAppContextSetup(null);
	}

	@Test
S
Sam Brannen 已提交
83
	public void mockMvcSetupAndConfigureDriver() throws Exception {
84 85
		Assume.group(TestGroup.PERFORMANCE);

S
Sam Brannen 已提交
86 87
		this.driver = MockMvcHtmlUnitDriverBuilder
				.mockMvcSetup(this.mockMvc)
88 89 90 91 92 93 94
				.configureDriver(new WebConnectionHtmlUnitDriver());

		assertMvcProcessed("http://localhost/test");
		assertDelegateProcessed("http://example.com/");
	}

	@Test
S
Sam Brannen 已提交
95
	public void mockMvcSetupAndCreateDriver() throws Exception {
96 97
		Assume.group(TestGroup.PERFORMANCE);

S
Sam Brannen 已提交
98 99
		this.driver = MockMvcHtmlUnitDriverBuilder
				.mockMvcSetup(this.mockMvc)
100 101 102 103 104 105 106
				.createDriver();

		assertMvcProcessed("http://localhost/test");
		assertDelegateProcessed("http://example.com/");
	}

	@Test
S
Sam Brannen 已提交
107 108 109
	public void javaScriptEnabledByDefault() {
		this.driver = MockMvcHtmlUnitDriverBuilder
				.mockMvcSetup(this.mockMvc)
110 111
				.createDriver();

S
Sam Brannen 已提交
112
		assertTrue(this.driver.isJavascriptEnabled());
113 114 115
	}

	@Test
S
Sam Brannen 已提交
116 117 118
	public void javaScriptDisabled() {
		this.driver = MockMvcHtmlUnitDriverBuilder
				.mockMvcSetup(this.mockMvc)
119 120 121
				.javascriptEnabled(false)
				.createDriver();

S
Sam Brannen 已提交
122
		assertFalse(this.driver.isJavascriptEnabled());
123 124 125 126 127 128 129 130 131 132 133
	}

	private void assertMvcProcessed(String url) throws Exception {
		assertThat(get(url), containsString(EXPECTED_BODY));
	}

	private void assertDelegateProcessed(String url) throws Exception {
		assertThat(get(url), not(containsString(EXPECTED_BODY)));
	}

	private String get(String url) throws IOException {
S
Sam Brannen 已提交
134 135
		this.driver.get(url);
		return this.driver.getPageSource();
136 137
	}

S
Sam Brannen 已提交
138

139 140 141
	@Configuration
	@EnableWebMvc
	static class Config {
S
Sam Brannen 已提交
142

143 144
		@RestController
		static class ContextPathController {
S
Sam Brannen 已提交
145

146 147 148 149 150 151 152 153
			@RequestMapping
			public String contextPath(HttpServletRequest request) {
				return EXPECTED_BODY;
			}
		}
	}

}