Example usage for org.springframework.mock.web MockServletContext addInitParameter

List of usage examples for org.springframework.mock.web MockServletContext addInitParameter

Introduction

In this page you can find the example usage for org.springframework.mock.web MockServletContext addInitParameter.

Prototype

public void addInitParameter(String name, String value) 

Source Link

Usage

From source file:org.jasig.cas.client.authentication.AuthenticationFilterTests.java

@Test
public void customRedirectStrategy() throws Exception {
    final AuthenticationFilter f = new AuthenticationFilter();
    final MockServletContext context = new MockServletContext();
    context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL);
    context.addInitParameter("service", CAS_SERVICE_URL);
    context.addInitParameter("authenticationRedirectStrategyClass",
            "org.jasig.cas.client.authentication.FacesCompatibleAuthenticationRedirectStrategy");
    f.init(new MockFilterConfig(context));
}

From source file:org.jasig.cas.client.authentication.AuthenticationFilterTests.java

@Test
public void testAllowsRenewContextParam() throws Exception {
    final AuthenticationFilter f = new AuthenticationFilter();
    final MockServletContext context = new MockServletContext();
    context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL);
    context.addInitParameter("service", CAS_SERVICE_URL);
    context.addInitParameter("renew", "true");
    f.init(new MockFilterConfig(context));
    final Field renewField = AuthenticationFilter.class.getDeclaredField("renew");
    renewField.setAccessible(true);//from  w ww  .jav a2 s .co  m
    assertTrue((Boolean) renewField.get(f));
}

From source file:org.jasig.cas.client.authentication.AuthenticationFilterTests.java

@Test
public void testIgnorePatterns() throws Exception {
    final AuthenticationFilter f = new AuthenticationFilter();
    final MockServletContext context = new MockServletContext();
    context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL);

    context.addInitParameter("ignorePattern", "=valueTo(\\w+)");
    context.addInitParameter("service", CAS_SERVICE_URL);
    f.init(new MockFilterConfig(context));

    final MockHttpServletRequest request = new MockHttpServletRequest();
    final String URL = CAS_SERVICE_URL + "?param=valueToIgnore";
    request.setRequestURI(URL);//from  w ww.  j  a va 2s  .  co  m

    final MockHttpSession session = new MockHttpSession();
    request.setSession(session);

    final MockHttpServletResponse response = new MockHttpServletResponse();

    final FilterChain filterChain = new FilterChain() {
        public void doFilter(ServletRequest request, ServletResponse response)
                throws IOException, ServletException {
        }
    };

    f.doFilter(request, response, filterChain);
    assertNull(response.getRedirectedUrl());
}

From source file:org.jasig.cas.client.authentication.AuthenticationFilterTests.java

@Test
public void testIgnorePatternsWithContainsMatching() throws Exception {
    final AuthenticationFilter f = new AuthenticationFilter();
    final MockServletContext context = new MockServletContext();
    context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL);

    context.addInitParameter("ignorePattern", "=valueToIgnore");
    context.addInitParameter("ignoreUrlPatternType", "CONTAINS");
    context.addInitParameter("service", CAS_SERVICE_URL);
    f.init(new MockFilterConfig(context));

    final MockHttpServletRequest request = new MockHttpServletRequest();
    final String URL = CAS_SERVICE_URL + "?param=valueToIgnore";
    request.setRequestURI(URL);/*from ww w. j  a  va 2  s .  c om*/

    final MockHttpSession session = new MockHttpSession();
    request.setSession(session);

    final MockHttpServletResponse response = new MockHttpServletResponse();

    final FilterChain filterChain = new FilterChain() {
        public void doFilter(ServletRequest request, ServletResponse response)
                throws IOException, ServletException {
        }
    };

    f.doFilter(request, response, filterChain);
    assertNull(response.getRedirectedUrl());
}

From source file:org.jasig.cas.client.authentication.AuthenticationFilterTests.java

@Test
public void testIgnorePatternsWithExactClassname() throws Exception {
    final AuthenticationFilter f = new AuthenticationFilter();
    final MockServletContext context = new MockServletContext();
    context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL);

    context.addInitParameter("ignorePattern", "=valueToIgnore");
    context.addInitParameter("ignoreUrlPatternType", ContainsPatternUrlPatternMatcherStrategy.class.getName());
    context.addInitParameter("service", CAS_SERVICE_URL);
    f.init(new MockFilterConfig(context));

    final MockHttpServletRequest request = new MockHttpServletRequest();
    final String URL = CAS_SERVICE_URL + "?param=valueToIgnore";
    request.setRequestURI(URL);/* www  . jav a  2 s .c  o m*/

    final MockHttpSession session = new MockHttpSession();
    request.setSession(session);

    final MockHttpServletResponse response = new MockHttpServletResponse();

    final FilterChain filterChain = new FilterChain() {
        public void doFilter(ServletRequest request, ServletResponse response)
                throws IOException, ServletException {
        }
    };

    f.doFilter(request, response, filterChain);
    assertNull(response.getRedirectedUrl());
}

From source file:org.jasig.cas.client.authentication.AuthenticationFilterTests.java

@Test
public void testIgnorePatternsWithInvalidClassname() throws Exception {
    final AuthenticationFilter f = new AuthenticationFilter();
    final MockServletContext context = new MockServletContext();
    context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL);

    context.addInitParameter("ignorePattern", "=valueToIgnore");
    context.addInitParameter("ignoreUrlPatternType", "unknown.class.name");
    context.addInitParameter("service", CAS_SERVICE_URL);
    f.init(new MockFilterConfig(context));

    final MockHttpServletRequest request = new MockHttpServletRequest();
    final String URL = CAS_SERVICE_URL + "?param=valueToIgnore";
    request.setRequestURI(URL);/*from   w  w w . ja va 2 s  . c om*/

    final MockHttpSession session = new MockHttpSession();
    request.setSession(session);

    final MockHttpServletResponse response = new MockHttpServletResponse();

    final FilterChain filterChain = new FilterChain() {
        public void doFilter(ServletRequest request, ServletResponse response)
                throws IOException, ServletException {
        }
    };

    f.doFilter(request, response, filterChain);
    System.out.println(response.getRedirectedUrl());
}

From source file:org.jasig.cas.client.authentication.AuthenticationFilterTests.java

@Test
public void testIgnorePatternsWithExactMatching() throws Exception {
    final AuthenticationFilter f = new AuthenticationFilter();
    final MockServletContext context = new MockServletContext();
    context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL);

    final URL url = new URL(CAS_SERVICE_URL + "?param=valueToIgnore");

    context.addInitParameter("ignorePattern", url.toExternalForm());
    context.addInitParameter("ignoreUrlPatternType", "EXACT");
    context.addInitParameter("service", CAS_SERVICE_URL);
    f.init(new MockFilterConfig(context));

    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme(url.getProtocol());
    request.setServerName(url.getHost());
    request.setServerPort(url.getPort());
    request.setQueryString(url.getQuery());
    request.setRequestURI(url.getPath());

    final MockHttpSession session = new MockHttpSession();
    request.setSession(session);// w  ww.j a  v  a  2s .  c  om

    final MockHttpServletResponse response = new MockHttpServletResponse();

    final FilterChain filterChain = new FilterChain() {
        public void doFilter(ServletRequest request, ServletResponse response)
                throws IOException, ServletException {
        }
    };

    f.doFilter(request, response, filterChain);
    assertNull(response.getRedirectedUrl());
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoaderTests.java

@Test
public void testRegisteredContextInitializerCanAccessServletContextParamsViaEnvironment() {
    MockServletContext sc = new MockServletContext("");
    // config file doesn't matter.  just a placeholder
    sc.addInitParameter(PortletContextLoader.CONFIG_LOCATION_PARAM,
            "/org/springframework/web/context/WEB-INF/empty-context.xml");

    sc.addInitParameter("someProperty", "someValue");
    sc.addInitParameter(PortletContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM,
            EnvApplicationContextInitializer.class.getName());
    PortletContextLoaderListener listener = new PortletContextLoaderListener();
    listener.contextInitialized(new ServletContextEvent(sc));

    //initialize the portlet application context, needed because PortletContextLoaderListener.contextInitialized doesn't actually create
    //the portlet app context due to lack of PortletContext reference
    MockPortletContext pc = new MockPortletContext(sc);
    PortletApplicationContextUtils2.getPortletApplicationContext(pc);
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoaderTests.java

@Test
public void testContextLoaderListenerWithUnkownContextInitializer() {
    MockServletContext sc = new MockServletContext("");
    // config file doesn't matter.  just a placeholder
    sc.addInitParameter(PortletContextLoader.CONFIG_LOCATION_PARAM,
            "/org/springframework/web/context/WEB-INF/empty-context.xml");
    sc.addInitParameter(PortletContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils
            .arrayToCommaDelimitedString(new Object[] { UnknownContextInitializer.class.getName() }));
    PortletContextLoaderListener listener = new PortletContextLoaderListener();
    listener.contextInitialized(new ServletContextEvent(sc));

    try {//from   w ww. j  a  v a2s  . c  om
        //initialize the portlet application context, needed because PortletContextLoaderListener.contextInitialized doesn't actually create
        //the portlet app context due to lack of PortletContext reference
        MockPortletContext pc = new MockPortletContext(sc);
        PortletApplicationContextUtils2.getPortletApplicationContext(pc);

        fail("expected exception");
    } catch (IllegalArgumentException ex) {
        assertTrue(ex.getMessage().contains("not assignable"));
    }
}