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

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

Introduction

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

Prototype

public MockServletContext() 

Source Link

Document

Create a new MockServletContext , using no base path and a DefaultResourceLoader (i.e.

Usage

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   w w w.jav  a2s.  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.web.flow.AuthenticationViaFormActionTests.java

@Test
public void testTestBindingWithCredentialsBinder() throws Exception {
    final MockRequestContext context = new MockRequestContext();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(),
            new MockHttpServletRequest(), new MockHttpServletResponse()));
    //      context.setLastEvent(new Event(this, "test"));

    final CredentialsBinder cb = new CredentialsBinder() {

        public void bind(HttpServletRequest request, Credentials credentials) {
            ((UsernamePasswordCredentials) credentials).setUsername("test2");
            ((UsernamePasswordCredentials) credentials).setPassword("test2");
        }//from  w  w w  .j  av  a 2  s . co  m

        public boolean supports(Class<?> clazz) {
            return true;
        }

    };
    this.action.setCredentialsBinder(cb);
    //     this.action.bindAndValidate(context);

    //       assertEquals(
    //           "test2",
    //           ((UsernamePasswordCredentials) context
    //               .getFlowScope().get(
    //                   "credentials")).getUsername());

}

From source file:org.settings4j.helper.web.Log4jConfigurationLoaderTest.java

private MockServletContext prepareServletContext(final String log4DefaultConfiguration,
        final String log4ConfigurationKey) {
    final StringBuffer propertiesString = new StringBuffer();
    if (StringUtils.isNotEmpty(log4DefaultConfiguration)) {
        propertiesString.append(log4ConfigurationKey);
        propertiesString.append("=");
        propertiesString.append(log4DefaultConfiguration);
    }/*from  ww  w .j  a  v  a2  s  .co  m*/

    final MockServletContext servletContext = new MockServletContext();
    servletContext.addInitParameter(DefaultPropertiesLoader.DEFAULT_PROPERTIES, propertiesString.toString());
    servletContext.addInitParameter(Log4jConfigurationLoader.LOG4J_CONFIG_SETTINGS4JKEY, log4ConfigurationKey);
    return servletContext;
}

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.  ja  v  a2 s. 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.sventon.appl.ApplicationTest.java

@Test
public void cleanupDeletedRepositoryConfigurationsDirectoryWhenDirContainsValidConfigFileName()
        throws Exception {

    //set up two repositories

    final MockServletContext servletContext = new MockServletContext();
    servletContext.setContextPath("sventon-test");
    configDirectory.setServletContext(servletContext);

    System.out.println("config dir: " + configDirectory.getConfigRootDirectory().getAbsolutePath());

    final File repos1 = new File(configDirectory.getRepositoriesDirectory(), "testrepos1");
    final File repos2 = new File(configDirectory.getRepositoriesDirectory(), "testrepos2");

    final Application application = new Application(configDirectory);
    application.setConfigurationFileName(CONFIG_FILE_NAME);

    final RepositoryConfiguration repositoryConfiguration1 = new RepositoryConfiguration("testrepos1");
    repositoryConfiguration1.setRepositoryUrl("http://localhost/1");
    repositoryConfiguration1.setUserCredentials(new Credentials("user1", "abc123"));
    repositoryConfiguration1.setCacheUsed(false);
    repositoryConfiguration1.setZippedDownloadsAllowed(false);

    final RepositoryConfiguration repositoryConfiguration2 = new RepositoryConfiguration("testrepos2");
    repositoryConfiguration2.setRepositoryUrl("http://localhost/2");
    repositoryConfiguration2.setUserCredentials(new Credentials("user2", "123abc"));
    repositoryConfiguration2.setCacheUsed(false);
    repositoryConfiguration2.setZippedDownloadsAllowed(false);

    application.addConfiguration(repositoryConfiguration1);
    application.addConfiguration(repositoryConfiguration2);

    application.persistRepositoryConfigurations();

    //File should now be written
    assertTrue(new File(repos1, CONFIG_FILE_NAME).exists());
    assertTrue(new File(repos2, CONFIG_FILE_NAME).exists());

    File[] files = application.getBackupConfigDirectories();
    assertEquals("No files should be marked for deletion", 0, files.length);

    application.cleanupOldConfigDirectories(application.getBackupConfigDirectories());
    assertTrue(new File(repos1, CONFIG_FILE_NAME).exists()); //Dir not deleted, the props file had a valid name
    assertTrue(new File(repos2, CONFIG_FILE_NAME).exists());

}

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);/*from   w w  w  .j a va2s  . 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 ww  .  j av 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);
    System.out.println(response.getRedirectedUrl());
}

From source file:com.liferay.frontend.js.loader.modules.extender.internal.JSLoaderModulesServletTest.java

protected JSLoaderModulesServlet buildJSLoaderModulesServlet(Map<String, Object> properties) throws Exception {

    JSLoaderModulesServlet jsLoaderModulesServlet = new JSLoaderModulesServlet();

    ReflectionTestUtil.setFieldValue(jsLoaderModulesServlet, "_portal", PortalUtil.getPortal());

    jsLoaderModulesServlet.activate(mock(ComponentContext.class), properties);

    MockServletContext mockServletContext = new MockServletContext();

    mockServletContext.setContextPath("/loader");

    jsLoaderModulesServlet.init(new MockServletConfig(mockServletContext));

    jsLoaderModulesServlet.setDetails(Converter.cnv(Details.class, properties));

    JSLoaderModulesTracker jsLoaderModulesTracker = new JSLoaderModulesTracker();

    jsLoaderModulesTracker.setDetails(Converter.cnv(Details.class, properties));

    jsLoaderModulesServlet.setJSLoaderModulesTracker(jsLoaderModulesTracker);

    NPMRegistry npmRegistry = new NPMRegistryImpl();

    jsLoaderModulesServlet.setNPMRegistry(npmRegistry);

    return jsLoaderModulesServlet;
}

From source file:org.soybeanMilk.test.unit.web.TestWebGenericConverter.java

@Test
public void convert_servletContextToTarget_noConverter() throws Exception {
    GenericConvertException re = null;//  ww  w  . ja v  a  2s . c  o m

    try {
        converter.convert(new MockServletContext(), JavaBean.class);
    } catch (GenericConvertException e) {
        re = e;
    }

    Assert.assertTrue((re.getMessage().startsWith("can not find Converter for converting")));
}

From source file:org.soybeanMilk.test.unit.web.TestWebGenericConverter.java

@Test
public void convert_servletContextToTarget_hasConverter() throws Exception {
    final JavaBean bean = new JavaBean();

    converter.addConverter(ServletContext.class, JavaBean.class, new Converter() {
        @SuppressWarnings("unchecked")
        public <T> T convert(Object sourceObj, Type targetType) throws ConvertException {
            return (T) bean;
        }//from w  ww  . ja  v  a 2 s  .c  o  m
    });

    JavaBean re = converter.convert(new MockServletContext(), JavaBean.class);

    Assert.assertTrue((re == bean));
}