Skip to content

Support loading WebApplicationContexts in the TCF #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,47 @@

package org.springframework.test.web.mock.servlet.samples.context;

import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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.mock.servlet.MockMvc;
import org.springframework.test.web.mock.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

/**
* Tests with Java configuration.
*
* The TestContext framework doesn't support WebApplicationContext yet:
* https://jira.springsource.org/browse/SPR-5243
*
* A custom {@link ContextLoader} is used to load the WebApplicationContext.
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=WebContextLoader.class, classes={WebConfig.class})
@WebAppConfiguration("src/test/resources/META-INF/web-resources")
@ContextConfiguration(classes = WebConfig.class)
public class JavaTestContextTests {

@Autowired
private WebApplicationContext wac;

private MockMvc mockMvc;


@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}

@Test
public void tilesDefinitions() throws Exception {
this.mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp"));
this.mockMvc.perform(get("/"))//
.andExpect(status().isOk())//
.andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* 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.mock.servlet.samples.context;

import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.get;
Expand All @@ -34,6 +35,7 @@
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
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.mock.servlet.MockMvc;
import org.springframework.test.web.mock.servlet.MvcResult;
import org.springframework.test.web.mock.servlet.ResultMatcher;
Expand All @@ -44,32 +46,25 @@
/**
* Basic example that includes Spring Security configuration.
*
* <p>Note that currently there are no {@link ResultMatcher}' built specifically
* for asserting the Spring Security context. However, it's quite easy to put
* them together as shown below and Spring Security extensions will become
* available in the near future.
* <p>Note that currently there are no {@linkplain ResultMatcher ResultMatchers}
* built specifically for asserting the Spring Security context. However, it's
* quite easy to put them together as shown below, and Spring Security extensions
* will become available in the near future.
*
* <p>This also demonstrates a custom {@link RequestPostProcessor} which authenticates
* a user to a particular {@link HttpServletRequest}.
*
* <p>Also see the Javadoc of {@link GenericWebContextLoader}, a class that
* provides temporary support for loading WebApplicationContext by extending
* the TestContext framework.
*
* @author Rob Winch
* @author Rossen Stoyanchev
* @author Sam Brannen
* @see SecurityRequestPostProcessors
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader=WebContextLoader.class,
value={
"classpath:org/springframework/test/web/mock/servlet/samples/context/security.xml",
"classpath:org/springframework/test/web/mock/servlet/samples/servlet-context.xml"
})
@WebAppConfiguration("src/test/resources/META-INF/web-resources")
@ContextConfiguration({ "security.xml", "../servlet-context.xml" })
public class SpringSecurityTests {

private static String SEC_CONTEXT_ATTR = HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY;
private static final String SEC_CONTEXT_ATTR = HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY;

@Autowired
private FilterChainProxy springSecurityFilterChain;
Expand All @@ -79,57 +74,67 @@ public class SpringSecurityTests {

private MockMvc mockMvc;


@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
.addFilters(this.springSecurityFilterChain).build();
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)//
.addFilters(this.springSecurityFilterChain)//
.build();
}

@Test
public void requiresAuthentication() throws Exception {
mockMvc.perform(get("/user"))
.andExpect(redirectedUrl("http://localhost/spring_security_login"));
mockMvc.perform(get("/user")).//
andExpect(redirectedUrl("http://localhost/spring_security_login"));
}

@Test
public void accessGranted() throws Exception {
this.mockMvc.perform(get("/").with(userDeatilsService("user")))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp"));
this.mockMvc.perform(get("/").//
with(userDeatilsService("user"))).//
andExpect(status().isOk()).//
andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp"));
}

@Test
public void accessDenied() throws Exception {
this.mockMvc.perform(get("/").with(user("user").roles("DENIED")))
.andExpect(status().isForbidden());
this.mockMvc.perform(get("/")//
.with(user("user").roles("DENIED")))//
.andExpect(status().isForbidden());
}

@Test
public void userAuthenticates() throws Exception {
final String username = "user";
mockMvc.perform(post("/j_spring_security_check").param("j_username", username).param("j_password", "password"))
.andExpect(redirectedUrl("/"))
.andExpect(new ResultMatcher() {
public void match(MvcResult mvcResult) throws Exception {
HttpSession session = mvcResult.getRequest().getSession();
SecurityContext securityContext = (SecurityContext) session.getAttribute(SEC_CONTEXT_ATTR);
Assert.assertEquals(securityContext.getAuthentication().getName(), username);
}
});
mockMvc.perform(post("/j_spring_security_check").//
param("j_username", username).//
param("j_password", "password")).//
andExpect(redirectedUrl("/")).//
andExpect(new ResultMatcher() {

public void match(MvcResult mvcResult) throws Exception {
HttpSession session = mvcResult.getRequest().getSession();
SecurityContext securityContext = (SecurityContext) session.getAttribute(SEC_CONTEXT_ATTR);
Assert.assertEquals(securityContext.getAuthentication().getName(), username);
}
});
}

@Test
public void userAuthenticateFails() throws Exception {
final String username = "user";
mockMvc.perform(post("/j_spring_security_check").param("j_username", username).param("j_password", "invalid"))
.andExpect(redirectedUrl("/spring_security_login?login_error"))
.andExpect(new ResultMatcher() {
public void match(MvcResult mvcResult) throws Exception {
HttpSession session = mvcResult.getRequest().getSession();
SecurityContext securityContext = (SecurityContext) session.getAttribute(SEC_CONTEXT_ATTR);
Assert.assertNull(securityContext);
}
});
mockMvc.perform(post("/j_spring_security_check").//
param("j_username", username).//
param("j_password", "invalid")).//
andExpect(redirectedUrl("/spring_security_login?login_error")).//
andExpect(new ResultMatcher() {

public void match(MvcResult mvcResult) throws Exception {
HttpSession session = mvcResult.getRequest().getSession();
SecurityContext securityContext = (SecurityContext) session.getAttribute(SEC_CONTEXT_ATTR);
Assert.assertNull(securityContext);
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,47 @@

package org.springframework.test.web.mock.servlet.samples.context;

import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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.mock.servlet.MockMvc;
import org.springframework.test.web.mock.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

/**
* Tests with XML configuration.
*
* The TestContext framework doesn't support WebApplicationContext yet:
* https://jira.springsource.org/browse/SPR-5243
*
* A custom {@link ContextLoader} is used to load the WebApplicationContext.
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader=WebContextLoader.class,
locations={"/org/springframework/test/web/mock/servlet/samples/servlet-context.xml"})
@WebAppConfiguration("src/test/resources/META-INF/web-resources")
@ContextConfiguration("../servlet-context.xml")
public class XmlTestContextTests {

@Autowired
private WebApplicationContext wac;

private MockMvc mockMvc;


@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}

@Test
public void tilesDefinitions() throws Exception {
this.mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp"));
this.mockMvc.perform(get("/"))//
.andExpect(status().isOk())//
.andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp"));
}

}

1 change: 1 addition & 0 deletions spring-test/.springBeans
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<configs>
<config>src/test/java/org/springframework/test/context/junit4/profile/xml/DefaultProfileXmlConfigTests-context.xml</config>
<config>src/test/java/org/springframework/test/context/junit4/aci/xml/MultipleInitializersXmlConfigTests-context.xml</config>
<config>src/test/resources/org/springframework/test/context/web/BasicXmlWacTests-config.xml</config>
</configs>
<configSets>
</configSets>
Expand Down
Loading