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

@Test
public void testSuccessfulAuthenticationWithNoService() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockRequestContext context = new MockRequestContext();

    request.addParameter("username", "test");
    request.addParameter("password", "test");

    context.setExternalContext(// w  w w .ja v  a  2  s. c  o m
            new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    context.getRequestScope().put("credentials", TestUtils.getCredentialsWithSameUsernameAndPassword());
    //        this.action.bind(context);
    //        assertEquals("success", this.action.submit(context).getId());
}

From source file:com.xemantic.tadedon.guice.servlet.GuiceServletsTest.java

@Test
public void shouldGetProductionStageWhenFooParamProcutionIsSpecified() throws Exception {
    // given//from ww w  .jav  a  2 s.  c o m
    MockServletContext context = new MockServletContext();
    context.addInitParameter("fooParam", "PRODUCTION");

    // when
    Stage stage = GuiceServlets.getStage(context, "fooParam");

    // then
    assertThat(stage, is(Stage.PRODUCTION));
}

From source file:org.jasig.cas.web.flow.GenerateServiceTicketActionTests.java

@Test
public void testTicketGrantingTicketFromRequest() throws Exception {
    MockRequestContext context = new MockRequestContext();
    context.getFlowScope().put("service", TestUtils.getService());
    MockHttpServletRequest request = new MockHttpServletRequest();
    context.setExternalContext(/*w w  w.  j  a  v  a2s . c  o  m*/
            new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    request.addParameter("service", "service");
    WebUtils.putTicketGrantingTicketInRequestScope(context, this.ticketGrantingTicket);

    this.action.execute(context);

    assertNotNull(WebUtils.getServiceTicketFromRequestScope(context));
}

From source file:com.autentia.wuija.spring.RegisterWebSessionScopeTestContextLoader.java

@Override
protected void customizeContext(GenericWebApplicationContext context) {
    final MockServletContext servlet = new MockServletContext();
    context.setServletContext(servlet);/*from   w  w  w .  ja v  a 2  s  .  c  o m*/

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

    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

    context.getBeanFactory().registerScope("session", new SessionScope());
}

From source file:org.joinfaces.mock.JsfMock.java

public void init(ApplicationContext applicationContext) {
    this.mockFacesContext = FacesContextMocker.mockFacesContext();
    this.mockApplication = Mockito.mock(Application.class);
    this.mockViewRoot = Mockito.mock(UIViewRoot.class);
    this.mockExternalContext = Mockito.mock(ExternalContext.class);
    this.mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);
    this.mockHttpServletResponse = Mockito.mock(HttpServletResponse.class);
    this.mockHttpSession = Mockito.mock(HttpSession.class);
    this.mockTagConfig = Mockito.mock(TagConfig.class);
    this.mockFaceletHandler = new MockFaceletHandler();
    this.mockTagAttributes = new MockTagAttributes();
    this.mockTag = new Tag(null, null, null, null, this.mockTagAttributes);
    this.mockFaceletContext = new MockFaceletContext(this.mockFacesContext);

    this.mockViewMap = new HashMap<String, Object>();
    this.mockServletContext = new MockServletContext();
    if (applicationContext != null) {
        this.mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                applicationContext);//from www.j a  v a 2  s .com
    }

    Mockito.when(this.mockTagConfig.getNextHandler()).thenReturn(this.mockFaceletHandler);
    Mockito.when(this.mockTagConfig.getTag()).thenReturn(this.mockTag);

    Mockito.when(this.mockFacesContext.getApplication()).thenReturn(this.mockApplication);
    Mockito.when(this.mockApplication.getSupportedLocales()).thenReturn(createLocales().iterator());

    Mockito.when(this.mockFacesContext.getViewRoot()).thenReturn(this.mockViewRoot);
    Mockito.when(this.mockViewRoot.getLocale()).thenReturn(new Locale("en"));
    Mockito.when(this.mockViewRoot.getViewMap()).thenReturn(this.mockViewMap);

    Mockito.when(this.mockFacesContext.getExternalContext()).thenReturn(this.mockExternalContext);
    Mockito.when(this.mockExternalContext.getRequest()).thenReturn(this.mockHttpServletRequest);
    Mockito.when(this.mockHttpServletRequest.getSession()).thenReturn(this.mockHttpSession);
    Mockito.when(this.mockExternalContext.getResponse()).thenReturn(this.mockHttpServletResponse);
    Mockito.when(this.mockExternalContext.getContext()).thenReturn(this.mockServletContext);

    Map<String, String> requestMap = new HashMap<String, String>();
    Mockito.when(this.mockExternalContext.getRequestParameterMap()).thenReturn(requestMap);
}

From source file:com.github.persapiens.jsfboot.mock.JsfMock.java

public void init(ApplicationContext applicationContext) {
    this.mockFacesContext = FacesContextMocker.mockFacesContext();
    this.mockApplication = Mockito.mock(Application.class);
    this.mockViewRoot = Mockito.mock(UIViewRoot.class);
    this.mockExternalContext = Mockito.mock(ExternalContext.class);
    this.mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);
    this.mockHttpServletResponse = Mockito.mock(HttpServletResponse.class);
    this.mockHttpSession = Mockito.mock(HttpSession.class);
    this.mockTagConfig = Mockito.mock(TagConfig.class);
    this.mockFaceletHandler = new MockFaceletHandler();
    this.mockTagAttributes = new MockTagAttributes();
    this.mockTag = new Tag(null, null, null, null, this.mockTagAttributes);
    this.mockFaceletContext = new MockFaceletContext(this.mockFacesContext);

    this.mockViewMap = new HashMap<>();
    this.mockServletContext = new MockServletContext();
    if (applicationContext != null) {
        this.mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                applicationContext);//from w  ww  .  j ava  2 s  .c o m
    }

    Mockito.when(this.mockTagConfig.getNextHandler()).thenReturn(this.mockFaceletHandler);
    Mockito.when(this.mockTagConfig.getTag()).thenReturn(this.mockTag);

    Mockito.when(this.mockFacesContext.getApplication()).thenReturn(this.mockApplication);
    Mockito.when(this.mockApplication.getSupportedLocales()).thenReturn(createLocales().iterator());

    Mockito.when(this.mockFacesContext.getViewRoot()).thenReturn(this.mockViewRoot);
    Mockito.when(this.mockViewRoot.getLocale()).thenReturn(new Locale("en"));
    Mockito.when(this.mockViewRoot.getViewMap()).thenReturn(this.mockViewMap);

    Mockito.when(this.mockFacesContext.getExternalContext()).thenReturn(this.mockExternalContext);
    Mockito.when(this.mockExternalContext.getRequest()).thenReturn(this.mockHttpServletRequest);
    Mockito.when(this.mockHttpServletRequest.getSession()).thenReturn(this.mockHttpSession);
    Mockito.when(this.mockExternalContext.getResponse()).thenReturn(this.mockHttpServletResponse);
    Mockito.when(this.mockExternalContext.getContext()).thenReturn(this.mockServletContext);

    Map<String, String> requestMap = new HashMap<>();
    Mockito.when(this.mockExternalContext.getRequestParameterMap()).thenReturn(requestMap);
}

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

@Test
public void testPopulateFromParams() throws Exception {
    HttpServletRequest request = MockMvcRequestBuilders.get("/").param("foo", "aaa").param("bar", "100")
            .buildRequest(new MockServletContext());
    Request req = new DefaultRequest(request);
    Foo foo = req.params(Foo.class);
    assertThat(foo.getFoo(), is("aaa"));
    assertThat(foo.getBar(), is(100));/*from  www. j a v a  2s . c  o m*/
}

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

private WebObjectSource createWebObjectSource() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockServletContext application = new MockServletContext();

    WebObjectSource os = new DefaultWebObjectSource(request, response, application, new WebGenericConverter());

    return os;//  w w  w.j a  v a  2s.c  om
}

From source file:de.uni_koeln.spinfo.maalr.services.editor.TestEditorService.java

@Before
public void beforeTest() throws Exception {
    Database.getInstance().deleteAllEntries();
    loginManager.logout();/*from   ww  w . j  ava2s. c  om*/
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    MockServletContext servlet = new MockServletContext();
    context.setServletContext(servlet);

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

    context.refresh();
    context.getBeanFactory().registerScope("session", new SessionScope());
    loginManager.login("admin", "admin");
    File file = File.createTempFile("maalr", "test");
    File indexDir = new File(file.getParentFile(), "maalr_test" + UUID.randomUUID().toString() + "_idx");
    Assert.assertFalse(indexDir.exists());
    indexDir.mkdir();
    file.deleteOnExit();
    LuceneConfiguration environment = new LuceneConfiguration();
    environment.setBaseDirectory(indexDir.getAbsolutePath());
    Dictionary d = new Dictionary();
    d.setEnvironment(environment);
}

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

@Test
public void testHandleRequestInternal() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final SubmitConfigurationsController ctrl = new SubmitConfigurationsController();
    final MockServletContext servletContext = new MockServletContext();
    servletContext.setContextPath("sventon-test");
    configDirectory.setServletContext(servletContext);

    ctrl.setScheduler(new StdScheduler(null, null) {
        public void triggerJob(final String string, final String string1) {
        }//  ww w  . ja  v  a  2  s . c om
    });

    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", "abc123"));
    repositoryConfiguration2.setCacheUsed(false);
    repositoryConfiguration2.setZippedDownloadsAllowed(false);

    application.addConfiguration(repositoryConfiguration1);
    application.addConfiguration(repositoryConfiguration2);
    application.setConfigured(false);
    ctrl.setApplication(application);
    ctrl.setServletContext(new MockServletContext());

    final File configFile1 = new File(configDirectory.getRepositoriesDirectory(), "testrepos1");
    final File configFile2 = new File(configDirectory.getRepositoriesDirectory(), "testrepos2");

    assertFalse(configFile1.exists());
    assertFalse(configFile2.exists());

    final ModelAndView modelAndView = ctrl.handleRequestInternal(request, response);
    assertNotNull(modelAndView);
    assertNull(modelAndView.getViewName()); // Will be null as it is a redirect view.

    //File should now be written
    assertTrue(configFile1.exists());
    assertTrue(configFile2.exists());
    FileUtils.deleteDirectory(configDirectory.getConfigRootDirectory());
    assertFalse(configFile1.exists());
    assertFalse(configFile2.exists());
    assertTrue(application.isConfigured());
}