Exemplo n.º 1
0
  @Test
  public void updateTargetUrlWithContextLoader() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);

    MockServletContext servletContext = new MockServletContext();
    ContextLoader contextLoader = new ContextLoader(wac);
    contextLoader.initWebApplicationContext(servletContext);

    try {
      RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
      wac.getBean(RequestDataValueProcessorWrapper.class)
          .setRequestDataValueProcessor(mockProcessor);

      RedirectView rv = new RedirectView();
      rv.setUrl("/path");

      MockHttpServletRequest request = createRequest();
      HttpServletResponse response = new MockHttpServletResponse();

      given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");

      rv.render(new ModelMap(), request, response);

      verify(mockProcessor).processUrl(request, "/path");
    } finally {
      contextLoader.closeWebApplicationContext(servletContext);
    }
  }
  /** It's not that complicated so we'll just run it straight through here. */
  @Test
  public void publishedEventIsReceivedbyListener() {
    HttpSessionEventPublisher publisher = new HttpSessionEventPublisher();

    StaticWebApplicationContext context = new StaticWebApplicationContext();

    MockServletContext servletContext = new MockServletContext();
    servletContext.setAttribute(
        StaticWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);

    context.setServletContext(servletContext);
    context.registerSingleton("listener", MockApplicationListener.class, null);
    context.refresh();

    MockHttpSession session = new MockHttpSession(servletContext);
    MockApplicationListener listener = (MockApplicationListener) context.getBean("listener");

    HttpSessionEvent event = new HttpSessionEvent(session);

    publisher.sessionCreated(event);

    assertNotNull(listener.getCreatedEvent());
    assertNull(listener.getDestroyedEvent());
    assertEquals(session, listener.getCreatedEvent().getSession());

    listener.setCreatedEvent(null);
    listener.setDestroyedEvent(null);

    publisher.sessionDestroyed(event);
    assertNotNull(listener.getDestroyedEvent());
    assertNull(listener.getCreatedEvent());
    assertEquals(session, listener.getDestroyedEvent().getSession());
  }
Exemplo n.º 3
0
  @Test
  public void updateTargetUrl() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);
    wac.setServletContext(new MockServletContext());
    wac.refresh();

    RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
    wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);

    RedirectView rv = new RedirectView();
    rv.setApplicationContext(wac); // Init RedirectView with WebAppCxt
    rv.setUrl("/path");

    MockHttpServletRequest request = createRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    HttpServletResponse response = new MockHttpServletResponse();

    given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");

    rv.render(new ModelMap(), request, response);

    verify(mockProcessor).processUrl(request, "/path");
  }