コード例 #1
0
  protected void action(String key, MessageType type) {
    {
      List<String> sessionMessages = new ArrayList<String>();
      sessionMessages.add("Test session message");
      List<String> requestMessages = new ArrayList<String>();
      requestMessages.add("Test request message");

      HttpSession session = EasyMock.createStrictMock(HttpSession.class);
      EasyMock.expect(session.getAttribute(key)).andReturn(sessionMessages);
      EasyMock.replay(session);

      HttpServletRequest request = EasyMock.createStrictMock(HttpServletRequest.class);
      EasyMock.expect(request.getAttribute(key)).andReturn(requestMessages);
      EasyMock.expect(request.getSession()).andReturn(session);
      EasyMock.replay(request);

      FlashScope scope = new FlashScope(request);
      List<String> messages = scope.getActionMessages(type);
      assertEquals(2, messages.size());
      assertEquals("Test request message", messages.get(0));
      assertEquals("Test session message", messages.get(1));

      EasyMock.verify(request, session);
    }

    {
      HttpSession session = EasyMock.createStrictMock(HttpSession.class);
      EasyMock.expect(session.getAttribute(key)).andReturn(null);
      EasyMock.replay(session);

      HttpServletRequest request = EasyMock.createStrictMock(HttpServletRequest.class);
      EasyMock.expect(request.getAttribute(key)).andReturn(null);
      EasyMock.expect(request.getSession()).andReturn(session);
      EasyMock.replay(request);

      FlashScope scope = new FlashScope(request);
      List<String> messages = scope.getActionMessages(type);
      assertEquals(0, messages.size());

      EasyMock.verify(request, session);
    }

    {
      List<String> messages = new ArrayList<String>();

      HttpSession session = EasyMock.createStrictMock(HttpSession.class);
      EasyMock.expect(session.getAttribute(key)).andReturn(messages);
      EasyMock.replay(session);

      HttpServletRequest request = EasyMock.createStrictMock(HttpServletRequest.class);
      EasyMock.expect(request.getSession()).andReturn(session);
      EasyMock.replay(request);

      FlashScope scope = new FlashScope(request);
      scope.addActionMessage(type, "Test message");
      assertEquals(1, messages.size());
      assertEquals("Test message", messages.get(0));

      EasyMock.verify(request, session);
    }

    {
      Capture list = new Capture();
      HttpSession session = EasyMock.createStrictMock(HttpSession.class);
      EasyMock.expect(session.getAttribute(key)).andReturn(null);
      session.setAttribute(eq(key), list.capture());
      EasyMock.replay(session);

      HttpServletRequest request = EasyMock.createStrictMock(HttpServletRequest.class);
      EasyMock.expect(request.getSession()).andReturn(session);
      EasyMock.replay(request);

      FlashScope scope = new FlashScope(request);
      scope.addActionMessage(type, "Test message");
      List<String> messages = (List<String>) list.object;
      assertEquals(1, messages.size());
      assertEquals("Test message", messages.get(0));

      EasyMock.verify(request, session);
    }
  }