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 testSessionPreloading() 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()); ServletUnitClient wc = sr.newClient(); wc.getSession(true).setAttribute("color", "green"); WebRequest request = new GetMethodWebRequest("http://localhost/" + resourceName2); WebResponse response = wc.getResponse(request); assertNotNull("No response received", response); assertEquals("content type", "text/plain", response.getContentType()); assertEquals("requested resource", "You posted green", response.getText()); assertEquals("Returned cookie count", 0, response.getNewCookieNames().length); }
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); }