public void testInvocationContext() throws Exception {
    final String resourceName = "something/interesting";

    ServletRunner sr = new ServletRunner();
    sr.registerServlet(resourceName, StatefulServlet.class.getName());
    ServletUnitClient suc = sr.newClient();

    WebRequest request = new PostMethodWebRequest("http://localhost/" + resourceName);
    request.setParameter("color", "red");

    InvocationContext ic = suc.newInvocation(request);
    StatefulServlet ss = (StatefulServlet) ic.getServlet();
    assertNull("A session already exists", ss.getColor(ic.getRequest()));

    ss.setColor(ic.getRequest(), "blue");
    assertEquals("Color in session", "blue", ss.getColor(ic.getRequest()));

    Enumeration e = ic.getRequest().getSession().getAttributeNames();
    assertNotNull("No attribute list returned", e);
    assertTrue("No attribute names in list", e.hasMoreElements());
    assertEquals("First attribute name", "color", e.nextElement());
    assertTrue("List did not end after one name", !e.hasMoreElements());

    String[] names = ic.getRequest().getSession().getValueNames();
    assertEquals("number of value names", 1, names.length);
    assertEquals("first name", "color", names[0]);
  }
  public void testInvocationCompletion() throws Exception {
    final String resourceName = "something/interesting";

    ServletRunner sr = new ServletRunner();
    sr.registerServlet(resourceName, StatefulServlet.class.getName());
    ServletUnitClient suc = sr.newClient();

    WebRequest request = new PostMethodWebRequest("http://localhost/" + resourceName);
    request.setParameter("color", "red");

    InvocationContext ic = suc.newInvocation(request);
    StatefulServlet ss = (StatefulServlet) ic.getServlet();
    ss.setColor(ic.getRequest(), "blue");
    ss.writeSelectMessage("blue", ic.getResponse().getWriter());

    WebResponse response = ic.getServletResponse();
    assertEquals("requested resource", "You selected blue", response.getText());
    assertEquals("Returned cookie count", 1, response.getNewCookieNames().length);
  }
  public void testInvocationContextUpdate() throws Exception {
    final String resourceName = "something/interesting";

    ServletRunner sr = new ServletRunner();
    sr.registerServlet(resourceName, StatefulServlet.class.getName());
    ServletUnitClient suc = sr.newClient();

    WebRequest request = new PostMethodWebRequest("http://localhost/" + resourceName);
    request.setParameter("color", "red");

    InvocationContext ic = suc.newInvocation(request);
    StatefulServlet ss = (StatefulServlet) ic.getServlet();
    ss.setColor(ic.getRequest(), "blue");
    suc.getResponse(ic);

    WebResponse response = suc.getResponse("http://localhost/" + resourceName);
    assertNotNull("No response received", response);
    assertEquals("content type", "text/plain", response.getContentType());
    assertEquals("requested resource", "You posted blue", response.getText());
    assertEquals("Returned cookie count", 0, response.getNewCookieNames().length);
  }