Example #1
0
  public void testNoInitialState() throws Exception {
    final String resourceName = "something/interesting";

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

    WebRequest request = new GetMethodWebRequest("http://localhost/" + resourceName);
    WebResponse response = sr.getResponse(request);
    assertNotNull("No response received", response);
    assertEquals("content type", "text/plain", response.getContentType());
    assertEquals("requested resource", "No session found", response.getText());
    assertEquals("Returned cookie count", 0, response.getNewCookieNames().length);
  }
Example #2
0
  public void testStatePreservation() throws Exception {
    final String resourceName1 = "something/interesting/start";
    final String resourceName2 = "something/continue";

    ServletRunner sr = new ServletRunner();
    sr.registerServlet(resourceName1, StatefulServlet.class.getName());
    sr.registerServlet(resourceName2, StatefulServlet.class.getName());
    WebClient wc = sr.newClient();

    WebRequest request = new PostMethodWebRequest("http://localhost/" + resourceName1);
    request.setParameter("color", "red");
    WebResponse response = wc.getResponse(request);
    assertNotNull("No response received", response);
    assertEquals("content type", "text/plain", response.getContentType());
    assertEquals("requested resource", "You selected red", response.getText());

    request = new GetMethodWebRequest("http://localhost/" + resourceName2);
    response = wc.getResponse(request);
    assertNotNull("No response received", response);
    assertEquals("content type", "text/plain", response.getContentType());
    assertEquals("requested resource", "You posted red", response.getText());
    assertEquals("Returned cookie count", 0, response.getNewCookieNames().length);
  }
Example #3
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);
  }
Example #4
0
  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);
  }