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:com.github.persapiens.jsfboot.mojarra.MojarraServletContextConfigurerIT.java

public void testConfigure() {
    ServletContext servletContext = new MockServletContext();

    MojarraServletContextConfigurer mojarraServletContextConfigurer = MojarraServletContextConfigurer.builder()
            .mojarraProperties(this.mojarraProperties).servletContext(servletContext).build();

    mojarraServletContextConfigurer.configure();

    assertThat(servletContext.getInitParameter(MojarraServletContextConfigurer.PREFFIX + ".clientStateTimeout"))
            .isEqualTo("10");
}

From source file:com.cloudera.oryx.lambda.serving.ModelManagerListenerIT.java

@Test
public void testListener() throws Exception {
    Map<String, Object> overlayConfig = new HashMap<>();
    overlayConfig.put("oryx.serving.model-manager-class", MockServingModelManager.class.getName());
    Config config = ConfigUtils.overlayOn(overlayConfig, getConfig());
    String serializedConfig = ConfigUtils.serialize(config);

    MockServletContext mockContext = new MockServletContext();
    mockContext.addInitParameter(ConfigUtils.class.getName() + ".serialized", serializedConfig);

    startMessaging();/*from  ww w.  j  ava2 s .  co m*/

    ModelManagerListener<?, ?, ?> listener = new ModelManagerListener<>();
    listener.init(mockContext);
    try {
        listener.contextInitialized(new ServletContextEvent(mockContext));
        ServingModelManager<?> manager = (ServingModelManager<?>) mockContext
                .getAttribute(ModelManagerListener.MANAGER_KEY);
        assertNotNull(manager);
        assertFalse(manager.isReadOnly());
        assertNotNull(manager.getConfig());
    } finally {
        listener.contextDestroyed(new ServletContextEvent(mockContext));
    }
}

From source file:com.github.persapiens.jsfboot.myfaces.MyfacesServletContextConfigurerIT.java

public void testConfigure() {
    ServletContext servletContext = new MockServletContext();

    MyfacesServletContextConfigurer myfacesServletContextConfigurer = MyfacesServletContextConfigurer.builder()
            .myfacesProperties(this.myfacesProperties).servletContext(servletContext).build();

    myfacesServletContextConfigurer.configure();

    assertThat(servletContext/*from   w  w w .  j  a  va  2 s  .  c o  m*/
            .getInitParameter(MyfacesServletContextConfigurer.PREFFIX + "STRICT_JSF_2_CC_EL_RESOLVER"))
                    .isEqualTo("myElResolver");
}

From source file:org.sventon.web.ctrl.SubmitConfigurationsControllerTest.java

@Before
public void setUp() throws Exception {
    configDirectory = TestUtils.getTestConfigDirectory();
    configDirectory.setCreateDirectories(false);
    final MockServletContext servletContext = new MockServletContext();
    servletContext.setContextPath("sventon-test");
    configDirectory.setServletContext(servletContext);
    application = new Application(configDirectory);
    application.setConfigurationFileName(TestUtils.CONFIG_FILE_NAME);
}

From source file:org.sventon.web.ctrl.ListRepositoriesControllerTest.java

@Before
public void setUp() throws Exception {
    ConfigDirectory configDirectory = TestUtils.getTestConfigDirectory();
    configDirectory.setCreateDirectories(false);
    final MockServletContext servletContext = new MockServletContext();
    servletContext.setContextPath("sventon-test");
    configDirectory.setServletContext(servletContext);
    application = new Application(configDirectory);

    final UserRepositoryContext context1 = new UserRepositoryContext();
    context1.setCredentials(new Credentials("UID1", "PWD1"));
    final UserRepositoryContext context2 = new UserRepositoryContext();
    context2.setCredentials(new Credentials("UID2", "PWD2"));
    userContext = new UserContext();

    final RepositoryName repo1 = new RepositoryName("repo1");
    final RepositoryName repo2 = new RepositoryName("repo2");

    userContext.add(repo1, context1);//from  ww  w  .  j a  va 2s .c om
    userContext.add(repo2, context2);
}

From source file:org.hdiv.web.servlet.tags.AbstractTagTests.java

protected MockPageContext createPageContext() {
    MockServletContext sc = new MockServletContext();
    SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
    wac.setServletContext(sc);//w ww  .  j a  va 2  s .c  om
    wac.setNamespace("test");
    wac.refresh();

    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();
    if (inDispatcherServlet()) {
        request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
        LocaleResolver lr = new AcceptHeaderLocaleResolver();
        request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
        ThemeResolver tr = new FixedThemeResolver();
        request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
        request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
    } else {
        sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    }

    return new MockPageContext(sc, request, response);
}

From source file:org.sventon.web.command.ConfigCommandValidatorTest.java

@Test
public void testValidate() throws Exception {
    final ConfigDirectory configDirectory = TestUtils.getTestConfigDirectory();
    configDirectory.setCreateDirectories(false);
    final MockServletContext servletContext = new MockServletContext();
    servletContext.setContextPath("sventon-test");
    configDirectory.setServletContext(servletContext);
    final Application application = new Application(configDirectory);

    final ConfigCommandValidator validator = new ConfigCommandValidator(false);
    validator.setApplication(application);

    final ConfigCommand command = new ConfigCommand();

    BindException exception = new BindException(command, "test");
    validator.validate(command, exception);

    // An empty command is valid
    assertEquals(0, exception.getAllErrors().size());

    // Invalid repository name
    command.setName("Illegal whitespace in name");
    command.setRepositoryUrl("svn://domain.com/svn/");
    command.setUserPassword("");
    command.setUserName("");
    validator.validate(command, exception);
    assertEquals(1, exception.getAllErrors().size());
    assertEquals("config.error.illegal-name", exception.getFieldError("name").getCode());
    exception = new BindException(command, "test");

    // Empty name is not ok
    command.setName("");
    command.setRepositoryUrl("svn://domain.com/svn/");
    command.setUserPassword("");
    command.setUserName("");
    validator.validate(command, exception);
    assertEquals(1, exception.getAllErrors().size());
    assertEquals("config.error.illegal-name", exception.getFieldError("name").getCode());
    exception = new BindException(command, "test");

    // Valid (typical) input
    command.setRepositoryUrl("svn://domain.com/svn/");
    command.setName("default");
    command.setUserPassword("");
    command.setUserName("");
    command.setCacheUserName("");
    command.setCacheUserPassword("");
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    // Valid input, spaces will be trimmed
    command.setRepositoryUrl(" svn://domain.com/svn/ ");
    command.setUserPassword("");
    command.setUserName("");
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    command.setUserPassword(null);//from w  w w  .ja v  a2  s . com
    command.setUserName(null);
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    command.setRepositoryUrl("");
    validator.validate(command, exception);
    assertEquals(1, exception.getAllErrors().size());
    assertEquals("config.error.illegal-url", exception.getFieldError("repositoryUrl").getCode());

    exception = new BindException(command, "test");
    command.setRepositoryUrl("notavalidurl");
    validator.validate(command, exception);
    assertEquals(1, exception.getAllErrors().size());
    assertEquals("config.error.illegal-url", exception.getFieldError("repositoryUrl").getCode());

    exception = new BindException(command, "test");
    command.setRepositoryUrl("svn://domain.com/svn/");
    command.setUserPassword("");
    command.setUserName("");
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());

    //if user based access is used, test connection uid and pwd can be supplied
    command.setRepositoryUrl("svn://domain.com/svn/");
    command.setName("default");
    command.setUserPassword("admin");
    command.setUserName("super-secret-pwd123");
    command.setAccessMethod(USER);
    command.setCacheUserName("");
    command.setCacheUserPassword("");
    validator.validate(command, exception);
    assertEquals(0, exception.getAllErrors().size());
}

From source file:newcontroller.handler.impl.DefaultRequestTest.java

@Test
public void testParam() throws Exception {
    HttpServletRequest request = MockMvcRequestBuilders.get("/").param("foo", "aaa")
            .buildRequest(new MockServletContext());
    Request req = new DefaultRequest(request);
    assertThat(req.param("foo").isPresent(), is(true));
    assertThat(req.param("foo").get(), is("aaa"));
    assertThat(req.param("bar").isPresent(), is(false));
}

From source file:org.joinfaces.richfaces.RichfacesServletContextInitializerIT.java

@Test
public void testOnStartup() throws ServletException {
    RichfacesServletContextInitializer bootsfacesServletContextInitializer = new RichfacesServletContextInitializer(
            this.bootsfacesProperties);

    ServletContext servletContext = new MockServletContext();

    bootsfacesServletContextInitializer.onStartup(servletContext);

    assertThat(servletContext.getInitParameter("org.richfaces.skin")).isEqualTo("This is a skin");
}

From source file:org.cloudfoundry.identity.uaa.integration.codestore.ExpiringCodeStoreMockMvcTests.java

@BeforeClass
public static void setUp() throws Exception {
    webApplicationContext = new XmlWebApplicationContext();
    webApplicationContext.setServletContext(new MockServletContext());
    webApplicationContext.setConfigLocation("file:./src/main/webapp/WEB-INF/spring-servlet.xml");
    new YamlServletProfileInitializer().initialize(webApplicationContext);
    webApplicationContext.refresh();/*  w  w  w  . j a  va  2s  .  co  m*/
    FilterChainProxy springSecurityFilterChain = webApplicationContext.getBean(FilterChainProxy.class);

    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).addFilter(springSecurityFilterChain)
            .build();
    testClient = new TestClient(mockMvc);
    loginToken = testClient.getClientCredentialsOAuthAccessToken("login", "loginsecret", "oauth.login");
}