Example #1
0
  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]);
  }
Example #2
0
  public void testStateCookies() throws Exception {
    final String resourceName = "something/interesting";

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

    WebRequest request = new PostMethodWebRequest("http://localhost/" + resourceName);
    request.setParameter("color", "red");
    WebResponse response = sr.getResponse(request);
    assertNotNull("No response received", response);
    assertEquals("Returned cookie count", 1, response.getNewCookieNames().length);
  }
Example #3
0
  public void testSessionAccess() 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());

    WebRequest request = new PostMethodWebRequest("http://localhost/" + resourceName1);
    request.setParameter("color", "yellow");
    sr.getResponse(request);

    assertNotNull("No session was created", sr.getSession(false));
    assertEquals(
        "Color attribute in session", "yellow", sr.getSession(false).getAttribute("color"));
  }
 static String getStringParameter(WebRequest request, String name) {
   String p;
   if ((p = request.getParameter(name)) != null) {
     return p;
   } else {
     return "";
   }
 }
Example #5
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 #6
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);
  }
Example #7
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);
  }
 static int parseIntParameter(WebRequest request, String name, int ifMissing) {
   String p;
   if ((p = request.getParameter(name)) != null) {
     try {
       return asInt(p);
     } catch (NumberFormatException ee) {
       logger.error(
           new StringWriter()
               .append("Could not cast event property '")
               .append(name)
               .append(": ")
               .append(p)
               .append("' to int")
               .toString());
       return ifMissing;
     }
   } else {
     return ifMissing;
   }
 }