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.omnifaces.OmnifacesServletContextInitializerIT.java

public void testOnStartup() throws ServletException {
    OmnifacesServletContextInitializer omnifacesServletContextInitializer = new OmnifacesServletContextInitializer(
            this.omnifacesProperties);

    ServletContext servletContext = new MockServletContext();

    omnifacesServletContextInitializer.onStartup(servletContext);

    assertThat(servletContext.getInitParameter(CacheInitializerListener.CACHE_PROVIDER_INIT_PARAM_NAME))
            .isEqualTo("myCacheProvider");
}

From source file:gov.nih.nci.protexpress.ui.actions.registration.test.RegistrationActionTest.java

public void testValidate() throws Exception {
    RegistrationRequest request = new RegistrationRequest();
    request.setLoginName("login");
    request.setEmail("test@email.com");
    registrationAction.setRegistrationRequest(request);
    registrationAction.prepare();//w ww .j  a  va 2 s  .  c  o m
    registrationAction.validate();

    MockServletContext context = new MockServletContext();
    context.addInitParameter("ldap.install", "true");
    ServletActionContext.setServletContext(context);
    registrationAction.prepare();
    registrationAction.validate();

    registrationAction.setLdapAuthenticate(false);
    registrationAction.validate();
}

From source file:com.trenako.web.test.AbstractSpringTagsTest.java

@Before
public void setup() throws Exception {
    mockServletContext = new MockServletContext();

    // mocking the spring web application context
    mockWebApplicationContext = mock(WebApplicationContext.class);
    when(mockWebApplicationContext.getServletContext()).thenReturn(mockServletContext);
    when(mockWebApplicationContext.getAutowireCapableBeanFactory())
            .thenReturn(mock(AutowireCapableBeanFactory.class));

    mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            mockWebApplicationContext);/*from   www .  j  a va 2 s.  c om*/

    // mocking the servlet request
    mockRequest = new MockHttpServletRequest();
    mockRequest.setContextPath("/trenako-web");
    mockRequest.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/trenako-web");
    mockPageContext = new MockPageContext(mockServletContext, mockRequest);

    // mocking the message source to always return the default value
    mockMessageSource = mock(MessageSource.class);
    when(mockMessageSource.getMessage(anyString(), any(Object[].class), anyString(), any(Locale.class)))
            .thenAnswer(new Answer<String>() {
                @Override
                public String answer(InvocationOnMock invocation) throws Throwable {
                    Object[] args = invocation.getArguments();
                    return (String) args[2];
                }
            });

    // give the tag under test the chance to setup itself
    setupTag(mockPageContext, mockMessageSource);
}

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

@Test
public void testNoTgtToSet() throws Exception {
    this.context.setExternalContext(new ServletExternalContext(new MockServletContext(),
            new MockHttpServletRequest(), new MockHttpServletResponse()));

    assertEquals("success", this.action.execute(this.context).getId());
}

From source file:com.github.hateoas.forms.spring.AffordanceBuilderFactoryTest.java

@Before
public void setUp() {
    request = MockMvcRequestBuilders.get("http://example.com/").buildRequest(new MockServletContext());
    final RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
}

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerMacroTests.java

public void setUp() throws Exception {
    wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());

    //final Template expectedTemplate = new Template();
    fc = new FreeMarkerConfigurer();
    fc.setPreferFileSystemAccess(false);
    fc.afterPropertiesSet();//from w  w  w.java  2 s  .  c  om

    wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
    wac.refresh();

    request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    response = new MockHttpServletResponse();
}

From source file:com.mtgi.analytics.aop.config.v11.WebContextConfigurationTest.java

@Before
public void setUp() {
    //we shirk the usual Unitils spring initialization for us, so that we can select a non-default
    //spring application context.  The web context should switch the default initialization behavior
    //for the bt:manager tag.
    spring = new XmlWebApplicationContext();
    spring.setServletContext(new MockServletContext());
    spring.setConfigLocations(new String[] {
            "com/mtgi/analytics/aop/config/v11/DefaultConfigurationTest-applicationContext.xml" });
    spring.refresh();/*from w  w  w .j  a v a2s.c  om*/

    testExecutor = (ThreadPoolTaskExecutor) spring.getBean("testExecutor");
    defaultTrackingManager = (BehaviorTrackingManagerImpl) spring.getBean("defaultTrackingManager");
    testScheduler = (Scheduler) spring.getBean("testScheduler");
}

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

@Test
public void testParams1() throws Exception {
    HttpServletRequest request = MockMvcRequestBuilders.get("/").param("foo", "aaa").param("foo", "bbb")
            .param("bar", "ccc").buildRequest(new MockServletContext());
    Request req = new DefaultRequest(request);
    Map<String, List<String>> expected = new LinkedHashMap<>();
    expected.put("foo", Arrays.asList("aaa", "bbb"));
    expected.put("bar", Arrays.asList("ccc"));
    assertThat(req.params(), is(expected));
}

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

@Test
public void shouldGetDevelopmentStageWhenJunkIsSpecified() throws Exception {
    // given// w w w .j a  v  a 2 s .c  o m
    MockServletContext context = new MockServletContext();
    context.addInitParameter(GuiceServlets.STAGE_PARAMETER, "${application.stage}"); // will be filtered later

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

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

From source file:ar.com.zauber.commons.web.proxy.impl.PathBasedURLRequestMapperTest.java

/** @throws Exception on error */
public final void testNoServletContext() throws Exception {
    final String ctxPath = "/nexusaaa-0.0";
    final String servletContext = "/bin";

    final PathBasedURLRequestMapper mapper = new PathBasedURLRequestMapper(
            new InmutableURLRequestMapper(new InmutableURLResult(new URL(base))));
    mapper.setStripContextPath(false);/*from  www  . ja va2s .  c  o  m*/
    mapper.setStripServletPath(false);

    final MockServletContext ctx = new MockServletContext();

    final MockHttpServletRequest request = new MockHttpServletRequest(ctx, "GET",
            ctxPath + servletContext + "/");
    request.setContextPath(ctxPath);
    request.setServletPath(servletContext);
    assertEquals(new URL(base + ctxPath + servletContext + "/"),
            mapper.getProxiedURLFromRequest(request).getURL());
}