List of usage examples for org.springframework.mock.web MockFilterConfig MockFilterConfig
public MockFilterConfig(@Nullable ServletContext servletContext, String filterName)
From source file:org.hdiv.filter.ValidatorFilterTest.java
public void testFilterCreation() { ValidatorFilter filter = new ValidatorFilter(); HttpServletRequest request = HDIVUtil.getHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); FilterConfig filterConfig = new MockFilterConfig(request.getSession().getServletContext(), "hdivFilter"); FilterChain filterChain = new MockFilterChain(); try {/*from w w w.j av a 2 s . c o m*/ filter.init(filterConfig); filter.doFilter(request, response, filterChain); // Validation error because is not start page String redirectUrl = response.getRedirectedUrl(); assertEquals(getConfig().getErrorPage(), redirectUrl); } catch (ServletException e) { e.printStackTrace(); assertTrue(false); } catch (IOException e) { e.printStackTrace(); assertTrue(false); } }
From source file:org.opennms.netmgt.ncs.rest.AbstractSpringJerseyRestTestCase.java
@Before public void setUp() throws Throwable { beforeServletStart();/*from w w w.j a v a 2s.com*/ setServletContext(new MockServletContext("file:src/main/webapp")); getServletContext().addInitParameter("contextConfigLocation", "file:src/main/resources/META-INF/opennms/component-service.xml"); getServletContext().addInitParameter("parentContextKey", "testDaoContext"); ServletContextEvent e = new ServletContextEvent(getServletContext()); setContextListener(new ContextLoaderListener()); getContextListener().contextInitialized(e); getServletContext().setContextPath(contextPath); setServletConfig(new MockServletConfig(getServletContext(), "dispatcher")); /* getServletConfig().addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig"); getServletConfig().addInitParameter("com.sun.jersey.config.property.packages", "org.opennms.netmgt.ncs.rest"); */ try { MockFilterConfig filterConfig = new MockFilterConfig(getServletContext(), "openSessionInViewFilter"); setFilter(new OpenSessionInViewFilter()); getFilter().init(filterConfig); setDispatcher(new SpringServlet()); getDispatcher().init(getServletConfig()); } catch (ServletException se) { throw se.getRootCause(); } setWebAppContext(WebApplicationContextUtils.getWebApplicationContext(getServletContext())); afterServletStart(); System.err.println("------------------------------------------------------------------------------"); }
From source file:org.opennms.core.test.rest.AbstractSpringJerseyRestTestCase.java
@Before public void setUp() throws Throwable { beforeServletStart();//from w w w .ja va 2 s . c om setUser("admin", new String[] { "ROLE_ADMIN" }); DaoTestConfigBean bean = new DaoTestConfigBean(); bean.afterPropertiesSet(); MockDatabase db = new MockDatabase(true); DataSourceFactory.setInstance(db); XADataSourceFactory.setInstance(db); try { MockFilterConfig filterConfig = new MockFilterConfig(servletContext, "openSessionInViewFilter"); setFilter(new OpenSessionInViewFilter()); getFilter().init(filterConfig); // Jersey /* setServletConfig(new MockServletConfig(servletContext, "dispatcher")); getServletConfig().addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig"); getServletConfig().addInitParameter("com.sun.jersey.config.property.packages", "org.codehaus.jackson.jaxrs;org.opennms.web.rest;org.opennms.web.rest.config"); getServletConfig().addInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter"); getServletConfig().addInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter"); setDispatcher(new SpringServlet()); getDispatcher().init(getServletConfig()); */ // Apache CXF setServletConfig(new MockServletConfig(servletContext, "dispatcher")); getServletConfig().addInitParameter("config-location", m_cxfContextPath); CXFServlet servlet = new CXFServlet(); setDispatcher(servlet); getDispatcher().init(getServletConfig()); } catch (ServletException se) { throw se.getRootCause(); } afterServletStart(); System.err.println("------------------------------------------------------------------------------"); }
From source file:org.opennms.web.rest.AbstractSpringJerseyRestTestCase.java
@Before public void setUp() throws Throwable { beforeServletStart();//from w ww .j av a2s .c o m DaoTestConfigBean bean = new DaoTestConfigBean(); bean.afterPropertiesSet(); MockDatabase db = new MockDatabase(true); DataSourceFactory.setInstance(db); setServletContext(new MockServletContext("file:src/main/webapp")); getServletContext().addInitParameter("contextConfigLocation", "classpath:/org/opennms/web/rest/applicationContext-test.xml " + "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml " + "classpath:/META-INF/opennms/applicationContext-soa.xml " + "classpath*:/META-INF/opennms/component-service.xml " + "classpath*:/META-INF/opennms/component-dao.xml " + "classpath:/META-INF/opennms/applicationContext-reportingCore.xml " + "classpath:/META-INF/opennms/applicationContext-databasePopulator.xml " + "classpath:/org/opennms/web/svclayer/applicationContext-svclayer.xml " + "classpath:/META-INF/opennms/applicationContext-mockEventProxy.xml " + "classpath:/applicationContext-jersey-test.xml " + "classpath:/META-INF/opennms/applicationContext-reporting.xml " + "classpath:/META-INF/opennms/applicationContext-mock-usergroup.xml " + "classpath:/META-INF/opennms/applicationContext-minimal-conf.xml " + "/WEB-INF/applicationContext-spring-security.xml " + "/WEB-INF/applicationContext-jersey.xml"); getServletContext().addInitParameter("parentContextKey", "daoContext"); ServletContextEvent e = new ServletContextEvent(getServletContext()); setContextListener(new ContextLoaderListener()); getContextListener().contextInitialized(e); getServletContext().setContextPath(contextPath); setServletConfig(new MockServletConfig(getServletContext(), "dispatcher")); getServletConfig().addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig"); getServletConfig().addInitParameter("com.sun.jersey.config.property.packages", "org.opennms.web.rest"); try { MockFilterConfig filterConfig = new MockFilterConfig(getServletContext(), "openSessionInViewFilter"); setFilter(new OpenSessionInViewFilter()); getFilter().init(filterConfig); setDispatcher(new SpringServlet()); getDispatcher().init(getServletConfig()); } catch (ServletException se) { throw se.getRootCause(); } setWebAppContext(WebApplicationContextUtils.getWebApplicationContext(getServletContext())); afterServletStart(); System.err.println("------------------------------------------------------------------------------"); }
From source file:org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilterTests.java
@Test public void testFilterProcessesUrlVariationsRespected() throws Exception { // Setup our HTTP request MockHttpServletRequest request = createMockAuthenticationRequest(); request.setServletPath("/j_OTHER_LOCATION"); request.setRequestURI("/mycontext/j_OTHER_LOCATION"); // Setup our filter configuration MockFilterConfig config = new MockFilterConfig(null, null); // Setup our expectation that the filter chain will not be invoked, as we redirect // to defaultTargetUrl MockFilterChain chain = new MockFilterChain(false); MockHttpServletResponse response = new MockHttpServletResponse(); // Setup our test object, to grant access MockAuthenticationFilter filter = new MockAuthenticationFilter(true); filter.setFilterProcessesUrl("/j_OTHER_LOCATION"); filter.setAuthenticationSuccessHandler(successHandler); // Test//from w w w .ja va 2s.c om filter.doFilter(request, response, chain); assertThat(response.getRedirectedUrl()).isEqualTo("/mycontext/logged_in.jsp"); assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull(); assertThat(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()) .isEqualTo("test"); }
From source file:org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilterTests.java
@Test public void testIgnoresAnyServletPathOtherThanFilterProcessesUrl() throws Exception { // Setup our HTTP request MockHttpServletRequest request = createMockAuthenticationRequest(); request.setServletPath("/some.file.html"); request.setRequestURI("/mycontext/some.file.html"); // Setup our filter configuration MockFilterConfig config = new MockFilterConfig(null, null); // Setup our expectation that the filter chain will be invoked, as our request is // for a page the filter isn't monitoring MockFilterChain chain = new MockFilterChain(true); MockHttpServletResponse response = new MockHttpServletResponse(); // Setup our test object, to deny access MockAuthenticationFilter filter = new MockAuthenticationFilter(false); // Test/* w w w . ja v a 2 s .com*/ filter.doFilter(request, response, chain); }
From source file:org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilterTests.java
@Test public void testNormalOperationWithDefaultFilterProcessesUrl() throws Exception { // Setup our HTTP request MockHttpServletRequest request = createMockAuthenticationRequest(); HttpSession sessionPreAuth = request.getSession(); // Setup our filter configuration MockFilterConfig config = new MockFilterConfig(null, null); // Setup our expectation that the filter chain will not be invoked, as we redirect // to defaultTargetUrl MockFilterChain chain = new MockFilterChain(false); MockHttpServletResponse response = new MockHttpServletResponse(); // Setup our test object, to grant access MockAuthenticationFilter filter = new MockAuthenticationFilter(true); filter.setFilterProcessesUrl("/j_mock_post"); filter.setSessionAuthenticationStrategy(mock(SessionAuthenticationStrategy.class)); filter.setAuthenticationSuccessHandler(successHandler); filter.setAuthenticationFailureHandler(failureHandler); filter.setAuthenticationManager(mock(AuthenticationManager.class)); filter.afterPropertiesSet();//from ww w .jav a 2s. com // Test filter.doFilter(request, response, chain); assertThat(response.getRedirectedUrl()).isEqualTo("/mycontext/logged_in.jsp"); assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull(); assertThat(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()) .isEqualTo("test"); // Should still have the same session assertThat(request.getSession()).isEqualTo(sessionPreAuth); }
From source file:org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilterTests.java
@Test public void testSuccessLoginThenFailureLoginResultsInSessionLosingToken() throws Exception { // Setup our HTTP request MockHttpServletRequest request = createMockAuthenticationRequest(); // Setup our filter configuration MockFilterConfig config = new MockFilterConfig(null, null); // Setup our expectation that the filter chain will not be invoked, as we redirect // to defaultTargetUrl MockFilterChain chain = new MockFilterChain(false); MockHttpServletResponse response = new MockHttpServletResponse(); // Setup our test object, to grant access MockAuthenticationFilter filter = new MockAuthenticationFilter(true); filter.setFilterProcessesUrl("/j_mock_post"); filter.setAuthenticationSuccessHandler(successHandler); // Test/*from w w w. ja va 2 s .c o m*/ filter.doFilter(request, response, chain); assertThat(response.getRedirectedUrl()).isEqualTo("/mycontext/logged_in.jsp"); assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull(); assertThat(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()) .isEqualTo("test"); // Now try again but this time have filter deny access // Setup our HTTP request // Setup our expectation that the filter chain will not be invoked, as we redirect // to authenticationFailureUrl chain = new MockFilterChain(false); response = new MockHttpServletResponse(); // Setup our test object, to deny access filter = new MockAuthenticationFilter(false); filter.setFilterProcessesUrl("/j_mock_post"); filter.setAuthenticationFailureHandler(failureHandler); // Test filter.doFilter(request, response, chain); assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); }
From source file:org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilterTests.java
@Test public void testSuccessfulAuthenticationInvokesSuccessHandlerAndSetsContext() throws Exception { // Setup our HTTP request MockHttpServletRequest request = createMockAuthenticationRequest(); // Setup our filter configuration MockFilterConfig config = new MockFilterConfig(null, null); // Setup our expectation that the filter chain will be invoked, as we want to go // to the location requested in the session MockFilterChain chain = new MockFilterChain(true); MockHttpServletResponse response = new MockHttpServletResponse(); // Setup our test object, to grant access MockAuthenticationFilter filter = new MockAuthenticationFilter(true); filter.setFilterProcessesUrl("/j_mock_post"); AuthenticationSuccessHandler successHandler = mock(AuthenticationSuccessHandler.class); filter.setAuthenticationSuccessHandler(successHandler); // Test// w w w . java 2 s. c om filter.doFilter(request, response, chain); verify(successHandler).onAuthenticationSuccess(any(HttpServletRequest.class), any(HttpServletResponse.class), any(Authentication.class)); assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull(); }
From source file:org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilterTests.java
@Test public void testFailedAuthenticationInvokesFailureHandler() throws Exception { // Setup our HTTP request MockHttpServletRequest request = createMockAuthenticationRequest(); // Setup our filter configuration MockFilterConfig config = new MockFilterConfig(null, null); // Setup our expectation that the filter chain will not be invoked, as we redirect // to authenticationFailureUrl MockFilterChain chain = new MockFilterChain(false); MockHttpServletResponse response = new MockHttpServletResponse(); // Setup our test object, to deny access MockAuthenticationFilter filter = new MockAuthenticationFilter(false); AuthenticationFailureHandler failureHandler = mock(AuthenticationFailureHandler.class); filter.setAuthenticationFailureHandler(failureHandler); // Test//from ww w .ja va2s .c o m filter.doFilter(request, response, chain); verify(failureHandler).onAuthenticationFailure(any(HttpServletRequest.class), any(HttpServletResponse.class), any(AuthenticationException.class)); assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); }